rad 6.2.0
Loading...
Searching...
No Matches
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
20namespace rad {
21
30template <typename TYPEREQ, typename TYPEREP>
32 public:
33 SMRequestor(const std::string& endpoint, const std::string& identity,
34 boost::asio::io_context& 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;
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
67template <typename TYPEREQ, typename TYPEREP>
68SMRequestor<TYPEREQ, TYPEREP>::SMRequestor(const std::string& endpoint, const std::string& identity,
69 boost::asio::io_context& 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
85template <typename TYPEREQ, typename TYPEREP>
89
97template <typename TYPEREQ, typename TYPEREP>
98size_t SMRequestor<TYPEREQ, TYPEREP>::Send(const TYPEREQ& payload, const long timeout) {
100 return m_msg_requestor.Send(payload, timeout);
101}
102
110template <typename TYPEREQ, typename TYPEREP>
111void SMRequestor<TYPEREQ, TYPEREP>::ReplyHandler(const std::error_code& err_code, TYPEREP reply) {
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
128template <typename TYPEREQ, typename TYPEREP>
131 return m_reply_payload;
132}
133
134} // namespace rad
135
136#endif // RAD_SM_REQUESTOR_HPP
AnyEvent class header file.
Definition msgRequestor.hpp:34
Definition smAdapter.hpp:60
Definition smRequestor.hpp:31
SMRequestor & operator=(const SMRequestor &)=delete
virtual ~SMRequestor()
Definition smRequestor.hpp:86
size_t Send(const TYPEREQ &payload, const long timeout=0)
Definition smRequestor.hpp:98
const TYPEREP & GetReplyPayload()
Definition smRequestor.hpp:129
SMRequestor(const std::string &endpoint, const std::string &identity, boost::asio::io_context &ios, SMAdapter &sm, UniqueEvent &&ok_event, UniqueEvent &&err_event, UniqueEvent &&timeout_event)
Definition smRequestor.hpp:68
SMRequestor(const SMRequestor &)=delete
Logger class.
#define RAD_TRACE(logger)
Definition logger.hpp:21
public header file.
MsgRequestor class header file.
Definition actionsApp.cpp:23
std::unique_ptr< AnyEvent > UniqueEvent
Definition anyEvent.hpp:45
log4cplus::Logger & GetLogger()
Definition logger.cpp:72
Definition errors.hpp:58
SMAdapter class header file.