ifw  0.0.1-dev
 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/Dispatcher.hpp>
14 #include <rad/MsgRequestor.hpp>
15 #include <rad/MsgHandler.hpp>
16 
17 #include <functional>
18 
19 
20 namespace rad {
21 
22 template<typename TYPEREQ, typename TYPEREP>
23 class SMRequestor {
24 
25 public:
26  SMRequestor(const std::string& endpoint,
27  const std::string& identity,
28  boost::asio::io_service& ios,
29  rad::Dispatcher& dispatcher,
30  UniqueEvent&& okEvent,
31  UniqueEvent&& errEvent,
32  UniqueEvent&& timeoutEvent);
33  virtual ~SMRequestor();
34 
35  size_t Send(const TYPEREQ& payload, const long timeout = 0);
36 
37  const TYPEREP& GetReplyPayload();
38 
39  SMRequestor(const SMRequestor&) = delete;
40  SMRequestor& operator= (const SMRequestor&) = delete;
41 
42 private:
43  void ReplyHandler(const std::error_code& errCode, TYPEREP reply);
44 
45  MsgRequestor<TYPEREQ, TYPEREP> mMsgRequestor;
46  rad::Dispatcher& mDispatcher;
47  UniqueEvent mOkEvent;
48  UniqueEvent mErrEvent;
49  UniqueEvent mTimeoutEvent;
50  TYPEREP mReplyPayload;
51 };
52 
53 
54 
58 template<typename TYPEREQ, typename TYPEREP>
59 SMRequestor<TYPEREQ, TYPEREP>::SMRequestor(const std::string& endpoint,
60  const std::string& identity,
61  boost::asio::io_service& ios,
62  Dispatcher& dispatcher,
63  UniqueEvent&& okEvent,
64  UniqueEvent&& errEvent,
65  UniqueEvent&& timeoutEvent)
66 : mMsgRequestor(endpoint, identity, ios, std::bind(&SMRequestor<TYPEREQ, TYPEREP>::ReplyHandler, this, std::placeholders::_1, std::placeholders::_2)),
67  mDispatcher(dispatcher),
68  mOkEvent(std::move(okEvent)),
69  mErrEvent(std::move(errEvent)),
70  mTimeoutEvent(std::move(timeoutEvent))
71 {
72  RAD_LOG_TRACE();
73 }
74 
78 template<typename TYPEREQ, typename TYPEREP>
80 {
81  RAD_LOG_TRACE();
82 }
83 
93 template<typename TYPEREQ, typename TYPEREP>
94 size_t SMRequestor<TYPEREQ, TYPEREP>::Send(const TYPEREQ& payload, const long timeout)
95 {
96  RAD_LOG_TRACE();
97  return mMsgRequestor.Send(payload, timeout);
98 }
99 
100 template<typename TYPEREQ, typename TYPEREP>
102 {
103  RAD_LOG_TRACE();
104 
105  if (!errCode) {
106  mReplyPayload = reply;
107 
108  // @todo check if it is OK/ERR reply
109  mDispatcher.dispatch(*mOkEvent);
110  } else if (errCode == rad::ErrorCodes::REPLY_TIMEOUT) {
111  mDispatcher.dispatch(*mTimeoutEvent);
112  }
113  //mDispatcher.dispatch(*mErrEvent);
114 }
115 
116 template<typename TYPEREQ, typename TYPEREP>
118 {
119  RAD_LOG_TRACE();
120  return mReplyPayload;
121 }
122 
123 } // namespace rad
124 
125 #endif
Definition: SMRequestor.hpp:23
SMRequestor(const std::string &endpoint, const std::string &identity, boost::asio::io_service &ios, rad::Dispatcher &dispatcher, UniqueEvent &&okEvent, UniqueEvent &&errEvent, UniqueEvent &&timeoutEvent)
Definition: SMRequestor.hpp:59
SMRequestor & operator=(const SMRequestor &)=delete
size_t Send(const TYPEREQ &payload, const long timeout=0)
Definition: SMRequestor.hpp:94
optional bool timeout
Definition: requests.proto:13
const TYPEREP & GetReplyPayload()
Definition: SMRequestor.hpp:117
Definition: Dispatcher.hpp:22
std::unique_ptr< AnyEvent > UniqueEvent
Definition: AnyEvent.hpp:48
optional string reply
Definition: requests.proto:26
#define RAD_LOG_TRACE()
Definition: Logger.hpp:319
Definition: MsgRequestor.hpp:34
optional int32 error_code
Definition: topics.proto:14
virtual ~SMRequestor()
Definition: SMRequestor.hpp:79