9 #ifndef RAD_MAL_REPLIER_HPP
10 #define RAD_MAL_REPLIER_HPP
12 #include <rad/assert.hpp>
13 #include <rad/logger.hpp>
15 #include <mal/Cii.hpp>
16 #include <mal/Mal.hpp>
17 #include <mal/utility/LoadMal.hpp>
37 const std::optional<elt::mal::Mal::Properties> mal_properties = {})
41 m_server = elt::mal::CiiFactory::getInstance().createServer(
42 uri, elt::mal::rr::qos::QoS::DEFAULT,
43 mal_properties ? *mal_properties : elt::mal::Mal::Properties());
46 LOG4CPLUS_DEBUG(
GetLogger(),
"Created MAL server for <" << uri <<
">");
48 m_thread = std::thread([
this]() { m_server->run(); });
61 if (m_thread.joinable() ==
false) {
62 LOG4CPLUS_ERROR(
GetLogger(),
"Thread not joinable!");
67 LOG4CPLUS_ERROR(
GetLogger(),
"Unknown exception when closing MAL server!");
69 LOG4CPLUS_DEBUG(
GetLogger(),
"Terminated MAL server thread.");
79 template <
class SERVICE_TYPE>
80 void RegisterService(
const std::string& name, std::shared_ptr<SERVICE_TYPE> service) {
83 std::shared_ptr<elt::mal::rr::RrEntity> rrservice = service;
84 m_server->registerService<SERVICE_TYPE,
false>(name, rrservice);
85 LOG4CPLUS_DEBUG(
GetLogger(),
"Registered service <" << name <<
">");
96 m_server->unregisterService(name);
97 LOG4CPLUS_DEBUG(
GetLogger(),
"Unregistered service <" << name <<
">");
104 m_thread = std::thread(&Replier::Run,
this);
105 LOG4CPLUS_DEBUG(
GetLogger(),
"Started MAL server thread.");
117 m_mal_server->close();
118 if (m_thread.joinable() ==
false) {
119 LOG4CPLUS_ERROR(
GetLogger(),
"Thread not joinable!");
123 LOG4CPLUS_DEBUG(
GetLogger(),
"Terminated MAL server thread.");
131 std::unique_ptr<elt::mal::rr::Server> m_server;
132 std::thread m_thread;
136 template<
typename SERVICE_TYPE,
typename INTERFACE_TYPE>
139 Replier(
const std::string& serverUri,
140 const std::string& malType,
141 const std::string& serviceName,
145 elt::mal::CiiFactory& factory = elt::mal::CiiFactory::getInstance();
147 elt::mal::Mal::Properties mal_properties;
148 auto malInstance = elt::mal::loadMal(malType, mal_properties);
150 elt::mal::Uri sUri = elt::mal::Uri(serverUri);
151 std::string scheme = sUri.scheme().to_string();
152 factory.registerMal(scheme, malInstance);
153 LOG4CPLUS_DEBUG(
GetLogger(),
"Registered MAL.");
155 m_mal_server = factory.createServer(sUri, elt::mal::rr::qos::QoS::DEFAULT, mal_properties);
157 std::shared_ptr<elt::mal::rr::RrEntity> serviceImpl = std::make_shared<INTERFACE_TYPE>(sm);
158 m_mal_server->registerService<SERVICE_TYPE,
false>(serviceName, serviceImpl);
160 LOG4CPLUS_DEBUG(
GetLogger(),
"Registered MAL <" << malType <<
"> service <" << serviceName <<
"> on <" << sUri <<
">");
166 if (m_mal_server !=
nullptr) {
167 m_thread = std::thread(&Replier::Run,
this);
168 LOG4CPLUS_DEBUG(
GetLogger(),
"Started MAL server thread.");
175 if (m_mal_server !=
nullptr) {
183 if (m_mal_server !=
nullptr) {
184 m_mal_server->close();
185 if (m_thread.joinable() ==
false) {
186 std::cout <<
"Thread not joinable!\n";
190 LOG4CPLUS_DEBUG(
GetLogger(),
"Terminated MAL server thread.");
198 std::unique_ptr<elt::mal::rr::Server> m_server;
199 std::thread m_thread;
206 #endif // RAD_MAL_REPLIER_HPP
log4cplus::Logger & GetLogger()
Definition: logger.cpp:43
void UnregisterService(const std::string &name)
Definition: replier.hpp:93
Replier & operator=(const Replier &)=delete
#define RAD_ASSERTPTR(a)
Definition: assert.hpp:19
Definition: smAdapter.hpp:42
~Replier()
Definition: replier.hpp:55
#define RAD_TRACE(logger)
Definition: logger.hpp:19
Replier(const elt::mal::Uri &uri, const std::optional< elt::mal::Mal::Properties > mal_properties={})
Definition: replier.hpp:36
void RegisterService(const std::string &name, std::shared_ptr< SERVICE_TYPE > service)
Definition: replier.hpp:80