8#include "daq/slc/nsm/NSMCallback.h"
10#include "daq/slc/system/LogFile.h"
12#include "daq/slc/nsm/NSMCommunicator.h"
13#include "daq/slc/nsm/NSMHandlerException.h"
15#include "daq/slc/version/Version.h"
24NSMCallback::NSMCallback(
const int timeout)
28 reg(NSMCommand::ERROR);
30 reg(NSMCommand::VGET);
31 reg(NSMCommand::VSET);
32 reg(NSMCommand::VREPLY);
33 reg(NSMCommand::VLISTGET);
34 reg(NSMCommand::VLISTSET);
40int NSMCallback::reset()
42 int revision = Callback::reset();
49int NSMCallback::addDefaultHandlers()
51 return add(
new NSMVHandlerText(
"version",
true,
false, DAQ_SLC_VERSION::DAQ_SLC_VERSION),
false,
true);
54void NSMCallback::addNode(
const NSMNode& node)
56 std::string name = node.getName();
57 if (m_nodes.find(name) == m_nodes.end()) {
58 m_nodes.insert(NSMNodeMap::value_type(name, node));
65 for (NSMNodeMap::iterator it = m_nodes.begin();
66 it != m_nodes.end();) {
68 msg_out.setNodeName(node.getName());
69 if (NSMCommunicator::send(msg_out)) {
77void NSMCallback::log(LogFile::Priority pri,
const char* format, ...)
82 vsnprintf(ss,
sizeof(ss), format, ap);
84 log(pri, std::string(ss));
87void NSMCallback::log(LogFile::Priority pri,
const std::string& msg)
89 LogFile::put(pri, msg);
91 if (getLogNode().
getName().size() > 0) {
94 pri, getCategory(), msg)));
97 LogFile::error(e.what());
105 if (cmd == NSMCommand::OK || cmd == NSMCommand::ERROR ||
106 cmd == NSMCommand::FATAL) {
107 if (msg.getLength() > 0) {
108 if (cmd == NSMCommand::OK) {
109 ok(msg.getNodeName(), msg.getData());
110 }
else if (cmd == NSMCommand::ERROR) {
111 error(msg.getNodeName(), msg.getData());
112 }
else if (cmd == NSMCommand::FATAL) {
113 fatal(msg.getNodeName(), msg.getData());
117 }
else if (cmd == NSMCommand::LOG) {
118 int id = com.getNodeIdByName(msg.getNodeName());
121 lmsg.setNodeName(msg.getNodeName());
122 if (msg.getNParams() > 0) {
123 lmsg.setPriority((LogFile::Priority)msg.getParam(0));
125 if (msg.getNParams() > 1) lmsg.setDate(msg.getParam(1));
126 if (msg.getNParams() > 2) lmsg.setCategory(msg.getParam(2));
127 lmsg.setMessage(msg.getLength() > 0 ? msg.getData() :
"");
130 }
else if (cmd == NSMCommand::VGET) {
131 if (msg.getLength() > 0) {
132 vget(msg.getNodeName(), msg.getData());
135 }
else if (cmd == NSMCommand::VSET) {
136 if (msg.getLength() > 0) {
142 }
else if (cmd == NSMCommand::VREPLY) {
143 vreply(com, msg.getData(), msg.getParam(0));
145 }
else if (cmd == NSMCommand::VLISTGET) {
148 }
else if (cmd == NSMCommand::VLISTSET) {
155void NSMCallback::notify(
const NSMVar& var)
157 std::string vname = var.getName();
159 if (var_out.getNode().size() == 0) {
160 var_out.setNode(getNode().
getName());
163 if (m_node_v_m.find(vname) != m_node_v_m.end()) {
165 NSMNodeMap& node_v(m_node_v_m[vname]);
166 for (NSMNodeMap::iterator inode = node_v.begin();
167 inode != node_v.end();) {
169 if (NSMCommunicator::send(
NSMMessage(node, var_out))) {
172 node_v.erase(inode++);
176 LogFile::error(e.what());
181void NSMCallback::vget(
const std::string& nodename,
const std::string& vname)
186 NSMVHandler* handler_p = getHandler_p(nodename, vname);
189 handler.handleGet(var);
190 var.setNode(getNode().
getName());
195 if (m_node_v_m.find(vname) == m_node_v_m.end()) {
196 m_node_v_m.insert(NSMNodeMapMap::value_type(vname, NSMNodeMap()));
198 NSMNodeMap& node_v(m_node_v_m[vname]);
199 if (node_v.find(nodename) == node_v.end()) {
200 node_v.insert(NSMNodeMap::value_type(nodename,
NSMNode(nodename)));
201 std::string filename = (
"/tmp/nsmvget." + StringUtil::tolower(getNode().
getName()));
202 std::ofstream fout(filename.c_str(), std::ios::app);
203 fout << nodename <<
" " << vname << std::endl;
207 LogFile::error(e.what());
215 NSMVHandler* handler_p = getHandler_p(var.getNode(), var.getName());
218 if (var.getName() == handler.getName() &&
219 (var.getNode() == handler.getNode() ||
220 (var.getNode().size() == 0 && handler.getNode() == getNode().
getName()) ||
221 (handler.getNode().size() == 0 && var.getNode() == getNode().
getName())) &&
223 NSMNode node(msg.getNodeName());
225 if ((result = handler.handleSet(var)) &&
226 (var.getNode().size() == 0 || var.getNode() == getNode().
getName())) {
229 if (var.getNode().size() == 0) {
230 NSMCommunicator::send(
NSMMessage(node, NSMCommand::VREPLY,
231 result, var.getName()));
236 LogFile::error(e.what());
242 std::stringstream ss;
245 for (NSMVHandlerList::iterator it = m_handler.begin();
246 it != m_handler.end(); ++it) {
248 if (handler.getNode().size() == 0) {
249 ss << handler.getName() <<
":"
250 << handler.get().getTypeLabel() <<
":"
251 << (int)handler.useGet() <<
":"
252 << (int)handler.useSet() <<
":"
258 std::string nodename = com.getMessage().getNodeName();
261 NSMCommunicator::send(
NSMMessage(node, NSMCommand::VLISTSET,
264 LogFile::error(e.what());
277 std::string data = com.getMessage().getData();
278 StringList s = StringUtil::split(data,
'\n');
279 std::vector<vlistentry_t> vlist;
281 for (
size_t i = 0; i < s.size(); i++) {
282 StringList ss = StringUtil::split(s[i],
':');
285 ss[1], ss[2] ==
"1", ss[3] ==
"1"
288 if (length < ss[0].size()) length = ss[0].size();
291 for (
size_t i = 0; i < vlist.size(); i++) {
293 std::cout << StringUtil::form(StringUtil::form(
"%%-%ds ", length).c_str(), en.name.c_str())
294 <<
" : " << en.type <<
" "
295 << (en.useGet ?
"get " :
"")
296 << (en.useSet ?
"set " :
"") << std::endl;
303 if (!m_data.isAvailable() && m_data.getName().size() > 0 &&
304 m_data.getFormat().size() > 0 && m_data.getRevision() > 0) {
305 m_data.allocate(com);
308 LogFile::warning(e.what());
310 for (NSMDataMap::iterator it = m_datas.begin();
311 it != m_datas.end(); ++it) {
314 if (!data.isAvailable() && data.getName().size() > 0 &&
315 data.getFormat().size() > 0) {
TString getName(const TObject *obj)
human-readable name (e.g.
Abstract base class for different kinds of events.