rad  2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
topicSub.hpp
Go to the documentation of this file.
1 
9 #ifndef RAD_TOPIC_SUB_HPP
10 #define RAD_TOPIC_SUB_HPP
11 
12 #include <rad/topicHandler.hpp>
13 
14 #include <azmq/message.hpp>
15 #include <azmq/socket.hpp>
16 
17 #include <boost/asio.hpp>
18 
19 #include <array>
20 
21 namespace rad {
22 
28 class TopicSub {
29  public:
30  using TopicHandler_t = std::function<void(const std::string&, const void*, size_t)>;
31  using TopicHandlerMap_t = std::unordered_map<std::string, TopicHandler_t>;
32 
33  TopicSub(boost::asio::io_service& ios, std::unique_ptr<TopicHandler>&& fallback);
34  ~TopicSub();
35 
36  void Subscribe(const std::string& topic_name);
37  void Subscribe(const std::string& topic_name, TopicHandler_t handler);
38  void Unsubscribe(const std::string& topic_name);
39 
40  void Connect(const std::string& endpoint);
41  void Disconnect(const std::string& endpoint);
42  void Flush();
43 
44  TopicSub(const TopicSub&) = delete;
45  TopicSub& operator=(const TopicSub&) = delete;
46  // Enable moving
47  TopicSub(TopicSub&& rhs) = default;
48 
49  private:
50  void Register();
51  void Callback(boost::system::error_code const& error_code, azmq::message& msg, size_t nbytes);
52 
53  azmq::sub_socket m_socket;
54  std::unique_ptr<TopicHandler> m_fallback_handler;
55  TopicHandlerMap_t m_handlers_map;
56 };
57 
58 } // namespace rad
59 
60 #endif // RAD_TOPIC_SUB_HPP
void Unsubscribe(const std::string &topic_name)
Definition: topicSub.cpp:72
TopicSub(boost::asio::io_service &ios, std::unique_ptr< TopicHandler > &&fallback)
Definition: topicSub.cpp:25
void Flush()
Definition: topicSub.cpp:117
Definition: topicSub.hpp:28
void Connect(const std::string &endpoint)
Definition: topicSub.cpp:83
void Subscribe(const std::string &topic_name)
Definition: topicSub.cpp:50
std::unordered_map< std::string, TopicHandler_t > TopicHandlerMap_t
Definition: topicSub.hpp:31
std::function< void(const std::string &, const void *, size_t)> TopicHandler_t
Definition: topicSub.hpp:30
def handler
Definition: test_dispatcher.py:11
TopicSub & operator=(const TopicSub &)=delete
~TopicSub()
Definition: topicSub.cpp:33
void Disconnect(const std::string &endpoint)
Definition: topicSub.cpp:107