rad 6.2.0
Loading...
Searching...
No Matches
publisher.hpp
Go to the documentation of this file.
1
9#ifndef RAD_MAL_PUBLISHER_HPP
10#define RAD_MAL_PUBLISHER_HPP
11
12#include <rad/assert.hpp>
13#include <rad/logger.hpp>
14
15#include <mal/Cii.hpp>
16#include <mal/Mal.hpp>
17#include <mal/utility/LoadMal.hpp>
18
19#include <chrono>
20#include <memory>
21#include <optional>
22
23namespace rad {
24namespace cii {
25
34template <typename TOPIC_TYPE>
35class Publisher {
36 public:
43 explicit Publisher(const elt::mal::Uri& uri,
44 const std::optional<elt::mal::Mal::Properties> mal_properties = {})
45 : m_publisher() {
47
48 m_publisher = elt::mal::CiiFactory::getInstance().getPublisher<TOPIC_TYPE>(
49 elt::mal::Uri(uri), elt::mal::ps::qos::QoS::DEFAULT,
50 mal_properties ? *mal_properties : elt::mal::Mal::Properties());
51 RAD_ASSERTPTR(m_publisher);
52
53#if 0
54 elt::mal::CiiFactory& factory = elt::mal::CiiFactory::getInstance();
55
56 elt::mal::Mal::Properties mal_properties;
57 auto mal_instance = elt::mal::loadMal(malType, mal_properties);
58
59 auto uri = elt::mal::Uri(uriStr);
60 std::string scheme = uri.scheme().to_string();
61 factory.registerMal(scheme, mal_instance);
62
63 m_publisher = factory.getPublisher<TOPIC_TYPE>(uri, elt::mal::ps::qos::QoS::DEFAULT, mal_properties);
64 if (m_publisher == nullptr) {
65 throw std::runtime_error("Cannot create publisher");
66 }
67
68 auto topicInstance = m_publisher->createDataEntity();
69 if (topicInstance == nullptr) {
70 throw std::runtime_error("Publisher cannot create data entity");
71 }
72
73 m_instance_publisher = m_publisher->createInstancePublisher(*topicInstance);
74 if (m_instance_publisher == nullptr) {
75 throw std::runtime_error("Publisher cannot create instance publisher");
76 }
77#endif
78#if 0
79 auto sample = m_instance_publisher->createDataEntity();
80 if (sample == nullptr) {
81 throw std::runtime_error("Instance publisher cannot create data entity");
82 }
83#endif
84 }
85
90
96 std::shared_ptr<TOPIC_TYPE> CreateTopic() const {
98 return m_publisher->createDataEntity();
99 }
100
104 std::shared_ptr<::elt::mal::Mal> GetMal() const {
106 return m_publisher->getMal();
107 }
108
112 template<class T>
113 std::shared_ptr<T> CreateDataEntity() const {
115 return m_publisher->createDataEntity();
116 }
117
123 void Publish(const TOPIC_TYPE& topic) {
125 m_publisher->publish(topic,
126 std::chrono::milliseconds(0) /* timeout = 0 for async publishing */);
127 }
128
129 Publisher(const Publisher&) = delete;
130 Publisher& operator=(const Publisher&) = delete;
131
132 private:
133 std::unique_ptr<elt::mal::ps::Publisher<TOPIC_TYPE>> m_publisher; // MAL topic publisher.
134 // std::unique_ptr<elt::mal::ps::InstancePublisher<TOPIC_TYPE>> m_instance_publisher;
135};
136
137} // namespace cii
138} // namespace rad
139
140#endif // RAD_MAL_SUBSCRIBER_HPP
Assert header file.
#define RAD_ASSERTPTR(a)
Definition assert.hpp:19
Definition publisher.hpp:35
std::shared_ptr< T > CreateDataEntity() const
Definition publisher.hpp:113
Publisher(const elt::mal::Uri &uri, const std::optional< elt::mal::Mal::Properties > mal_properties={})
Definition publisher.hpp:43
std::shared_ptr<::elt::mal::Mal > GetMal() const
Definition publisher.hpp:104
Publisher(const Publisher &)=delete
std::shared_ptr< TOPIC_TYPE > CreateTopic() const
Definition publisher.hpp:96
~Publisher()
Definition publisher.hpp:89
Publisher & operator=(const Publisher &)=delete
void Publish(const TOPIC_TYPE &topic)
Definition publisher.hpp:123
Logger class.
#define RAD_TRACE(logger)
Definition logger.hpp:21
Definition actionsApp.cpp:23
log4cplus::Logger & GetLogger()
Definition logger.cpp:72