rad  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
21 namespace rad {
22 namespace cii {
23 
32 template <typename TOPIC_TYPE>
33 class Publisher {
34  public:
41  Publisher(const elt::mal::Uri& uri,
42  const std::optional<elt::mal::Mal::Properties> mal_properties = {})
43  : m_publisher() {
45 
46  m_publisher = elt::mal::CiiFactory::getInstance().getPublisher<TOPIC_TYPE>(
47  elt::mal::Uri(uri), elt::mal::ps::qos::QoS::DEFAULT,
48  mal_properties ? *mal_properties : elt::mal::Mal::Properties());
49  RAD_ASSERTPTR(m_publisher);
50 
51 #if 0
52  elt::mal::CiiFactory& factory = elt::mal::CiiFactory::getInstance();
53 
54  elt::mal::Mal::Properties mal_properties;
55  auto mal_instance = elt::mal::loadMal(malType, mal_properties);
56 
57  auto uri = elt::mal::Uri(uriStr);
58  std::string scheme = uri.scheme().to_string();
59  factory.registerMal(scheme, mal_instance);
60 
61  m_publisher = factory.getPublisher<TOPIC_TYPE>(uri, elt::mal::ps::qos::QoS::DEFAULT, mal_properties);
62  if (m_publisher == nullptr) {
63  throw std::runtime_error("Cannot create publisher");
64  }
65 
66  auto topicInstance = m_publisher->createDataEntity();
67  if (topicInstance == nullptr) {
68  throw std::runtime_error("Publisher cannot create data entity");
69  }
70 
71  m_instance_publisher = m_publisher->createInstancePublisher(*topicInstance);
72  if (m_instance_publisher == nullptr) {
73  throw std::runtime_error("Publisher cannot create instance publisher");
74  }
75 #endif
76 #if 0
77  auto sample = m_instance_publisher->createDataEntity();
78  if (sample == nullptr) {
79  throw std::runtime_error("Instance publisher cannot create data entity");
80  }
81 #endif
82  }
83 
88 
94  std::shared_ptr<TOPIC_TYPE> CreateTopic() const {
96  return m_publisher->createDataEntity();
97  }
98 
104  void Publish(const TOPIC_TYPE& topic) {
105  RAD_TRACE(GetLogger());
106  m_publisher->publish(topic,
107  std::chrono::milliseconds(0) /* timeout = 0 for async publishing */);
108  }
109 
110  Publisher(const Publisher&) = delete;
111  Publisher& operator=(const Publisher&) = delete;
112 
113  private:
114  std::unique_ptr<elt::mal::ps::Publisher<TOPIC_TYPE>> m_publisher; // MAL topic publisher.
115  // std::unique_ptr<elt::mal::ps::InstancePublisher<TOPIC_TYPE>> m_instance_publisher;
116 };
117 
118 } // namespace cii
119 } // namespace rad
120 
121 #endif // RAD_MAL_SUBSCRIBER_HPP
log4cplus::Logger & GetLogger()
Definition: logger.cpp:43
void Publish(const TOPIC_TYPE &topic)
Definition: publisher.hpp:104
#define RAD_ASSERTPTR(a)
Definition: assert.hpp:19
Publisher & operator=(const Publisher &)=delete
Publisher(const elt::mal::Uri &uri, const std::optional< elt::mal::Mal::Properties > mal_properties={})
Definition: publisher.hpp:41
Definition: publisher.hpp:33
std::shared_ptr< TOPIC_TYPE > CreateTopic() const
Definition: publisher.hpp:94
#define RAD_TRACE(logger)
Definition: logger.hpp:19
~Publisher()
Definition: publisher.hpp:87