Mercurial > hg > orthanc-stone
annotate Framework/Loaders/LoaderStateMachine.cpp @ 1224:37bc7f115f81 broker
integration mainline->broker
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 07 Dec 2019 18:45:37 +0100 |
parents | a0a33e5ea5bb |
children |
rev | line source |
---|---|
815 | 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-2019 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 "LoaderStateMachine.h" | |
23 | |
24 #include <Core/OrthancException.h> | |
25 | |
26 namespace OrthancStone | |
27 { | |
28 void LoaderStateMachine::State::Handle(const OrthancRestApiCommand::SuccessMessage& message) | |
29 { | |
30 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
31 } | |
32 | |
33 | |
34 void LoaderStateMachine::State::Handle(const GetOrthancImageCommand::SuccessMessage& message) | |
35 { | |
36 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
37 } | |
38 | |
39 | |
40 void LoaderStateMachine::State::Handle(const GetOrthancWebViewerJpegCommand::SuccessMessage& message) | |
41 { | |
42 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
43 } | |
44 | |
45 | |
1128
8e3763d1736a
removing CustomOracleCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1075
diff
changeset
|
46 void LoaderStateMachine::Schedule(OracleCommandBase* command) |
815 | 47 { |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
48 LOG(TRACE) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::Schedule()"; |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
956
diff
changeset
|
49 |
1128
8e3763d1736a
removing CustomOracleCommand
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1075
diff
changeset
|
50 std::auto_ptr<OracleCommandBase> protection(command); |
815 | 51 |
52 if (command == NULL) | |
53 { | |
54 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
55 } | |
56 | |
57 if (!command->HasPayload()) | |
58 { | |
59 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange, | |
60 "The payload must contain the next state"); | |
61 } | |
62 pendingCommands_.push_back(protection.release()); | |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
956
diff
changeset
|
63 |
815 | 64 Step(); |
65 } | |
66 | |
67 | |
68 void LoaderStateMachine::Start() | |
69 { | |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
70 LOG(TRACE) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::Start()"; |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
71 |
815 | 72 if (active_) |
73 { | |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
956
diff
changeset
|
74 LOG(TRACE) << "LoaderStateMachine::Start() called while active_ is true"; |
815 | 75 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
76 } | |
77 | |
78 active_ = true; | |
79 | |
80 for (size_t i = 0; i < simultaneousDownloads_; i++) | |
81 { | |
82 Step(); | |
83 } | |
84 } | |
85 | |
86 | |
87 void LoaderStateMachine::Step() | |
88 { | |
89 if (!pendingCommands_.empty() && | |
90 activeCommands_ < simultaneousDownloads_) | |
91 { | |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
956
diff
changeset
|
92 |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
956
diff
changeset
|
93 IOracleCommand* nextCommand = pendingCommands_.front(); |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
956
diff
changeset
|
94 |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
95 LOG(TRACE) << " LoaderStateMachine(" << std::hex << this << std::dec << |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
96 ")::Step(): activeCommands_ (" << activeCommands_ << |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
97 ") < simultaneousDownloads_ (" << simultaneousDownloads_ << |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
98 ") --> will Schedule command addr " << std::hex << nextCommand << std::dec; |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
956
diff
changeset
|
99 |
1075 | 100 boost::shared_ptr<IObserver> observer(GetSharedObserver()); |
101 oracle_.Schedule(observer, nextCommand); | |
815 | 102 pendingCommands_.pop_front(); |
103 | |
104 activeCommands_++; | |
105 } | |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
956
diff
changeset
|
106 else |
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
956
diff
changeset
|
107 { |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
108 LOG(TRACE) << " LoaderStateMachine(" << std::hex << this << std::dec << |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
109 ")::Step(): activeCommands_ (" << activeCommands_ << |
1061
eb28dfe432f7
fixed typo in assert. msg. + getters for custom windowing in grayscale layer configurator.
Benjamin Golinvaux <bgo@osimis.io>
parents:
977
diff
changeset
|
110 ") >= simultaneousDownloads_ (" << simultaneousDownloads_ << |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
111 ") --> will NOT Schedule command"; |
964
91f827272c1f
Added cache-control headers for POST requests + #ifdef'd tracing logs + trace on context restored
Benjamin Golinvaux <bgo@osimis.io>
parents:
956
diff
changeset
|
112 } |
815 | 113 } |
114 | |
115 | |
116 void LoaderStateMachine::Clear() | |
117 { | |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
118 LOG(TRACE) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::Clear()"; |
815 | 119 for (PendingCommands::iterator it = pendingCommands_.begin(); |
120 it != pendingCommands_.end(); ++it) | |
121 { | |
122 delete *it; | |
123 } | |
124 | |
125 pendingCommands_.clear(); | |
126 } | |
127 | |
128 | |
129 void LoaderStateMachine::HandleExceptionMessage(const OracleCommandExceptionMessage& message) | |
130 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
840
diff
changeset
|
131 LOG(ERROR) << "LoaderStateMachine::HandleExceptionMessage: error in the state machine, stopping all processing"; |
840
47fc7919977d
Added details on errors in LoaderStateMachine + dummy change in loader.cpp
Benjamin Golinvaux <bgo@osimis.io>
parents:
815
diff
changeset
|
132 LOG(ERROR) << "Error: " << message.GetException().What() << " Details: " << |
47fc7919977d
Added details on errors in LoaderStateMachine + dummy change in loader.cpp
Benjamin Golinvaux <bgo@osimis.io>
parents:
815
diff
changeset
|
133 message.GetException().GetDetails(); |
47fc7919977d
Added details on errors in LoaderStateMachine + dummy change in loader.cpp
Benjamin Golinvaux <bgo@osimis.io>
parents:
815
diff
changeset
|
134 Clear(); |
815 | 135 } |
136 | |
137 template <typename T> | |
138 void LoaderStateMachine::HandleSuccessMessage(const T& message) | |
139 { | |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
140 if (activeCommands_ <= 0) { |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
141 LOG(ERROR) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::HandleSuccessMessage : activeCommands_ should be > 0 but is: " << activeCommands_; |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
142 } |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
143 else { |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
144 activeCommands_--; |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
145 try |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
146 { |
1135
a0a33e5ea5bb
IOracleCommand::Clone()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1134
diff
changeset
|
147 dynamic_cast<State&>(message.GetOrigin().GetPayload()).Handle(message); |
975
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
148 Step(); |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
149 } |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
150 catch (Orthanc::OrthancException& e) |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
151 { |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
152 LOG(ERROR) << "Error in the state machine, stopping all processing: " << |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
153 e.What() << " Details: " << e.GetDetails(); |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
154 Clear(); |
e75fd08d6c75
Cleaning in ICallable + changed fingerprint to plain char array to allow for
Benjamin Golinvaux <bgo@osimis.io>
parents:
973
diff
changeset
|
155 } |
815 | 156 } |
157 } | |
158 | |
159 | |
160 LoaderStateMachine::LoaderStateMachine(IOracle& oracle, | |
161 IObservable& oracleObservable) : | |
162 oracle_(oracle), | |
163 active_(false), | |
164 simultaneousDownloads_(4), | |
165 activeCommands_(0) | |
166 { | |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
167 LOG(TRACE) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::LoaderStateMachine()"; |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
168 |
1065
9d42f89b8c3c
improved readability
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1064
diff
changeset
|
169 // TODO => Move this out of constructor |
9d42f89b8c3c
improved readability
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1064
diff
changeset
|
170 Register<OrthancRestApiCommand::SuccessMessage>(oracleObservable, &LoaderStateMachine::HandleSuccessMessage); |
9d42f89b8c3c
improved readability
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1064
diff
changeset
|
171 Register<GetOrthancImageCommand::SuccessMessage>(oracleObservable, &LoaderStateMachine::HandleSuccessMessage); |
9d42f89b8c3c
improved readability
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1064
diff
changeset
|
172 Register<GetOrthancWebViewerJpegCommand::SuccessMessage>(oracleObservable, &LoaderStateMachine::HandleSuccessMessage); |
9d42f89b8c3c
improved readability
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1064
diff
changeset
|
173 Register<OracleCommandExceptionMessage>(oracleObservable, &LoaderStateMachine::HandleExceptionMessage); |
815 | 174 } |
175 | |
973
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
176 LoaderStateMachine::~LoaderStateMachine() |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
177 { |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
178 LOG(TRACE) << "LoaderStateMachine(" << std::hex << this << std::dec << ")::~LoaderStateMachine()"; |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
179 Clear(); |
38409549db43
Log with addresses + added fingerprint mechanism to avoid calling zombie objects
Benjamin Golinvaux <bgo@osimis.io>
parents:
964
diff
changeset
|
180 } |
815 | 181 |
182 void LoaderStateMachine::SetSimultaneousDownloads(unsigned int count) | |
183 { | |
184 if (active_) | |
185 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
840
diff
changeset
|
186 LOG(ERROR) << "LoaderStateMachine::SetSimultaneousDownloads called while active_ is true"; |
815 | 187 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
188 } | |
189 else if (count == 0) | |
190 { | |
191 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
192 } | |
193 else | |
194 { | |
195 simultaneousDownloads_ = count; | |
196 } | |
197 } | |
198 } |