Merge "Initial commit of source directory"
[ric-app/hw.git] / src / xapp-utils / xapp_rmr.hpp
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 #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 <thread>
39 #include <functional>
40 #include <map>
41 #include <mutex>
42 #include <sys/epoll.h>
43 #include <rmr/rmr.h>
44 #include <rmr/RIC_message_types.h>
45 #include <mdclog/mdclog.h>
46 #include <vector>
47
48 typedef struct{
49         struct timespec ts;
50         int32_t message_type;
51         int32_t state;
52         int32_t payload_length;
53         unsigned char xid[RMR_MAX_XID]; //space for user transaction id.
54         unsigned char sid[RMR_MAX_SID]; //sender ID for return to sender needs.(ACKS required)
55         unsigned char src[RMR_MAX_SRC]; //name of the sender (source)
56 }  xapp_rmr_header;
57
58
59 class XappRmr{
60 private:
61         std::string _xapp_name;
62         std::string _proto_port;
63         int _nattempts;
64         bool _rmr_is_ready;
65         void* _xapp_rmr_ctx;
66         rmr_mbuf_t*             _xapp_send_buff;                                        // send buffer
67         rmr_mbuf_t*             _xapp_received_buff;                                    // received buffer
68
69
70 public:
71
72         XappRmr(std::string, std::string, int rmrattempts=10);
73         ~XappRmr(void);
74         void xapp_rmr_init(void);
75
76         template <class MessageProcessor>
77         void xapp_rmr_receive(MessageProcessor&&, XappRmr *parent);
78         bool xapp_rmr_send(xapp_rmr_header*, void*);
79         bool xapp_rmr_rts();
80
81 };
82
83 //RMR receive
84 template <class MessageProcessor>
85 void XappRmr::xapp_rmr_receive(MessageProcessor&& msgproc, XappRmr *parent){
86                 char*   listen_port;
87
88                 if( (listen_port = getenv( "RMR_RCV_PORT" )) == NULL ) {
89                         mdclog_write(MDCLOG_ERR,"No Listening port assigned, file= %s, line=%d",__FILE__,__LINE__);
90                 }
91
92                 if(!_rmr_is_ready){
93                         mdclog_write( MDCLOG_ERR, "RMR Shows Not Ready in RECEIVER, file= %s, line=%d ",__FILE__,__LINE__);
94                         return;
95                 }
96
97                   while( 1 ) {
98                           parent->_xapp_received_buff = rmr_rcv_msg( parent->_xapp_rmr_ctx, parent->_xapp_received_buff );                                              // block until one arrives
99                         if( parent->_xapp_received_buff->mtype < 0 || parent->_xapp_received_buff->state != RMR_OK ) {
100                                 mdclog_write(MDCLOG_ERR, "bad msg:  state=%d  errno=%d, file= %s, line=%d", parent->_xapp_received_buff->state, errno, __FILE__,__LINE__ );
101                                 return;
102                         } else {
103                                 std::cout << "The Message Received is:" << (char*)parent->_xapp_received_buff->payload <<std::endl;
104                                 std::cout << "The Message Received Type is:" << _xapp_received_buff->mtype <<std::endl;
105                                 _xapp_send_buff = msgproc(_xapp_received_buff);
106                                 if(_xapp_send_buff !=NULL)
107                                         xapp_rmr_rts();
108                                 //sleep(10);
109                                 //_xapp_received_buff = NULL;
110
111                         }
112
113                 }
114             return;
115 }
116
117 #endif /* XAPP_RMR_XAPP_RMR_H_ */