9 #ifndef RAD_MAL_REQUESTOR_HPP
10 #define RAD_MAL_REQUESTOR_HPP
12 #include <type_traits>
14 #include <rad/logger.hpp>
15 #include <rad/mal/utils.hpp>
16 #include <rad/smAdapter.hpp>
18 #include <mal/Cii.hpp>
19 #include <mal/Mal.hpp>
20 #include <mal/utility/LoadMal.hpp>
38 template <
typename INTERFACE_TYPE>
49 const std::optional<elt::mal::Mal::Properties> mal_properties = {})
53 m_client = elt::mal::CiiFactory::getInstance().getClient<INTERFACE_TYPE>(
54 uri, elt::mal::rr::qos::QoS::DEFAULT,
55 mal_properties ? *mal_properties : elt::mal::Mal::Properties());
56 LOG4CPLUS_DEBUG(
GetLogger(),
"Created MAL client for <" << uri <<
">");
74 std::shared_ptr<INTERFACE_TYPE> m_client;
88 template <
class EVENT,
class T>
90 ::elt::mal::future<T> fut = ami->next();
91 fut.then([=, &sm](::elt::mal::future<T> fut) {
94 "Received async partial reply, triggering associated event <"
95 <<
typeid(EVENT).name() <<
">");
96 sm.
PostEvent(std::make_unique<EVENT>(fut.get()));
98 "Received async partial reply, triggering associated event - done!");
99 if (ami->isDone() ==
false) {
100 RoutePartialReply<EVENT, T>(ami, sm);
102 "Invoked RoutePartialReply for the next partial/final reply");
105 LOG4CPLUS_ERROR(
GetLogger(),
"Unknown exception when receiving partial reply");
108 LOG4CPLUS_DEBUG(log4cplus::Logger::getInstance(
rad::LOGGER_NAME),
"Installed AMI continuation");
122 template <
class EVENT,
class FUT>
124 CancellationToken token;
125 rep_future.then([&](FUT res) {
126 if (token.IsCancelled()) {
130 LOG4CPLUS_DEBUG(
GetLogger(),
"Received async reply, triggering associated event <"
131 <<
typeid(EVENT).name() <<
">");
132 sm.
PostEvent(std::make_unique<EVENT>(res));
134 "Received async reply, triggering associated event - done!");
136 LOG4CPLUS_ERROR(
GetLogger(),
"Unknown exception when receiving the reply");
139 LOG4CPLUS_DEBUG(
GetLogger(),
"Installed future continuation");
155 template <
class EVENT,
class EVENT_ERR,
class FUT>
157 CancellationToken token;
158 rep_future.then([&, token](FUT res) {
159 if (token.IsCancelled()) {
163 if (res.has_value()) {
165 if constexpr (std::is_void<typename FUT::value_type>::value) {
167 "Received async VOID reply, triggering associated event <"
168 <<
typeid(EVENT).name() <<
">");
174 "Received async reply, triggering associated event <"
175 <<
typeid(EVENT).name() <<
">");
176 sm.
PostEvent(std::make_unique<EVENT>(res.get()));
179 "Received async reply, triggering associated event - done!");
180 }
else if (res.has_exception()) {
181 LOG4CPLUS_DEBUG(
GetLogger(),
"Exception waiting for reply");
182 sm.
PostEvent(std::make_unique<EVENT_ERR>(res.get_exception_ptr()));
187 LOG4CPLUS_ERROR(
GetLogger(),
"Unknown exception when receiving the reply");
190 LOG4CPLUS_DEBUG(
GetLogger(),
"Installed future continuation");
208 template <
class EVENT,
class EVENT_TIMEOUT,
class EVENT_ERR,
class FUT>
210 CancellationToken token;
211 rep_future.then([&, token](FUT res) {
212 if (token.IsCancelled()) {
216 if (res.has_value()) {
218 if constexpr (std::is_void<typename FUT::value_type>::value) {
220 "Received async VOID reply, triggering associated event <"
221 <<
typeid(EVENT).name() <<
">");
227 "Received async reply, triggering associated event <"
228 <<
typeid(EVENT).name() <<
">");
229 sm.
PostEvent(std::make_unique<EVENT>(res.get()));
232 "Received async reply, triggering associated event - done!");
233 }
else if (res.has_exception()) {
234 LOG4CPLUS_DEBUG(
GetLogger(),
"Received exception as reply, rethrowing it.");
235 boost::rethrow_exception(res.get_exception_ptr());
239 }
catch (
const elt::mal::TimeoutException& e) {
240 LOG4CPLUS_DEBUG(
GetLogger(),
"ExceptionTimeout while waiting for reply");
241 sm.
PostEvent(std::make_unique<EVENT_TIMEOUT>());
243 LOG4CPLUS_DEBUG(
GetLogger(),
"Received exception as reply, post associated event <"
244 <<
typeid(EVENT).name() <<
">");
245 sm.
PostEvent(std::make_unique<EVENT_ERR>(res.get_exception_ptr()));
248 LOG4CPLUS_DEBUG(
GetLogger(),
"Installed future continuation");
255 #endif // RAD_MAL_REQUESTOR_HPP_
log4cplus::Logger & GetLogger()
Definition: logger.cpp:43
CancellationToken RouteReply(FUT &&rep_future, rad::SMAdapter &sm)
Definition: requestor.hpp:123
std::shared_ptr< INTERFACE_TYPE > & GetInterface()
Definition: requestor.hpp:65
Requestor & operator=(const Requestor &)=delete
void PostEvent(SharedEvent e)
Definition: smAdapter.cpp:181
const std::string LOGGER_NAME
Definition: logger.hpp:74
Definition: smAdapter.hpp:42
#define RAD_ASSERTNEVER()
Definition: assert.hpp:20
#define RAD_TRACE(logger)
Definition: logger.hpp:19
void RoutePartialReply(std::shared_ptr<::elt::mal::rr::Ami< T >> ami, rad::SMAdapter &sm)
Definition: requestor.hpp:89
CancellationToken RouteReplyWithTimeout(FUT &&rep_future, rad::SMAdapter &sm)
Definition: requestor.hpp:209
Requestor(const elt::mal::Uri &uri, const std::optional< elt::mal::Mal::Properties > mal_properties={})
Definition: requestor.hpp:48
Definition: requestor.hpp:39