ifw-daq  2.1.0-pre1
IFW Data Acquisition modules
requestor.hpp
Go to the documentation of this file.
1 /**
2  * @file
3  * @ingroup daq_ocm_serverctl
4  * @copyright ESO - European Southern Observatory
5  *
6  * @brief
7  */
8 #ifndef OCF_OCM_SERVERCTL_REQUESTOR_HPP_
9 #define OCF_OCM_SERVERCTL_REQUESTOR_HPP_
10 
11 #include <iostream>
12 #include <type_traits>
13 #include <unordered_map>
14 #include <vector>
15 
16 #include <CLI/App.hpp>
17 #include <boost/asio/io_service.hpp>
18 
19 #include <Daqif.hpp>
20 #include <Stdif.hpp>
21 #include <rad/logger.hpp>
22 
23 std::ostream& operator<<(std::ostream& os, std::shared_ptr<daqif::DaqStatus> const& s);
24 std::ostream& operator<<(std::ostream& os, std::shared_ptr<daqif::DaqReply> const& s);
25 std::ostream& operator<<(std::ostream& os, std::vector<std::shared_ptr<daqif::DaqStatus>> const& s);
26 std::ostream&
27 operator<<(std::ostream& os, std::vector<std::shared_ptr<daqif::AwaitDaqReply>> const& s);
28 
29 template <class T>
30 struct JsonPrint {
31  JsonPrint(T const& t_arg) : t(t_arg) {
32  }
33  T const& t;
34 };
35 
36 std::ostream& operator<<(std::ostream& os, JsonPrint<std::shared_ptr<daqif::DaqStatus>> const& s);
37 std::ostream& operator<<(std::ostream& os, JsonPrint<std::shared_ptr<daqif::DaqReply>> const& s);
38 std::ostream&
39 operator<<(std::ostream& os, JsonPrint<std::shared_ptr<daqif::AwaitDaqReply>> const& s);
40 std::ostream& operator<<(std::ostream& os, JsonPrint<std::string> const& s);
41 std::ostream&
42 operator<<(std::ostream& os, JsonPrint<std::vector<std::shared_ptr<daqif::DaqStatus>>> const& s);
43 
44 struct CommonArgs {
45  bool json = false;
46  bool status = false;
47  unsigned timeout = 10;
48  std::string req_addr;
49  std::string pub_addr;
50  elt::mal::Mal* mal;
51 
52  CommonArgs() = default;
53  CommonArgs(CommonArgs const&) = delete;
54  CommonArgs& operator=(CommonArgs const&) = delete;
55 };
56 
57 struct Requestor {
58  explicit Requestor(CommonArgs& args);
59  virtual ~Requestor() {
60  }
61  /**
62  * Add arguments to command.
63  */
64  virtual void AddOptions(CLI::App* app);
65 
66  /**
67  * Receives a parsed variables_map that include the options added with AddOptions.
68  */
69  virtual void
70  Handle(::stdif::StdCmdsSync& std_cmds, ::daqif::OcmDaqControlSync& daq_cmds, CLI::App* arg) = 0;
71 
72 protected:
74 };
75 
78  virtual void AddOptions(CLI::App* app) override;
79  virtual void Handle(::stdif::StdCmdsSync& std_cmds,
80  ::daqif::OcmDaqControlSync& daq_cmds,
81  CLI::App* arg) override;
82 
83 private:
84  std::string m_logger;
85  std::string m_level;
86 };
87 
88 /**
89  * Simple requestor for commands without argument
90  *
91  * @tparam Func is either a StdCmdsSync or OcmDaqControlSync member function pointer, taking no
92  * arguments and returning a type that has an ostream << operator: E.g.: `using Func =
93  * decltype(&daqif::StdCmds::Enable)`;
94  */
95 template <class Func>
97  SimpleRequestor(Func func, CommonArgs& args) : Requestor(args), m_func(func) {
98  }
99  void Handle(::stdif::StdCmdsSync& std_cmds,
100  ::daqif::OcmDaqControlSync& daq_cmds,
101  CLI::App* arg) override {
102  if constexpr (std::is_invocable_v<Func, decltype(std_cmds)>) {
103  auto reply = (std_cmds.*m_func)();
104  if (m_args.json) {
105  std::cout << JsonPrint(reply);
106  } else {
107  std::cout << "Reply: " << reply << std::endl;
108  }
109  } else if constexpr (std::is_invocable_v<Func, decltype(daq_cmds)>) {
110  auto reply = (daq_cmds.*m_func)();
111  if (m_args.json) {
112  std::cout << JsonPrint(reply);
113  } else {
114  std::cout << "Reply: " << reply << std::endl;
115  }
116  } else {
117  }
118  }
119 
120  Func m_func;
121 };
122 
123 /**
124  * Simple Daq commands that accepts a single argument `id` and returns a shared_ptr type
125  * that can be formatted with ostream<< operator when dereferenced.
126  */
127 template <class Func>
129  SimpleDaqRequestor(Func func, CommonArgs& args) : Requestor(args), m_func(func) {
130  }
131 
132  void AddOptions(CLI::App* sub) {
133  sub->add_option("id", m_id, "DAQ ID");
134  }
135 
136  void Handle(::stdif::StdCmdsSync& std_cmds,
137  ::daqif::OcmDaqControlSync& daq_cmds,
138  CLI::App* arg) override {
139  auto reply = (daq_cmds.*m_func)(m_id);
140  if (m_args.json) {
141  std::cout << JsonPrint(reply);
142  } else {
143  std::cout << "Reply: " << reply << std::endl;
144  }
145  }
146 
147 private:
148  std::string m_id;
149  Func m_func;
150 };
151 
152 template <class Func>
154  NoArgRequestor(Func func, CommonArgs& args) : Requestor(args), m_func(func) {
155  }
156 
157  void Handle(::stdif::StdCmdsSync& std_cmds,
158  ::daqif::OcmDaqControlSync& daq_cmds,
159  CLI::App* arg) override {
160  auto reply = (daq_cmds.*m_func)();
161  if (m_args.json) {
162  std::cout << JsonPrint(reply);
163  } else {
164  std::cout << "Reply: " << reply << std::endl;
165  }
166  }
167 
168  Func m_func;
169 };
170 
172  std::string m_id;
173  std::string m_prefix;
174  std::string m_primary_sources;
175  std::string m_metadata_sources;
176  std::string m_properties;
177 
178  using Requestor::Requestor;
179 
180  void AddOptions(CLI::App* app) override;
181  void Handle(::stdif::StdCmdsSync& std_cmds,
182  ::daqif::OcmDaqControlSync& daq_cmds,
183  CLI::App* arg) override;
184 
185 private:
186 };
187 
189  using Requestor::Requestor;
190 
191  void AddOptions(CLI::App* app) override;
192  void Handle(::stdif::StdCmdsSync& std_cmds,
193  ::daqif::OcmDaqControlSync& daq_cmds,
194  CLI::App* arg) override;
195 
196 private:
197  std::string m_id;
198  std::string m_keywords;
199 };
200 
202  using Requestor::Requestor;
203 
204  void AddOptions(CLI::App* app) override;
205  void Handle(::stdif::StdCmdsSync& std_cmds,
206  ::daqif::OcmDaqControlSync& daq_cmds,
207  CLI::App* arg) override;
208 
209 private:
210  std::string m_id;
211  std::string m_state;
212  std::string m_substate;
213  double m_timeout;
214 };
215 
216 #endif // #ifndef OCF_OCM_SERVERCTL_REQUESTOR_HPP_
SimpleDaqRequestor::AddOptions
void AddOptions(CLI::App *sub)
Add arguments to command.
Definition: requestor.hpp:132
CommonArgs::status
bool status
Definition: requestor.hpp:46
CommonArgs
Definition: requestor.hpp:44
NoArgRequestor::m_func
Func m_func
Definition: requestor.hpp:168
Requestor::Requestor
Requestor(CommonArgs &args)
Definition: requestor.cpp:129
JsonPrint
Definition: requestor.hpp:30
SimpleDaqRequestor::SimpleDaqRequestor
SimpleDaqRequestor(Func func, CommonArgs &args)
Definition: requestor.hpp:129
CommonArgs::req_addr
std::string req_addr
Definition: requestor.hpp:48
AwaitStateRequestor::Handle
void Handle(::stdif::StdCmdsSync &std_cmds, ::daqif::OcmDaqControlSync &daq_cmds, CLI::App *arg) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.cpp:205
SimpleRequestor
Simple requestor for commands without argument.
Definition: requestor.hpp:96
Requestor::AddOptions
virtual void AddOptions(CLI::App *app)
Add arguments to command.
Definition: requestor.cpp:132
CommonArgs::json
bool json
Definition: requestor.hpp:45
NoArgRequestor::Handle
void Handle(::stdif::StdCmdsSync &std_cmds, ::daqif::OcmDaqControlSync &daq_cmds, CLI::App *arg) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.hpp:157
StartDaqRequestor
Definition: requestor.hpp:171
UpdateKeywordsRequestor::AddOptions
void AddOptions(CLI::App *app) override
Add arguments to command.
Definition: requestor.cpp:174
NoArgRequestor
Definition: requestor.hpp:153
StartDaqRequestor::m_primary_sources
std::string m_primary_sources
Definition: requestor.hpp:174
operator<<
std::ostream & operator<<(std::ostream &os, std::shared_ptr< daqif::DaqStatus > const &s)
Definition: requestor.cpp:18
SimpleDaqRequestor::Handle
void Handle(::stdif::StdCmdsSync &std_cmds, ::daqif::OcmDaqControlSync &daq_cmds, CLI::App *arg) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.hpp:136
CommonArgs::CommonArgs
CommonArgs(CommonArgs const &)=delete
JsonPrint::t
T const & t
Definition: requestor.hpp:33
SimpleDaqRequestor
Simple Daq commands that accepts a single argument id and returns a shared_ptr type that can be forma...
Definition: requestor.hpp:128
CommonArgs::mal
elt::mal::Mal * mal
Definition: requestor.hpp:50
StartDaqRequestor::Handle
void Handle(::stdif::StdCmdsSync &std_cmds, ::daqif::OcmDaqControlSync &daq_cmds, CLI::App *arg) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.cpp:162
UpdateKeywordsRequestor::Handle
void Handle(::stdif::StdCmdsSync &std_cmds, ::daqif::OcmDaqControlSync &daq_cmds, CLI::App *arg) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.cpp:179
CommonArgs::timeout
unsigned timeout
Definition: requestor.hpp:47
JsonPrint::JsonPrint
JsonPrint(T const &t_arg)
Definition: requestor.hpp:31
StartDaqRequestor::m_properties
std::string m_properties
Definition: requestor.hpp:176
AwaitStateRequestor::AddOptions
void AddOptions(CLI::App *app) override
Add arguments to command.
Definition: requestor.cpp:190
SimpleRequestor::Handle
void Handle(::stdif::StdCmdsSync &std_cmds, ::daqif::OcmDaqControlSync &daq_cmds, CLI::App *arg) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.hpp:99
CommonArgs::operator=
CommonArgs & operator=(CommonArgs const &)=delete
StartDaqRequestor::m_id
std::string m_id
Definition: requestor.hpp:172
Requestor::Handle
virtual void Handle(::stdif::StdCmdsSync &std_cmds, ::daqif::OcmDaqControlSync &daq_cmds, CLI::App *arg)=0
Receives a parsed variables_map that include the options added with AddOptions.
SetLogLevelRequestor::AddOptions
virtual void AddOptions(CLI::App *app) override
Add arguments to command.
Definition: requestor.cpp:135
CommonArgs::CommonArgs
CommonArgs()=default
SetLogLevelRequestor
Definition: requestor.hpp:76
StartDaqRequestor::m_prefix
std::string m_prefix
Definition: requestor.hpp:173
Requestor::~Requestor
virtual ~Requestor()
Definition: requestor.hpp:59
SimpleRequestor::m_func
Func m_func
Definition: requestor.hpp:120
SetLogLevelRequestor::Handle
virtual void Handle(::stdif::StdCmdsSync &std_cmds, ::daqif::OcmDaqControlSync &daq_cmds, CLI::App *arg) override
Receives a parsed variables_map that include the options added with AddOptions.
Definition: requestor.cpp:140
StartDaqRequestor::AddOptions
void AddOptions(CLI::App *app) override
Add arguments to command.
Definition: requestor.cpp:154
CommonArgs::pub_addr
std::string pub_addr
Definition: requestor.hpp:49
Requestor::m_args
CommonArgs & m_args
Definition: requestor.hpp:73
AwaitStateRequestor
Definition: requestor.hpp:201
SimpleRequestor::SimpleRequestor
SimpleRequestor(Func func, CommonArgs &args)
Definition: requestor.hpp:97
NoArgRequestor::NoArgRequestor
NoArgRequestor(Func func, CommonArgs &args)
Definition: requestor.hpp:154
StartDaqRequestor::m_metadata_sources
std::string m_metadata_sources
Definition: requestor.hpp:175
UpdateKeywordsRequestor
Definition: requestor.hpp:188
Requestor
Definition: requestor.hpp:57