11 #include "daq/slc/nsm/NSMCommunicator.h"
13 #include <daq/slc/base/TimeoutException.h>
14 #include <daq/slc/nsm/NSMCallback.h>
15 #include <daq/slc/nsm/NSMHandlerException.h>
16 #include <daq/slc/nsm/NSMNotConnectedException.h>
19 #include <nsm2/nsm2.h>
20 #include <nsm2/nsmlib2.h>
21 #include <nsm2/belle2nsm.h>
28 #include <sys/select.h>
31 #include <netinet/in.h>
32 #include <arpa/inet.h>
33 #include <daq/slc/system/LockGuard.h>
35 #define NSM_DEBUGMODE 1
39 NSMCommunicatorList NSMCommunicator::g_comm;
40 Mutex NSMCommunicator::g_mutex;
41 Mutex NSMCommunicator::g_mutex_select;
50 for (NSMCommunicatorList::iterator it = g_comm.begin();
51 it != g_comm.end(); it++) {
53 if (com.m_nsmc != NULL) {
54 FD_SET(com.m_nsmc->sock, &fds);
55 if (highest < com.m_nsmc->sock)
56 highest = com.m_nsmc->sock;
63 double us = modf(usec, &s);
64 timeval t = {(long)s, (
long)(us * 1000000)};
65 ret = ::select(highest + 1, &fds, NULL, NULL, &t);
67 ret = ::select(highest + 1, &fds, NULL, NULL, NULL);
69 if (ret != -1 || (errno != EINTR && errno != EAGAIN))
break;
74 for (NSMCommunicatorList::iterator it = g_comm.begin();
75 it != g_comm.end(); it++) {
77 if (com.m_nsmc != NULL) {
78 if (FD_ISSET(com.m_nsmc->sock, &fds)) {
79 com.m_message.read(com.m_nsmc);
80 com.m_message.setRequestName();
81 b2nsm_context(com.m_nsmc);
91 for (NSMCommunicatorList::iterator it = g_comm.begin();
92 it != g_comm.end(); it++) {
94 if (com.isConnected(node))
return com;
99 bool NSMCommunicator::send(
const NSMMessage& msg)
103 #if NSM_PACKAGE_VERSION >= 1914
106 for (NSMCommunicatorList::iterator it = g_comm.begin();
107 it != g_comm.end(); it++) {
109 const char* node = msg.getNodeName();
110 if (com.isConnected(node)) {
112 const char* req = msg.getRequestName();
113 if (node != NULL && req != NULL &&
114 strlen(node) > 0 && strlen(req) > 0) {
115 b2nsm_context(com.m_nsmc);
116 if (b2nsm_sendany(node, req, msg.getNParams(), (
int*)msg.getParams(),
117 msg.getLength(), msg.getData(), NULL) < 0) {
118 emsg = b2nsm_strerror();
124 if (alive && !sent) {
128 #warning "Wrong version of nsm2. try source daq/slc/extra/nsm2/export.sh"
133 NSMCommunicator::NSMCommunicator(
const std::string& host,
int port)
142 NSMCommunicator::NSMCommunicator(
NSMcontext* nsmc)
145 m_id = m_nsmc->nodeid;
149 void NSMCommunicator::init(
const NSMNode& node,
150 const std::string& host,
int port)
152 #if NSM_PACKAGE_VERSION >= 1914
154 if (host.size() > 0) m_host = host;
155 if (port > 0) m_port = port;
156 if (node.getName().size() == 0) {
159 m_nsmc = b2nsm_init2(node.getName().c_str(), 0, host.c_str(), port, port);
160 if (m_nsmc == NULL) {
163 node.getName().c_str(), host.c_str(),
164 m_port, b2nsm_strerror()));
166 g_comm.push_back(
this);
167 nsmlib_usesig(m_nsmc, 0);
168 m_id = m_nsmc->nodeid;
171 #warning "Wrong version of nsm2. try source daq/slc/extra/nsm2/export.sh"
180 throw (std::out_of_range(
"No callback was registered"));
183 void NSMCommunicator::setCallback(
NSMCallback* callback)
185 b2nsm_context(m_nsmc);
186 if (callback != NULL) {
187 m_callback = callback;
188 NSMCallback::NSMCommandList& req_v(callback->getCommandList());
189 for (NSMCallback::NSMCommandList::iterator it = req_v.begin();
190 it != req_v.end(); it++) {
192 if (b2nsm_callback(command.getLabel(), NULL) < 0) {
195 command.getLabel()));
201 int NSMCommunicator::getNodeIdByName(
const std::string& name)
203 #if NSM_PACKAGE_VERSION >= 1914
204 b2nsm_context(m_nsmc);
205 return b2nsm_nodeid(name.c_str());
211 const std::string NSMCommunicator::getNodeNameById(
int id)
213 const char* name = nsmlib_nodename(m_nsmc,
id);
214 if (name == NULL)
return "";
218 int NSMCommunicator::getNodePidByName(
const std::string& name)
220 #if NSM_PACKAGE_VERSION >= 1914
221 b2nsm_context(m_nsmc);
222 return b2nsm_nodepid(name.c_str());
228 bool NSMCommunicator::isConnected(
const std::string& node)
230 bool is_online = getNodeIdByName(node) >= 0 &&
231 getNodePidByName(node) > 0;
242 void NSMCommunicator::setMessage(
const NSMMessage& msg)