X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fcontrol%2Ftransaction.go;h=df4d7db4147a34c101a05330b2d5a3184bb5dd74;hb=a9bf76cb8dec6e52e7699edf1631c214647f8beb;hp=f686b44630a4cdae29622cbf2c3915c5d63c399a;hpb=0388dd945789dae802aaa93c5062e3ae4c45ddf1;p=ric-plt%2Fsubmgr.git diff --git a/pkg/control/transaction.go b/pkg/control/transaction.go index f686b44..df4d7db 100644 --- a/pkg/control/transaction.go +++ b/pkg/control/transaction.go @@ -20,11 +20,114 @@ package control import ( + "gerrit.o-ran-sc.org/r/ric-plt/e2ap/pkg/e2ap" "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp" "strconv" "sync" + "time" ) +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +type TransactionIf interface { + String() string + Release() + SendEvent(interface{}, time.Duration) (bool, bool) + WaitEvent(time.Duration) (interface{}, bool) +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- + +type Transaction struct { + mutex sync.Mutex // + Seq uint64 //transaction sequence + tracker *Tracker //tracker instance + Meid *xapp.RMRMeid //meid transaction related + ReqId RequestId // + Mtype int //Encoded message type to be send + Payload *e2ap.PackedData //Encoded message to be send + EventChan chan interface{} +} + +func (t *Transaction) String() string { + return "trans(" + strconv.FormatUint(uint64(t.Seq), 10) + "/" + t.Meid.RanName + "/" + t.ReqId.String() + ")" +} + +func (t *Transaction) SendEvent(event interface{}, waittime time.Duration) (bool, bool) { + if waittime > 0 { + select { + case t.EventChan <- event: + return true, false + case <-time.After(waittime): + return false, true + } + return false, false + } + t.EventChan <- event + return true, false +} + +func (t *Transaction) WaitEvent(waittime time.Duration) (interface{}, bool) { + if waittime > 0 { + select { + case event := <-t.EventChan: + return event, false + case <-time.After(waittime): + return nil, true + } + } + event := <-t.EventChan + return event, false +} + +func (t *Transaction) GetReqId() *RequestId { + t.mutex.Lock() + defer t.mutex.Unlock() + return &t.ReqId +} + +func (t *Transaction) GetMtype() int { + t.mutex.Lock() + defer t.mutex.Unlock() + return t.Mtype +} + +func (t *Transaction) GetMeid() *xapp.RMRMeid { + t.mutex.Lock() + defer t.mutex.Unlock() + if t.Meid != nil { + return t.Meid + } + return nil +} + +func (t *Transaction) GetPayload() *e2ap.PackedData { + t.mutex.Lock() + defer t.mutex.Unlock() + return t.Payload +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- +type TransactionSubs struct { + Transaction // +} + +func (t *TransactionSubs) String() string { + return "transsubs(" + t.Transaction.String() + ")" +} + +func (t *TransactionSubs) Release() { + t.mutex.Lock() + xapp.Logger.Debug("RELEASE %s", t.String()) + t.tracker = nil + t.mutex.Unlock() +} + //----------------------------------------------------------------------------- // //----------------------------------------------------------------------------- @@ -34,59 +137,61 @@ type TransactionXappKey struct { } func (key *TransactionXappKey) String() string { - return key.RmrEndpoint.String() + "/" + key.Xid + return "transkey(" + key.RmrEndpoint.String() + "/" + key.Xid + ")" } //----------------------------------------------------------------------------- // //----------------------------------------------------------------------------- -type Transaction struct { - mutex sync.Mutex - tracker *Tracker // tracker instance - Subs *Subscription - RmrEndpoint RmrEndpoint - Xid string // xapp xid in req - OrigParams *xapp.RMRParams // request orginal params - RespReceived bool - ForwardRespToXapp bool +type TransactionXapp struct { + Transaction // + XappKey *TransactionXappKey // } -func (t *Transaction) String() string { - t.mutex.Lock() - defer t.mutex.Unlock() - var subId string = "?" - if t.Subs != nil { - subId = strconv.FormatUint(uint64(t.Subs.Seq), 10) +func (t *TransactionXapp) String() string { + var transkey string = "transkey(N/A)" + if t.XappKey != nil { + transkey = t.XappKey.String() } - return subId + "/" + t.RmrEndpoint.String() + "/" + t.Xid + return "transxapp(" + t.Transaction.String() + "/" + transkey + ")" } -func (t *Transaction) CheckResponseReceived() bool { +func (t *TransactionXapp) GetEndpoint() *RmrEndpoint { t.mutex.Lock() defer t.mutex.Unlock() - if t.RespReceived == false { - t.RespReceived = true - return false + if t.XappKey != nil { + return &t.XappKey.RmrEndpoint } - return true + return nil } -func (t *Transaction) RetryTransaction() { +func (t *TransactionXapp) GetXid() string { t.mutex.Lock() defer t.mutex.Unlock() - t.RespReceived = false + if t.XappKey != nil { + return t.XappKey.Xid + } + return "" } -func (t *Transaction) Release() { +func (t *TransactionXapp) GetSrc() string { t.mutex.Lock() defer t.mutex.Unlock() - if t.Subs != nil { - t.Subs.UnSetTransaction(t) - } - if t.tracker != nil { - xappkey := TransactionXappKey{t.RmrEndpoint, t.Xid} - t.tracker.UnTrackTransaction(xappkey) + if t.XappKey != nil { + return t.XappKey.RmrEndpoint.String() } - t.Subs = nil + return "" +} + +func (t *TransactionXapp) Release() { + t.mutex.Lock() + xapp.Logger.Debug("RELEASE %s", t.String()) + tracker := t.tracker + xappkey := t.XappKey t.tracker = nil + t.mutex.Unlock() + + if tracker != nil && xappkey != nil { + tracker.UnTrackTransaction(*xappkey) + } }