BlueZero (BØ)
Middleware for distributed applications
Public Member Functions | Protected Member Functions | Protected Attributes | Friends | List of all members
b0::Node Class Reference

The abstraction for a node in the network. More...

#include <b0/node.h>

Inheritance diagram for b0::Node:
b0::logger::LogInterface b0::logger::Console b0::resolver::Resolver

Public Member Functions

 Node (std::string nodeName="")
 Create a node with a given name. More...
 
virtual ~Node ()
 Destruct this node. More...
 
void setResolverAddress (const std::string &addr)
 Specify a different value for the resolver address. Otherwise B0_RESOLVER env var is used.
 
virtual void init ()
 Initialize the node (connect to resolve, start heartbeat, announce node name) More...
 
virtual void shutdown ()
 Shutdown the node (stop all running threads, send shutdown notification) More...
 
bool shutdownRequested () const
 Return wether shutdown has requested (by Node::shutdown() method or by pressing CTRL-C)
 
virtual void spinOnce ()
 Read all available messages from the various ZeroMQ sockets, and dispatch them to callbacks. More...
 
virtual void spin (double spinRate=10.0)
 Run the spin loop (continuously call spinOnce(), at the specified rate, and call cleanup() at the end) More...
 
virtual void cleanup ()
 Node cleanup: stop all threads, send a shutdown notification to resolver, and so on...
 
void log (LogLevel level, std::string message) const override
 Log a message to the default logger of this node.
 
std::string getName () const
 Get the name assigned by resolver to this node.
 
NodeState getState () const
 Get the state of this node.
 
void * getContext ()
 Get the ZeroMQ Context.
 
virtual std::string getXPUBSocketAddress () const
 Retrieve address of the proxy's XPUB socket.
 
virtual std::string getXSUBSocketAddress () const
 Retrieve address of the proxy's XSUB socket.
 
virtual std::string hostname ()
 Return the public address (IP or hostname) to reach this node on the network.
 
virtual int pid ()
 Return the process id of this node.
 
virtual std::string threadID ()
 Return the thread identifier of this node.
 
virtual int freeTCPPort ()
 Find and return an available TCP port.
 
virtual void notifyTopic (std::string topic_name, bool reverse, bool active)
 Notify topic publishing/subscription start or end.
 
virtual void notifyService (std::string service_name, bool reverse, bool active)
 Notify service advertising/use start or end.
 
virtual void announceService (std::string service_name, std::string addr)
 Announce service address.
 
virtual void resolveService (std::string service_name, std::string &addr)
 Resolve service address by name.
 
virtual void setAnnounceTimeout (int timeout=-1)
 Set the timeout for the announce phase. See b0::resolver::Client::setAnnounceTimeout()
 
int64_t hardwareTimeUSec () const
 Return this computer's clock time in microseconds.
 
int64_t timeUSec ()
 Return the adjusted time in microseconds. See Time Synchronization for details.
 
- Public Member Functions inherited from b0::logger::LogInterface
template<typename... Arguments>
void log (LogLevel level, std::string const &fmt, Arguments &&... args) const
 Log a message using a format string.
 

Protected Member Functions

virtual void startHeartbeatThread ()
 Start the heartbeat thread. More...
 
virtual std::string freeTCPAddress ()
 Find and return an available tcp address, e.g. tcp://hostname:portnumber.
 
virtual void announceNode ()
 Announce this node to resolver.
 
virtual void notifyShutdown ()
 Notify resolver of this node shutdown.
 
virtual void heartbeatLoop ()
 The heartbeat message loop (run in its own thread)
 

Protected Attributes

std::string resolv_addr_
 Target address of resolver client.
 
logger::LogInterfacep_logger_
 The logger of this node.
 

Friends

class Socket
 

Additional Inherited Members

- Public Types inherited from b0::logger::LogInterface
enum  LogLevel {
  trace = 0, debug = 10, info = 20, warn = 30,
  error = 40, fatal = 50
}
 

Detailed Description

The abstraction for a node in the network.

You must create at most one node per thread. You can have many nodes in one process by creating several threads.

Examples:
client_node.cpp, client_node_object.cpp, multi_publisher.cpp, multi_subscriber.cpp, publisher_node.cpp, publisher_node_object.cpp, server_node.cpp, server_node_object.cpp, subscriber_node.cpp, subscriber_node_object.cpp, and subscriber_node_object_manual.cpp.

Constructor & Destructor Documentation

◆ Node()

b0::Node::Node ( std::string  nodeName = "")

Create a node with a given name.

Parameters
nodeNamethe name of the node
See also
getName()

Create a node with a given name.

A message will be send to resolver to announce this node presence on the network. If a node with the same name already exists in the network, this node will get a different name.

The heartbeat thread will be started to let resolver track the online status of this node.

Examples:
publisher_node_object.cpp, server_node_object.cpp, subscriber_node_object.cpp, and subscriber_node_object_manual.cpp.

◆ ~Node()

virtual b0::Node::~Node ( )
virtual

Destruct this node.

Any threads, such as the heartbeat thread, will be stopped, and the sockets will be freeed.

Member Function Documentation

◆ init()

virtual void b0::Node::init ( )
virtual

Initialize the node (connect to resolve, start heartbeat, announce node name)

If you need to extend the init phase, when overriding it in your subclass, remember to first call this Node::init() (unless you know what you are doing).

Reimplemented in b0::resolver::Resolver.

Examples:
client_node.cpp, multi_publisher.cpp, and publisher_node.cpp.

◆ shutdown()

virtual void b0::Node::shutdown ( )
virtual

Shutdown the node (stop all running threads, send shutdown notification)

If you need to perform additional cleanup, when overriding this method in your subclass, remember to first call this Node::shutdown() (unless you know what you are doing).

Reimplemented in b0::resolver::Resolver.

◆ spin()

virtual void b0::Node::spin ( double  spinRate = 10.0)
virtual

Run the spin loop (continuously call spinOnce(), at the specified rate, and call cleanup() at the end)

Parameters
spinRatethe approximate frequency (in Hz) at which spinOnce() will be called

◆ spinOnce()

virtual void b0::Node::spinOnce ( )
virtual

Read all available messages from the various ZeroMQ sockets, and dispatch them to callbacks.

This method will call b0::Subscriber::spinOnce() and b0::ServiceServer::spinOnce() on the subscribers and service servers that belong to this node.

Warning: every message sent on a topic which has no registered callback will be discarded.

Examples:
multi_publisher.cpp, publisher_node.cpp, and publisher_node_object.cpp.

◆ startHeartbeatThread()

virtual void b0::Node::startHeartbeatThread ( )
protectedvirtual

Start the heartbeat thread.

The heartbeat thread will periodically send a heartbeat message to inform the resolver node that this node is alive.


The documentation for this class was generated from the following file: