2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
17 // This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 // platform project (RICP).
23 // #cgo LDFLAGS: -L/usr/local/lib -lrmr_nng -lnng
24 // #include <rmr/rmr.h>
25 // #include <stdlib.h>
29 "github.com/pkg/errors"
34 func (*Context) Init(port string, maxMsgSize int, maxRetries int, flags int) *Messenger {
36 defer C.free(unsafe.Pointer(pp))
37 ctx := NewContext(maxMsgSize, maxRetries, flags, C.rmr_init(pp, C.int(maxMsgSize), C.int(flags)))
40 time.Sleep(time.Second)
41 if time.Since(start) >= time.Minute {
45 // Configure the rmr to make rounds of attempts to send a message before notifying the application that it should retry.
46 // Each round is about 1000 attempts with a short sleep between each round.
47 C.rmr_set_stimeout(ctx.RmrCtx, C.int(0))
52 func (ctx *Context) SendMsg(msg *MBuf) (*MBuf, error) {
54 allocatedCMBuf, err := ctx.getAllocatedCRmrMBuf(msg, ctx.MaxMsgSize)
58 if state := allocatedCMBuf.state; state != RMR_OK {
59 errorMessage := fmt.Sprintf("#rmrCgoApi.SendMsg - Failed to get allocated message. state: %v - %s", state, states[int(state)])
60 return nil, errors.New(errorMessage)
62 defer C.rmr_free_msg(allocatedCMBuf)
64 for i := 0; i < ctx.MaxRetries; i++ {
65 currCMBuf := C.rmr_send_msg(ctx.RmrCtx, allocatedCMBuf)
66 if state := currCMBuf.state; state != RMR_OK {
67 if state != RMR_ERR_RETRY {
68 errorMessage := fmt.Sprintf("#rmrCgoApi.SendMsg - Failed to send message. state: %v - %s", state, states[int(state)])
69 return nil, errors.New(errorMessage)
71 time.Sleep(100 * time.Millisecond)
74 return convertToMBuf(currCMBuf)
77 return nil, errors.New(fmt.Sprintf("#rmrCgoApi.SendMsg - Too many retries"))
80 func (ctx *Context) RecvMsg() (*MBuf, error) {
81 allocatedCMBuf, err := C.rmr_alloc_msg(ctx.RmrCtx, C.int(ctx.MaxMsgSize))
85 if state := allocatedCMBuf.state; state != RMR_OK {
86 errorMessage := fmt.Sprintf("#rmrCgoApi.SendMsg - Failed to get allocated message. state: %v - %s", state, states[int(state)])
87 return nil, errors.New(errorMessage)
89 defer C.rmr_free_msg(allocatedCMBuf)
91 currCMBuf := C.rmr_rcv_msg(ctx.RmrCtx, allocatedCMBuf)
92 if state := currCMBuf.state; state != RMR_OK {
93 errorMessage := fmt.Sprintf("#rmrCgoApi.RecvMsg - Failed to receive message. state: %v - %s", state, states[int(state)])
94 return nil, errors.New(errorMessage)
97 return convertToMBuf(currCMBuf)
100 func (ctx *Context) IsReady() bool {
101 return int(C.rmr_ready(ctx.RmrCtx)) != 0
104 func (ctx *Context) Close() {
105 C.rmr_close(ctx.RmrCtx)