opensurgsim
Messenger.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013-2016, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 
17 #ifndef SURGSIM_FRAMEWORK_MESSENGER_H
18 #define SURGSIM_FRAMEWORK_MESSENGER_H
19 
20 
21 #include "SurgSim/Framework/Timer.h"
22 #include "SurgSim/Framework/Component.h"
23 
24 #include <boost/thread/mutex.hpp>
25 
26 namespace SurgSim
27 {
28 namespace Framework
29 {
30 
40 class Messenger
41 {
42 public:
43 
45  struct Event
46  {
47  Event(const std::string& name, const std::string& sender, double time, const boost::any& data) :
48  name(name), sender(sender), time(time), data(data) {}
49 
50  Event() : time(0.0) {}
51  std::string name;
52  std::string sender;
53  double time;
54  boost::any data;
55  };
56 
57  typedef std::function<void(const Event&)> EventCallback;
58 
59 
60  Messenger();
61 
63  void update();
64 
69  void publish(const std::string& event, const std::string& sender, const boost::any& data = boost::any());
70 
75  void publish(const std::string& event,
76  const std::shared_ptr<Component>& sender,
77  const boost::any& data = boost::any());
78 
84  void subscribe(const std::string& event, const std::shared_ptr<SurgSim::Framework::Component>& subscriber,
85  const EventCallback& callback);
86 
90  void subscribe(const std::shared_ptr<SurgSim::Framework::Component>& subscriber,
91  const EventCallback& callback);
92 
96  void unsubscribe(const std::string& event, const std::shared_ptr<SurgSim::Framework::Component>& subscriber);
97 
100  void unsubscribe(const std::shared_ptr<SurgSim::Framework::Component>& subscriber);
101 
102 private:
103 
106 
107  typedef std::pair<std::weak_ptr<SurgSim::Framework::Component>, EventCallback> Subscriber;
108 
110  std::unordered_map<std::string, std::vector<Subscriber>> m_subscribers;
111 
113  std::vector<Subscriber> m_universalSubscribers;
114 
116  boost::mutex m_subscriberMutex;
117 
119  std::vector<Event> m_events;
120 
122  boost::mutex m_eventMutex;
123 
125  void sendEvent(const Event& event, const std::vector<Subscriber>& receivers);
126 
127 };
128 }
129 }
130 
131 #endif
Messenger()
To receive events this is the format of the callback.
Definition: Messenger.cpp:57
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
double time
Name of the sender.
Definition: Messenger.h:53
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...
Definition: Messenger.cpp:104
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.
Definition: Messenger.cpp:93
boost::any data
Time the event is received.
Definition: Messenger.h:54
Datastructure to contain basic event data.
Definition: Messenger.h:45
Timer class, measures execution times.
Definition: Timer.h:31
std::string sender
Name of the event.
Definition: Messenger.h:52
void update()
Execute all the queued up callbacks.
Definition: Messenger.cpp:62
Messenger implements asynchronous communication to OSS, components can add themselves as subscribers ...
Definition: Messenger.h:40
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 &#39;event&#39;...
Definition: Messenger.cpp:135