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)
 
NSMMessagegetMessage ()
 
const NSMMessagegetMessage () const
 
void setMessage (const NSMMessage &msg)
 
const NSMNodegetNode () const
 
int getNodeIdByName (const std::string &name)
 
int getNodePidByName (const std::string &name)
 
const std::string & getHostName ()
 
int getPort ()
 
NSMCallbackgetCallback ()
 
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 NSMCommunicatorselect (double sec)
 
static NSMCommunicatorconnected (const std::string &node)
 
static NSMCommunicatorList & get ()
 
static bool send (const NSMMessage &msg)
 

Private Types

typedef std::vector< NSMCommunicator * > NSMCommunicatorList
 

Private Member Functions

NSMcontextgetContext ()
 
bool sendRequest (const NSMMessage &msg)
 

Private Attributes

int m_id
 
NSMcontextm_nsmc
 
NSMCallbackm_callback
 
NSMMessage m_message
 
std::string m_host
 
int m_port
 
NSMNode m_node
 
std::queue< NSMMessagem_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 
)

Definition at line 140 of file NSMCommunicator.cc.

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

◆ NSMCommunicator() [2/2]

NSMCommunicator ( NSMcontext nsmc)
explicit

Definition at line 149 of file NSMCommunicator.cc.

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

◆ ~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 182 of file NSMCommunicator.cc.

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

◆ 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 208 of file NSMCommunicator.cc.

209{
210#if NSM_PACKAGE_VERSION >= 1914
211 b2nsm_context(m_nsmc);
212 return b2nsm_nodeid(name.c_str());
213#else
214 return -1;
215#endif
216}

◆ getNodeNameById()

const std::string getNodeNameById ( int  id)

Definition at line 218 of file NSMCommunicator.cc.

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

◆ getNodePidByName()

int getNodePidByName ( const std::string &  name)

Definition at line 225 of file NSMCommunicator.cc.

226{
227#if NSM_PACKAGE_VERSION >= 1914
228 b2nsm_context(m_nsmc);
229 return b2nsm_nodepid(name.c_str());
230#else
231 return -1;
232#endif
233}

◆ 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 156 of file NSMCommunicator.cc.

158{
159#if NSM_PACKAGE_VERSION >= 1914
160 LockGuard lockGuard(g_mutex);
161 if (host.size() > 0) m_host = host;
162 if (port > 0) m_port = port;
163 if (node.getName().size() == 0) {
164 throw (NSMHandlerException("Error during init2 (nodename is empty)"));
165 }
166 m_nsmc = b2nsm_init2(node.getName().c_str(), 0, host.c_str(), port, port);
167 if (m_nsmc == NULL) {
168 m_id = -1;
169 throw (NSMHandlerException("Error during init2 (%s=>%s:%d): %s",
170 node.getName().c_str(), host.c_str(),
171 m_port, b2nsm_strerror()));
172 }
173 g_comm.push_back(this);
174 nsmlib_usesig(m_nsmc, 0);
175 m_id = m_nsmc->nodeid;
176 m_node = node;
177#else
178#warning "Wrong version of nsm2. try source daq/slc/extra/nsm2/export.sh"
179#endif
180}
Lock Guard for a Mutex instance.
Definition: LockGuard.h:47

◆ isConnected()

bool isConnected ( const std::string &  node)

Definition at line 235 of file NSMCommunicator.cc.

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

◆ popQueue()

NSMMessage popQueue ( )

Definition at line 242 of file NSMCommunicator.cc.

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

◆ 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 (com.isConnected(node)) {
118 alive = true;
119 const char* req = msg.getRequestName();
120 if (node != NULL && req != NULL &&
121 strlen(node) > 0 && strlen(req) > 0) {
122 b2nsm_context(com.m_nsmc);
123 if (b2nsm_sendany(node, req, msg.getNParams(), (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 190 of file NSMCommunicator.cc.

191{
192 b2nsm_context(m_nsmc);
193 if (callback != NULL) {
194 m_callback = callback;
195 NSMCallback::NSMCommandList& req_v(callback->getCommandList());
196 for (NSMCallback::NSMCommandList::iterator it = req_v.begin();
197 it != req_v.end(); ++it) {
198 NSMCommand& command(*it);
199 if (b2nsm_callback(command.getLabel(), NULL) < 0) {
200 m_id = -1;
201 throw (NSMHandlerException("Failed to register callback (%s)",
202 command.getLabel()));
203 }
204 }
205 }
206}

◆ 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 249 of file NSMCommunicator.cc.

250{
251 m_message = msg;
252}

Friends And Related Function 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
private

Definition at line 96 of file NSMCommunicator.h.


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