annotate Framework/Messages/IObservable.cpp @ 1186:3284c3fd96ad broker

merge
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 22 Nov 2019 09:51:47 +0100
parents 5e164c629923
children 0ca50d275b9a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
403
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
1 /**
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
2 * Stone of Orthanc
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
4 * Department, University Hospital of Liege, Belgium
439
b70e9be013e4 preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 403
diff changeset
5 * Copyright (C) 2017-2019 Osimis S.A., Belgium
403
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
6 *
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
7 * This program is free software: you can redistribute it and/or
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
8 * modify it under the terms of the GNU Affero General Public License
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
9 * as published by the Free Software Foundation, either version 3 of
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
10 * the License, or (at your option) any later version.
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
11 *
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
12 * This program is distributed in the hope that it will be useful, but
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
15 * Affero General Public License for more details.
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
16 *
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
17 * You should have received a copy of the GNU Affero General Public License
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
19 **/
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
20
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
21
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
22 #include "IObservable.h"
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
973
38409549db43 Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents: 643
diff changeset
26 #include <Core/Logging.h>
403
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
27
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
28 #include <cassert>
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
29
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
30 namespace OrthancStone
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
31 {
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
32 IObservable::~IObservable()
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
33 {
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
34 // delete all callables (this will also unregister them from the broker)
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
35 for (Callables::const_iterator it = callables_.begin();
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
36 it != callables_.end(); ++it)
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
37 {
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
38 for (std::set<ICallable*>::const_iterator
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
39 it2 = it->second.begin(); it2 != it->second.end(); ++it2)
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
40 {
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
41 delete *it2;
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
42 }
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
43 }
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
44 }
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
45
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
46
1065
9d42f89b8c3c improved readability
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1064
diff changeset
47 void IObservable::RegisterCallable(ICallable* callable)
403
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
48 {
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
49 if (callable == NULL)
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
50 {
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
51 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
52 }
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
56 }
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
60 {
1148
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
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
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
63
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
64 if (found != callables_.end())
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
65 {
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
66 for (std::set<ICallable*>::const_iterator
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
67 it = found->second.begin(); it != found->second.end(); ++it)
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
68 {
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
101 }
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
102 }
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
103 }
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
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 {
1148
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1100
diff changeset
115 //LOG(TRACE) << "IObservable::EmitMessage observer = " << std::hex << &observer << std::dec;
1075
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1067
diff changeset
116
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1067
diff changeset
117 boost::shared_ptr<IObserver> lock(observer.lock());
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1067
diff changeset
118 if (lock)
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1067
diff changeset
119 {
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1067
diff changeset
120 EmitMessageInternal(lock.get(), message);
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 1067
diff changeset
121 }
623
42dadae61fa9 renamed IObservable::EmitMessage() as BroadcastMessage()
Sebastien Jodogne <s.jodogne@gmail.com>
parents: 440
diff changeset
122 }
403
99e31898910e IObservable.cpp
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff changeset
123 }