Mercurial > hg > orthanc-stone
annotate Framework/Messages/IObservable.cpp @ 1223:04fd875b91f4
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 07 Dec 2019 18:41:54 +0100 |
parents | 262a0244e9b2 |
children | e713f1a99861 2d8ab34c8c91 |
rev | line source |
---|---|
403 | 1 /** |
2 * Stone of Orthanc | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
439 | 5 * Copyright (C) 2017-2019 Osimis S.A., Belgium |
403 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU Affero General Public License | |
9 * as published by the Free Software Foundation, either version 3 of | |
10 * the License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * Affero General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU Affero General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "IObservable.h" | |
23 | |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
643
diff
changeset
|
24 #include <Core/Logging.h> |
403 | 25 #include <Core/OrthancException.h> |
26 | |
27 #include <cassert> | |
28 | |
29 namespace OrthancStone | |
30 { | |
31 IObservable::~IObservable() | |
32 { | |
33 // delete all callables (this will also unregister them from the broker) | |
34 for (Callables::const_iterator it = callables_.begin(); | |
35 it != callables_.end(); ++it) | |
36 { | |
37 for (std::set<ICallable*>::const_iterator | |
38 it2 = it->second.begin(); it2 != it->second.end(); ++it2) | |
39 { | |
40 delete *it2; | |
41 } | |
42 } | |
43 | |
44 // unregister the forwarders but don't delete them (they'll be | |
45 // deleted by the observable they are observing as any other | |
46 // callable) | |
47 for (Forwarders::iterator it = forwarders_.begin(); | |
48 it != forwarders_.end(); ++it) | |
49 { | |
50 IMessageForwarder* fw = *it; | |
51 broker_.Unregister(dynamic_cast<IObserver&>(*fw)); | |
52 } | |
53 } | |
54 | |
55 | |
56 void IObservable::RegisterObserverCallback(ICallable* callable) | |
57 { | |
58 if (callable == NULL) | |
59 { | |
60 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
61 } | |
62 | |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
623
diff
changeset
|
63 const MessageIdentifier& id = callable->GetMessageIdentifier(); |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
623
diff
changeset
|
64 callables_[id].insert(callable); |
403 | 65 } |
66 | |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
67 void IObservable::Unregister(IObserver *observer) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
68 { |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
69 LOG(TRACE) << "IObservable::Unregister for IObserver at addr: " |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
70 << std::hex << observer << std::dec; |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
71 // delete all callables from this observer |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
72 for (Callables::iterator itCallableSet = callables_.begin(); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
73 itCallableSet != callables_.end(); ++itCallableSet) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
74 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
75 for (std::set<ICallable*>::const_iterator |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
76 itCallable = itCallableSet->second.begin(); itCallable != itCallableSet->second.end(); ) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
77 { |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
78 if ((*itCallable)->GetObserver() == observer) |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
79 { |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
80 LOG(TRACE) << " ** IObservable::Unregister : deleting callable: " |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
81 << std::hex << (*itCallable) << std::dec; |
428
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
82 delete *itCallable; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
83 itCallableSet->second.erase(itCallable++); |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
84 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
85 else |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
86 ++itCallable; |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
87 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
88 } |
751fb354149e
ability to change the scene of the RadiographyWidget
am@osimis.io
parents:
403
diff
changeset
|
89 } |
403 | 90 |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
91 void IObservable::EmitMessageInternal(const IObserver* receiver, |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
92 const IMessage& message) |
403 | 93 { |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
94 LOG(TRACE) << "IObservable::EmitMessageInternal receiver = " |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
95 << std::hex << receiver << std::dec; |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
623
diff
changeset
|
96 Callables::const_iterator found = callables_.find(message.GetIdentifier()); |
403 | 97 |
98 if (found != callables_.end()) | |
99 { | |
100 for (std::set<ICallable*>::const_iterator | |
101 it = found->second.begin(); it != found->second.end(); ++it) | |
102 { | |
103 assert(*it != NULL); | |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
104 |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
105 const IObserver* observer = (*it)->GetObserver(); |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
106 if (broker_.IsActive(*observer)) |
403 | 107 { |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
108 if (receiver == NULL || // Are we broadcasting? |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
109 observer == receiver) // Not broadcasting, but this is the receiver |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
110 { |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
111 (*it)->Apply(message); |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
112 } |
403 | 113 } |
114 } | |
115 } | |
116 } | |
117 | |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
118 |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
119 void IObservable::BroadcastMessage(const IMessage& message) |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
120 { |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
121 EmitMessageInternal(NULL, message); |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
122 } |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
123 |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
124 |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
125 void IObservable::EmitMessage(const IObserver& observer, |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
126 const IMessage& message) |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
127 { |
977
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
128 LOG(TRACE) << "IObservable::EmitMessage observer = " |
262a0244e9b2
Added missing Unregister for objects that register by the broker + logs + guard in FetchContext
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
129 << std::hex << &observer << std::dec; |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
130 EmitMessageInternal(&observer, message); |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
131 } |
403 | 132 |
133 void IObservable::RegisterForwarder(IMessageForwarder* forwarder) | |
134 { | |
135 if (forwarder == NULL) | |
136 { | |
137 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
138 } | |
139 | |
140 forwarders_.insert(forwarder); | |
141 } | |
142 } |