2de57d9d871ef7b273807c55f7ac2cf4644f4e33
[ric-plt/e2mgr.git] / E2Manager / rmrCgo / rmrCgoApi.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
4 //
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
8 //
9 //      http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16 //
17
18 package rmrCgo
19
20 // #cgo LDFLAGS: -L/usr/local/lib -lrmr_nng -lnng
21 // #include <rmr/rmr.h>
22 // #include <stdlib.h>
23 import "C"
24 import (
25         "fmt"
26         "github.com/pkg/errors"
27         "strings"
28         "time"
29         "unsafe"
30
31         "e2mgr/logger"
32 )
33
34 func (*Context) Init(port string, maxMsgSize int, flags int, logger *logger.Logger) *RmrMessenger {//TODO remove pointer from interface
35         pp := C.CString(port)
36         defer C.free(unsafe.Pointer(pp))
37         logger.Debugf("#rmrCgoApi.Init - Going to initiate RMR router")
38         ctx := NewContext(maxMsgSize, flags, C.rmr_init(pp, C.int(maxMsgSize), C.int(flags)), logger)
39         start := time.Now()
40         //TODO use time.Ticker()
41         for !ctx.IsReady() {
42                 time.Sleep(time.Second)
43                 if time.Since(start) >= time.Minute {
44                         logger.Debugf("#rmrCgoApi.Init - Routing table is not ready")
45                         start = time.Now()
46                 }
47         }
48         logger.Infof("#rmrCgoApi.Init - RMR router has been initiated")
49         r := RmrMessenger(ctx)
50         return &r
51 }
52
53 func (ctx *Context) SendMsg(msg *MBuf, maxMsgSize int) (*MBuf, error) {
54         ctx.checkContextInitialized()
55         ctx.Logger.Debugf("#rmrCgoApi.SendMsg - Going to send message. MBuf: %v", *msg)
56         allocatedCMBuf := ctx.getAllocatedCRmrMBuf(ctx.Logger, msg, maxMsgSize)
57         defer C.rmr_free_msg(allocatedCMBuf)
58         state := allocatedCMBuf.state
59         if state != RMR_OK {
60                 errorMessage := fmt.Sprintf("#rmrCgoApi.SendMsg - Failed to get allocated message. state: %v - %s", state, states[int(state)])
61                 ctx.Logger.Errorf(errorMessage)
62                 return nil, errors.New(errorMessage)
63         }
64
65         //TODO: if debug enabled
66         transactionId := string(*msg.XAction)
67         tmpTid := strings.TrimSpace(transactionId)
68         ctx.Logger.Infof("[E2 Manager -> RMR] #rmrCgoApi.SendMsg - Going to send message %v for transaction id: %s", *msg, tmpTid)
69
70         currCMBuf := C.rmr_send_msg(ctx.RmrCtx, allocatedCMBuf)
71         state = currCMBuf.state
72         ctx.Logger.Debugf("#rmrCgoApi.SendMsg - The current message  state: %v, message buffer:%v", state, currCMBuf)
73
74         if state != RMR_OK {
75                 errorMessage := fmt.Sprintf("#rmrCgoApi.SendMsg - Failed to send message. state: %v - %s", state, states[int(state)])
76                 ctx.Logger.Errorf(errorMessage)
77                 return nil, errors.New(errorMessage)
78         }
79
80         ctx.Logger.Debugf("#rmrCgoApi.SendMsg - The message has been sent successfully ")
81         return convertToMBuf(ctx.Logger, currCMBuf), nil
82 }
83
84 func (ctx *Context) RecvMsg() (*MBuf, error) {
85         ctx.checkContextInitialized()
86         ctx.Logger.Debugf("#rmrCgoApi.RecvMsg - Going to receive message")
87         allocatedCMBuf := C.rmr_alloc_msg(ctx.RmrCtx, C.int(ctx.MaxMsgSize))
88         defer C.rmr_free_msg(allocatedCMBuf)
89
90         currCMBuf := C.rmr_rcv_msg(ctx.RmrCtx, allocatedCMBuf)
91         state := currCMBuf.state
92
93         if state != RMR_OK {
94                 errorMessage := fmt.Sprintf("#rmrCgoApi.RecvMsg - Failed to receive message. state: %v - %s", state, states[int(state)])
95                 ctx.Logger.Errorf(errorMessage)
96                 return nil, errors.New(errorMessage)
97         }
98
99         mbuf := convertToMBuf(ctx.Logger, currCMBuf)
100         transactionId := string(*mbuf.XAction)
101         tmpTid := strings.TrimSpace(transactionId)
102         ctx.Logger.Infof("[RMR -> E2 Manager] #rmrCgoApi.RecvMsg - message %v has been received for transaction id: %s", *mbuf, tmpTid)
103         return mbuf, nil
104 }
105
106 func (ctx *Context) RtsMsg(msg *MBuf) {
107         ctx.checkContextInitialized()
108         ctx.Logger.Debugf("#rmrCgoApi.RtsMsg - Going to return message to the sender")
109         allocatedCMBuf := C.rmr_alloc_msg(ctx.RmrCtx, C.int(ctx.MaxMsgSize))
110         defer C.rmr_free_msg(allocatedCMBuf)
111         C.rmr_rts_msg(ctx.RmrCtx, allocatedCMBuf)
112 }
113
114 func (ctx *Context) IsReady() bool {
115         ctx.Logger.Debugf("#rmrCgoApi.IsReady - Going to check if routing table is initialized")
116         return int(C.rmr_ready(ctx.RmrCtx)) != 0
117 }
118
119 func (ctx *Context) Close() {
120         ctx.Logger.Debugf("#rmrCgoApi.Close - Going to close RMR context")
121         C.rmr_close(ctx.RmrCtx)
122         time.Sleep(100 * time.Millisecond)
123 }