Mercurial > hg > orthanc-stone
annotate UnitTestsSources/TestMessageBroker2_connect_ok.cpp @ 427:3f9017db1738 am-vsol-upgrade-radiography-export
wip
author | am@osimis.io |
---|---|
date | Fri, 23 Nov 2018 16:06:23 +0100 |
parents | 3897f9f28cfa |
children | b70e9be013e4 |
rev | line source |
---|---|
299
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
1 /** |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
2 * Stone of Orthanc |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
4 * Department, University Hospital of Liege, Belgium |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., Belgium |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
6 * |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
7 * This program is free software: you can redistribute it and/or |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
8 * modify it under the terms of the GNU Affero General Public License |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
9 * as published by the Free Software Foundation, either version 3 of |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
10 * the License, or (at your option) any later version. |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
11 * |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
12 * This program is distributed in the hope that it will be useful, but |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
13 * WITHOUT ANY WARRANTY; without even the implied warranty of |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
15 * Affero General Public License for more details. |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
16 * |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
17 * You should have received a copy of the GNU Affero General Public License |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
19 **/ |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
20 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
21 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
22 #include "gtest/gtest.h" |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
23 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
24 #include <boost/noncopyable.hpp> |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
25 #include <boost/function.hpp> |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
26 #include <boost/bind.hpp> |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
27 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
28 #include <string> |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
29 #include <map> |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
30 #include <set> |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
31 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
32 int testCounter = 0; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
33 namespace { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
34 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
35 enum MessageType |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
36 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
37 // used in unit tests only |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
38 MessageType_Test1, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
39 MessageType_Test2, |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
40 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
41 MessageType_LastGenericStoneMessage |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
42 }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
43 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
44 struct IMessage : public boost::noncopyable |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
45 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
46 MessageType messageType_; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
47 public: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
48 IMessage(const MessageType& messageType) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
49 : messageType_(messageType) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
50 {} |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
51 virtual ~IMessage() {} |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
52 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
53 MessageType GetType() const {return messageType_;} |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
54 }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
55 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
56 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
57 class IObserver; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
58 class IObservable; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
59 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
60 /* |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
61 * This is a central message broker. It keeps track of all observers and knows |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
62 * when an observer is deleted. |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
63 * This way, it can prevent an observable to send a message to a dead observer. |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
64 */ |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
65 class MessageBroker : public boost::noncopyable |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
66 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
67 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
68 std::set<IObserver*> activeObservers_; // the list of observers that are currently alive (that have not been deleted) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
69 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
70 public: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
71 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
72 void Register(IObserver& observer) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
73 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
74 activeObservers_.insert(&observer); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
75 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
76 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
77 void Unregister(IObserver& observer) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
78 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
79 activeObservers_.erase(&observer); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
80 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
81 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
82 void EmitMessage(IObservable& from, std::set<IObserver*> observers, const IMessage& message); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
83 }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
84 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
85 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
86 class IObserver : public boost::noncopyable |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
87 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
88 protected: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
89 MessageBroker& broker_; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
90 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
91 public: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
92 IObserver(MessageBroker& broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
93 : broker_(broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
94 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
95 broker_.Register(*this); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
96 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
97 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
98 virtual ~IObserver() |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
99 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
100 broker_.Unregister(*this); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
101 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
102 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
103 void HandleMessage_(IObservable &from, const IMessage &message) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
104 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
105 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
106 HandleMessage(from, message); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
107 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
108 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
109 virtual void HandleMessage(IObservable& from, const IMessage& message) = 0; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
110 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
111 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
112 protected: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
113 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
114 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
115 }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
116 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
117 // struct ICallableObserver |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
118 // { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
119 // IObserver* observer; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
120 // }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
121 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
122 // typedef void (IObserver::*ObserverSingleMesssageHandler)(IObservable& from, const IMessage& message); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
123 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
124 // template <typename TObserver> |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
125 // struct CallableObserver : public ICallableObserver |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
126 // { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
127 // void (TObserver::*ptrToMemberHandler)(IObservable& from, const IMessage& message); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
128 // }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
129 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
130 struct CallableObserver |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
131 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
132 IObserver* observer; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
133 boost::function<void (IObservable& from, const IMessage& message)> f; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
134 }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
135 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
136 class IObservable : public boost::noncopyable |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
137 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
138 protected: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
139 MessageBroker& broker_; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
140 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
141 std::set<IObserver*> observers_; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
142 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
143 std::map<MessageType, std::set<CallableObserver*> > callables_; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
144 public: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
145 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
146 IObservable(MessageBroker& broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
147 : broker_(broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
148 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
149 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
150 virtual ~IObservable() |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
151 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
152 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
153 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
154 void EmitMessage(const IMessage& message) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
155 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
156 //broker_.EmitMessage(*this, observers_, message); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
157 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
158 // TODO check if observer is still alive and call ! |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
159 CallableObserver* callable = *(callables_[message.GetType()].begin()); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
160 callable->f(*this, message); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
161 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
162 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
163 void RegisterObserver(IObserver& observer) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
164 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
165 observers_.insert(&observer); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
166 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
167 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
168 void UnregisterObserver(IObserver& observer) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
169 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
170 observers_.erase(&observer); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
171 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
172 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
173 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
174 //template<typename TObserver> void Connect(MessageType messageType, IObserver& observer, void (TObserver::*ptrToMemberHandler)(IObservable& from, const IMessage& message)) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
175 void Connect(MessageType messageType, IObserver& observer, boost::function<void (IObservable& from, const IMessage& message)> f) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
176 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
177 callables_[messageType] = std::set<CallableObserver*>(); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
178 CallableObserver* callable = new CallableObserver(); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
179 callable->observer = &observer; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
180 callable->f = f; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
181 callables_[messageType].insert(callable); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
182 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
183 }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
184 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
185 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
186 class MyObservable : public IObservable |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
187 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
188 public: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
189 MyObservable(MessageBroker& broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
190 : IObservable(broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
191 {} |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
192 }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
193 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
194 class MyObserver : public IObserver |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
195 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
196 public: |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
197 MyObserver(MessageBroker& broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
198 : IObserver(broker) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
199 {} |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
200 virtual void HandleMessage(IObservable& from, const IMessage& message) {} |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
201 void HandleSpecificMessage(IObservable& from, const IMessage& message) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
202 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
203 testCounter++; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
204 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
205 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
206 }; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
207 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
208 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
209 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
210 //#define STONE_CONNECT(observabe, messageType, observerPtr, observerMemberFnPtr) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
211 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
212 TEST(MessageBroker2, Test1) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
213 { |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
214 MessageBroker broker; |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
215 MyObservable observable(broker); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
216 MyObserver observer(broker); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
217 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
218 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
219 observable.Connect(MessageType_Test1, observer, boost::bind(&MyObserver::HandleSpecificMessage, &observer, _1, _2)); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
220 //STONE_CONNECT(observable, MessageType_Test1, observer, &MyObserver::HandleSpecificMessage) |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
221 observable.EmitMessage(IMessage(MessageType_Test1)); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
222 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
223 ASSERT_EQ(1, testCounter); |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
224 } |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
225 |
3897f9f28cfa
backup work in progress: updated messaging framework with ICallable
am@osimis.io
parents:
diff
changeset
|
226 |