2 * Copyright 2020 AT&T Intellectual Property
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 // Created by adi ENZEL on 2/12/20.
35 #define MAX_RECEIVED_BUFFER 8192
37 class RmrException: public std::exception
41 explicit RmrException(std::string msg) : msg(std::move(msg)){}
43 const char* what() const noexcept override {
52 void *rmrCtx = nullptr;
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");
62 rmr_set_stimeout(rmrCtx, 0); // disable retries for any send operation
63 cout << "Wait for RMR_Ready" << endl;
67 if ((rmrReady = rmr_ready(rmrCtx)) == 0) {
71 if (count % 60 == 0) {
72 cout << "Wait for RMR ready state : " << count << " seconds" << endl;
75 cout << "RMR running" << endl;
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;
90 throw RmrException("Failed to add RMR descriptor to epoll.");
95 RmrClient(const char *address, int epoll_fd) {
97 getRmrContext(address, epoll_fd);
98 } catch (RmrException &e) {
99 cout << e.what() << endl;
104 RmrClient() = delete;
105 RmrClient(const RmrClient &) = delete;
106 RmrClient &operator=(const RmrClient &) = delete;
107 RmrClient &operator&&(const RmrClient &) = delete;
110 inline void *getRmrCtx() const {
114 int getRmrFd() const {
118 inline rmr_mbuf_t *allocateRmrMsg(int size) {return (rmr_alloc_msg(rmrCtx, size));}
120 static inline void freeRmrMsg(rmr_mbuf_t *msg) {rmr_free_msg(msg);}