rad  5.1.0
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 
23 namespace rad {
24 namespace cii {
25 
34 template <typename TOPIC_TYPE>
35 class 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 
106  void Publish(const TOPIC_TYPE& topic) {
107  RAD_TRACE(GetLogger());
108  m_publisher->publish(topic,
109  std::chrono::milliseconds(0) /* timeout = 0 for async publishing */);
110  }
111 
112  Publisher(const Publisher&) = delete;
113  Publisher& operator=(const Publisher&) = delete;
114 
115  private:
116  std::unique_ptr<elt::mal::ps::Publisher<TOPIC_TYPE>> m_publisher; // MAL topic publisher.
117  // std::unique_ptr<elt::mal::ps::InstancePublisher<TOPIC_TYPE>> m_instance_publisher;
118 };
119 
120 } // namespace cii
121 } // namespace rad
122 
123 #endif // RAD_MAL_SUBSCRIBER_HPP
Assert header file.
#define RAD_ASSERTPTR(a)
Definition: assert.hpp:19
Definition: publisher.hpp:35
Publisher(const elt::mal::Uri &uri, const std::optional< elt::mal::Mal::Properties > mal_properties={})
Definition: publisher.hpp:43
Publisher & operator=(const Publisher &)=delete
Publisher(const Publisher &)=delete
~Publisher()
Definition: publisher.hpp:89
std::shared_ptr< TOPIC_TYPE > CreateTopic() const
Definition: publisher.hpp:96
void Publish(const TOPIC_TYPE &topic)
Definition: publisher.hpp:106
Logger class.
#define RAD_TRACE(logger)
Definition: logger.hpp:24
Definition: actionsApp.cpp:20
log4cplus::Logger & GetLogger()
Definition: logger.cpp:70