Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Messages/IObservable.cpp @ 1886:ca89fec8c48f
test
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 14 Jan 2022 19:10:30 +0100 |
parents | 7053b8a0aaec |
children | 07964689cb0b |
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 | |
1871
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
7053b8a0aaec
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1870
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
403 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public License |
403 | 10 * as published by the Free Software Foundation, either version 3 of |
11 * the License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
16 * Lesser General Public License for more details. |
403 | 17 * |
1598
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
19 * License along with this program. If not, see |
8563ea5d8ae4
relicensing some files, cf. osimis bm26 and chu agreement on 2020-05-20
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1512
diff
changeset
|
20 * <http://www.gnu.org/licenses/>. |
403 | 21 **/ |
22 | |
23 | |
24 #include "IObservable.h" | |
25 | |
1100
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
26 #include "../StoneException.h" |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
27 |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1279
diff
changeset
|
28 #include <Logging.h> |
403 | 29 |
30 #include <cassert> | |
1858
be88206f8d78
display what() of C++ exceptions on error while processing a message
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
31 #include <stdexcept> |
403 | 32 |
33 namespace OrthancStone | |
34 { | |
35 IObservable::~IObservable() | |
36 { | |
37 // delete all callables (this will also unregister them from the broker) | |
38 for (Callables::const_iterator it = callables_.begin(); | |
39 it != callables_.end(); ++it) | |
40 { | |
41 for (std::set<ICallable*>::const_iterator | |
42 it2 = it->second.begin(); it2 != it->second.end(); ++it2) | |
43 { | |
44 delete *it2; | |
45 } | |
46 } | |
47 } | |
48 | |
49 | |
1065
9d42f89b8c3c
improved readability
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1064
diff
changeset
|
50 void IObservable::RegisterCallable(ICallable* callable) |
403 | 51 { |
52 if (callable == NULL) | |
53 { | |
54 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
55 } | |
56 | |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
623
diff
changeset
|
57 const MessageIdentifier& id = callable->GetMessageIdentifier(); |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
623
diff
changeset
|
58 callables_[id].insert(callable); |
403 | 59 } |
60 | |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
61 void IObservable::EmitMessageInternal(const IObserver* receiver, |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
62 const IMessage& message) |
403 | 63 { |
1148 | 64 //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
|
65 Callables::const_iterator found = callables_.find(message.GetIdentifier()); |
403 | 66 |
67 if (found != callables_.end()) | |
68 { | |
69 for (std::set<ICallable*>::const_iterator | |
70 it = found->second.begin(); it != found->second.end(); ++it) | |
71 { | |
72 assert(*it != NULL); | |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
73 |
1067
05b2e71ed145
removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1065
diff
changeset
|
74 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
|
75 |
e713f1a99861
replacing MessageBroker by weak_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
76 if (observer) |
403 | 77 { |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
78 if (receiver == NULL || // Are we broadcasting? |
1059
e713f1a99861
replacing MessageBroker by weak_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
79 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
|
80 { |
1100
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
81 try |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
82 { |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
83 (*it)->Apply(message); |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
84 } |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
85 catch (Orthanc::OrthancException& e) |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
86 { |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
87 LOG(ERROR) << "Exception on callable: " << e.What(); |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
88 } |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
89 catch (StoneException& e) |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
90 { |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
91 LOG(ERROR) << "Exception on callable: " << e.What(); |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
92 } |
1858
be88206f8d78
display what() of C++ exceptions on error while processing a message
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
93 catch (std::exception& e) |
be88206f8d78
display what() of C++ exceptions on error while processing a message
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
94 { |
be88206f8d78
display what() of C++ exceptions on error while processing a message
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
95 LOG(ERROR) << "C++ exception on callable: " << e.what(); |
be88206f8d78
display what() of C++ exceptions on error while processing a message
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
96 } |
1100
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
97 catch (...) |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
98 { |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
99 LOG(ERROR) << "Native exception on callable"; |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
100 } |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
101 } |
403 | 102 } |
1064
af4b9cba905f
simplify how observers are regsitered, and how callables are created
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1059
diff
changeset
|
103 else |
af4b9cba905f
simplify how observers are regsitered, and how callables are created
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1059
diff
changeset
|
104 { |
af4b9cba905f
simplify how observers are regsitered, and how callables are created
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1059
diff
changeset
|
105 // 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
|
106 // 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
|
107 } |
403 | 108 } |
109 } | |
110 } | |
111 | |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
112 |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
113 void IObservable::BroadcastMessage(const IMessage& message) |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
114 { |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
115 EmitMessageInternal(NULL, message); |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
116 } |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
117 |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
118 |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1075
diff
changeset
|
119 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
|
120 const IMessage& message) |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
121 { |
1468
323bf6040f5d
fix for boost::program_options
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1455
diff
changeset
|
122 //LOG(TRACE) << "IObservable::EmitMessage observer = " << std::hex << observer.get() << std::dec; |
1075 | 123 |
124 boost::shared_ptr<IObserver> lock(observer.lock()); | |
125 if (lock) | |
403 | 126 { |
1075 | 127 EmitMessageInternal(lock.get(), message); |
403 | 128 } |
129 } | |
130 } |