Belle II Software development
NSMCommunicator Class Reference

Public Member Functions

 NSMCommunicator (const std::string &host="", int port=-1)
 
 NSMCommunicator (NSMcontext *nsmc)
 
void init (const NSMNode &node, const std::string &host, int port)
 
void setCallback (NSMCallback *callback)
 
void callContext ()
 
int getId () const
 
void setId (int id)
 
NSMMessage & getMessage ()
 
const NSMMessage & getMessage () const
 
void setMessage (const NSMMessage &msg)
 
const NSMNode & getNode () const
 
int getNodeIdByName (const std::string &name)
 
int getNodePidByName (const std::string &name)
 
const std::string & getHostName ()
 
int getPort ()
 
NSMCallback & getCallback ()
 
bool isConnected (const std::string &node)
 
const std::string getNodeNameById (int id)
 
void pushQueue (const NSMMessage &msg)
 
bool hasQueue () const
 
NSMMessage popQueue ()
 

Static Public Member Functions

static NSMCommunicator & select (double sec)
 
static NSMCommunicator & connected (const std::string &node)
 
static NSMCommunicatorList & get ()
 
static bool send (const NSMMessage &msg)
 

Private Types

typedef std::vector< NSMCommunicator * > NSMCommunicatorList
 

Private Member Functions

NSMcontext * getContext ()
 
bool sendRequest (const NSMMessage &msg)
 

Private Attributes

int m_id
 
NSMcontext * m_nsmc
 
NSMCallback * m_callback
 
NSMMessage m_message
 
std::string m_host
 
int m_port = 0
 
NSMNode m_node
 
std::queue< NSMMessage > m_msg_q
 

Static Private Attributes

static NSMCommunicatorList g_comm
 
static Mutex g_mutex
 
static Mutex g_mutex_select
 

Friends

class NSMData
 

Detailed Description

Definition at line 32 of file NSMCommunicator.h.

Member Typedef Documentation

◆ NSMCommunicatorList

typedef std::vector<NSMCommunicator*> NSMCommunicatorList
private

Definition at line 34 of file NSMCommunicator.h.

Constructor & Destructor Documentation

◆ NSMCommunicator() [1/2]

NSMCommunicator ( const std::string & host = "",
int port = -1 )
explicit

Definition at line 140 of file NSMCommunicator.cc.

140 : m_host(host)
141{
142 m_id = -1;
143 m_nsmc = NULL;
144 m_callback = NULL;
145 m_port = port;
146}

◆ NSMCommunicator() [2/2]

NSMCommunicator ( NSMcontext * nsmc)
explicit

Definition at line 148 of file NSMCommunicator.cc.

149{
150 m_nsmc = nsmc;
151 m_id = m_nsmc->nodeid;
152 m_callback = NULL;
153}

◆ ~NSMCommunicator()

~NSMCommunicator ( )
inline

Definition at line 52 of file NSMCommunicator.h.

52{}

Member Function Documentation

◆ connected()

NSMCommunicator & connected ( const std::string & node)
static

Definition at line 96 of file NSMCommunicator.cc.

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}

◆ get()

static NSMCommunicatorList & get ( )
inlinestatic

Definition at line 41 of file NSMCommunicator.h.

41{ return g_comm; }

◆ getCallback()

NSMCallback & getCallback ( )

Definition at line 181 of file NSMCommunicator.cc.

182{
183 if (m_callback) {
184 return *m_callback;
185 }
186 throw (std::out_of_range("No callback was registered"));
187}

◆ getContext()

NSMcontext * getContext ( )
inlineprivate

Definition at line 85 of file NSMCommunicator.h.

85{ return m_nsmc; }

◆ getHostName()

const std::string & getHostName ( )
inline

Definition at line 75 of file NSMCommunicator.h.

75{ return m_host; }

◆ getId()

int getId ( ) const
inline

Definition at line 63 of file NSMCommunicator.h.

63{ return m_id; }

◆ getMessage() [1/2]

NSMMessage & getMessage ( )
inline

Definition at line 65 of file NSMCommunicator.h.

65{ return m_message; }

