FairMQ  1.4.33
C++ Message Queuing Library and Framework
Commands.h
1 /********************************************************************************
2  * Copyright (C) 2017 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
8 
9 #ifndef FAIR_MQ_SDK_COMMANDFACTORY
10 #define FAIR_MQ_SDK_COMMANDFACTORY
11 
12 #include <fairmq/States.h>
13 
14 #include <vector>
15 #include <string>
16 #include <memory>
17 #include <type_traits>
18 #include <stdexcept>
19 
20 namespace fair::mq::sdk::cmd
21 {
22 
23 enum class Format : int {
24  Binary,
25  JSON
26 };
27 
28 enum class Result : int {
29  Ok,
30  Failure
31 };
32 
33 enum class Type : int
34 {
35  check_state, // args: { }
36  change_state, // args: { transition }
37  dump_config, // args: { }
38  subscribe_to_state_change, // args: { }
39  unsubscribe_from_state_change, // args: { }
40  state_change_exiting_received, // args: { }
41  get_properties, // args: { request_id, property_query }
42  set_properties, // args: { request_id, properties }
43  subscription_heartbeat, // args: { interval }
44 
45  current_state, // args: { device_id, current_state }
46  transition_status, // args: { device_id, task_id, Result, transition, current_state }
47  config, // args: { device_id, config_string }
48  state_change_subscription, // args: { device_id, task_id, Result }
49  state_change_unsubscription, // args: { device_id, task_id, Result }
50  state_change, // args: { device_id, task_id, last_state, current_state }
51  properties, // args: { device_id, request_id, Result, properties }
52  properties_set // args: { device_id, request_id, Result }
53 };
54 
55 struct Cmd
56 {
57  explicit Cmd(const Type type) : fType(type) {}
58  virtual ~Cmd() = default;
59 
60  Type GetType() const { return fType; }
61 
62  private:
63  Type fType;
64 };
65 
66 struct CheckState : Cmd
67 {
68  explicit CheckState() : Cmd(Type::check_state) {}
69 };
70 
71 struct ChangeState : Cmd
72 {
73  explicit ChangeState(Transition transition)
74  : Cmd(Type::change_state)
75  , fTransition(transition)
76  {}
77 
78  Transition GetTransition() const { return fTransition; }
79  void SetTransition(Transition transition) { fTransition = transition; }
80 
81  private:
82  Transition fTransition;
83 };
84 
85 struct DumpConfig : Cmd
86 {
87  explicit DumpConfig() : Cmd(Type::dump_config) {}
88 };
89 
90 struct SubscribeToStateChange : Cmd
91 {
92  explicit SubscribeToStateChange(int64_t interval)
93  : Cmd(Type::subscribe_to_state_change)
94  , fInterval(interval)
95  {}
96 
97  int64_t GetInterval() const { return fInterval; }
98  void SetInterval(int64_t interval) { fInterval = interval; }
99 
100  private:
101  int64_t fInterval;
102 };
103 
104 struct UnsubscribeFromStateChange : Cmd
105 {
106  explicit UnsubscribeFromStateChange() : Cmd(Type::unsubscribe_from_state_change) {}
107 };
108 
109 struct StateChangeExitingReceived : Cmd
110 {
111  explicit StateChangeExitingReceived() : Cmd(Type::state_change_exiting_received) {}
112 };
113 
114 struct GetProperties : Cmd
115 {
116  GetProperties(std::size_t request_id, std::string query)
117  : Cmd(Type::get_properties)
118  , fRequestId(request_id)
119  , fQuery(std::move(query))
120  {}
121 
122  auto GetRequestId() const -> std::size_t { return fRequestId; }
123  auto SetRequestId(std::size_t requestId) -> void { fRequestId = requestId; }
124  auto GetQuery() const -> std::string { return fQuery; }
125  auto SetQuery(std::string query) -> void { fQuery = std::move(query); }
126 
127  private:
128  std::size_t fRequestId;
129  std::string fQuery;
130 };
131 
132 struct SetProperties : Cmd
133 {
134  SetProperties(std::size_t request_id, std::vector<std::pair<std::string, std::string>> properties)
135  : Cmd(Type::set_properties)
136  , fRequestId(request_id)
137  , fProperties(std::move(properties))
138  {}
139 
140  auto GetRequestId() const -> std::size_t { return fRequestId; }
141  auto SetRequestId(std::size_t requestId) -> void { fRequestId = requestId; }
142  auto GetProps() const -> std::vector<std::pair<std::string, std::string>> { return fProperties; }
143  auto SetProps(std::vector<std::pair<std::string, std::string>> properties) -> void { fProperties = std::move(properties); }
144 
145  private:
146  std::size_t fRequestId;
147  std::vector<std::pair<std::string, std::string>> fProperties;
148 };
149 
150 struct SubscriptionHeartbeat : Cmd
151 {
152  explicit SubscriptionHeartbeat(int64_t interval)
153  : Cmd(Type::subscription_heartbeat)
154  , fInterval(interval)
155  {}
156 
157  int64_t GetInterval() const { return fInterval; }
158  void SetInterval(int64_t interval) { fInterval = interval; }
159 
160  private:
161  int64_t fInterval;
162 };
163 
164 struct CurrentState : Cmd
165 {
166  explicit CurrentState(const std::string& id, State currentState)
167  : Cmd(Type::current_state)
168  , fDeviceId(id)
169  , fCurrentState(currentState)
170  {}
171 
172  std::string GetDeviceId() const { return fDeviceId; }
173  void SetDeviceId(const std::string& deviceId) { fDeviceId = deviceId; }
174  fair::mq::State GetCurrentState() const { return fCurrentState; }
175  void SetCurrentState(fair::mq::State state) { fCurrentState = state; }
176 
177  private:
178  std::string fDeviceId;
179  fair::mq::State fCurrentState;
180 };
181 
182 struct TransitionStatus : Cmd
183 {
184  explicit TransitionStatus(const std::string& deviceId, const uint64_t taskId, const Result result, const Transition transition, State currentState)
185  : Cmd(Type::transition_status)
186  , fDeviceId(deviceId)
187  , fTaskId(taskId)
188  , fResult(result)
189  , fTransition(transition)
190  , fCurrentState(currentState)
191  {}
192 
193  std::string GetDeviceId() const { return fDeviceId; }
194  void SetDeviceId(const std::string& deviceId) { fDeviceId = deviceId; }
195  uint64_t GetTaskId() const { return fTaskId; }
196  void SetTaskId(const uint64_t taskId) { fTaskId = taskId; }
197  Result GetResult() const { return fResult; }
198  void SetResult(const Result result) { fResult = result; }
199  Transition GetTransition() const { return fTransition; }
200  void SetTransition(const Transition transition) { fTransition = transition; }
201  fair::mq::State GetCurrentState() const { return fCurrentState; }
202  void SetCurrentState(fair::mq::State state) { fCurrentState = state; }
203 
204  private:
205  std::string fDeviceId;
206  uint64_t fTaskId;
207  Result fResult;
208  Transition fTransition;
209  fair::mq::State fCurrentState;
210 };
211 
212 struct Config : Cmd
213 {
214  explicit Config(const std::string& id, const std::string& config)
215  : Cmd(Type::config)
216  , fDeviceId(id)
217  , fConfig(config)
218  {}
219 
220  std::string GetDeviceId() const { return fDeviceId; }
221  void SetDeviceId(const std::string& deviceId) { fDeviceId = deviceId; }
222  std::string GetConfig() const { return fConfig; }
223  void SetConfig(const std::string& config) { fConfig = config; }
224 
225  private:
226  std::string fDeviceId;
227  std::string fConfig;
228 };
229 
230 struct StateChangeSubscription : Cmd
231 {
232  explicit StateChangeSubscription(const std::string& id, const uint64_t taskId, const Result result)
233  : Cmd(Type::state_change_subscription)
234  , fDeviceId(id)
235  , fTaskId(taskId)
236  , fResult(result)
237  {}
238 
239  std::string GetDeviceId() const { return fDeviceId; }
240  void SetDeviceId(const std::string& deviceId) { fDeviceId = deviceId; }
241  uint64_t GetTaskId() const { return fTaskId; }
242  void SetTaskId(const uint64_t taskId) { fTaskId = taskId; }
243  Result GetResult() const { return fResult; }
244  void SetResult(const Result result) { fResult = result; }
245 
246  private:
247  std::string fDeviceId;
248  uint64_t fTaskId;
249  Result fResult;
250 };
251 
252 struct StateChangeUnsubscription : Cmd
253 {
254  explicit StateChangeUnsubscription(const std::string& id, const uint64_t taskId, const Result result)
255  : Cmd(Type::state_change_unsubscription)
256  , fDeviceId(id)
257  , fTaskId(taskId)
258  , fResult(result)
259  {}
260 
261  std::string GetDeviceId() const { return fDeviceId; }
262  void SetDeviceId(const std::string& deviceId) { fDeviceId = deviceId; }
263  uint64_t GetTaskId() const { return fTaskId; }
264  void SetTaskId(const uint64_t taskId) { fTaskId = taskId; }
265  Result GetResult() const { return fResult; }
266  void SetResult(const Result result) { fResult = result; }
267 
268  private:
269  std::string fDeviceId;
270  uint64_t fTaskId;
271  Result fResult;
272 };
273 
274 struct StateChange : Cmd
275 {
276  explicit StateChange(const std::string& deviceId, const uint64_t taskId, const State lastState, const State currentState)
277  : Cmd(Type::state_change)
278  , fDeviceId(deviceId)
279  , fTaskId(taskId)
280  , fLastState(lastState)
281  , fCurrentState(currentState)
282  {}
283 
284  std::string GetDeviceId() const { return fDeviceId; }
285  void SetDeviceId(const std::string& deviceId) { fDeviceId = deviceId; }
286  uint64_t GetTaskId() const { return fTaskId; }
287  void SetTaskId(const uint64_t taskId) { fTaskId = taskId; }
288  fair::mq::State GetLastState() const { return fLastState; }
289  void SetLastState(const fair::mq::State state) { fLastState = state; }
290  fair::mq::State GetCurrentState() const { return fCurrentState; }
291  void SetCurrentState(const fair::mq::State state) { fCurrentState = state; }
292 
293  private:
294  std::string fDeviceId;
295  uint64_t fTaskId;
296  fair::mq::State fLastState;
297  fair::mq::State fCurrentState;
298 };
299 
300 struct Properties : Cmd
301 {
302  Properties(std::string deviceId, std::size_t requestId, const Result result, std::vector<std::pair<std::string, std::string>> properties)
303  : Cmd(Type::properties)
304  , fDeviceId(std::move(deviceId))
305  , fRequestId(requestId)
306  , fResult(result)
307  , fProperties(std::move(properties))
308  {}
309 
310  auto GetDeviceId() const -> std::string { return fDeviceId; }
311  auto SetDeviceId(std::string deviceId) -> void { fDeviceId = std::move(deviceId); }
312  auto GetRequestId() const -> std::size_t { return fRequestId; }
313  auto SetRequestId(std::size_t requestId) -> void { fRequestId = requestId; }
314  auto GetResult() const -> Result { return fResult; }
315  auto SetResult(Result result) -> void { fResult = result; }
316  auto GetProps() const -> std::vector<std::pair<std::string, std::string>> { return fProperties; }
317  auto SetProps(std::vector<std::pair<std::string, std::string>> properties) -> void { fProperties = std::move(properties); }
318 
319  private:
320  std::string fDeviceId;
321  std::size_t fRequestId;
322  Result fResult;
323  std::vector<std::pair<std::string, std::string>> fProperties;
324 };
325 
326 struct PropertiesSet : Cmd {
327  PropertiesSet(std::string deviceId, std::size_t requestId, Result result)
328  : Cmd(Type::properties_set)
329  , fDeviceId(std::move(deviceId))
330  , fRequestId(requestId)
331  , fResult(result)
332  {}
333 
334  auto GetDeviceId() const -> std::string { return fDeviceId; }
335  auto SetDeviceId(std::string deviceId) -> void { fDeviceId = std::move(deviceId); }
336  auto GetRequestId() const -> std::size_t { return fRequestId; }
337  auto SetRequestId(std::size_t requestId) -> void { fRequestId = requestId; }
338  auto GetResult() const -> Result { return fResult; }
339  auto SetResult(Result result) -> void { fResult = result; }
340 
341  private:
342  std::string fDeviceId;
343  std::size_t fRequestId;
344  Result fResult;
345 };
346 
347 template<typename C, typename... Args>
348 std::unique_ptr<Cmd> make(Args&&... args)
349 {
350  return std::make_unique<C>(std::forward<Args>(args)...);
351 }
352 
353 struct Cmds
354 {
355  using container = std::vector<std::unique_ptr<Cmd>>;
356  struct CommandFormatError : std::runtime_error { using std::runtime_error::runtime_error; };
357 
358  explicit Cmds() {}
359 
360  template<typename... Rest>
361  explicit Cmds(std::unique_ptr<Cmd>&& first, Rest&&... rest)
362  {
363  Unpack(std::forward<std::unique_ptr<Cmd>&&>(first), std::forward<Rest>(rest)...);
364  }
365 
366  void Add(std::unique_ptr<Cmd>&& cmd) { fCmds.emplace_back(std::move(cmd)); }
367 
368  template<typename C, typename... Args>
369  void Add(Args&&... args)
370  {
371  static_assert(std::is_base_of<Cmd, C>::value, "Only types derived from fair::mq::cmd::Cmd are allowed");
372  Add(make<C>(std::forward<Args>(args)...));
373  }
374 
375  Cmd& At(size_t i) { return *(fCmds.at(i)); }
376 
377  size_t Size() const { return fCmds.size(); }
378  void Reset() { fCmds.clear(); }
379 
380  std::string Serialize(const Format type = Format::Binary) const;
381  void Deserialize(const std::string&, const Format type = Format::Binary);
382 
383  private:
384  container fCmds;
385 
386  void Unpack() {}
387 
388  template <class... Rest>
389  void Unpack(std::unique_ptr<Cmd>&& first, Rest&&... rest)
390  {
391  fCmds.emplace_back(std::move(first));
392  Unpack(std::forward<Rest>(rest)...);
393  }
394 
395  public:
396  using iterator = container::iterator;
397  using const_iterator = container::const_iterator;
398 
399  auto begin() -> decltype(fCmds.begin()) { return fCmds.begin(); }
400  auto end() -> decltype(fCmds.end()) { return fCmds.end(); }
401  auto cbegin() -> decltype(fCmds.cbegin()) { return fCmds.cbegin(); }
402  auto cend() -> decltype(fCmds.cend()) { return fCmds.cend(); }
403 };
404 
405 std::string GetResultName(const Result result);
406 std::string GetTypeName(const Type type);
407 
408 inline std::ostream& operator<<(std::ostream& os, const Result& result) { return os << GetResultName(result); }
409 inline std::ostream& operator<<(std::ostream& os, const Type& type) { return os << GetTypeName(type); }
410 
411 } // namespace fair::mq::sdk::cmd
412 
413 #endif /* FAIR_MQ_SDK_COMMANDFACTORY */
fair::mq::sdk::cmd::ChangeState
Definition: Commands.h:78
fair::mq::sdk::cmd::Cmd
Definition: Commands.h:62
fair::mq::sdk::cmd::SubscribeToStateChange
Definition: Commands.h:97
fair::mq::sdk::cmd::GetProperties
Definition: Commands.h:121
fair::mq::sdk::cmd::CheckState
Definition: Commands.h:73
fair::mq::sdk::cmd::Cmds
Definition: Commands.h:360
fair::mq::sdk::cmd::StateChangeExitingReceived
Definition: Commands.h:116

privacy