GSLAM  3.0.0
Classes | Public Member Functions | Static Public Member Functions | List of all members
Messenger Class Reference

A tiny class implemented ROS like Pub/Sub messaging. More...

Public Member Functions

template<class M >
Publisher advertise (const std::string &topic, uint32_t queue_size=0)
 Create a Publisher. More...
 
template<class M >
void publish (const std::string &topic, const M &message)
 publish a message without creating a publisher More...
 
Subscriber subscribe (const std::string &topic, uint32_t queue_size, SvarFunction callback)
 subscribe a topic with callback function More...
 
Subscriber subscribe (const std::string &topic, SvarFunction callback, uint32_t queue_size=0)
 subscribe a topic with callback function
 
template<class T , class M >
Subscriber subscribe (const std::string &topic, uint32_t queue_size, void(T::*fp)(const std::shared_ptr< M > &), T *obj)
 subscribe a topic with callback member function
 
template<class M >
Subscriber subscribe (const std::string &topic, int queue_size, void(*fp)(const M &))
 subscribe a topic with callback function
 
template<class T , class M >
Subscriber subscribe (const std::string &topic, uint32_t queue_size, void(T::*fp)(const M &), T *obj)
 subscribe a topic with callback member function
 
std::vector< Publisher > getPublishers () const
 get all publishers
 
std::vector< Subscriber > getSubscribers () const
 get all subscribers
 
std::string introduction (int width=80) const
 list all publishers and subscribers
 
void join (const Publisher &pub)
 Let the Publisher join this Messenger space.
 
void join (const Subscriber &sub)
 Let the Subscriber join this Messenger space.
 
void join (Messenger another)
 Let all publishers and subscribers left the old space.
 

Static Public Member Functions

static Messenger & instance ()
 
static int exec ()
 Notify and wait Subscribers to shutdown.
 

Detailed Description

A tiny class implemented ROS like Pub/Sub messaging.

Messenger: A light-weight, efficient, thread-safe message publish and subscribe tool similar with ROS, a popular robot operating system.

The tool has the following features:

// example.cpp
#include <GSLAM/core/Messenger.h>
using namespace GSLAM;
int main(int argc,char** argv)
{
Subscriber sub=messenger.subscribe("topic_name",[](std::string msg){
std::cerr<<"Received string msg "<<msg<<std::endl;
});
// publish message without creating a Publisher
messenger.publish("topic_name","hello world!");
Publisher pub=messenger.advertise<std::string>("topic_name");
pub.publish("hello Messenger!");
return 0;
}
Warning
The Subscriber will auto shutdown, please hold the Subscriber instance until you want to unsubscribe.

Member Function Documentation

static Messenger& instance ( )
static
Returns
The Messenger instance (messenger)
Publisher advertise ( const std::string &  topic,
uint32_t  queue_size = 0 
)

Create a Publisher.

Parameters
topicthe topic name
queue_sizewhen queue_size>0, messages will be sent in another thread
Returns
the Publisher created
void publish ( const std::string &  topic,
const M &  message 
)

publish a message without creating a publisher

Parameters
topicthe topic name
messagethe message will be sent in this thread
Subscriber subscribe ( const std::string &  topic,
uint32_t  queue_size,
SvarFunction  callback 
)

subscribe a topic with callback function

Parameters
topicthe topic name
queue_sizewhen queue_size>0, messages will be handled in a separate thread
callbackthe callback function used to handle the messages