DDS  ver. 3.5.3.8.g5fe284b
Tools.h
Go to the documentation of this file.
1 // Copyright 2019 GSI, Inc. All rights reserved.
2 //
3 //
4 //
5 
6 #ifndef DDS_TOOLS_H
7 #define DDS_TOOLS_H
8 
9 // DDS
10 #include "ToolsProtocol.h"
11 // STD
12 #include <iostream>
13 #include <mutex>
14 #include <string>
15 // BOOST
16 #include <boost/property_tree/ptree.hpp>
17 #include <boost/uuid/uuid.hpp>
18 
19 namespace dds
20 {
21  namespace tools_api
22  {
156  class CSession
157  {
158  public:
159  enum class EAgentState
160  {
161  active = 0,
162  idle,
163  executing
164  };
165 
167  CSession();
169  ~CSession();
171  boost::uuids::uuid create();
174  void attach(const std::string& _sid);
177  void attach(const boost::uuids::uuid& _sid);
179  void shutdown();
181  void detach();
183  bool IsRunning() const;
186  boost::uuids::uuid getSessionID() const;
189  static std::string getDefaultSessionIDString();
192  static boost::uuids::uuid getDefaultSessionID();
201  void blockCurrentThread();
203  void unblockCurrentThread();
204 
207  template <class T>
208  void sendRequest(typename T::ptr_t _request);
209 
215  template <class Request_t>
216  void syncSendRequest(const typename Request_t::request_t& _requestData,
217  const std::chrono::seconds& _timeout = std::chrono::seconds(0),
218  std::ostream* _out = nullptr);
219 
226  template <class Request_t>
227  void syncSendRequest(const typename Request_t::request_t& _requestData,
228  typename Request_t::response_t& _responseData,
229  const std::chrono::seconds& _timeout = std::chrono::seconds(0),
230  std::ostream* _out = nullptr);
231 
238  template <class Request_t>
239  void syncSendRequest(const typename Request_t::request_t& _requestData,
240  typename Request_t::responseVector_t& _responseDataVector,
241  const std::chrono::seconds& _timeout = std::chrono::seconds(0),
242  std::ostream* _out = nullptr);
243 
250  template <CSession::EAgentState _state>
251  void waitForNumAgents(size_t _numAgents,
252  const std::chrono::seconds& _timeout = std::chrono::seconds(0),
253  const std::chrono::milliseconds& _requestInterval = std::chrono::milliseconds(500),
254  std::ostream* _out = nullptr);
255 
265  std::string userDefaultsGetValueForKey(const std::string& _key) const noexcept;
266 
267  private:
269  void subscribe();
272  bool isDDSAvailable() const;
273 
274  void notify(std::istream& _stream);
275 
276  // Map request ID to the actual request object
277  typedef std::map<requestID_t, boost::any> requests_t;
278 
279  template <class T>
280  void processRequest(requests_t::mapped_type _request,
281  const boost::property_tree::ptree::value_type& _child,
282  std::function<void(typename T::ptr_t)> _processResponseCallback);
283 
284  struct SImpl;
285  std::shared_ptr<SImpl> m_impl;
286 
287  std::mutex m_mtxRequests;
288  };
289  } // namespace tools_api
290 } // namespace dds
291 
292 #endif /* DDS_TOOLS_H */
void syncSendRequest(const typename Request_t::request_t &_requestData, const std::chrono::seconds &_timeout=std::chrono::seconds(0), std::ostream *_out=nullptr)
Sends the sync request to DDS commander.
Definition: Tools.cpp:401
void blockCurrentThread()
blockCurrentThread Blocks current thread.
Definition: Tools.cpp:223
~CSession()
A destructor.
Definition: Tools.cpp:74
std::string userDefaultsGetValueForKey(const std::string &_key) const noexcept
This method returns a configuration value for a given configuration key. It uses the DDS configuratio...
Definition: Tools.cpp:550
static boost::uuids::uuid getDefaultSessionID()
Returns the default DDS session ID.
Definition: Tools.cpp:207
boost::uuids::uuid getSessionID() const
Returns DDS session ID.
Definition: Tools.cpp:197
void waitForNumAgents(size_t _numAgents, const std::chrono::seconds &_timeout=std::chrono::seconds(0), const std::chrono::milliseconds &_requestInterval=std::chrono::milliseconds(500), std::ostream *_out=nullptr)
Wait for the required number of agents with a certain state.
Definition: AgentConnectionManager.h:13
void attach(const std::string &_sid)
Attaches to an existing DDS session.
Definition: Tools.cpp:135
boost::uuids::uuid create()
Creates a new DDS session.
Definition: Tools.cpp:80
void unblockCurrentThread()
Stop DDS session.
Definition: Tools.cpp:234
CSession()
Constructor of a DDS Session class.
Definition: Tools.cpp:69
The main class of the Tools API. It represets a DDS session.
Definition: Tools.h:156
void sendRequest(typename T::ptr_t _request)
Sends the async request to DDS commander.
Definition: Tools.cpp:379
EAgentState
Definition: Tools.h:159
void shutdown()
Shutdown currently attached DDS session.
Definition: Tools.cpp:160
void detach()
Detach from the session without shutting it down.
Definition: Tools.cpp:186
bool IsRunning() const
Check if DDS session is running.
Definition: Tools.cpp:265
static std::string getDefaultSessionIDString()
Returns the default DDS session ID as a string.
Definition: Tools.cpp:202