8#include "daq/slc/nsm/AbstractNSMCallback.h"
10#include "daq/slc/system/Time.h"
12#include "daq/slc/base/TimeoutException.h"
13#include "daq/slc/database/DBObject.h"
15#include "daq/slc/nsm/NSMCommunicator.h"
21AbstractNSMCallback::AbstractNSMCallback(
int timeout)
26int AbstractNSMCallback::addDB(
const DBObject& obj)
28 DBObject::NameValueList list;
29 obj.search(list,
"",
true);
31 for (DBObject::NameValueList::iterator it = list.begin();
32 it != list.end(); ++it) {
33 const std::string& name(it->name);
34 if (name.size() == 0 || name.at(0) ==
'$')
continue;
37 id = Callback::add(
new NSMVHandlerInt(name,
true,
true, (
int) * ((
bool*)it->buf)));
38 set(name, (
int) * ((
bool*)it->buf));
41 id = Callback::add(
new NSMVHandlerInt(name,
true,
true, (
int) * ((
char*)it->buf)));
42 set(name, (
int) * ((
char*)it->buf));
45 id = Callback::add(
new NSMVHandlerInt(name,
true,
true, (
int) * ((
short*)it->buf)));
46 set(name, (
int) * ((
short*)it->buf));
49 id = Callback::add(
new NSMVHandlerInt(name,
true,
true, *((
int*)it->buf)));
50 set(name, (
int) * ((
int*)it->buf));
53 id = Callback::add(
new NSMVHandlerInt(name,
true,
true, (
int) * ((
long long*)it->buf)));
54 set(name, (
int) * ((
long long*)it->buf));
57 id = Callback::add(
new NSMVHandlerFloat(name,
true,
true, *((
float*)it->buf)));
58 set(name, * ((
float*)it->buf));
61 id = Callback::add(
new NSMVHandlerFloat(name,
true,
true, (
float) * ((
double*)it->buf)));
62 set(name, (
float) * ((
double*)it->buf));
65 id = Callback::add(
new NSMVHandlerText(name,
true,
true, *((std::string*)it->buf)));
66 set(name, *((std::string*)it->buf));
79 double t0 =
Time().get();
82 double t1 = (timeout - (t - t0) > 0 ? timeout - (t - t0) : 0);
85 const std::string reqname = msg.getRequestName();
86 if ((cmd == NSMCommand::UNKNOWN || cmd == reqname) &&
87 (node.getName().size() == 0 || msg.getNodeName() == node.getName())) {
95int AbstractNSMCallback::wait(
double timeout)
97 double t0 =
Time().get();
102 double t1 = (timeout - (t - t0) > 0 ? timeout - (t - t0) : 0);
107 if (cmd == NSMCommand::VGET || cmd == NSMCommand::VLISTGET) {
119bool AbstractNSMCallback::try_wait()
122 perform(wait(
NSMNode(), NSMCommand::UNKNOWN, 0));
123 }
catch (
const std::exception& e) {
131 const int* pars = msg.getParams();
132 const char* node = msg.getData();
133 const char* name = (msg.getData() + pars[2] + 1);
134 const char* value = (msg.getData() + pars[2] + 1 + pars[3] + 1);
135 var =
NSMVar(name, (NSMVar::Type)pars[0], pars[1], value);
138 var.setDate(pars[5]);
144 if (handler && node.getName().size() > 0) {
145 NSMVar var(handler->get());
147 const std::string name = handler->getName();
148 NSMCommunicator::send(
NSMMessage(node, NSMCommand::VGET, name));
149 if (timeout < 0)
return true;
150 double t0 =
Time().get();
152 double tout = timeout;
154 double t1 = (tout - (t - t0) > 0 ? tout - (t - t0) : 0);
158 if (cmd == NSMCommand::VSET) {
159 if (node.getName() == msg.getData() &&
160 var.getName() == (msg.getData() + msg.getParam(2) + 1)) {
162 return handler->handleSet(var);
164 }
else if (cmd == NSMCommand::VGET || cmd == NSMCommand::VLISTGET ||
165 cmd == NSMCommand::OK || cmd == NSMCommand::ERROR || cmd == NSMCommand::FATAL) {
176bool AbstractNSMCallback::get(
const NSMNode& node,
NSMVar& var,
179 if (node.getName().size() > 0) {
180 const std::string name = var.getName();
181 NSMCommunicator::send(
NSMMessage(node, NSMCommand::VGET, name));
182 if (timeout < 0)
return true;
183 double t0 =
Time().get();
185 double tout = timeout;
187 double t1 = (tout - (t - t0) > 0 ? tout - (t - t0) : 0);
191 if (cmd == NSMCommand::VSET) {
192 if (msg.getLength() > 0 && msg.getData() != NULL &&
193 node.getName() == msg.getData() &&
194 var.getName() == (msg.getData() + msg.getParam(2) + 1)) {
198 }
else if (cmd == NSMCommand::VGET || cmd == NSMCommand::VLISTGET ||
199 cmd == NSMCommand::OK || cmd == NSMCommand::ERROR || cmd == NSMCommand::FATAL) {
210bool AbstractNSMCallback::set(
const NSMNode& node,
const NSMVar& var,
213 if (node.getName().size() > 0) {
215 if (timeout < 0)
return true;
216 double t0 =
Time().get();
218 double tout = timeout;
220 double t1 = (tout - (t - t0) > 0 ? tout - (t - t0) : 0);
224 if (cmd == NSMCommand::VREPLY && var.getName() == msg.getData()) {
225 bool ret = msg.getParam(0) > 0;
228 }
else if (cmd == NSMCommand::VGET || cmd == NSMCommand::VLISTGET ||
229 cmd == NSMCommand::OK || cmd == NSMCommand::ERROR || cmd == NSMCommand::FATAL) {
240bool AbstractNSMCallback::get(
const NSMNode& node,
const std::string& name,
241 int& val,
int timeout)
243 return get_t(node, name, val, timeout, NSMVar::INT, 0);
246bool AbstractNSMCallback::get(
const NSMNode& node,
const std::string& name,
247 float& val,
int timeout)
249 return get_t(node, name, val, timeout, NSMVar::FLOAT, 0);
252bool AbstractNSMCallback::get(
const NSMNode& node,
const std::string& name,
253 std::string& val,
int timeout)
255 return get_t(node, name, val, timeout, NSMVar::TEXT, 1);
258bool AbstractNSMCallback::get(
const NSMNode& node,
const std::string& name,
259 std::vector<int>& val,
int timeout)
261 return get_t(node, name, val, timeout, NSMVar::INT, 1);
264bool AbstractNSMCallback::get(
const NSMNode& node,
const std::string& name,
265 std::vector<float>& val,
int timeout)
267 return get_t(node, name, val, timeout, NSMVar::FLOAT, 1);
270bool AbstractNSMCallback::set(
const NSMNode& node,
const std::string& name,
271 int val,
int timeout)
273 return set(node,
NSMVar(name, val), timeout);
276bool AbstractNSMCallback::set(
const NSMNode& node,
const std::string& name,
277 float val,
int timeout)
279 return set(node,
NSMVar(name, val), timeout);
282bool AbstractNSMCallback::set(
const NSMNode& node,
const std::string& name,
283 const std::string& val,
int timeout)
285 return set(node,
NSMVar(name, val), timeout);
288bool AbstractNSMCallback::set(
const NSMNode& node,
const std::string& name,
289 const std::vector<int>& val,
int timeout)
291 return set(node,
NSMVar(name, val), timeout);
294bool AbstractNSMCallback::set(
const NSMNode& node,
const std::string& name,
295 const std::vector<float>& val,
int timeout)
297 return set(node,
NSMVar(name, val), timeout);
300bool AbstractNSMCallback::get(
const DBObject& obj)
302 DBObject::NameValueList list;
304 for (DBObject::NameValueList::iterator it = list.begin();
305 it != list.end(); ++it) {
306 const std::string& name(it->name);
307 if (name.size() == 0 || name.at(0) ==
'$')
continue;
313 if (get(name, vi)) *((
bool*)it->buf) = (bool)vi;
316 if (get(name, vi)) *((
char*)it->buf) = (
char)vi;
319 if (get(name, vi)) *((
short*)it->buf) = (
short)vi;
322 if (get(name, vi)) *((
int*)it->buf) = (
int)vi;
325 if (get(name, vi)) *((
long long*)it->buf) = vi;
328 if (get(name, vf)) *((
float*)it->buf) = vf;
330 case DBField::DOUBLE:
331 if (get(name, vf)) *((
double*)it->buf) = vf;
334 if (get(name, vs)) *((std::string*)it->buf) = vs;
Abstract base class for different kinds of events.