Mercurial > hg > orthanc-stone
annotate OrthancStone/Sources/Messages/IObservable.cpp @ 1867:4fa025398a38
todo
author | Alain Mazy <am@osimis.io> |
---|---|
date | Thu, 23 Dec 2021 16:06:37 +0100 |
parents | be88206f8d78 |
children | 3889ae96d2e9 |
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 | |
1739
9ac2a65d4172
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1598
diff
changeset
|
5 * Copyright (C) 2017-2021 Osimis S.A., Belgium |
403 | 6 * |
7 * 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
|
8 * modify it under the terms of the GNU Lesser General Public License |
403 | 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 | |
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
|
14 * 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
|
15 * Lesser General Public License for more details. |
403 | 16 * |
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
|
17 * 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
|
18 * 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
|
19 * <http://www.gnu.org/licenses/>. |
403 | 20 **/ |
21 | |
22 | |
23 #include "IObservable.h" | |
24 | |
1100
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
25 #include "../StoneException.h" |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
26 |
1455
30deba7bc8e2
simplifying include_directories
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1279
diff
changeset
|
27 #include <Logging.h> |
403 | 28 |
29 #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
|
30 #include <stdexcept> |
403 | 31 |
32 namespace OrthancStone | |
33 { | |
34 IObservable::~IObservable() | |
35 { | |
36 // delete all callables (this will also unregister them from the broker) | |
37 for (Callables::const_iterator it = callables_.begin(); | |
38 it != callables_.end(); ++it) | |
39 { | |
40 for (std::set<ICallable*>::const_iterator | |
41 it2 = it->second.begin(); it2 != it->second.end(); ++it2) | |
42 { | |
43 delete *it2; | |
44 } | |
45 } | |
46 } | |
47 | |
48 | |
1065
9d42f89b8c3c
improved readability
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1064
diff
changeset
|
49 void IObservable::RegisterCallable(ICallable* callable) |
403 | 50 { |
51 if (callable == NULL) | |
52 { | |
53 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
54 } | |
55 | |
643
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
623
diff
changeset
|
56 const MessageIdentifier& id = callable->GetMessageIdentifier(); |
f0008c55e5f7
getting rid of MessageType enumeration
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
623
diff
changeset
|
57 callables_[id].insert(callable); |
403 | 58 } |
59 | |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
60 void IObservable::EmitMessageInternal(const IObserver* receiver, |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
61 const IMessage& message) |
403 | 62 { |
1148 | 63 //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
|
64 Callables::const_iterator found = callables_.find(message.GetIdentifier()); |
403 | 65 |
66 if (found != callables_.end()) | |
67 { | |
68 for (std::set<ICallable*>::const_iterator | |
69 it = found->second.begin(); it != found->second.end(); ++it) | |
70 { | |
71 assert(*it != NULL); | |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
72 |
1067
05b2e71ed145
removed MessageForwarder, unit tests are ok
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1065
diff
changeset
|
73 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
|
74 |
e713f1a99861
replacing MessageBroker by weak_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
75 if (observer) |
403 | 76 { |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
77 if (receiver == NULL || // Are we broadcasting? |
1059
e713f1a99861
replacing MessageBroker by weak_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
977
diff
changeset
|
78 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
|
79 { |
1100
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
80 try |
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 (*it)->Apply(message); |
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 catch (Orthanc::OrthancException& e) |
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 LOG(ERROR) << "Exception on callable: " << e.What(); |
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 catch (StoneException& e) |
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 LOG(ERROR) << "Exception on callable: " << e.What(); |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
91 } |
1858
be88206f8d78
display what() of C++ exceptions on error while processing a message
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
92 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
|
93 { |
be88206f8d78
display what() of C++ exceptions on error while processing a message
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1739
diff
changeset
|
94 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
|
95 } |
1100
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
96 catch (...) |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
97 { |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
98 LOG(ERROR) << "Native exception on callable"; |
08cc0d47aa94
exception handling in IObservable::EmitMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1098
diff
changeset
|
99 } |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
100 } |
403 | 101 } |
1064
af4b9cba905f
simplify how observers are regsitered, and how callables are created
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1059
diff
changeset
|
102 else |
af4b9cba905f
simplify how observers are regsitered, and how callables are created
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1059
diff
changeset
|
103 { |
af4b9cba905f
simplify how observers are regsitered, and how callables are created
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1059
diff
changeset
|
104 // 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
|
105 // 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
|
106 } |
403 | 107 } |
108 } | |
109 } | |
110 | |
623
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
111 |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
112 void IObservable::BroadcastMessage(const IMessage& message) |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
113 { |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
114 EmitMessageInternal(NULL, message); |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
115 } |
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 |
1098
17660df24c36
simplification of IOracleRunner
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1075
diff
changeset
|
118 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
|
119 const IMessage& message) |
42dadae61fa9
renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
440
diff
changeset
|
120 { |
1468
323bf6040f5d
fix for boost::program_options
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1455
diff
changeset
|
121 //LOG(TRACE) << "IObservable::EmitMessage observer = " << std::hex << observer.get() << std::dec; |
1075 | 122 |
123 boost::shared_ptr<IObserver> lock(observer.lock()); | |
124 if (lock) | |
403 | 125 { |
1075 | 126 EmitMessageInternal(lock.get(), message); |
403 | 127 } |
128 } | |
129 } |