ifw  0.0.1-dev
 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/socket.hpp>
15 #include <azmq/message.hpp>
16 
17 #include <boost/asio.hpp>
18 
19 #include <array>
20 
21 namespace rad {
22 
28 class TopicSub {
29 
30 public:
31  using TopicHandler_t = std::function<void (const std::string&, const void*, size_t)>;
32  using TopicHandlerMap_t = std::unordered_map<std::string, TopicHandler_t>;
33 
34  TopicSub(boost::asio::io_service& ios, std::unique_ptr<TopicHandler>&& fallback);
35  ~TopicSub();
36 
37  void Subscribe(const std::string& topicName);
38  void Subscribe(const std::string& topicName, TopicHandler_t handler);
39  void Unsubscribe(const std::string& topicName);
40 
41  void Connect(const std::string& endpoint);
42  void Disconnect(const std::string& endpoint);
43  void Flush();
44 
45  TopicSub(const TopicSub&) = delete;
46  TopicSub& operator= (const TopicSub&) = delete;
47  // Enable moving
48  TopicSub(TopicSub&& rhs) = default;
49 
50 private:
51  void Register();
52  void Callback(boost::system::error_code const& errorCode, azmq::message& msg, size_t nbytes);
53 
54  azmq::sub_socket mSocket;
55  std::unique_ptr<TopicHandler> mFallbackHandler;
56  TopicHandlerMap_t mHandlersMap;
57 };
58 
59 } // namespace rad
60 
61 #endif
void Subscribe(const std::string &topicName)
Definition: TopicSub.cpp:54
TopicSub(boost::asio::io_service &ios, std::unique_ptr< TopicHandler > &&fallback)
Definition: TopicSub.cpp:26
void Flush()
Definition: TopicSub.cpp:129
Definition: TopicSub.hpp:28
std::function< void(const std::string &, const void *, size_t)> TopicHandler_t
Definition: TopicSub.hpp:31
optional string msg
Definition: topics.proto:7
void Connect(const std::string &endpoint)
Definition: TopicSub.cpp:95
std::unordered_map< std::string, TopicHandler_t > TopicHandlerMap_t
Definition: TopicSub.hpp:32
def handler
Definition: test_dispatcher.py:11
optional int32 error_code
Definition: topics.proto:14
TopicSub & operator=(const TopicSub &)=delete
void Unsubscribe(const std::string &topicName)
Definition: TopicSub.cpp:81
~TopicSub()
Definition: TopicSub.cpp:36
void Disconnect(const std::string &endpoint)
Definition: TopicSub.cpp:118