opensurgsim
Classes | Public Types | Public Member Functions | List of all members
SurgSim::Framework::Messenger Class Reference

Messenger implements asynchronous communication to OSS, components can add themselves as subscribers to this class when it is in the system, any component can publish events to the messenger. More...

#include <Messenger.h>

Classes

struct  Event
 Datastructure to contain basic event data. More...
 

Public Types

typedef std::function< void(const Event &)> EventCallback
 

Public Member Functions

 Messenger ()
 To receive events this is the format of the callback.
 
void update ()
 Execute all the queued up callbacks.
 
void publish (const std::string &event, const std::string &sender, const boost::any &data=boost::any())
 Put an event onto the queue to be sent to all subscribers. More...
 
void publish (const std::string &event, const std::shared_ptr< Component > &sender, const boost::any &data=boost::any())
 Put an event onto the queue to be sent to all subscribers. More...
 
void subscribe (const std::string &event, const std::shared_ptr< SurgSim::Framework::Component > &subscriber, const EventCallback &callback)
 Subscribe to receiving events, when an event occurs that matches the event the callback function will be called in the update loop of this class. More...
 
void subscribe (const std::shared_ptr< SurgSim::Framework::Component > &subscriber, const EventCallback &callback)
 Subscribe to receiving all events, the subscriber will get notified of all events in the system. More...
 
void unsubscribe (const std::string &event, const std::shared_ptr< SurgSim::Framework::Component > &subscriber)
 Unsubscribe from receiving specific events, prevent subscriber from receiving events of type 'event'. More...
 
void unsubscribe (const std::shared_ptr< SurgSim::Framework::Component > &subscriber)
 Remove all subscriptions for the given subscriber. More...
 

Detailed Description

Messenger implements asynchronous communication to OSS, components can add themselves as subscribers to this class when it is in the system, any component can publish events to the messenger.

Events are named via free strings, to be notified the name that was used to subscribe has to match the name that was used to post the event. The publish function doesn't block it just stores the event in a local queue that will be worked off during when update is called by the thread. That will cause a delay in the posting of the event. The event structure sent to the receiver contains the senders full name, the actual name of the event, the time that the event was received by the messenger (this based on a local clock inside the messenger) and some optional data. To decode the data the receiver has to know what type the original data was in.

Member Function Documentation

§ publish() [1/2]

void SurgSim::Framework::Messenger::publish ( const std::string &  event,
const std::string &  sender,
const boost::any &  data = boost::any() 
)

Put an event onto the queue to be sent to all subscribers.

Parameters
eventThe name of the event
senderThe name of the sender
dataOptional data

§ publish() [2/2]

void SurgSim::Framework::Messenger::publish ( const std::string &  event,
const std::shared_ptr< Component > &  sender,
const boost::any &  data = boost::any() 
)

Put an event onto the queue to be sent to all subscribers.

Parameters
eventThe name of the event
senderThe Component doing the publishing
dataOptional data

§ subscribe() [1/2]

void SurgSim::Framework::Messenger::subscribe ( const std::string &  event,
const std::shared_ptr< SurgSim::Framework::Component > &  subscriber,
const EventCallback &  callback 
)

Subscribe to receiving events, when an event occurs that matches the event the callback function will be called in the update loop of this class.

Parameters
eventThe name of the event that the subscriber wants to receive
subscriberThe component receiving the callback
callbackThe function to be called when the event occurs

§ subscribe() [2/2]

void SurgSim::Framework::Messenger::subscribe ( const std::shared_ptr< SurgSim::Framework::Component > &  subscriber,
const EventCallback &  callback 
)

Subscribe to receiving all events, the subscriber will get notified of all events in the system.

Parameters
subscriberThe component receiving the callback
callbackThe function to be called when the event occurs

§ unsubscribe() [1/2]

void SurgSim::Framework::Messenger::unsubscribe ( const std::string &  event,
const std::shared_ptr< SurgSim::Framework::Component > &  subscriber 
)

Unsubscribe from receiving specific events, prevent subscriber from receiving events of type 'event'.

Parameters
eventThe name of the event that the subscriber doesn't want to receive any more
subscriberThe subscriber that wants to be unsubscribed

§ unsubscribe() [2/2]

void SurgSim::Framework::Messenger::unsubscribe ( const std::shared_ptr< SurgSim::Framework::Component > &  subscriber)

Remove all subscriptions for the given subscriber.

Parameters
subscriberThe subscriber that wants to be unsubscribed

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