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