13#ifndef HLCC_CPPUTIL_REQUESTOR_HPP
14#define HLCC_CPPUTIL_REQUESTOR_HPP
35template <
typename INTERFACE_TYPE>
57 explicit Requestor(log4cplus::Logger& logger,
const elt::mal::Uri& uri,
58 const std::optional<elt::mal::Mal::Properties> mal_properties = {})
70 const std::optional<elt::mal::Mal::Properties> mal_properties = {}) {
72 std::lock_guard lck {m_mutex};
77 LOG4CPLUS_DEBUG(m_logger,
"SetConnectionInfo will close old client");
84 m_client = elt::mal::CiiFactory::getInstance().getClient<INTERFACE_TYPE>(
85 uri, elt::mal::rr::qos::QoS::DEFAULT,
86 mal_properties ? *mal_properties : elt::mal::Mal::Properties());
88 LOG4CPLUS_DEBUG(m_logger,
"Created rr client for " << uri);
94 m_client->registerConnectionListener([
this, uri](
bool connected) {
95 m_connected.store(connected);
96 LOG4CPLUS_DEBUG(m_logger,
"Connected with " << uri <<
": " << connected);
98 }
catch (
const std::exception& ex) {
101 LOG4CPLUS_WARN(m_logger,
"Failed to create rr client for " << uri <<
": " << ex.what());
113 std::lock_guard lck {m_mutex};
115 LOG4CPLUS_DEBUG(m_logger,
"Requestor::GetInterface() called without having an interface client. The user must call SetConnectionInfo first.");
132 bool Connect(std::chrono::seconds conn_timeout) {
133 std::lock_guard lck {m_mutex};
138 elt::mal::future<void> conn_future = m_client->asyncConnect();
139 ::boost::chrono::seconds conn_timeout_cii{conn_timeout.count()};
140 LOG4CPLUS_TRACE(m_logger,
"Requestor::Connect() called asyncConnect() and will wait max " << conn_timeout_cii.count() <<
" s.");
141 auto future_status = conn_future.wait_for(conn_timeout_cii);
142 bool success = future_status == (boost::future_status::ready);
143 LOG4CPLUS_TRACE(m_logger,
"Requestor::Connect() returned from waiting for connection, success=" << success);
147 LOG4CPLUS_DEBUG(m_logger,
"Requestor::Connect() failed because of missing MAL client.");
156 mutable std::recursive_mutex m_mutex;
157 log4cplus::Logger& m_logger;
159 std::shared_ptr<INTERFACE_TYPE> m_client;
164 std::atomic<bool> m_connected{
false};
Definition requestor.hpp:36
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:57
Requestor(log4cplus::Logger &logger)
Definition requestor.hpp:42
Requestor & operator=(const Requestor &)=delete
bool Connect(std::chrono::seconds conn_timeout)
Definition requestor.hpp:132
std::shared_ptr< INTERFACE_TYPE > & GetInterface()
Definition requestor.hpp:112
void SetConnectionInfo(const elt::mal::Uri &uri, const std::optional< elt::mal::Mal::Properties > mal_properties={})
Definition requestor.hpp:69
Definition ciiTypesToString.cpp:7