DDS  ver. 3.6
ToolsProtocolCore.h
Go to the documentation of this file.
1 // Copyright 2019 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 
6 #ifndef DDS_TOOLSPROTOCOLCORE_H
7 #define DDS_TOOLSPROTOCOLCORE_H
8 
9 // STD
10 #include <string>
11 // BOOST
12 #include <boost/crc.hpp>
13 #include <boost/property_tree/json_parser.hpp>
14 #include <boost/uuid/uuid.hpp>
15 #include <boost/uuid/uuid_generators.hpp>
16 #include <boost/uuid/uuid_io.hpp>
17 
18 /*
19 {
20  "dds": {
21  "tools-api": {
22  "message": {
23  "msg": "string",
24  "severity": 123,
25  "requestID": 123
26  },
27  "done": {
28  "requestID": 123
29  },
30  "progress":{
31  "completed": 123,
32  "total": 123,
33  "errors": 123,
34  "time": 123,
35  "srcCommand": 123
36  },
37  "submit": {
38  "rms": "string",
39  "instances": 123,
40  "config": "string",
41  "pluginPath": "string",
42  "requestID": 123
43  },
44  "topology": {
45  "updateType": 123,
46  "topologyFile": "string",
47  "disableValidation": false,
48  "requestID": 123
49  },
50  "getlog": {
51  "requestID": 123
52  },
53  "commanderInfo":
54  {
55  "pid":123,
56  "idleAgentsCount": 123,
57  "activeTopologyName": "string",
58  "requestID": 123
59  },
60  "agentInfo":
61  {
62  "index": 0,
63  "lobbyLeader" : true,
64  "agentID": 3456,
65  "taskID": 5678,
66  "startUpTime": 12345,
67  "agentState": "executing",
68  "username": "user1"
69  "host": "host1",
70  "DDSPath": "/path/to/dds",
71  "agentPid": 34
72  "requestID": 123
73  },
74  "agentCount":
75  {
76  "activeSlotsCount": 123,
77  "idleSlotsCount" : 234,
78  "executingSlotsCount": 345,
79  "requestID": 123
80  },
81  "subscribeOnTaskDone":
82  {
83  "requestID": 123
84  },
85  "taskDone":
86  {
87  "requestID": 123,
88  "taskID": 123,
89  "exitCode": 123,
90  "signal": 123
91  }
92  "agentCommand":
93  {
94  "command": 123,
95  "arg1": 123,
96  "arg2": "string"
97  }
98  }
99  }
100 }
101 */
102 
103 #define DDS_TOOLS_DECLARE_DATA_CLASS(theBaseClass, theClass, theTag) \
104  struct theClass : theBaseClass<theClass> \
105  { \
106  theClass() \
107  { \
108  } \
109  theClass(const boost::property_tree::ptree& _pt) \
110  { \
111  fromPT(_pt); \
112  } \
113  \
114  private: \
115  friend SBaseData<theClass>; \
116  friend theBaseClass<theClass>; \
117  void _fromPT(const boost::property_tree::ptree& /*_pt*/) \
118  { \
119  } \
120  void _toPT(boost::property_tree::ptree& /*_pt*/) const \
121  { \
122  } \
123  static constexpr const char* _protocolTag = theTag; \
124  \
125  public: \
126  bool operator==(const theClass& _val) const \
127  { \
128  return SBaseData::operator==(_val); \
129  } \
130  };
131 
132 namespace dds
133 {
134  namespace tools_api
135  {
136  typedef uint64_t requestID_t;
137 
138  template <class T>
139  struct SBaseData
140  {
142 
145  std::string toJSON() const
146  {
147  boost::property_tree::ptree pt;
148 
149  toPT(pt);
150 
151  boost::property_tree::ptree ptParent;
152 
153  auto parentPtr = static_cast<const T*>(this);
154  ptParent.put_child("dds.tools-api." + std::string(parentPtr->_protocolTag), pt);
155 
156  std::stringstream json;
157  boost::property_tree::write_json(json, ptParent);
158 
159  return json.str();
160  }
161 
164  void toPT(boost::property_tree::ptree& _pt) const
165  {
166  auto parentPtr{ static_cast<const T*>(this) };
167  _pt.put<requestID_t>("requestID", m_requestID);
168  parentPtr->_toPT(_pt);
169  }
170 
173  void fromPT(const boost::property_tree::ptree& _pt)
174  {
175  auto parentPtr{ static_cast<T*>(this) };
176  m_requestID = _pt.get<uint64_t>("requestID", 0);
177  parentPtr->_fromPT(_pt);
178  }
179 
181  bool operator==(const T& _val) const
182  {
183  return (m_requestID == _val.m_requestID);
184  }
185 
187  friend std::ostream& operator<<(std::ostream& _os, const SBaseData& _data)
188  {
189  auto parentPtr = static_cast<const T*>(_data);
190  return _os << "requestID: " << _data.m_requestID << "; protocolTag: " << parentPtr->_protocolTag;
191  }
192 
194  std::string defaultToString() const
195  {
196  std::stringstream ss;
197  auto parentPtr = static_cast<const T*>(this);
198  ss << "requestID: " << m_requestID << "; protocolTag: " << parentPtr->_protocolTag;
199  return ss.str();
200  }
201  };
202 
203  template <class T>
204  struct SBaseRequestData : public SBaseData<T>
205  {
206  };
207 
208  template <class T>
209  struct SBaseResponseData : public SBaseData<T>
210  {
211  };
212 
213  struct SEmptyResponseData : SBaseResponseData<SEmptyResponseData>
214  {
215  private:
217  void _fromPT(const boost::property_tree::ptree& /*_pt*/)
218  {
219  throw std::runtime_error("DDS Tools API: bad usage of SEmptyResponseData");
220  }
221 
222  void _toPT(boost::property_tree::ptree& /*_pt*/) const
223  {
224  throw std::runtime_error("DDS Tools API: bad usage of SEmptyResponseData");
225  }
226  static std::string _protocolTag()
227  {
228  throw std::runtime_error("DDS Tools API: bad usage of SEmptyResponseData");
229  }
230 
231  public:
233  bool operator==(const SEmptyResponseData& /*_val*/) const
234  {
235  throw std::runtime_error("DDS Tools API: bad usage of SEmptyRequestData");
236  }
237  };
238 
239  // forward declaration
240  struct SDoneResponseData;
241  struct SMessageResponseData;
242  struct SProgressResponseData;
243 
244  template <class TRequest, class TResponse>
246  {
247  using response_t = TResponse;
248  using request_t = TRequest;
249  using responseVector_t = std::vector<response_t>;
250 
251  typedef std::shared_ptr<SBaseRequestImpl> ptr_t;
252 
254  typedef std::function<void(const response_t&)> callbackResponse_t;
256  typedef std::function<void(const SProgressResponseData&)> callbackProgress_t;
258  typedef std::function<void(const SMessageResponseData&)> callbackMessage_t;
260  typedef std::function<void()> callbackDone_t;
261 
262  public:
263  static ptr_t makeRequest(const request_t& _request)
264  {
265  // we don't use make_shared here, becasue the constructor is private
266  auto ptr = std::shared_ptr<SBaseRequestImpl>(new SBaseRequestImpl());
267  requestID_t requestID = ptr->m_request.m_requestID;
268  ptr->m_request = _request;
269  ptr->m_request.m_requestID = requestID;
270  return ptr;
271  }
272 
273  void setResponseCallback(callbackResponse_t _callbackResponse)
274  {
275  m_callbackResponse = _callbackResponse;
276  }
277 
278  void setProgressCallback(callbackProgress_t _callbackProgress)
279  {
280  m_callbackProgress = _callbackProgress;
281  }
282 
283  void setMessageCallback(callbackMessage_t _callbackMessage)
284  {
285  m_callbackMessage = _callbackMessage;
286  }
287 
288  void setDoneCallback(callbackDone_t _callbackDone)
289  {
290  m_callbackDone = _callbackDone;
291  }
292 
295  {
296  try
297  {
298  if (m_callbackResponse)
299  m_callbackResponse(_arg);
300  }
301  catch (const std::exception& e)
302  {
303  throw std::runtime_error(std::string("ResponseCallback: ") + e.what());
304  }
305  }
306 
308  {
309  try
310  {
311  if (m_callbackProgress)
312  m_callbackProgress(_arg);
313  }
314  catch (const std::exception& e)
315  {
316  throw std::runtime_error(std::string("ProgressCallback: ") + e.what());
317  }
318  }
319 
320  void execMessageCallback(const SMessageResponseData& _arg)
321  {
322  try
323  {
324  if (m_callbackMessage)
325  m_callbackMessage(_arg);
326  }
327  catch (const std::exception& e)
328  {
329  throw std::runtime_error(std::string("MessageCallback: ") + e.what());
330  }
331  }
332 
334  {
335  try
336  {
337  if (m_callbackDone)
338  m_callbackDone();
339  }
340  catch (const std::exception& e)
341  {
342  throw std::runtime_error(std::string("DoneCallback: ") + e.what());
343  }
344  }
345 
346  const request_t& getRequest() const
347  {
348  return m_request;
349  }
350 
351  protected:
353  {
354  const boost::uuids::uuid id = boost::uuids::random_generator()();
355  std::stringstream strid;
356  strid << id;
357 
358  m_request.m_requestID = crc64(strid.str());
359  }
361 
362  uint64_t crc64(const std::string& _str)
363  {
364  boost::crc_optimal<64, 0x04C11DB7, 0, 0, false, false> crc;
365  crc.process_bytes(_str.data(), _str.size());
366  return crc.checksum();
367  }
368 
369  private:
370  callbackResponse_t m_callbackResponse;
371  callbackProgress_t m_callbackProgress;
372  callbackMessage_t m_callbackMessage;
373  callbackDone_t m_callbackDone;
374 
375  request_t m_request;
376  };
377  } // namespace tools_api
378 } // namespace dds
379 
380 #endif /* DDS_TOOLSPROTOCOLCORE_H */
TRequest request_t
Definition: ToolsProtocolCore.h:248
bool operator==(const T &_val) const
Equality operator.
Definition: ToolsProtocolCore.h:181
requestID_t m_requestID
Definition: ToolsProtocolCore.h:141
void setDoneCallback(callbackDone_t _callbackDone)
Definition: ToolsProtocolCore.h:288
friend std::ostream & operator<<(std::ostream &_os, const SBaseData &_data)
Stream operator.
Definition: ToolsProtocolCore.h:187
std::shared_ptr< SBaseRequestImpl > ptr_t
Definition: ToolsProtocolCore.h:251
std::function< void(const SMessageResponseData &)> callbackMessage_t
Callback function for message notifications.
Definition: ToolsProtocolCore.h:258
uint64_t crc64(const std::string &_str)
Definition: ToolsProtocolCore.h:362
void setResponseCallback(callbackResponse_t _callbackResponse)
Definition: ToolsProtocolCore.h:273
std::string defaultToString() const
Default string representation.
Definition: ToolsProtocolCore.h:194
Definition: ToolsProtocolCore.h:213
ret_t crc(const std::string &_str)
Definition: CRC.h:19
static ptr_t makeRequest(const request_t &_request)
Definition: ToolsProtocolCore.h:263
std::function< void()> callbackDone_t
Callback function for a done notification.
Definition: ToolsProtocolCore.h:260
void fromPT(const boost::property_tree::ptree &_pt)
Init structure from boost's property tree.
Definition: ToolsProtocolCore.h:173
std::function< void(const response_t &)> callbackResponse_t
Callback function for a Response notification.
Definition: ToolsProtocolCore.h:254
Definition: ToolsProtocolCore.h:209
uint64_t requestID_t
Definition: ToolsProtocolCore.h:136
std::string toJSON() const
Fill structure into JSON.
Definition: ToolsProtocolCore.h:145
Miscellaneous functions and helpers are located here.
Definition: AgentConnectionManager.h:13
bool operator==(const SEmptyResponseData &) const
Equality operator.
Definition: ToolsProtocolCore.h:233
void execDoneCallback()
Definition: ToolsProtocolCore.h:333
Definition: ToolsProtocolCore.h:245
SBaseRequestImpl()
Definition: ToolsProtocolCore.h:352
void setMessageCallback(callbackMessage_t _callbackMessage)
Definition: ToolsProtocolCore.h:283
Structure holds information of a done response.
Definition: ToolsProtocol.h:50
const request_t & getRequest() const
Definition: ToolsProtocolCore.h:346
std::vector< response_t > responseVector_t
Definition: ToolsProtocolCore.h:249
std::function< void(const SProgressResponseData &)> callbackProgress_t
Callback function for progress notifications.
Definition: ToolsProtocolCore.h:256
void toPT(boost::property_tree::ptree &_pt) const
Fill structure into boost's property tree.
Definition: ToolsProtocolCore.h:164
TResponse response_t
Definition: ToolsProtocolCore.h:247
void execProgressCallback(const SProgressResponseData &_arg)
Definition: ToolsProtocolCore.h:307
Definition: ToolsProtocolCore.h:139
Definition: ToolsProtocolCore.h:204
void execMessageCallback(const SMessageResponseData &_arg)
Definition: ToolsProtocolCore.h:320
void setProgressCallback(callbackProgress_t _callbackProgress)
Definition: ToolsProtocolCore.h:278
void execResponseCallback(const response_t &_arg)
For tests or internal use.
Definition: ToolsProtocolCore.h:294