comparison OrthancStone/Sources/Messages/IObservable.cpp @ 1512:244ad1e4e76a

reorganization of folders
author Sebastien Jodogne <s.jodogne@gmail.com>
date Tue, 07 Jul 2020 16:21:02 +0200
parents Framework/Messages/IObservable.cpp@323bf6040f5d
children 8563ea5d8ae4
comparison
equal deleted inserted replaced
1511:9dfeee74c1e6 1512:244ad1e4e76a
1 /**
2 * Stone of Orthanc
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics
4 * Department, University Hospital of Liege, Belgium
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium
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
24 #include "../StoneException.h"
25
26 #include <Logging.h>
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
47 void IObservable::RegisterCallable(ICallable* callable)
48 {
49 if (callable == NULL)
50 {
51 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
52 }
53
54 const MessageIdentifier& id = callable->GetMessageIdentifier();
55 callables_[id].insert(callable);
56 }
57
58 void IObservable::EmitMessageInternal(const IObserver* receiver,
59 const IMessage& message)
60 {
61 //LOG(TRACE) << "IObservable::EmitMessageInternal receiver = " << std::hex << receiver << std::dec;
62 Callables::const_iterator found = callables_.find(message.GetIdentifier());
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);
70
71 boost::shared_ptr<IObserver> observer((*it)->GetObserver().lock());
72
73 if (observer)
74 {
75 if (receiver == NULL || // Are we broadcasting?
76 observer.get() == receiver) // Not broadcasting, but this is the receiver
77 {
78 try
79 {
80 (*it)->Apply(message);
81 }
82 catch (Orthanc::OrthancException& e)
83 {
84 LOG(ERROR) << "Exception on callable: " << e.What();
85 }
86 catch (StoneException& e)
87 {
88 LOG(ERROR) << "Exception on callable: " << e.What();
89 }
90 catch (...)
91 {
92 LOG(ERROR) << "Native exception on callable";
93 }
94 }
95 }
96 else
97 {
98 // TODO => Remove "it" from the list of callables => This
99 // allows to suppress the need for "Unregister()"
100 }
101 }
102 }
103 }
104
105
106 void IObservable::BroadcastMessage(const IMessage& message)
107 {
108 EmitMessageInternal(NULL, message);
109 }
110
111
112 void IObservable::EmitMessage(boost::weak_ptr<IObserver> observer,
113 const IMessage& message)
114 {
115 //LOG(TRACE) << "IObservable::EmitMessage observer = " << std::hex << observer.get() << std::dec;
116
117 boost::shared_ptr<IObserver> lock(observer.lock());
118 if (lock)
119 {
120 EmitMessageInternal(lock.get(), message);
121 }
122 }
123 }