8#include "daq/slc/nsm/NSMMessage.h"
15#include <nsm2/nsmsys2.h>
22#include <daq/slc/nsm/NSMData.h>
23#include <daq/slc/nsm/NSMHandlerException.h>
25#include <daq/slc/base/Reader.h>
26#include <daq/slc/base/Writer.h>
28#include <nsm2/nsmlib2.h>
29#include <nsm2/nsmsys2.h>
35const unsigned int NSMMessage::DATA_SIZE = NSM_TCPDATSIZ;
37void NSMMessage::init()
40 memset(&m_nsm_msg, 0,
sizeof(
NSMmsg));
47 m_nodename = node.getName();
48 m_reqname = NSMCommand::VSET.getLabel();
50 m_nsm_msg.pars[0] = (int)var.getType();
51 m_nsm_msg.pars[1] = (int)var.getLength();
52 m_nsm_msg.pars[2] = var.getNode().size();
53 m_nsm_msg.pars[3] = var.getName().size();
54 m_nsm_msg.pars[4] = var.getId();
55 m_nsm_msg.pars[5] = var.getDate();
56 if (var.getType() != NSMVar::NONE) {
57 int size = var.getNode().size() + 1 + var.getName().size() + 1 + var.size();
59 char* pdata = (
char*)m_data.ptr();
60 memset(pdata, 0, size);
61 memcpy(pdata, var.getNode().c_str(), var.getNode().size());
62 pdata += var.getNode().size();
65 memcpy(pdata, var.getName().c_str(), var.getName().size());
66 pdata += var.getName().size();
69 memcpy(pdata, var.get(), var.size());
78 m_nodename = node.getName();
79 m_reqname = NSMCommand::LOG.getLabel();
81 setParam(0, (
int)log.getPriority());
82 setParam(1, log.getDateInt());
83 setParam(2, log.getId());
84 setData(log.getMessage());
91 m_nodename = node.getName();
92 m_reqname = cmd.getLabel();
94 setParam(0, (
int)log.getPriority());
95 setParam(1, log.getDateInt());
96 setParam(2, log.getId());
97 setData(log.getMessage());
103 m_nodename = node.getName();
104 m_reqname = NSMCommand::DATASET.getLabel();
106 setParam(0, data.getRevision());
107 setParam(1, data.getSize());
108 std::string s = data.getName() +
"\n" + data.getFormat();
109 int len = s.size() + 1 + data.getSize();
112 memcpy(m_data.ptr(), s.c_str(), s.size());
113 memcpy(m_data.ptr() + s.size() + 1, data.get(), data.getSize());
117NSMMessage::NSMMessage()
122NSMMessage::NSMMessage(
const NSMNode& node)
125 m_nodename = node.getName();
128NSMMessage::NSMMessage(
const NSMNode& node,
132 m_nodename = node.getName();
133 m_reqname = cmd.getLabel();
136NSMMessage::NSMMessage(
const NSMNode& node,
141 m_nodename = node.getName();
142 m_reqname = cmd.getLabel();
143 m_nsm_msg.npar = npar;
144 memcpy(m_nsm_msg.pars, pars,
sizeof(
int) * npar);
147NSMMessage::NSMMessage(
const NSMNode& node,
150 const std::string& data)
153 m_nodename = node.getName();
154 m_reqname = cmd.getLabel();
155 m_nsm_msg.npar = npar;
156 memcpy(m_nsm_msg.pars, pars,
sizeof(
int) * npar);
160NSMMessage::NSMMessage(
const NSMNode& node,
162 int par,
const std::string& obj)
165 m_nodename = node.getName();
166 m_reqname = cmd.getLabel();
168 m_nsm_msg.pars[0] = par;
176 m_reqname = cmd.getLabel();
178 m_nsm_msg.pars[0] = par;
182 const std::string& data)
185 m_reqname = cmd.getLabel();
189NSMMessage::NSMMessage(
const NSMNode& node,
193 m_nodename = node.getName();
194 m_reqname = cmd.getLabel();
196 m_nsm_msg.pars[0] = par;
199NSMMessage::NSMMessage(
const NSMNode& node,
201 const std::string& data)
204 m_nodename = node.getName();
205 m_reqname = cmd.getLabel();
212 m_reqname = cmd.getLabel();
221NSMMessage::NSMMessage(
const NSMNode& node,
const NSMVar& var)
239 init(node, log, cmd);
242NSMMessage::NSMMessage(
const NSMVar& var)
247NSMMessage::NSMMessage(
const NSMData& data)
261 m_reqname = cmd.getLabel();
262 m_nsm_msg.npar = npar;
263 memcpy(m_nsm_msg.pars, pars,
sizeof(
int) * npar);
269 memcpy(&m_nsm_msg, &(msg.m_nsm_msg),
sizeof(m_nsm_msg));
270 m_nsm_msg.npar = msg.m_nsm_msg.npar;
271 m_nsm_msg.len = msg.m_nsm_msg.len;
272 m_nodename = msg.m_nodename;
273 m_reqname = msg.m_reqname;
274 m_hasobj = msg.m_hasobj;
276 m_data =
Buffer(m_nsm_msg.len);
277 memcpy(m_data.ptr(), msg.m_data.ptr(), m_nsm_msg.len);
281const char* NSMMessage::getRequestName()
const
283 if (m_reqname.size() > 0)
return m_reqname.c_str();
284 if (m_nsmc != NULL) {
285 const char* reqname = nsmlib_reqname(m_nsmc, m_nsm_msg.req);
286 if (reqname != NULL) {
289 return m_reqname.c_str();
292 return m_reqname.c_str();
296void NSMMessage::setRequestName()
298 if (m_nsmc != NULL) {
299 const char* reqname = nsmlib_reqname(m_nsmc, m_nsm_msg.req);
300 if (reqname != NULL) {
306void NSMMessage::setRequestName(
const std::string& reqname)
311void NSMMessage::setRequestName(
const NSMCommand& cmd)
313 m_reqname = cmd.getLabel();
316void NSMMessage::setNodeName(
const std::string& nodename)
318 m_nodename = nodename;
321void NSMMessage::setNodeName(
const NSMNode& node)
323 m_nodename = node.getName();
326const char* NSMMessage::getNodeName()
const
328 if (m_nodename.size() > 0)
return m_nodename.c_str();
330 return nsmlib_nodename(m_nsmc, m_nsm_msg.node);
332 return m_nodename.c_str();
335unsigned short NSMMessage::getRequestId()
const
337 return m_nsm_msg.req;
340unsigned short NSMMessage::getSequenceId()
const
342 return m_nsm_msg.seq;
345unsigned short NSMMessage::getNodeId()
const
347 return m_nsm_msg.node;
350unsigned short NSMMessage::getNParams()
const
352 return m_nsm_msg.npar;
355int NSMMessage::getParam(
int i)
const
357 return m_nsm_msg.pars[i];
360#if NSM_PACKAGE_VERSION >= 1914
361const int* NSMMessage::getParams()
const
363 return m_nsm_msg.pars;
366int* NSMMessage::getParams()
368 return m_nsm_msg.pars;
371const unsigned int* NSMMessage::getParams()
const
373 return m_nsm_msg.pars;
376unsigned int* NSMMessage::getParams()
378 return m_nsm_msg.pars;
380#warning "Wrong version of nsm2. try source daq/slc/extra/nsm2/export.sh"
383unsigned int NSMMessage::getLength()
const
385 return m_nsm_msg.len;
388const char* NSMMessage::getData()
const
390 if (m_nsm_msg.len > 0)
return (
const char*)m_data.ptr();
394void NSMMessage::setRequestId(
unsigned short id)
399void NSMMessage::setSequenceId(
unsigned short id)
404void NSMMessage::setNodeId(
unsigned short id)
409void NSMMessage::setNParams(
unsigned short npar)
411 m_nsm_msg.npar = npar;
414void NSMMessage::setParam(
int i,
unsigned int v)
416 m_nsm_msg.pars[i] = v;
419void NSMMessage::setData(
int len,
const char* data)
422 if (len > 0 && data != NULL) {
424 memcpy(m_data.ptr(), data, len);
429void NSMMessage::setData(
const std::string& text)
431 setData(text.size() + 1, text.c_str());
434int NSMMessage::try_read(
int sock,
char* buf,
int datalen)
437 while (recvlen < datalen) {
439 if ((ret = ::read(sock, buf + recvlen, datalen)) <= 0) {
440 if (ret == -1 && errno == EINTR)
continue;
441 if (ret < 0)
return -1;
450 if (nsmc == NULL)
return 0;
452 int sock = nsmc->sock;
457 if ((ret = try_read(sock, (
char*)&hp, datalen)) < 0) {
461 m_nsm_msg.req = ntohs(hp.req);
462 m_nsm_msg.seq = ntohs(hp.seq);
463 m_nsm_msg.node = ntohs(hp.src);
464 m_nsm_msg.npar = hp.npar;
465 m_nsm_msg.len = ntohs(hp.len);
466 m_nsm_msg.pars[0] = m_nsm_msg.pars[1] = 0;
468 datalen =
sizeof(int32) * m_nsm_msg.npar;
470 if ((ret = try_read(sock, (
char*)(m_nsm_msg.pars), datalen)) < 0) {
474 for (
int i = 0; i < m_nsm_msg.npar; i++) {
475 m_nsm_msg.pars[i] = ntohl(m_nsm_msg.pars[i]);
479 datalen = m_nsm_msg.len;
482 if ((ret = try_read(sock, (
char*)m_data.ptr(), datalen)) < 0) {
490void NSMMessage::readObject(
Reader& reader)
492 setRequestName(reader.readString());
493 setNodeName(reader.readString());
494 setNParams(reader.readInt());
495 for (
int i = 0; i < getNParams(); i++) {
496 setParam(i, reader.readInt());
498 size_t len = reader.readInt();
502 reader.read(m_data.ptr(), len);
506void NSMMessage::writeObject(
Writer& writer)
const
508 writer.writeString(getRequestName());
509 writer.writeString(getNodeName());
510 writer.writeInt(getNParams());
511 for (
int i = 0; i < getNParams(); i++) {
512 writer.writeInt(getParam(i));
517 writer.writeInt(getLength());
519 if (getLength() > 0) {
520 writer.write(m_data.ptr(), getLength());
Abstract base class for different kinds of events.