X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=pkg%2Fxapp%2Frmr.go;h=9f988dcf1d396acc714a7d13890dd2c6729f0aa7;hb=f22b458846a20a4a9fcafb49e3195ab44a16840e;hp=90bc64b6430d19f906572086aef59c4e4d5dd4f7;hpb=d28b8dd34b07b5af77802ba408d859b00215cd35;p=ric-plt%2Fxapp-frame.git diff --git a/pkg/xapp/rmr.go b/pkg/xapp/rmr.go index 90bc64b..9f988dc 100755 --- a/pkg/xapp/rmr.go +++ b/pkg/xapp/rmr.go @@ -64,6 +64,8 @@ int wait_epoll(int epoll_fd,int rcv_fd) { import "C" import ( + "bytes" + "crypto/md5" "fmt" "github.com/spf13/viper" "strings" @@ -98,6 +100,9 @@ var RMRErrors = map[int]string{ C.RMR_ERR_NOTSUPP: "the request is not supported, or RMr was not initialized for the request", } +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- type RMRParams struct { Mtype int Payload []byte @@ -113,6 +118,15 @@ type RMRParams struct { status int } +func (params *RMRParams) String() string { + var b bytes.Buffer + fmt.Fprintf(&b, "params(Src=%s Mtype=%d SubId=%d Xid=%s Meid=%s Paylens=%d/%d Paymd5=%x)", params.Src, params.Mtype, params.SubId, params.Xid, params.Meid, params.PayloadLen, len(params.Payload), md5.Sum(params.Payload)) + return b.String() +} + +//----------------------------------------------------------------------------- +// +//----------------------------------------------------------------------------- func NewRMRClientWithParams(protPort string, maxSize int, threadType int, statDesc string) *RMRClient { p := C.CString(protPort) m := C.int(maxSize) @@ -244,11 +258,21 @@ func (m *RMRClient) parseMessage(rxBuffer *C.rmr_mbuf_t) { func (m *RMRClient) Allocate(size int) *C.rmr_mbuf_t { m.contextMux.Lock() defer m.contextMux.Unlock() - buf := C.rmr_alloc_msg(m.context, C.int(size)) - if buf == nil { + outbuf := C.rmr_alloc_msg(m.context, C.int(size)) + if outbuf == nil { + Logger.Error("rmrClient: Allocating message buffer failed!") + } + return outbuf +} + +func (m *RMRClient) ReAllocate(inbuf *C.rmr_mbuf_t, size int) *C.rmr_mbuf_t { + m.contextMux.Lock() + defer m.contextMux.Unlock() + outbuf := C.rmr_realloc_msg(inbuf, C.int(size)) + if outbuf == nil { Logger.Error("rmrClient: Allocating message buffer failed!") } - return buf + return outbuf } func (m *RMRClient) Free(mbuf *C.rmr_mbuf_t) { @@ -268,18 +292,41 @@ func (m *RMRClient) SendRts(params *RMRParams) bool { return m.Send(params, true) } -func (m *RMRClient) CopyBuffer(params *RMRParams) *C.rmr_mbuf_t { - if params.Mbuf != nil { - m.Free(params.Mbuf) - params.Mbuf = nil +func (m *RMRClient) SendWithRetry(params *RMRParams, isRts bool, to time.Duration) (err error) { + status := m.Send(params, isRts) + i := 0 + for ; i < int(to)*2 && status == false; i++ { + status = m.Send(params, isRts) + if status == false { + time.Sleep(500 * time.Millisecond) + } } + if status == false { + err = fmt.Errorf("Failed with retries(%d) %s", i, params.String()) + if params.Mbuf != nil { + m.Free(params.Mbuf) + params.Mbuf = nil + } + } + return +} + +func (m *RMRClient) CopyBuffer(params *RMRParams) *C.rmr_mbuf_t { payLen := len(params.Payload) if params.PayloadLen != 0 { payLen = params.PayloadLen } - txBuffer := m.Allocate(payLen) + txBuffer := params.Mbuf + params.Mbuf = nil + + if txBuffer != nil { + txBuffer = m.ReAllocate(txBuffer, payLen) + } else { + txBuffer = m.Allocate(payLen) + } + if txBuffer == nil { return nil }