d2fabd2dc486b109f7320a657f0941e1e9be30a7
[ric-app/hw.git] / src / xapp-utils / xapp_rmr.hpp
1 /*
2 ==================================================================================
3
4         Copyright (c) 2019-2020 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 #ifndef XAPP_RMR_XAPP_RMR_H_
21 #define XAPP_RMR_XAPP_RMR_H_
22
23
24 #ifdef __GNUC__
25 #define likely(x)  __builtin_expect((x), 1)
26 #define unlikely(x) __builtin_expect((x), 0)
27 #else
28 #define likely(x) (x)
29 #define unlikely(x) (x)
30 #endif
31
32 #include <iostream>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <error.h>
37 #include <assert.h>
38 #include <sstream>
39 #include <thread>
40 #include <functional>
41 #include <map>
42 #include <mutex>
43 #include <sys/epoll.h>
44 #include <rmr/rmr.h>
45 #include <rmr/RIC_message_types.h>
46 #include <mdclog/mdclog.h>
47 #include <vector>
48
49 typedef struct{
50         struct timespec ts;
51         int32_t message_type;
52         int32_t state;
53         int32_t payload_length;
54         unsigned char sid[RMR_MAX_SID]; //sender ID for return to sender needs.(ACKS required)[RMR_MAX_SID]
55         unsigned char src[RMR_MAX_SRC]; //name of the sender (source)[RMR_MAX_SRC]
56         unsigned char meid[RMR_MAX_MEID]={};
57
58 }  xapp_rmr_header;
59
60
61 class XappRmr{
62 private:
63         std::string _proto_port;
64         int _nattempts;
65         bool _rmr_is_ready;
66     bool _listen;
67         void* _xapp_rmr_ctx;
68         rmr_mbuf_t*             _xapp_send_buff;                                        // send buffer
69         rmr_mbuf_t*             _xapp_received_buff;                                    // received buffer
70
71
72 public:
73
74         XappRmr(std::string, int rmrattempts=10);
75         ~XappRmr(void);
76         void xapp_rmr_init(bool);
77
78         template <class MessageProcessor>
79         void xapp_rmr_receive(MessageProcessor&&, XappRmr *parent);
80
81         template <class MessageProcessor>
82         void xapp_test_receiver(MessageProcessor&&, XappRmr *parent);
83         bool xapp_rmr_send(xapp_rmr_header*, void*);
84
85         bool rmr_header(xapp_rmr_header*);
86         void set_listen(bool);
87         bool get_listen(void);
88         int get_is_ready(void);
89         bool get_isRunning(void);
90         void* get_rmr_context(void);
91
92 };
93
94
95 // main workhorse thread which does the listen->process->respond loop
96 template <class MsgHandler>
97 void XappRmr::xapp_rmr_receive(MsgHandler&& msgproc, XappRmr *parent){
98
99         bool* resend = new bool(false);
100         // Get the thread id
101         std::thread::id my_id = std::this_thread::get_id();
102         std::stringstream thread_id;
103         std::stringstream ss;
104
105         thread_id << my_id;
106
107         // Get the rmr context from parent (all threads and parent use same rmr context. rmr context is expected to be thread safe)
108         if(!parent->get_is_ready()){
109                         mdclog_write( MDCLOG_ERR, "RMR Shows Not Ready in RECEIVER, file= %s, line=%d ",__FILE__,__LINE__);
110                         return;
111         }
112         void *rmr_context = parent->get_rmr_context();
113         assert(rmr_context != NULL);
114
115         // Get buffer specific to this thread
116         this->_xapp_received_buff = NULL;
117         this->_xapp_received_buff = rmr_alloc_msg(rmr_context, RMR_DEF_SIZE);
118         assert(this->_xapp_received_buff != NULL);
119
120         mdclog_write(MDCLOG_INFO, "Starting receiver thread %s",  thread_id.str().c_str());
121
122         while(parent->get_listen()) {
123                 mdclog_write(MDCLOG_INFO, "Listening at Thread: %s",  thread_id.str().c_str());
124
125                 this->_xapp_received_buff = rmr_rcv_msg( rmr_context, this->_xapp_received_buff );
126                 //this->_xapp_received_buff = rmr_call( rmr_context, this->_xapp_received_buff);
127
128                 if( this->_xapp_received_buff->mtype < 0 || this->_xapp_received_buff->state != RMR_OK ) {
129                         mdclog_write(MDCLOG_ERR, "bad msg:  state=%d  errno=%d, file= %s, line=%d", this->_xapp_received_buff->state, errno, __FILE__,__LINE__ );
130                         return;
131                 }
132                 else
133                 {
134                         mdclog_write(MDCLOG_INFO,"RMR Received Message of Type: %d",this->_xapp_received_buff->mtype);
135                         mdclog_write(MDCLOG_INFO,"RMR Received Message: %s",(char*)this->_xapp_received_buff->payload);
136
137                     //in case message handler returns true, need to resend the message.
138                         msgproc(this->_xapp_received_buff, resend);
139
140                         if(*resend){
141                                 //mdclog_write(MDCLOG_INFO,"RMR Return to Sender Message of Type: %d",this->_xapp_received_buff->mtype);
142                                 //mdclog_write(MDCLOG_INFO,"RMR Return to Sender Message: %s",(char*)this->_xapp_received_buff->payload);
143                                 rmr_rts_msg(rmr_context, this->_xapp_received_buff );
144                                 sleep(1);
145                                 *resend = false;
146                         }
147                         continue;
148
149                 }
150
151         }
152         // Clean up
153         try{
154                 delete resend;
155                 rmr_free_msg(this->_xapp_received_buff);
156         }
157         catch(std::runtime_error &e){
158                 std::string identifier = __FILE__ +  std::string(", Line: ") + std::to_string(__LINE__) ;
159                 std::string error_string = identifier = " Error freeing RMR message ";
160                 mdclog_write(MDCLOG_ERR, error_string.c_str(), "");
161         }
162
163         return;
164 }
165
166
167
168
169 #endif /* XAPP_RMR_XAPP_RMR_H_ */