Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Messages/IObservable.cpp @ 1574:fb5e620430ae
fix
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 21 Sep 2020 18:29:53 +0200 |
parents | 244ad1e4e76a |
children | 8563ea5d8ae4 |
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 | |
1270
2d8ab34c8c91
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
5 * Copyright (C) 2017-2020 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 | |
1100
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
24 #include "../StoneException.h" |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
25 |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1279
diff
changeset
|
26 #include <Logging.h> |
403 | 27 |
28 #include <cassert> | |
29 | |
30 namespace OrthancStone | |
31 { | |
32 IObservable::~IObservable() | |
33 { | |
34 // delete all callables (this will also unregister them from the broker) | |
35 for (Callables::const_iterator it = callables_.begin(); | |
36 it != callables_.end(); ++it) | |
37 { | |
38 for (std::set<ICallable*>::const_iterator | |
39 it2 = it->second.begin(); it2 != it->second.end(); ++it2) | |
40 { | |
41 delete *it2; | |
42 } | |
43 } | |
44 } | |
45 | |
46 | |
1065
9d42f89b8c3c
improved readability
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1064
diff
changeset
|
47 void IObservable::RegisterCallable(ICallable* callable) |
403 | 48 { |
49 if (callable == NULL) | |
50 { | |
51 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
52 } | |
53 | |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
623
diff
changeset
|
54 const MessageIdentifier& id = callable->GetMessageIdentifier(); |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
623
diff
changeset
|
55 callables_[id].insert(callable); |
403 | 56 } |
57 | |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
58 void IObservable::EmitMessageInternal(const IObserver* receiver, |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
59 const IMessage& message) |
403 | 60 { |
1148 | 61 //LOG(TRACE) << "IObservable::EmitMessageInternal receiver = " << std::hex << receiver << std::dec; |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
623
diff
changeset
|
62 Callables::const_iterator found = callables_.find(message.GetIdentifier()); |
403 | 63 |
64 if (found != callables_.end()) | |
65 { | |
66 for (std::set<ICallable*>::const_iterator | |
67 it = found->second.begin(); it != found->second.end(); ++it) | |
68 { | |
69 assert(*it != NULL); | |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
70 |
1067
05b2e71ed145
removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1065
diff
changeset
|
71 boost::shared_ptr<IObserver> observer((*it)->GetObserver().lock()); |
1059
e713f1a99861
replacing MessageBroker by weak_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
72 |
e713f1a99861
replacing MessageBroker by weak_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
73 if (observer) |
403 | 74 { |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
75 if (receiver == NULL || // Are we broadcasting? |
1059
e713f1a99861
replacing MessageBroker by weak_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
76 observer.get() == receiver) // Not broadcasting, but this is the receiver |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
77 { |
1100
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
78 try |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
79 { |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
80 (*it)->Apply(message); |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
81 } |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
82 catch (Orthanc::OrthancException& e) |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
83 { |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
84 LOG(ERROR) << "Exception on callable: " << e.What(); |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
85 } |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
86 catch (StoneException& e) |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
87 { |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
88 LOG(ERROR) << "Exception on callable: " << e.What(); |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
89 } |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
90 catch (...) |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
91 { |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
92 LOG(ERROR) << "Native exception on callable"; |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
93 } |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
94 } |
403 | 95 } |
1064
af4b9cba905f
simplify how observers are regsitered, and how callables are created
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1059
diff
changeset
|
96 else |
af4b9cba905f
simplify how observers are regsitered, and how callables are created
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1059
diff
changeset
|
97 { |
af4b9cba905f
simplify how observers are regsitered, and how callables are created
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1059
diff
changeset
|
98 // TODO => Remove "it" from the list of callables => This |
af4b9cba905f
simplify how observers are regsitered, and how callables are created
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1059
diff
changeset
|
99 // allows to suppress the need for "Unregister()" |
af4b9cba905f
simplify how observers are regsitered, and how callables are created
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1059
diff
changeset
|
100 } |
403 | 101 } |
102 } | |
103 } | |
104 | |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
105 |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
106 void IObservable::BroadcastMessage(const IMessage& message) |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
107 { |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
108 EmitMessageInternal(NULL, message); |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
109 } |
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 |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1075
diff
changeset
|
112 void IObservable::EmitMessage(boost::weak_ptr<IObserver> observer, |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
113 const IMessage& message) |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
114 { |
1468
323bf6040f5d
fix for boost::program_options
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1455
diff
changeset
|
115 //LOG(TRACE) << "IObservable::EmitMessage observer = " << std::hex << observer.get() << std::dec; |
1075 | 116 |
117 boost::shared_ptr<IObserver> lock(observer.lock()); | |
118 if (lock) | |
403 | 119 { |
1075 | 120 EmitMessageInternal(lock.get(), message); |
403 | 121 } |
122 } | |
123 } |