1 #include "daq/slc/nsm/AbstractNSMCallback.h"
3 #include "daq/slc/system/Time.h"
5 #include "daq/slc/base/TimeoutException.h"
6 #include "daq/slc/database/DBObject.h"
8 #include "daq/slc/nsm/NSMCommunicator.h"
14 AbstractNSMCallback::AbstractNSMCallback(
int timeout)
19 int AbstractNSMCallback::addDB(
const DBObject& obj)
21 DBObject::NameValueList list;
22 obj.search(list,
"",
true);
24 for (DBObject::NameValueList::iterator it = list.begin();
25 it != list.end(); it++) {
26 const std::string& name(it->name);
27 if (name.size() == 0 || name.at(0) ==
'$')
continue;
30 id = Callback::add(
new NSMVHandlerInt(name,
true,
true, (
int) * ((
bool*)it->buf)));
31 set(name, (
int) * ((
bool*)it->buf));
34 id = Callback::add(
new NSMVHandlerInt(name,
true,
true, (
int) * ((
char*)it->buf)));
35 set(name, (
int) * ((
char*)it->buf));
38 id = Callback::add(
new NSMVHandlerInt(name,
true,
true, (
int) * ((
short*)it->buf)));
39 set(name, (
int) * ((
short*)it->buf));
42 id = Callback::add(
new NSMVHandlerInt(name,
true,
true, *((
int*)it->buf)));
43 set(name, (
int) * ((
int*)it->buf));
46 id = Callback::add(
new NSMVHandlerInt(name,
true,
true, (
int) * ((
long long*)it->buf)));
47 set(name, (
int) * ((
long long*)it->buf));
50 id = Callback::add(
new NSMVHandlerFloat(name,
true,
true, *((
float*)it->buf)));
51 set(name, * ((
float*)it->buf));
54 id = Callback::add(
new NSMVHandlerFloat(name,
true,
true, (
float) * ((
double*)it->buf)));
55 set(name, (
float) * ((
double*)it->buf));
58 id = Callback::add(
new NSMVHandlerText(name,
true,
true, *((std::string*)it->buf)));
59 set(name, *((std::string*)it->buf));
72 double t0 =
Time().get();
75 double t1 = (timeout - (t - t0) > 0 ? timeout - (t - t0) : 0);
78 const std::string reqname = msg.getRequestName();
79 if ((cmd == NSMCommand::UNKNOWN || cmd == reqname) &&
80 (node.getName().size() == 0 || msg.getNodeName() == node.getName())) {
88 int AbstractNSMCallback::wait(
double timeout)
90 double t0 =
Time().get();
95 double t1 = (timeout - (t - t0) > 0 ? timeout - (t - t0) : 0);
100 if (cmd == NSMCommand::VGET || cmd == NSMCommand::VLISTGET) {
112 bool AbstractNSMCallback::try_wait()
115 perform(wait(
NSMNode(), NSMCommand::UNKNOWN, 0));
116 }
catch (
const std::exception& e) {
124 const int* pars = msg.getParams();
125 const char* node = msg.getData();
126 const char* name = (msg.getData() + pars[2] + 1);
127 const char* value = (msg.getData() + pars[2] + 1 + pars[3] + 1);
128 var =
NSMVar(name, (NSMVar::Type)pars[0], pars[1], value);
131 var.setDate(pars[5]);
137 if (handler && node.getName().size() > 0) {
138 NSMVar var(handler->get());
140 const std::string name = handler->getName();
141 NSMCommunicator::send(
NSMMessage(node, NSMCommand::VGET, name));
142 if (timeout < 0)
return true;
143 double t0 =
Time().get();
145 double tout = timeout;
147 double t1 = (tout - (t - t0) > 0 ? tout - (t - t0) : 0);
151 if (cmd == NSMCommand::VSET) {
152 if (node.getName() == msg.getData() &&
153 var.getName() == (msg.getData() + msg.getParam(2) + 1)) {
155 return handler->handleSet(var);
157 }
else if (cmd == NSMCommand::VGET || cmd == NSMCommand::VLISTGET ||
158 cmd == NSMCommand::OK || cmd == NSMCommand::ERROR || cmd == NSMCommand::FATAL) {
169 bool AbstractNSMCallback::get(
const NSMNode& node,
NSMVar& var,
172 if (node.getName().size() > 0) {
173 const std::string name = var.getName();
174 NSMCommunicator::send(
NSMMessage(node, NSMCommand::VGET, name));
175 if (timeout < 0)
return true;
176 double t0 =
Time().get();
178 double tout = timeout;
180 double t1 = (tout - (t - t0) > 0 ? tout - (t - t0) : 0);
184 if (cmd == NSMCommand::VSET) {
185 if (msg.getLength() > 0 && msg.getData() != NULL &&
186 node.getName() == msg.getData() &&
187 var.getName() == (msg.getData() + msg.getParam(2) + 1)) {
191 }
else if (cmd == NSMCommand::VGET || cmd == NSMCommand::VLISTGET ||
192 cmd == NSMCommand::OK || cmd == NSMCommand::ERROR || cmd == NSMCommand::FATAL) {
203 bool AbstractNSMCallback::set(
const NSMNode& node,
const NSMVar& var,
206 if (node.getName().size() > 0) {
208 if (timeout < 0)
return true;
209 double t0 =
Time().get();
211 double tout = timeout;
213 double t1 = (tout - (t - t0) > 0 ? tout - (t - t0) : 0);
217 if (cmd == NSMCommand::VREPLY && var.getName() == msg.getData()) {
218 bool ret = msg.getParam(0) > 0;
221 }
else if (cmd == NSMCommand::VGET || cmd == NSMCommand::VLISTGET ||
222 cmd == NSMCommand::OK || cmd == NSMCommand::ERROR || cmd == NSMCommand::FATAL) {
233 bool AbstractNSMCallback::get(
const NSMNode& node,
const std::string& name,
234 int& val,
int timeout)
236 return get_t(node, name, val, timeout, NSMVar::INT, 0);
239 bool AbstractNSMCallback::get(
const NSMNode& node,
const std::string& name,
240 float& val,
int timeout)
242 return get_t(node, name, val, timeout, NSMVar::FLOAT, 0);
245 bool AbstractNSMCallback::get(
const NSMNode& node,
const std::string& name,
246 std::string& val,
int timeout)
248 return get_t(node, name, val, timeout, NSMVar::TEXT, 1);
251 bool AbstractNSMCallback::get(
const NSMNode& node,
const std::string& name,
252 std::vector<int>& val,
int timeout)
254 return get_t(node, name, val, timeout, NSMVar::INT, 1);
257 bool AbstractNSMCallback::get(
const NSMNode& node,
const std::string& name,
258 std::vector<float>& val,
int timeout)
260 return get_t(node, name, val, timeout, NSMVar::FLOAT, 1);
263 bool AbstractNSMCallback::set(
const NSMNode& node,
const std::string& name,
264 int val,
int timeout)
266 return set(node,
NSMVar(name, val), timeout);
269 bool AbstractNSMCallback::set(
const NSMNode& node,
const std::string& name,
270 float val,
int timeout)
272 return set(node,
NSMVar(name, val), timeout);
275 bool AbstractNSMCallback::set(
const NSMNode& node,
const std::string& name,
276 const std::string& val,
int timeout)
278 return set(node,
NSMVar(name, val), timeout);
281 bool AbstractNSMCallback::set(
const NSMNode& node,
const std::string& name,
282 const std::vector<int>& val,
int timeout)
284 return set(node,
NSMVar(name, val), timeout);
287 bool AbstractNSMCallback::set(
const NSMNode& node,
const std::string& name,
288 const std::vector<float>& val,
int timeout)
290 return set(node,
NSMVar(name, val), timeout);
293 bool AbstractNSMCallback::get(
DBObject& obj)
295 DBObject::NameValueList list;
297 for (DBObject::NameValueList::iterator it = list.begin();
298 it != list.end(); it++) {
299 const std::string& name(it->name);
300 if (name.size() == 0 || name.at(0) ==
'$')
continue;
306 if (get(name, vi)) *((
bool*)it->buf) = (bool)vi;
309 if (get(name, vi)) *((
char*)it->buf) = (
char)vi;
312 if (get(name, vi)) *((
short*)it->buf) = (
short)vi;
315 if (get(name, vi)) *((
int*)it->buf) = (
int)vi;
318 if (get(name, vi)) *((
long long*)it->buf) = vi;
321 if (get(name, vf)) *((
float*)it->buf) = vf;
323 case DBField::DOUBLE:
324 if (get(name, vf)) *((
double*)it->buf) = vf;
327 if (get(name, vs)) *((std::string*)it->buf) = vs;