Mercurial > hg > orthanc-stone
annotate Framework/Oracle/ThreadedOracle.cpp @ 981:c20dbaab360c
Ability to cope with empty "Referenced SOP Instance UID" (dicom path (3006,0039)[i] / (0x3006, 0x0040)[0] / (0x3006, 0x0016)[0] / (0x0008, 0x1155)) + better logs + code formating
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Fri, 06 Sep 2019 09:38:18 +0200 |
parents | a7351ad54960 |
children | 18d53a8b41b7 |
rev | line source |
---|---|
748 | 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 "ThreadedOracle.h" | |
23 | |
24 #include "GetOrthancImageCommand.h" | |
25 #include "GetOrthancWebViewerJpegCommand.h" | |
26 #include "OrthancRestApiCommand.h" | |
27 #include "SleepOracleCommand.h" | |
28 #include "OracleCommandExceptionMessage.h" | |
29 | |
30 #include <Core/Compression/GzipCompressor.h> | |
31 #include <Core/HttpClient.h> | |
32 #include <Core/OrthancException.h> | |
33 #include <Core/Toolbox.h> | |
34 | |
35 | |
36 namespace OrthancStone | |
37 { | |
38 class ThreadedOracle::Item : public Orthanc::IDynamicObject | |
39 { | |
40 private: | |
41 const IObserver& receiver_; | |
42 std::auto_ptr<IOracleCommand> command_; | |
43 | |
44 public: | |
45 Item(const IObserver& receiver, | |
46 IOracleCommand* command) : | |
47 receiver_(receiver), | |
48 command_(command) | |
49 { | |
50 if (command == NULL) | |
51 { | |
52 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
53 } | |
54 } | |
55 | |
56 const IObserver& GetReceiver() const | |
57 { | |
58 return receiver_; | |
59 } | |
60 | |
61 IOracleCommand& GetCommand() | |
62 { | |
63 assert(command_.get() != NULL); | |
64 return *command_; | |
65 } | |
66 }; | |
67 | |
68 | |
69 class ThreadedOracle::SleepingCommands : public boost::noncopyable | |
70 { | |
71 private: | |
72 class Item | |
73 { | |
74 private: | |
75 const IObserver& receiver_; | |
76 std::auto_ptr<SleepOracleCommand> command_; | |
77 boost::posix_time::ptime expiration_; | |
78 | |
79 public: | |
80 Item(const IObserver& receiver, | |
81 SleepOracleCommand* command) : | |
82 receiver_(receiver), | |
83 command_(command) | |
84 { | |
85 if (command == NULL) | |
86 { | |
87 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
88 } | |
89 | |
760
1181e1ad98ec
progressive loading working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
748
diff
changeset
|
90 expiration_ = (boost::posix_time::microsec_clock::local_time() + |
748 | 91 boost::posix_time::milliseconds(command_->GetDelay())); |
92 } | |
93 | |
94 const boost::posix_time::ptime& GetExpirationTime() const | |
95 { | |
96 return expiration_; | |
97 } | |
98 | |
99 void Awake(IMessageEmitter& emitter) | |
100 { | |
101 assert(command_.get() != NULL); | |
102 | |
103 SleepOracleCommand::TimeoutMessage message(*command_); | |
104 emitter.EmitMessage(receiver_, message); | |
105 } | |
106 }; | |
107 | |
108 typedef std::list<Item*> Content; | |
109 | |
110 boost::mutex mutex_; | |
111 Content content_; | |
112 | |
113 public: | |
114 ~SleepingCommands() | |
115 { | |
116 for (Content::iterator it = content_.begin(); it != content_.end(); ++it) | |
117 { | |
118 if (*it != NULL) | |
119 { | |
120 delete *it; | |
121 } | |
122 } | |
123 } | |
124 | |
125 void Add(const IObserver& receiver, | |
126 SleepOracleCommand* command) // Takes ownership | |
127 { | |
128 boost::mutex::scoped_lock lock(mutex_); | |
129 | |
130 content_.push_back(new Item(receiver, command)); | |
131 } | |
132 | |
133 void AwakeExpired(IMessageEmitter& emitter) | |
134 { | |
135 boost::mutex::scoped_lock lock(mutex_); | |
136 | |
760
1181e1ad98ec
progressive loading working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
748
diff
changeset
|
137 const boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); |
748 | 138 |
139 Content stillSleeping; | |
140 | |
141 for (Content::iterator it = content_.begin(); it != content_.end(); ++it) | |
142 { | |
143 if (*it != NULL && | |
144 (*it)->GetExpirationTime() <= now) | |
145 { | |
146 (*it)->Awake(emitter); | |
147 delete *it; | |
148 *it = NULL; | |
149 } | |
150 else | |
151 { | |
152 stillSleeping.push_back(*it); | |
153 } | |
154 } | |
155 | |
156 // Compact the still-sleeping commands | |
157 content_ = stillSleeping; | |
158 } | |
159 }; | |
160 | |
161 | |
162 static void CopyHttpHeaders(Orthanc::HttpClient& client, | |
163 const Orthanc::HttpClient::HttpHeaders& headers) | |
164 { | |
165 for (Orthanc::HttpClient::HttpHeaders::const_iterator | |
166 it = headers.begin(); it != headers.end(); it++ ) | |
167 { | |
168 client.AddHeader(it->first, it->second); | |
169 } | |
170 } | |
171 | |
172 | |
173 static void DecodeAnswer(std::string& answer, | |
174 const Orthanc::HttpClient::HttpHeaders& headers) | |
175 { | |
176 Orthanc::HttpCompression contentEncoding = Orthanc::HttpCompression_None; | |
177 | |
178 for (Orthanc::HttpClient::HttpHeaders::const_iterator it = headers.begin(); | |
179 it != headers.end(); ++it) | |
180 { | |
181 std::string s; | |
182 Orthanc::Toolbox::ToLowerCase(s, it->first); | |
183 | |
184 if (s == "content-encoding") | |
185 { | |
186 if (it->second == "gzip") | |
187 { | |
188 contentEncoding = Orthanc::HttpCompression_Gzip; | |
189 } | |
190 else | |
191 { | |
192 throw Orthanc::OrthancException(Orthanc::ErrorCode_NetworkProtocol, | |
193 "Unsupported HTTP Content-Encoding: " + it->second); | |
194 } | |
195 | |
196 break; | |
197 } | |
198 } | |
199 | |
200 if (contentEncoding == Orthanc::HttpCompression_Gzip) | |
201 { | |
202 std::string compressed; | |
203 answer.swap(compressed); | |
204 | |
205 Orthanc::GzipCompressor compressor; | |
206 compressor.Uncompress(answer, compressed.c_str(), compressed.size()); | |
765 | 207 |
208 LOG(INFO) << "Uncompressing gzip Encoding: from " << compressed.size() | |
209 << " to " << answer.size() << " bytes"; | |
748 | 210 } |
211 } | |
212 | |
213 | |
214 static void Execute(IMessageEmitter& emitter, | |
215 const Orthanc::WebServiceParameters& orthanc, | |
216 const IObserver& receiver, | |
217 const OrthancRestApiCommand& command) | |
218 { | |
219 Orthanc::HttpClient client(orthanc, command.GetUri()); | |
220 client.SetMethod(command.GetMethod()); | |
221 client.SetTimeout(command.GetTimeout()); | |
222 | |
223 CopyHttpHeaders(client, command.GetHttpHeaders()); | |
224 | |
225 if (command.GetMethod() == Orthanc::HttpMethod_Post || | |
226 command.GetMethod() == Orthanc::HttpMethod_Put) | |
227 { | |
228 client.SetBody(command.GetBody()); | |
229 } | |
230 | |
231 std::string answer; | |
232 Orthanc::HttpClient::HttpHeaders answerHeaders; | |
233 client.ApplyAndThrowException(answer, answerHeaders); | |
234 | |
235 DecodeAnswer(answer, answerHeaders); | |
236 | |
237 OrthancRestApiCommand::SuccessMessage message(command, answerHeaders, answer); | |
238 emitter.EmitMessage(receiver, message); | |
239 } | |
240 | |
241 | |
242 static void Execute(IMessageEmitter& emitter, | |
243 const Orthanc::WebServiceParameters& orthanc, | |
244 const IObserver& receiver, | |
245 const GetOrthancImageCommand& command) | |
246 { | |
247 Orthanc::HttpClient client(orthanc, command.GetUri()); | |
248 client.SetTimeout(command.GetTimeout()); | |
249 | |
250 CopyHttpHeaders(client, command.GetHttpHeaders()); | |
819 | 251 |
748 | 252 std::string answer; |
253 Orthanc::HttpClient::HttpHeaders answerHeaders; | |
254 client.ApplyAndThrowException(answer, answerHeaders); | |
255 | |
256 DecodeAnswer(answer, answerHeaders); | |
257 | |
258 command.ProcessHttpAnswer(emitter, receiver, answer, answerHeaders); | |
259 } | |
260 | |
261 | |
262 static void Execute(IMessageEmitter& emitter, | |
263 const Orthanc::WebServiceParameters& orthanc, | |
264 const IObserver& receiver, | |
265 const GetOrthancWebViewerJpegCommand& command) | |
266 { | |
267 Orthanc::HttpClient client(orthanc, command.GetUri()); | |
268 client.SetTimeout(command.GetTimeout()); | |
269 | |
270 CopyHttpHeaders(client, command.GetHttpHeaders()); | |
271 | |
272 std::string answer; | |
273 Orthanc::HttpClient::HttpHeaders answerHeaders; | |
274 client.ApplyAndThrowException(answer, answerHeaders); | |
275 | |
276 DecodeAnswer(answer, answerHeaders); | |
277 | |
278 command.ProcessHttpAnswer(emitter, receiver, answer); | |
279 } | |
280 | |
281 | |
282 void ThreadedOracle::Step() | |
283 { | |
284 std::auto_ptr<Orthanc::IDynamicObject> object(queue_.Dequeue(100)); | |
285 | |
286 if (object.get() != NULL) | |
287 { | |
288 Item& item = dynamic_cast<Item&>(*object); | |
289 | |
290 try | |
291 { | |
292 switch (item.GetCommand().GetType()) | |
293 { | |
294 case IOracleCommand::Type_Sleep: | |
295 { | |
296 SleepOracleCommand& command = dynamic_cast<SleepOracleCommand&>(item.GetCommand()); | |
297 | |
298 std::auto_ptr<SleepOracleCommand> copy(new SleepOracleCommand(command.GetDelay())); | |
299 | |
300 if (command.HasPayload()) | |
301 { | |
302 copy->SetPayload(command.ReleasePayload()); | |
303 } | |
304 | |
305 sleepingCommands_->Add(item.GetReceiver(), copy.release()); | |
306 | |
307 break; | |
308 } | |
309 | |
310 case IOracleCommand::Type_OrthancRestApi: | |
311 Execute(emitter_, orthanc_, item.GetReceiver(), | |
312 dynamic_cast<const OrthancRestApiCommand&>(item.GetCommand())); | |
313 break; | |
314 | |
315 case IOracleCommand::Type_GetOrthancImage: | |
316 Execute(emitter_, orthanc_, item.GetReceiver(), | |
317 dynamic_cast<const GetOrthancImageCommand&>(item.GetCommand())); | |
318 break; | |
319 | |
320 case IOracleCommand::Type_GetOrthancWebViewerJpeg: | |
321 Execute(emitter_, orthanc_, item.GetReceiver(), | |
322 dynamic_cast<const GetOrthancWebViewerJpegCommand&>(item.GetCommand())); | |
323 break; | |
324 | |
325 default: | |
326 throw Orthanc::OrthancException(Orthanc::ErrorCode_NotImplemented); | |
327 } | |
328 } | |
329 catch (Orthanc::OrthancException& e) | |
330 { | |
331 LOG(ERROR) << "Exception within the oracle: " << e.What(); | |
332 emitter_.EmitMessage(item.GetReceiver(), OracleCommandExceptionMessage(item.GetCommand(), e)); | |
333 } | |
334 catch (...) | |
335 { | |
336 LOG(ERROR) << "Threaded exception within the oracle"; | |
337 emitter_.EmitMessage(item.GetReceiver(), OracleCommandExceptionMessage | |
338 (item.GetCommand(), Orthanc::ErrorCode_InternalError)); | |
339 } | |
340 } | |
341 } | |
342 | |
343 | |
344 void ThreadedOracle::Worker(ThreadedOracle* that) | |
345 { | |
346 assert(that != NULL); | |
347 | |
348 for (;;) | |
349 { | |
350 { | |
351 boost::mutex::scoped_lock lock(that->mutex_); | |
352 if (that->state_ != State_Running) | |
353 { | |
354 return; | |
355 } | |
356 } | |
357 | |
358 that->Step(); | |
359 } | |
360 } | |
361 | |
362 | |
363 void ThreadedOracle::SleepingWorker(ThreadedOracle* that) | |
364 { | |
365 assert(that != NULL); | |
366 | |
367 for (;;) | |
368 { | |
369 { | |
370 boost::mutex::scoped_lock lock(that->mutex_); | |
371 if (that->state_ != State_Running) | |
372 { | |
373 return; | |
374 } | |
375 } | |
376 | |
377 that->sleepingCommands_->AwakeExpired(that->emitter_); | |
378 | |
379 boost::this_thread::sleep(boost::posix_time::milliseconds(that->sleepingTimeResolution_)); | |
380 } | |
381 } | |
382 | |
383 | |
384 void ThreadedOracle::StopInternal() | |
385 { | |
386 { | |
387 boost::mutex::scoped_lock lock(mutex_); | |
388 | |
389 if (state_ == State_Setup || | |
390 state_ == State_Stopped) | |
391 { | |
392 return; | |
393 } | |
394 else | |
395 { | |
396 state_ = State_Stopped; | |
397 } | |
398 } | |
399 | |
400 if (sleepingWorker_.joinable()) | |
401 { | |
402 sleepingWorker_.join(); | |
403 } | |
404 | |
405 for (size_t i = 0; i < workers_.size(); i++) | |
406 { | |
407 if (workers_[i] != NULL) | |
408 { | |
409 if (workers_[i]->joinable()) | |
410 { | |
411 workers_[i]->join(); | |
412 } | |
413 | |
414 delete workers_[i]; | |
415 } | |
416 } | |
417 } | |
418 | |
419 | |
420 ThreadedOracle::ThreadedOracle(IMessageEmitter& emitter) : | |
421 emitter_(emitter), | |
422 state_(State_Setup), | |
423 workers_(4), | |
424 sleepingCommands_(new SleepingCommands), | |
425 sleepingTimeResolution_(50) // By default, time resolution of 50ms | |
426 { | |
427 } | |
428 | |
429 | |
765 | 430 ThreadedOracle::~ThreadedOracle() |
431 { | |
432 if (state_ == State_Running) | |
433 { | |
434 LOG(ERROR) << "The threaded oracle is still running, explicit call to " | |
435 << "Stop() is mandatory to avoid crashes"; | |
436 } | |
437 | |
438 try | |
439 { | |
440 StopInternal(); | |
441 } | |
442 catch (Orthanc::OrthancException& e) | |
443 { | |
444 LOG(ERROR) << "Exception while stopping the threaded oracle: " << e.What(); | |
445 } | |
446 catch (...) | |
447 { | |
448 LOG(ERROR) << "Native exception while stopping the threaded oracle"; | |
449 } | |
450 } | |
451 | |
452 | |
748 | 453 void ThreadedOracle::SetOrthancParameters(const Orthanc::WebServiceParameters& orthanc) |
454 { | |
455 boost::mutex::scoped_lock lock(mutex_); | |
456 | |
457 if (state_ != State_Setup) | |
458 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
819
diff
changeset
|
459 LOG(ERROR) << "ThreadedOracle::SetOrthancParameters(): (state_ != State_Setup)"; |
748 | 460 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
461 } | |
462 else | |
463 { | |
464 orthanc_ = orthanc; | |
465 } | |
466 } | |
467 | |
468 | |
760
1181e1ad98ec
progressive loading working
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
748
diff
changeset
|
469 void ThreadedOracle::SetThreadsCount(unsigned int count) |
748 | 470 { |
471 boost::mutex::scoped_lock lock(mutex_); | |
472 | |
473 if (count <= 0) | |
474 { | |
475 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
476 } | |
477 else if (state_ != State_Setup) | |
478 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
819
diff
changeset
|
479 LOG(ERROR) << "ThreadedOracle::SetThreadsCount(): (state_ != State_Setup)"; |
748 | 480 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
481 } | |
482 else | |
483 { | |
484 workers_.resize(count); | |
485 } | |
486 } | |
487 | |
488 | |
489 void ThreadedOracle::SetSleepingTimeResolution(unsigned int milliseconds) | |
490 { | |
491 boost::mutex::scoped_lock lock(mutex_); | |
492 | |
493 if (milliseconds <= 0) | |
494 { | |
495 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
496 } | |
497 else if (state_ != State_Setup) | |
498 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
819
diff
changeset
|
499 LOG(ERROR) << "ThreadedOracle::SetSleepingTimeResolution(): (state_ != State_Setup)"; |
748 | 500 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
501 } | |
502 else | |
503 { | |
504 sleepingTimeResolution_ = milliseconds; | |
505 } | |
506 } | |
507 | |
508 | |
509 void ThreadedOracle::Start() | |
510 { | |
511 boost::mutex::scoped_lock lock(mutex_); | |
512 | |
513 if (state_ != State_Setup) | |
514 { | |
956
a7351ad54960
Made IsContextLost automatically set the flag by checking with the emscripten
Benjamin Golinvaux <bgo@osimis.io>
parents:
819
diff
changeset
|
515 LOG(ERROR) << "ThreadedOracle::Start(): (state_ != State_Setup)"; |
748 | 516 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); |
517 } | |
518 else | |
519 { | |
520 state_ = State_Running; | |
521 | |
522 for (unsigned int i = 0; i < workers_.size(); i++) | |
523 { | |
524 workers_[i] = new boost::thread(Worker, this); | |
525 } | |
526 | |
527 sleepingWorker_ = boost::thread(SleepingWorker, this); | |
528 } | |
529 } | |
530 | |
531 | |
532 void ThreadedOracle::Schedule(const IObserver& receiver, | |
533 IOracleCommand* command) | |
534 { | |
535 queue_.Enqueue(new Item(receiver, command)); | |
536 } | |
537 } |