Initial commit of source directory
[ric-app/hw.git] / src / xapp-utils / xapp_rmr.cc
1 /*
2 ==================================================================================
3
4         Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18  */
19
20
21 #include "xapp_rmr.hpp"
22
23 XappRmr::XappRmr(std::string xname, std::string port, int rmrattempts){
24
25         _proto_port = port;
26         _xapp_name = xname;
27         _nattempts = rmrattempts;
28         _xapp_rmr_ctx = NULL;
29         _xapp_received_buff = NULL;
30         _xapp_send_buff =NULL;
31         _rmr_is_ready = false;
32
33 };
34
35 XappRmr::~XappRmr(void){
36
37         // free memory
38         if(_xapp_received_buff)
39                 rmr_free_msg(_xapp_received_buff);
40
41         if(_xapp_send_buff)
42                 rmr_free_msg(_xapp_send_buff);
43
44         if (_xapp_rmr_ctx){
45                 rmr_close(_xapp_rmr_ctx);
46         }
47 };
48
49 //Get RMR Context.
50 void XappRmr::xapp_rmr_init(){
51
52         // Initialize the RMR context
53         _xapp_rmr_ctx = rmr_init(const_cast<char*>(_proto_port.c_str()), RMR_MAX_RCV_BYTES, RMRFL_NONE);
54
55         if ( _xapp_rmr_ctx == NULL){
56                 mdclog_write(MDCLOG_ERR,"Error Initializing RMR, file= %s, line=%d",__FILE__,__LINE__);
57         }
58         while( ! rmr_ready(_xapp_rmr_ctx) ) {
59                 mdclog_write(MDCLOG_INFO,">>> waiting for RMR, file= %s, line=%d",__FILE__,__LINE__);
60                 sleep(1);
61         }
62         _rmr_is_ready = true;
63         mdclog_write(MDCLOG_INFO,"RMR Context is Ready, file= %s, line=%d",__FILE__,__LINE__);
64
65         return;
66
67 }
68
69 bool XappRmr::xapp_rmr_rts()
70 {
71         _xapp_send_buff = rmr_realloc_payload( _xapp_send_buff, 128, false, false );  // ensure payload is large enough
72         strncpy( (char*)_xapp_send_buff->payload, "OK\n", rmr_payload_size( _xapp_send_buff) );
73         rmr_rts_msg(_xapp_rmr_ctx, _xapp_send_buff );
74         _xapp_send_buff = NULL;
75         return true;
76 }
77
78 //RMR Send with payload and header.
79 bool XappRmr::xapp_rmr_send(xapp_rmr_header *hdr, void *payload){
80
81         if( _xapp_send_buff == NULL ) {
82                 _xapp_send_buff = rmr_alloc_msg(_xapp_rmr_ctx, RMR_DEF_SIZE);
83         }
84
85
86         _xapp_send_buff->mtype  = hdr->message_type;
87
88         memcpy(_xapp_send_buff->payload, payload, hdr->payload_length);
89
90         _xapp_send_buff->len = hdr->payload_length;
91         if(!_rmr_is_ready) {
92                 mdclog_write(MDCLOG_ERR,"RMR Context is Not Ready in SENDER, file= %s, line=%d",__FILE__,__LINE__);
93                 return false;
94         }
95         if( _xapp_send_buff == NULL ) {
96                 return false;
97         }
98
99
100         while(_nattempts > 0){
101                 _xapp_send_buff = rmr_send_msg(_xapp_rmr_ctx,_xapp_send_buff);
102
103                 if(!_xapp_send_buff) {
104                         mdclog_write(MDCLOG_ERR,"Error In Sending Message , file= %s, line=%d",__FILE__,__LINE__);
105                         _nattempts--;
106                 }
107                 else if (_xapp_send_buff->state == RMR_OK){
108                         mdclog_write(MDCLOG_INFO,"The okay message is %d, file= %s, line=%d", RMR_OK, __FILE__,__LINE__);
109                         _nattempts = 0;
110                         return true;
111                 }
112                 else
113                 {
114                         mdclog_write(MDCLOG_INFO,"Need to retry RMR MSG NUM %d, file= %s, line=%d",_xapp_send_buff->state, __FILE__,__LINE__);
115                         _nattempts--;
116                 }
117                 sleep(1);
118         }
119         return false;
120 }
121