Belle II Software  release-05-01-25
AbstractNSMCallback.h
1 #ifndef _Belle2_AbstractNSMCallback_hh
2 #define _Belle2_AbstractNSMCallback_hh
3 
4 #include "daq/slc/nsm/Callback.h"
5 #include "daq/slc/nsm/NSMMessage.h"
6 #include "daq/slc/nsm/NSMNode.h"
7 #include "daq/slc/nsm/NSMCommand.h"
8 
9 #include <string>
10 #include <vector>
11 
12 namespace Belle2 {
18  class NSMCommunicator;
19  typedef std::map<std::string, NSMNode> NSMNodeMap;
20  typedef std::map<std::string, NSMNodeMap> NSMNodeMapMap;
21 
22  class AbstractNSMCallback : public Callback {
23 
24  friend class NSMCallback;
25 
26  public:
27  AbstractNSMCallback(int timeout);
28  virtual ~AbstractNSMCallback() {}
29 
30  public:
31  int addDB(const DBObject& obj);
32  bool get(const NSMNode& node, const std::string& name, int& val, int timeout = 5);
33  bool get(const NSMNode& node, const std::string& name, float& val, int timeout = 5);
34  bool get(const NSMNode& node, const std::string& name, std::string& val, int timeout = 5);
35  bool get(const NSMNode& node, const std::string& name, std::vector<int>& val, int timeout = 5);
36  bool get(const NSMNode& node, const std::string& name, std::vector<float>& val, int timeout = 5);
37  bool set(const NSMNode& node, const std::string& name, int val, int timeout = 5);
38  bool set(const NSMNode& node, const std::string& name, float val, int timeout = 5);
39  bool set(const NSMNode& node, const std::string& name, const std::string& val, int timeout = 5);
40  bool set(const NSMNode& node, const std::string& name, const std::vector<int>& val, int timeout = 5);
41  bool set(const NSMNode& node, const std::string& name, const std::vector<float>& val, int timeout = 5);
42  bool get(const NSMNode& node, NSMVHandler* handler, int timeout = 5);
43  bool get(DBObject& obj);
44  bool get(const std::string& name, int& val) { return get("", name, val); }
45  bool get(const std::string& name, float& val) { return get("", name, val); }
46  bool get(const std::string& name, std::string& val) { return get("", name, val); }
47  bool get(const std::string& name, std::vector<int>& val) { return get("", name, val); }
48  bool get(const std::string& name, std::vector<float>& val) { return get("", name, val); }
49  bool set(const std::string& name, int val) { return set("", name, val); }
50  bool set(const std::string& name, float val) { return set("", name, val); }
51  bool set(const std::string& name, const std::string& val) { return set("", name, val); }
52  bool set(const std::string& name, const std::vector<int>& val) { return set("", name, val); }
53  bool set(const std::string& name, const std::vector<float>& val) { return set("", name, val); }
54  bool get(const std::string& node, const std::string& name, int& val) { return get_t(node, name, val); }
55  bool get(const std::string& node, const std::string& name, float& val) { return get_t(node, name, val); }
56  bool get(const std::string& node, const std::string& name, std::string& val) { return get_t(node, name, val); }
57  bool get(const std::string& node, const std::string& name, std::vector<int>& val) { return get_t(node, name, val); }
58  bool get(const std::string& node, const std::string& name, std::vector<float>& val) { return get_t(node, name, val); }
59  bool set(const std::string& node, const std::string& name, int val) { return set_t(node, name, val); }
60  bool set(const std::string& node, const std::string& name, float val) { return set_t(node, name, val); }
61  bool set(const std::string& node, const std::string& name, const std::string& val) { return set_t(node, name, val); }
62  bool set(const std::string& node, const std::string& name, const std::vector<int>& val) { return set_t(node, name, val); }
63  bool set(const std::string& node, const std::string& name, const std::vector<float>& val) { return set_t(node, name, val); }
64  int wait(double timeout = 5);
65  NSMCommunicator& wait(const NSMNode& node, const NSMCommand& cmd,
66  double timeout = 5);
67  virtual void notify(const NSMVar& var) = 0;
68  bool try_wait();
69 
70  public:
71  NSMNode& getNode() { return m_node; }
72  const NSMNode& getNode() const { return m_node; }
73  void setNode(const NSMNode& node) { m_node = node; }
74  int getTimeout() const { return m_timeout; }
75  void setTimeout(int timeout) { m_timeout = timeout; }
76 
77  protected:
78  virtual bool perform(NSMCommunicator& com) = 0;
79  void readVar(const NSMMessage& msg, NSMVar& var);
80 
81  private:
82  NSMNode m_node;
83  int m_timeout;
84  NSMNodeMapMap m_node_v_m;
85 
86  public:
87  bool get(const NSMNode& node, NSMVar& var, int timeout = 5) ;
88  bool set(const NSMNode& node, const NSMVar& var, int timeout = 5);
89 
90  private:
91  template<typename T>
92  bool get_t(const NSMNode& node, const std::string& name,
93  T& val, int timeout, NSMVar::Type type = NSMVar::NONE, int len = 0)
94  {
95  NSMVar var(name);
96  var.setNode(node.getName());
97  if (get(node, var, timeout)) {
98  if (type == NSMVar::NONE ||
99  (type == var.getType() &&
100  ((len == 0 && var.getLength() == 0) ||
101  (len > 0 && var.getLength() > 0)))) {
102  var >> val;
103  return true;
104  }
105  }
106  return false;
107  }
108  template<typename T>
109  bool get_t(const std::string& node, const std::string& name, T& val)
110  {
111  NSMVHandler* handler = getHandler_p(node, name);
112  if (handler) {
113  NSMVar var;
114  handler->handleGet(var);
115  var >> val;
116  return true;
117  }
118  return false;
119  }
120  template<typename T>
121  bool set_t(const std::string& node, const std::string& name, const T& val)
122  {
123  NSMVHandler* handler = getHandler_p(node, name);
124  if (handler) {
125  handler->set(val);
126  if (node.size() == 0) {
127  notify(handler->get());
128  }
129  return true;
130  }
131  return false;
132  }
133  };
134 
136 };
137 
138 #endif
Belle2::NSMVHandler
Definition: NSMVHandler.h:15
Belle2::Callback
Definition: Callback.h:17
Belle2::NSMNode
Definition: NSMNode.h:14
Belle2::DBObject
Definition: DBObject.h:14
Belle2::AbstractNSMCallback
Definition: AbstractNSMCallback.h:22
Belle2::NSMCommunicator
Definition: NSMCommunicator.h:25
Belle2::NSMMessage
Definition: NSMMessage.h:29
Belle2::NSMCallback
Definition: NSMCallback.h:24
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::NSMCommand
Definition: NSMCommand.h:12
Belle2::NSMVar
Definition: NSMVar.h:16