Belle II Software development
NSMCommunicator.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8// revisions
9//
10// 20191119 - following currently unused functions are removed
11// void NSMCommunicator::callContext()
12// handling nsmlib_recv inside a user program is against the usage of NSM2
13// const std::string NSMCommunicator::getNodeHost(const std::string& nodename)
14// making hostname visible to application is against the philosophy of NSM2
15// const std::string NSMCommunicator::getNodeHost()
16// accessing NSM2 internal structure is against the philosophy of NSM2
17
18#include "daq/slc/nsm/NSMCommunicator.h"
19
20#include <daq/slc/base/TimeoutException.h>
21#include <daq/slc/nsm/NSMCallback.h>
22#include <daq/slc/nsm/NSMHandlerException.h>
23#include <daq/slc/nsm/NSMNotConnectedException.h>
24
25extern "C" {
26#include <nsm2/nsm2.h>
27#include <nsm2/nsmlib2.h>
28#include <nsm2/belle2nsm.h>
29}
30
31#include <cmath>
32#include <cstdio>
33#include <cstring>
34
35#include <sys/select.h>
36#include <errno.h>
37
38#include <netinet/in.h>
39#include <arpa/inet.h>
40#include <daq/slc/system/LockGuard.h>
41
42#define NSM_DEBUGMODE 1
43
44using namespace Belle2;
45
46NSMCommunicatorList NSMCommunicator::g_comm;
47Mutex NSMCommunicator::g_mutex;
48Mutex NSMCommunicator::g_mutex_select;
49
50NSMCommunicator& NSMCommunicator::select(double usec)
51{
52 fd_set fds;
53 int ret;
54 FD_ZERO(&fds);
55 int highest = 0;
56 LockGuard lockGuard(g_mutex_select);
57 for (NSMCommunicatorList::iterator it = g_comm.begin();
58 it != g_comm.end(); ++it) {
59 NSMCommunicator& com(*(*it));
60 if (com.m_nsmc != NULL) {
61 FD_SET(com.m_nsmc->sock, &fds);
62 if (highest < com.m_nsmc->sock)
63 highest = com.m_nsmc->sock;
64 }
65 }
66 while (true) {
67 errno = 0;
68 if (usec >= 0) {
69 double s = 0;
70 double us = modf(usec, &s);
71 timeval t = {(long)s, (long)(us * 1000000)};
72 ret = ::select(highest + 1, &fds, NULL, NULL, &t);
73 } else {
74 ret = ::select(highest + 1, &fds, NULL, NULL, NULL);
75 }
76 if (ret != -1 || (errno != EINTR && errno != EAGAIN)) break;
77 }
78 if (ret < 0) {
79 throw (NSMHandlerException("Failed to select"));
80 }
81 for (NSMCommunicatorList::iterator it = g_comm.begin();
82 it != g_comm.end(); ++it) {
83 NSMCommunicator& com(*(*it));
84 if (com.m_nsmc != NULL) {
85 if (FD_ISSET(com.m_nsmc->sock, &fds)) {
86 com.m_message.read(com.m_nsmc);
87 com.m_message.setRequestName();
88 b2nsm_context(com.m_nsmc);
89 return com;
90 }
91 }
92 }
93 throw (TimeoutException("NSMCommunicator::select was timed out"));
94}
95
96NSMCommunicator& NSMCommunicator::connected(const std::string& node)
97{
98 for (NSMCommunicatorList::iterator it = g_comm.begin();
99 it != g_comm.end(); ++it) {
100 NSMCommunicator& com(*(*it));
101 if (com.isConnected(node)) return com;
102 }
103 throw (NSMNotConnectedException("No connection for " + node));
104}
105
106bool NSMCommunicator::send(const NSMMessage& msg)
107{
108 bool sent = false;
109#if NSM_PACKAGE_VERSION >= 1914
110 bool alive = false;
111 LockGuard lockGuard(g_mutex);
112 std::string emsg;
113 for (NSMCommunicatorList::iterator it = g_comm.begin();
114 it != g_comm.end(); ++it) {
115 NSMCommunicator& com(*(*it));
116 const char* node = msg.getNodeName();
117 if (node == NULL || strlen(node) == 0) continue;
118 if (com.isConnected(node)) {
119 alive = true;
120 const char* req = msg.getRequestName();
121 if (req != NULL && strlen(req) > 0) {
122 b2nsm_context(com.m_nsmc);
123 if (b2nsm_sendany(node, req, msg.getNParams(), const_cast<int*>(msg.getParams()),
124 msg.getLength(), msg.getData(), NULL) < 0) {
125 emsg = b2nsm_strerror();
126 }
127 sent = true;
128 }
129 }
130 }
131 if (alive && !sent) {
132 throw (NSMHandlerException("Failed to send request: " + emsg));
133 }
134#else
135#warning "Wrong version of nsm2. try source daq/slc/extra/nsm2/export.sh"
136#endif
137 return sent;
138}
139
140NSMCommunicator::NSMCommunicator(const std::string& host, int port) : m_host(host)
141{
142 m_id = -1;
143 m_nsmc = NULL;
144 m_callback = NULL;
145 m_port = port;
146}
147
148NSMCommunicator::NSMCommunicator(NSMcontext* nsmc)
149{
150 m_nsmc = nsmc;
151 m_id = m_nsmc->nodeid;
152 m_callback = NULL;
153}
154
155void NSMCommunicator::init(const NSMNode& node,
156 const std::string& host, int port)
157{
158#if NSM_PACKAGE_VERSION >= 1914
159 LockGuard lockGuard(g_mutex);
160 if (host.size() > 0) m_host = host;
161 if (port > 0) m_port = port;
162 if (node.getName().size() == 0) {
163 throw (NSMHandlerException("Error during init2 (nodename is empty)"));
164 }
165 m_nsmc = b2nsm_init2(node.getName().c_str(), 0, host.c_str(), port, port);
166 if (m_nsmc == NULL) {
167 m_id = -1;
168 throw (NSMHandlerException("Error during init2 (%s=>%s:%d): %s",
169 node.getName().c_str(), host.c_str(),
170 m_port, b2nsm_strerror()));
171 }
172 g_comm.push_back(this);
173 nsmlib_usesig(m_nsmc, 0);
174 m_id = m_nsmc->nodeid;
175 m_node = node;
176#else
177#warning "Wrong version of nsm2. try source daq/slc/extra/nsm2/export.sh"
178#endif
179}
180
181NSMCallback& NSMCommunicator::getCallback()
182{
183 if (m_callback) {
184 return *m_callback;
185 }
186 throw (std::out_of_range("No callback was registered"));
187}
188
189void NSMCommunicator::setCallback(NSMCallback* callback)
190{
191 b2nsm_context(m_nsmc);
192 if (callback != NULL) {
193 m_callback = callback;
194 NSMCallback::NSMCommandList& req_v(callback->getCommandList());
195 for (NSMCallback::NSMCommandList::iterator it = req_v.begin();
196 it != req_v.end(); ++it) {
197 const NSMCommand& command(*it);
198 if (b2nsm_callback(command.getLabel(), NULL) < 0) {
199 m_id = -1;
200 throw (NSMHandlerException("Failed to register callback (%s)",
201 command.getLabel()));
202 }
203 }
204 }
205}
206
207int NSMCommunicator::getNodeIdByName(const std::string& name)
208{
209#if NSM_PACKAGE_VERSION >= 1914
210 b2nsm_context(m_nsmc);
211 return b2nsm_nodeid(name.c_str());
212#else
213 return -1;
214#endif
215}
216
217const std::string NSMCommunicator::getNodeNameById(int id)
218{
219 const char* name = nsmlib_nodename(m_nsmc, id);
220 if (name == NULL) return "";
221 return name;
222}
223
224int NSMCommunicator::getNodePidByName(const std::string& name)
225{
226#if NSM_PACKAGE_VERSION >= 1914
227 b2nsm_context(m_nsmc);
228 return b2nsm_nodepid(name.c_str());
229#else
230 return -1;
231#endif
232}
233
234bool NSMCommunicator::isConnected(const std::string& node)
235{
236 bool is_online = getNodeIdByName(node) >= 0 &&
237 getNodePidByName(node) > 0;
238 return is_online;
239}
240
241NSMMessage NSMCommunicator::popQueue()
242{
243 NSMMessage msg = m_msg_q.front();
244 m_msg_q.pop();
245 return msg;
246}
247
248void NSMCommunicator::setMessage(const NSMMessage& msg)
249{
250 m_message = msg;
251}
Abstract base class for different kinds of events.