◆ getMessage() [2/2]

const NSMMessage & getMessage ( ) const
inline

Definition at line 66 of file NSMCommunicator.h.

66{ return m_message; }

◆ getNode()

const NSMNode & getNode ( ) const
inline

Definition at line 68 of file NSMCommunicator.h.

68{ return m_node; }

◆ getNodeIdByName()

int getNodeIdByName ( const std::string & name)

Definition at line 207 of file NSMCommunicator.cc.

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}

◆ getNodeNameById()

const std::string getNodeNameById ( int id)

Definition at line 217 of file NSMCommunicator.cc.

218{
219 const char* name = nsmlib_nodename(m_nsmc, id);
220 if (name == NULL) return "";
221 return name;
222}

◆ getNodePidByName()

int getNodePidByName ( const std::string & name)

Definition at line 224 of file NSMCommunicator.cc.

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}

◆ getPort()

int getPort ( )
inline

Definition at line 76 of file NSMCommunicator.h.

76{ return m_port; }

◆ hasQueue()

bool hasQueue ( ) const
inline

Definition at line 81 of file NSMCommunicator.h.

81{ return !m_msg_q.empty(); }

◆ init()

void init ( const NSMNode & node,
const std::string & host,
int port )

Definition at line 155 of file NSMCommunicator.cc.

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}

◆ isConnected()

bool isConnected ( const std::string & node)

Definition at line 234 of file NSMCommunicator.cc.

235{
236 bool is_online = getNodeIdByName(node) >= 0 &&
237 getNodePidByName(node) > 0;
238 return is_online;
239}

◆ popQueue()

NSMMessage popQueue ( )

Definition at line 241 of file NSMCommunicator.cc.

242{
243 NSMMessage msg = m_msg_q.front();
244 m_msg_q.pop();
245 return msg;
246}

◆ pushQueue()

void pushQueue ( const NSMMessage & msg)
inline

Definition at line 80 of file NSMCommunicator.h.

80{ m_msg_q.push(msg); }

◆ select()

NSMCommunicator & select ( double sec)
static

Definition at line 50 of file NSMCommunicator.cc.

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}

◆ send()

bool send ( const NSMMessage & msg)
static

Definition at line 106 of file NSMCommunicator.cc.

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}

◆ setCallback()

void setCallback ( NSMCallback * callback)

Definition at line 189 of file NSMCommunicator.cc.

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}

◆ setId()

void setId ( int id)
inline

Definition at line 64 of file NSMCommunicator.h.

64{ m_id = id; }

◆ setMessage()

void setMessage ( const NSMMessage & msg)

Definition at line 248 of file NSMCommunicator.cc.

249{
250 m_message = msg;
251}

Friends And Related Symbol Documentation

◆ NSMData

friend class NSMData
friend

Definition at line 36 of file NSMCommunicator.h.

Member Data Documentation

◆ g_comm

NSMCommunicatorList g_comm
staticprivate

Definition at line 45 of file NSMCommunicator.h.

◆ g_mutex

Mutex g_mutex
staticprivate

Definition at line 46 of file NSMCommunicator.h.

◆ g_mutex_select

Mutex g_mutex_select
staticprivate

Definition at line 47 of file NSMCommunicator.h.

◆ m_callback

NSMCallback* m_callback
private

Definition at line 93 of file NSMCommunicator.h.

◆ m_host

std::string m_host
private

Definition at line 95 of file NSMCommunicator.h.

◆ m_id

int m_id
private

Definition at line 91 of file NSMCommunicator.h.

◆ m_message

NSMMessage m_message
private

Definition at line 94 of file NSMCommunicator.h.

◆ m_msg_q

std::queue<NSMMessage> m_msg_q
private

Definition at line 98 of file NSMCommunicator.h.

◆ m_node

NSMNode m_node
private

Definition at line 97 of file NSMCommunicator.h.

◆ m_nsmc

NSMcontext* m_nsmc
private

Definition at line 92 of file NSMCommunicator.h.

◆ m_port

int m_port = 0
private

Definition at line 96 of file NSMCommunicator.h.


The documentation for this class was generated from the following files: