rad  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
smRequestor.hpp
Go to the documentation of this file.
1 
9 #ifndef RAD_SM_REQUESTOR_HPP
10 #define RAD_SM_REQUESTOR_HPP
11 
12 #include <rad/anyEvent.hpp>
13 #include <rad/logger.hpp>
14 #include <rad/msgHandler.hpp>
15 #include <rad/msgRequestor.hpp>
16 #include <rad/smAdapter.hpp>
17 
18 #include <functional>
19 
20 namespace rad {
21 
30 template <typename TYPEREQ, typename TYPEREP>
31 class SMRequestor {
32  public:
33  SMRequestor(const std::string& endpoint, const std::string& identity,
34  boost::asio::io_service& ios, SMAdapter& sm, UniqueEvent&& ok_event,
35  UniqueEvent&& err_event, UniqueEvent&& timeout_event);
36  virtual ~SMRequestor();
37 
38  size_t Send(const TYPEREQ& payload, const long timeout = 0);
39 
40  const TYPEREP& GetReplyPayload();
41 
42  SMRequestor(const SMRequestor&) = delete;
43  SMRequestor& operator=(const SMRequestor&) = delete;
44 
45  private:
46  void ReplyHandler(const std::error_code& err_code, TYPEREP reply);
47 
48  MsgRequestor<TYPEREQ, TYPEREP> m_msg_requestor;
49  SMAdapter& m_sm;
50  UniqueEvent m_ok_event;
51  UniqueEvent m_err_event;
52  UniqueEvent m_timeout_event;
53  TYPEREP m_reply_payload;
54 };
55 
67 template <typename TYPEREQ, typename TYPEREP>
68 SMRequestor<TYPEREQ, TYPEREP>::SMRequestor(const std::string& endpoint, const std::string& identity,
69  boost::asio::io_service& ios, SMAdapter& sm,
70  UniqueEvent&& ok_event, UniqueEvent&& err_event,
71  UniqueEvent&& timeout_event)
72  : m_msg_requestor(endpoint, identity, ios,
73  std::bind(&SMRequestor<TYPEREQ, TYPEREP>::ReplyHandler, this,
74  std::placeholders::_1, std::placeholders::_2)),
75  m_sm(sm),
76  m_ok_event(std::move(ok_event)),
77  m_err_event(std::move(err_event)),
78  m_timeout_event(std::move(timeout_event)) {
80 }
81 
85 template <typename TYPEREQ, typename TYPEREP>
88 }
89 
97 template <typename TYPEREQ, typename TYPEREP>
98 size_t SMRequestor<TYPEREQ, TYPEREP>::Send(const TYPEREQ& payload, const long timeout) {
100  return m_msg_requestor.Send(payload, timeout);
101 }
102 
110 template <typename TYPEREQ, typename TYPEREP>
111 void SMRequestor<TYPEREQ, TYPEREP>::ReplyHandler(const std::error_code& err_code, TYPEREP reply) {
112  RAD_TRACE(GetLogger());
113 
114  if (err_code == rad::ErrorCodes::REPLY_TIMEOUT) {
115  LOG4CPLUS_DEBUG(GetLogger(), "Triggering timeout event.");
116  m_sm.ProcessEvent(*m_timeout_event);
117  } else {
118  LOG4CPLUS_DEBUG(GetLogger(), "Triggering OK reply event.");
119  m_reply_payload = reply;
120  // TODO check if it is OK/ERR reply
121  m_sm.ProcessEvent(*m_ok_event);
122  }
123 }
124 
128 template <typename TYPEREQ, typename TYPEREP>
130  RAD_TRACE(GetLogger());
131  return m_reply_payload;
132 }
133 
134 } // namespace rad
135 
136 #endif // RAD_SM_REQUESTOR_HPP
log4cplus::Logger & GetLogger()
Definition: logger.cpp:43
Definition: smRequestor.hpp:31
SMRequestor(const std::string &endpoint, const std::string &identity, boost::asio::io_service &ios, SMAdapter &sm, UniqueEvent &&ok_event, UniqueEvent &&err_event, UniqueEvent &&timeout_event)
Definition: smRequestor.hpp:68
SMRequestor & operator=(const SMRequestor &)=delete
size_t Send(const TYPEREQ &payload, const long timeout=0)
Definition: smRequestor.hpp:98
const TYPEREP & GetReplyPayload()
Definition: smRequestor.hpp:129
Definition: smAdapter.hpp:42
std::unique_ptr< AnyEvent > UniqueEvent
Definition: anyEvent.hpp:45
#define RAD_TRACE(logger)
Definition: logger.hpp:19
Definition: msgRequestor.hpp:34
virtual ~SMRequestor()
Definition: smRequestor.hpp:86