|
opensurgsim
|
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... | |
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.
| 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.
| event | The name of the event |
| sender | The name of the sender |
| data | Optional data |
| 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.
| event | The name of the event |
| sender | The Component doing the publishing |
| data | Optional data |
| 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.
| event | The name of the event that the subscriber wants to receive |
| subscriber | The component receiving the callback |
| callback | The function to be called when the event occurs |
| 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.
| subscriber | The component receiving the callback |
| callback | The function to be called when the event occurs |
| 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'.
| event | The name of the event that the subscriber doesn't want to receive any more |
| subscriber | The subscriber that wants to be unsubscribed |
| void SurgSim::Framework::Messenger::unsubscribe | ( | const std::shared_ptr< SurgSim::Framework::Component > & | subscriber | ) |
Remove all subscriptions for the given subscriber.
| subscriber | The subscriber that wants to be unsubscribed |
1.8.12