BlueZero (BØ)
Middleware for distributed applications
multi_publisher.cpp

This is an example of having multiple publishers in one node

#include <b0/node.h>
#include <b0/publisher.h>
#include <iostream>
int main(int argc, char **argv)
{
/*
* Create a node named "publisher"
*/
b0::Node node("publisher");
/*
* Create two publishers:
* - pubA publishes messages on topic "A"
* - pubB publishes messages on topic "B"
*/
b0::Publisher pubA(&node, "A");
b0::Publisher pubB(&node, "B");
/*
* Initialize the node (will announce node name to the network, and do other nice things)
*/
node.init();
int i = 0;
while(!node.shutdownRequested())
{
/*
* Process messages from node's sockets
*/
node.spinOnce();
/*
* Publish some data on "A"
*/
std::string msg1 = (boost::format("meow-%d") % i++).str();
pubA.publish(msg1);
/*
* Publish some data on "B"
*/
std::string msg2 = (boost::format("woof-%d") % i++).str();
pubB.publish(msg2);
/*
* Wait some time (simulate I/O wait...)
*/
boost::this_thread::sleep_for(boost::chrono::milliseconds(500));
}
/*
* Perform cleanup (stop threads, notify resolver that this node has quit, ...)
*/
node.cleanup();
return 0;
}