9#ifndef HLCC_CPPUTIL_REQUESTOR_HPP
10#define HLCC_CPPUTIL_REQUESTOR_HPP
31template <
typename INTERFACE_TYPE>
53 explicit Requestor(log4cplus::Logger& logger,
const elt::mal::Uri& uri,
54 const std::optional<elt::mal::Mal::Properties> mal_properties = {})
66 const std::optional<elt::mal::Mal::Properties> mal_properties = {}) {
68 std::lock_guard lck {m_mutex};
73 LOG4CPLUS_DEBUG(m_logger,
"SetConnectionInfo will close old client");
80 m_client = elt::mal::CiiFactory::getInstance().getClient<INTERFACE_TYPE>(
81 uri, elt::mal::rr::qos::QoS::DEFAULT,
82 mal_properties ? *mal_properties : elt::mal::Mal::Properties());
84 LOG4CPLUS_DEBUG(m_logger,
"Created rr client for " << uri);
90 m_client->registerConnectionListener([
this, uri](
bool connected) {
91 m_connected.store(connected);
92 LOG4CPLUS_DEBUG(m_logger,
"Connected with " << uri <<
": " << connected);
94 }
catch (
const std::exception& ex) {
97 LOG4CPLUS_WARN(m_logger,
"Failed to create rr client for " << uri <<
": " << ex.what());
109 std::lock_guard lck {m_mutex};
111 LOG4CPLUS_DEBUG(m_logger,
"Requestor::GetInterface() called without having an interface client. The user must call SetConnectionInfo first.");
128 bool Connect(std::chrono::seconds conn_timeout) {
129 std::lock_guard lck {m_mutex};
134 elt::mal::future<void> conn_future = m_client->asyncConnect();
135 ::boost::chrono::seconds conn_timeout_cii{conn_timeout.count()};
136 LOG4CPLUS_TRACE(m_logger,
"Requestor::Connect() called asyncConnect() and will wait max " << conn_timeout_cii.count() <<
" s.");
137 auto future_status = conn_future.wait_for(conn_timeout_cii);
138 bool success = future_status == (boost::future_status::ready);
139 LOG4CPLUS_TRACE(m_logger,
"Requestor::Connect() returned from waiting for connection, success=" << success);
143 LOG4CPLUS_DEBUG(m_logger,
"Requestor::Connect() failed because of missing MAL client.");
152 mutable std::recursive_mutex m_mutex;
153 log4cplus::Logger& m_logger;
155 std::shared_ptr<INTERFACE_TYPE> m_client;
160 std::atomic<bool> m_connected{
false};
Definition: requestor.hpp:32
Requestor(const Requestor &)=delete
Requestor(log4cplus::Logger &logger, const elt::mal::Uri &uri, const std::optional< elt::mal::Mal::Properties > mal_properties={})
Definition: requestor.hpp:53
Requestor(log4cplus::Logger &logger)
Definition: requestor.hpp:38
Requestor & operator=(const Requestor &)=delete
bool Connect(std::chrono::seconds conn_timeout)
Definition: requestor.hpp:128
std::shared_ptr< INTERFACE_TYPE > & GetInterface()
Definition: requestor.hpp:108
void SetConnectionInfo(const elt::mal::Uri &uri, const std::optional< elt::mal::Mal::Properties > mal_properties={})
Definition: requestor.hpp:65
Definition: requestor.hpp:19