2 ==================================================================================
4 Copyright (c) 2018-2019 AT&T Intellectual Property.
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
20 #ifndef XAPP_RMR_XAPP_RMR_H_
21 #define XAPP_RMR_XAPP_RMR_H_
25 #define likely(x) __builtin_expect((x), 1)
26 #define unlikely(x) __builtin_expect((x), 0)
29 #define unlikely(x) (x)
43 #include <sys/epoll.h>
45 #include <rmr/RIC_message_types.h>
46 #include <mdclog/mdclog.h>
53 int32_t payload_length;
54 unsigned char xid[RMR_MAX_XID]; //space for user transaction id.
55 unsigned char sid[RMR_MAX_SID]; //sender ID for return to sender needs.(ACKS required)
56 unsigned char src[RMR_MAX_SRC]; //name of the sender (source)
62 std::string _proto_port;
67 rmr_mbuf_t* _xapp_send_buff; // send buffer
68 rmr_mbuf_t* _xapp_received_buff; // received buffer
73 XappRmr(std::string, int rmrattempts=10);
75 void xapp_rmr_init(void);
77 template <class MessageProcessor>
78 void xapp_rmr_receive(MessageProcessor&&, XappRmr *parent);
79 bool xapp_rmr_send(xapp_rmr_header*, void*);
82 void set_listen(bool);
83 bool get_listen(void);
84 int get_is_ready(void);
85 bool get_isRunning(void);
86 void* get_rmr_context(void);
90 // main workhorse thread which does the listen->process->respond loop
91 template <class MsgHandler>
92 void XappRmr::xapp_rmr_receive(MsgHandler&& msgproc, XappRmr *parent){
94 bool* resend = new bool(false);
96 std::thread::id my_id = std::this_thread::get_id();
97 std::stringstream thread_id;
102 // Get the rmr context from parent (all threads and parent use same rmr context. rmr context is expected to be thread safe)
103 if(!parent->get_is_ready()){
104 mdclog_write( MDCLOG_ERR, "RMR Shows Not Ready in RECEIVER, file= %s, line=%d ",__FILE__,__LINE__);
107 void *rmr_context = parent->get_rmr_context();
108 assert(rmr_context != NULL);
110 // Get buffer specific to this thread
111 this->_xapp_received_buff = NULL;
112 this->_xapp_received_buff = rmr_alloc_msg(rmr_context, RMR_DEF_SIZE);
113 assert(this->_xapp_received_buff != NULL);
115 mdclog_write(MDCLOG_INFO, "Starting thread %s", thread_id.str().c_str());
117 while(parent->get_listen()) {
118 mdclog_write(MDCLOG_INFO, "Listening at Thread: %s", thread_id.str().c_str());
120 this->_xapp_received_buff = rmr_rcv_msg( rmr_context, this->_xapp_received_buff );
123 if( this->_xapp_received_buff->mtype < 0 || this->_xapp_received_buff->state != RMR_OK ) {
124 mdclog_write(MDCLOG_ERR, "bad msg: state=%d errno=%d, file= %s, line=%d", this->_xapp_received_buff->state, errno, __FILE__,__LINE__ );
129 mdclog_write(MDCLOG_INFO,"RMR Received Message of Type: %d",this->_xapp_received_buff->mtype);
130 mdclog_write(MDCLOG_INFO,"RMR Received Message: %s",(char*)this->_xapp_received_buff->payload);
132 //in case message handler returns true, need to resend the message.
133 msgproc(this->_xapp_received_buff, resend);
135 rmr_rts_msg(rmr_context, this->_xapp_received_buff );
147 rmr_free_msg(this->_xapp_received_buff);
149 catch(std::runtime_error &e){
150 std::string identifier = __FILE__ + std::string(", Line: ") + std::to_string(__LINE__) ;
151 std::string error_string = identifier = " Error freeing RMR message ";
152 mdclog_write(MDCLOG_ERR, error_string.c_str(), "");
161 #endif /* XAPP_RMR_XAPP_RMR_H_ */