Belle II Software development
NSMCallback.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_NSMCallback_hh
9#define _Belle2_NSMCallback_hh
10
11#include "daq/slc/nsm/AbstractNSMCallback.h"
12#include "daq/slc/nsm/NSMCommand.h"
13#include "daq/slc/nsm/NSMData.h"
14
15#include <string>
16#include <vector>
17#include <map>
18
19namespace Belle2 {
24
25 class NSMCommunicator;
26 class NSMMessage;
27
28 typedef std::map<std::string, NSMNode> NSMNodeMap;
29 typedef std::map<std::string, NSMData> NSMDataMap;
30
31 class NSMCallback : public AbstractNSMCallback {
32
33 friend class NSMCommunicator;
34 friend class NSMNodeDaemon;
35
36 public:
37 explicit NSMCallback(int timeout = 5);
38 virtual ~NSMCallback() override {}
39
40 public:
41 virtual void init(NSMCommunicator&) {}
42 virtual void term() {}
43 virtual void timeout(NSMCommunicator&) {}
44 bool perform(NSMCommunicator& com) override;
45 virtual void ok(const char* /*node*/, const char* /*data*/) {}
46 virtual void fatal(const char* /*node*/, const char* /*data*/) {}
47 virtual void error(const char* /*node*/, const char* /*data*/) {}
48 virtual void logset(const DAQLogMessage&) {}
49 virtual void vget(const std::string& nodename,
50 const std::string& vname);
51 virtual void vset(NSMCommunicator& com, const NSMVar& var);
52 virtual void vlistget(NSMCommunicator& com);
53 virtual void vlistset(NSMCommunicator& com);
54 virtual void vreply(NSMCommunicator&, const std::string&, bool) {}
55
56 public:
57 void reply(const NSMMessage& msg);
58 void log(LogFile::Priority pri, const char* format, ...);
59 void log(LogFile::Priority pri, const std::string& msg);
60 // cppcheck-suppress duplInheritedMember ; the derived class deliberately provides its own
61 int reset();
62
63 public:
64 NSMDataMap& getDataMap() { return m_datas; }
65 NSMData& getData(const std::string& name);
66 NSMData& getData() { return m_data; }
67 void openData(const std::string& name, const std::string& format,
68 int revision = -1);
69 void allocData(const std::string& name, const std::string& format,
70 int revision);
71 const std::string& getCategory() const { return m_category; }
72 void setCategory(const std::string& category) { m_category = category; }
73
74 protected:
75 void notify(const NSMVar& var) override;
76 void reg(const NSMCommand& cmd) { m_cmd_v.push_back(cmd); }
77 void addNode(const NSMNode& node);
78 const NSMNodeMap& getNodes() { return m_nodes; }
79 void setLogNode(const NSMNode& node) { m_lognode = node; }
80 const NSMNode& getLogNode() { return m_lognode; }
81
82 private:
83 typedef std::vector<NSMCommand> NSMCommandList;
84
85 private:
86 int addDefaultHandlers();
87
88 NSMCommandList& getCommandList() { return m_cmd_v; }
89 void alloc_open(NSMCommunicator& com);
90
91 private:
92 NSMCommandList m_cmd_v;
93 NSMNodeMap m_nodes;
94 NSMData m_data;
95 NSMDataMap m_datas;
96 NSMNode m_lognode;
97 std::string m_category;
98
99 };
100
101 inline NSMData& NSMCallback::getData(const std::string& name)
102 {
103 if (m_datas.find(name) != m_datas.end()) {
104 return m_datas[name];
105 }
106 throw (std::out_of_range("nodata registered : " + name));
107 }
108
109 inline void NSMCallback::openData(const std::string& name,
110 const std::string& format,
111 int revision)
112 {
113 if (m_datas.find(name) == m_datas.end()) {
114 m_datas.insert(NSMDataMap::value_type(name, NSMData(name, format, revision)));
115 }
116 }
117
118 inline void NSMCallback::allocData(const std::string& name,
119 const std::string& format,
120 int revision)
121 {
122 m_data = NSMData(name, format, revision);
123 }
124
126};
127
128#endif
Abstract base class for different kinds of events.