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