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