version 4.0.0 first support for new E2 ASN
[ric-plt/e2.git] / RIC-E2-TERMINATION / TEST / testAsn / rmrClient / rmrClient.h
1 /*
2  * Copyright 2020 AT&T Intellectual Property
3  * Copyright 2020 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 //
19 // Created by adi ENZEL on 2/12/20.
20 //
21
22 #include <iostream>
23 #include <string>
24 #include <cstring>
25 #include <exception>
26 #include <utility>
27
28 #include <unistd.h>
29
30 //rmr testing thread
31 #include <rmr/rmr.h>
32
33
34 using namespace std;
35 #define MAX_RECEIVED_BUFFER 8192
36
37 class RmrException: public std::exception
38 {
39     std::string msg;
40 public:
41     explicit RmrException(std::string  msg) : msg(std::move(msg)){}
42
43     const char* what() const  noexcept override {
44         return msg.c_str();
45     }
46 };
47
48
49
50 class RmrClient {
51 private:
52     void *rmrCtx = nullptr;
53     int rmr_fd = 0;
54
55     void getRmrContext(const char *address, int epoll_fd) {
56         rmrCtx = rmr_init((char *)address, MAX_RECEIVED_BUFFER, RMRFL_NONE);
57         if (rmrCtx == nullptr) {
58             cerr << "Failed to initialize RMR. address = " << address << endl;
59             throw RmrException("Failed to initialize RMR");
60         }
61
62         rmr_set_stimeout(rmrCtx, 0);    // disable retries for any send operation
63         cout << "Wait for RMR_Ready" << endl;
64         auto rmrReady = 0;
65         auto count = 0;
66         while (!rmrReady) {
67             if ((rmrReady = rmr_ready(rmrCtx)) == 0) {
68                 usleep(1000000);
69             }
70             count++;
71             if (count % 60 == 0) {
72                 cout << "Wait for RMR ready state : " << count << " seconds" << endl;
73             }
74         }
75         cout << "RMR running" << endl;
76
77
78         rmr_init_trace(rmrCtx, 200);
79         // get the RMR fd for the epoll
80         rmr_fd = rmr_get_rcvfd(rmrCtx);
81         struct epoll_event event{};
82         // add RMR fd to epoll
83         event.events = (EPOLLIN);
84         event.data.fd = rmr_fd;
85         // add listening RMR FD to epoll
86         if (epoll_ctl(epoll_fd, EPOLL_CTL_ADD, rmr_fd, &event)) {
87             cerr << "Failed to add RMR descriptor to epoll. " << strerror(errno) << endl;
88             close(rmr_fd);
89             rmr_close(rmrCtx);
90             throw RmrException("Failed to add RMR descriptor to epoll.");
91         }
92     }
93
94 public:
95     RmrClient(const char *address, int epoll_fd)  {
96         try {
97             getRmrContext(address, epoll_fd);
98         } catch (RmrException &e) {
99             cout << e.what() << endl;
100             exit(-1);
101         }
102     }
103
104     RmrClient() = delete;
105     RmrClient(const RmrClient &) = delete;
106     RmrClient &operator=(const RmrClient &) = delete;
107     RmrClient &operator&&(const RmrClient &) = delete;
108
109
110     inline void *getRmrCtx() const {
111         return rmrCtx;
112     }
113
114     int getRmrFd() const {
115         return rmr_fd;
116     }
117
118     inline rmr_mbuf_t *allocateRmrMsg(int size) {return (rmr_alloc_msg(rmrCtx, size));}
119
120     static inline void freeRmrMsg(rmr_mbuf_t *msg) {rmr_free_msg(msg);}
121 };