18#include "daq/slc/nsm/NSMCommunicator.h"
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>
27#include <nsm2/nsmlib2.h>
28#include <nsm2/belle2nsm.h>
35#include <sys/select.h>
38#include <netinet/in.h>
40#include <daq/slc/system/LockGuard.h>
42#define NSM_DEBUGMODE 1
46NSMCommunicatorList NSMCommunicator::g_comm;
47Mutex NSMCommunicator::g_mutex;
48Mutex NSMCommunicator::g_mutex_select;
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;
70 double us = modf(usec, &s);
71 timeval t = {(long)s, (
long)(us * 1000000)};
72 ret = ::select(highest + 1, &fds, NULL, NULL, &t);
74 ret = ::select(highest + 1, &fds, NULL, NULL, NULL);
76 if (ret != -1 || (errno != EINTR && errno != EAGAIN))
break;
79 throw (NSMHandlerException(
"Failed to select"));
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);
93 throw (TimeoutException(
"NSMCommunicator::select was timed out"));
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;
103 throw (NSMNotConnectedException(
"No connection for " + node));
106bool NSMCommunicator::send(
const NSMMessage& msg)
109#if NSM_PACKAGE_VERSION >= 1914
111 LockGuard lockGuard(g_mutex);
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)) {
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();
131 if (alive && !sent) {
132 throw (NSMHandlerException(
"Failed to send request: " + emsg));
135#warning "Wrong version of nsm2. try source daq/slc/extra/nsm2/export.sh"
140NSMCommunicator::NSMCommunicator(
const std::string& host,
int port) : m_host(host)
148NSMCommunicator::NSMCommunicator(NSMcontext* nsmc)
151 m_id = m_nsmc->nodeid;
155void NSMCommunicator::init(
const NSMNode& node,
156 const std::string& host,
int port)
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)"));
165 m_nsmc = b2nsm_init2(node.getName().c_str(), 0, host.c_str(), port, port);
166 if (m_nsmc == NULL) {
168 throw (NSMHandlerException(
"Error during init2 (%s=>%s:%d): %s",
169 node.getName().c_str(), host.c_str(),
170 m_port, b2nsm_strerror()));
172 g_comm.push_back(
this);
173 nsmlib_usesig(m_nsmc, 0);
174 m_id = m_nsmc->nodeid;
177#warning "Wrong version of nsm2. try source daq/slc/extra/nsm2/export.sh"
186 throw (std::out_of_range(
"No callback was registered"));
189void NSMCommunicator::setCallback(
NSMCallback* callback)
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) {
200 throw (NSMHandlerException(
"Failed to register callback (%s)",
201 command.getLabel()));
207int NSMCommunicator::getNodeIdByName(
const std::string& name)
209#if NSM_PACKAGE_VERSION >= 1914
210 b2nsm_context(m_nsmc);
211 return b2nsm_nodeid(name.c_str());
217const std::string NSMCommunicator::getNodeNameById(
int id)
219 const char* name = nsmlib_nodename(m_nsmc,
id);
220 if (name == NULL)
return "";
224int NSMCommunicator::getNodePidByName(
const std::string& name)
226#if NSM_PACKAGE_VERSION >= 1914
227 b2nsm_context(m_nsmc);
228 return b2nsm_nodepid(name.c_str());
234bool NSMCommunicator::isConnected(
const std::string& node)
236 bool is_online = getNodeIdByName(node) >= 0 &&
237 getNodePidByName(node) > 0;
243 NSMMessage msg = m_msg_q.front();
248void NSMCommunicator::setMessage(
const NSMMessage& msg)
Abstract base class for different kinds of events.