BlueZero (BØ)
Middleware for distributed applications
subscriber.h
1 #ifndef B0__SUBSCRIBER_H__INCLUDED
2 #define B0__SUBSCRIBER_H__INCLUDED
3 
4 #include <string>
5 
6 #include <boost/function.hpp>
7 #include <boost/bind.hpp>
8 
9 #include <b0/socket.h>
10 
11 namespace b0
12 {
13 
14 class Node;
15 
30 class Subscriber : public Socket
31 {
32 public:
34 
38  Subscriber(Node *node, std::string topic_name, boost::function<void(const std::string&)> callback = 0, bool managed = true, bool notify_graph = true);
39 
43  template<class TNode>
44  Subscriber(TNode *node, std::string topic_name, void (TNode::*callbackMethod)(const std::string&), bool managed = true, bool notify_graph = true)
45  : Subscriber(node, topic_name, boost::bind(callbackMethod, node, _1), managed, notify_graph)
46  {
47  // delegate constructor. leave empty
48  }
49 
53  template<class T>
54  Subscriber(Node *node, std::string topic_name, void (T::*callbackMethod)(const std::string&), T *callbackObject, bool managed = true, bool notify_graph = true)
55  : Subscriber(node, topic_name, boost::bind(callbackMethod, callbackObject, _1), managed, notify_graph)
56  {
57  // delegate constructor. leave empty
58  }
59 
63  virtual ~Subscriber();
64 
68  void log(LogLevel level, std::string message) const override;
69 
73  virtual void init() override;
74 
78  virtual void cleanup() override;
79 
83  virtual void spinOnce() override;
84 
88  std::string getTopicName();
89 
90 protected:
94  virtual void connect();
95 
99  virtual void disconnect();
100 
102  const bool notify_graph_;
103 
107  boost::function<void(std::string&)> callback_;
108 };
109 
110 } // namespace b0
111 
112 #endif // B0__SUBSCRIBER_H__INCLUDED
LogLevel
Definition: interface.h:23
virtual void disconnect()
Disconnect from the remote address.
Subscriber(Node *node, std::string topic_name, boost::function< void(const std::string &)> callback=0, bool managed=true, bool notify_graph=true)
Construct an Subscriber child of the specified Node, optionally using a boost::function as callback...
virtual void connect()
Connect to the remote address.
void bind(std::string const &addr)
Wrapper to zmq::socket_t::bind.
The abstraction for a node in the network.
Definition: node.h:40
virtual void cleanup() override
Perform cleanup and optionally send graph notify.
The subscriber class.
Definition: subscriber.h:30
Subscriber(TNode *node, std::string topic_name, void(TNode::*callbackMethod)(const std::string &), bool managed=true, bool notify_graph=true)
Construct a Subscriber child of a specified Node, with a method (of the Node subclass) as a callback...
Definition: subscriber.h:44
virtual ~Subscriber()
Subscriber destructor.
std::string getTopicName()
Return the name of this subscriber&#39;s topic.
The Socket class.
Definition: socket.h:31
const bool notify_graph_
If false this socket will not send announcement to resolv (i.e. it will be "invisible") ...
Definition: subscriber.h:102
virtual void log(LogLevel level, std::string message) const =0
Log a message to the remote logger, with a specified level.
virtual void spinOnce() override
Process incoming messages and call callbacks.
virtual void init() override
Perform initialization and optionally send graph notify.
Subscriber(Node *node, std::string topic_name, void(T::*callbackMethod)(const std::string &), T *callbackObject, bool managed=true, bool notify_graph=true)
Construct a Subscriber child of a specified Node, with a method as a callback.
Definition: subscriber.h:54
Definition: node.h:17
void log(LogLevel level, std::string message) const override
Log a message using node&#39;s logger, prepending this subscriber informations.
boost::function< void(std::string &)> callback_
Callback which will be called when a new message is read from the socket.
Definition: subscriber.h:107