Mercurial > hg > orthanc-transfers
annotate Framework/HttpQueries/HttpQueriesQueue.cpp @ 78:0898578d5708 OrthancTransfers-1.3 tip
closing OrthancTransfers-1.3
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 30 May 2024 22:45:33 +0200 |
parents | f4e828607f02 |
children | 1e396fb509ca |
rev | line source |
---|---|
0 | 1 /** |
2 * Transfers accelerator plugin for Orthanc | |
33
44a0430d7899
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
20
diff
changeset
|
3 * Copyright (C) 2018-2021 Osimis S.A., Belgium |
0 | 4 * |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU Affero General Public License | |
7 * as published by the Free Software Foundation, either version 3 of | |
8 * the License, or (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Affero General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Affero General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 **/ | |
18 | |
19 | |
20 #include "HttpQueriesQueue.h" | |
21 | |
20 | 22 #include <Logging.h> |
23 #include <OrthancException.h> | |
0 | 24 |
25 namespace OrthancPlugins | |
26 { | |
27 HttpQueriesQueue::Status HttpQueriesQueue::GetStatusInternal() const | |
28 { | |
29 if (successQueries_ == queries_.size()) | |
30 { | |
31 return Status_Success; | |
32 } | |
33 else if (isFailure_) | |
34 { | |
35 return Status_Failure; | |
36 } | |
37 else | |
38 { | |
39 return Status_Running; | |
40 } | |
41 } | |
42 | |
43 | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
44 HttpQueriesQueue::HttpQueriesQueue() : |
0 | 45 maxRetries_(0) |
46 { | |
47 Reset(); | |
48 } | |
49 | |
50 | |
51 HttpQueriesQueue::~HttpQueriesQueue() | |
52 { | |
53 for (size_t i = 0; i < queries_.size(); i++) | |
54 { | |
55 assert(queries_[i] != NULL); | |
56 delete queries_[i]; | |
57 } | |
58 } | |
59 | |
60 | |
61 unsigned int HttpQueriesQueue::GetMaxRetries() | |
62 { | |
63 boost::mutex::scoped_lock lock(mutex_); | |
64 return maxRetries_; | |
65 } | |
66 | |
67 | |
68 void HttpQueriesQueue::SetMaxRetries(unsigned int maxRetries) | |
69 { | |
70 boost::mutex::scoped_lock lock(mutex_); | |
71 maxRetries_ = maxRetries; | |
72 } | |
73 | |
74 | |
75 void HttpQueriesQueue::Reserve(size_t size) | |
76 { | |
77 boost::mutex::scoped_lock lock(mutex_); | |
78 queries_.reserve(size); | |
79 } | |
80 | |
81 | |
82 void HttpQueriesQueue::Reset() | |
83 { | |
84 boost::mutex::scoped_lock lock(mutex_); | |
85 position_ = 0; | |
86 downloadedSize_ = 0; | |
87 uploadedSize_ = 0; | |
88 successQueries_ = 0; | |
89 isFailure_ = false; | |
90 } | |
91 | |
92 | |
93 void HttpQueriesQueue::Enqueue(IHttpQuery* query) | |
94 { | |
95 if (query == NULL) | |
96 { | |
97 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
98 } | |
99 else | |
100 { | |
101 boost::mutex::scoped_lock lock(mutex_); | |
102 queries_.push_back(query); | |
103 } | |
104 } | |
105 | |
106 | |
3 | 107 bool HttpQueriesQueue::ExecuteOneQuery(size_t& networkTraffic) |
0 | 108 { |
109 networkTraffic = 0; | |
110 | |
111 unsigned int maxRetries; | |
112 IHttpQuery* query = NULL; | |
113 | |
114 { | |
115 boost::mutex::scoped_lock lock(mutex_); | |
116 | |
117 maxRetries = maxRetries_; | |
118 | |
119 if (position_ == queries_.size() || | |
120 isFailure_) | |
121 { | |
122 return false; | |
123 } | |
124 else | |
125 { | |
126 query = queries_[position_]; | |
127 position_ ++; | |
128 } | |
129 } | |
130 | |
131 std::string body; | |
132 | |
133 if (query->GetMethod() == Orthanc::HttpMethod_Post || | |
134 query->GetMethod() == Orthanc::HttpMethod_Put) | |
135 { | |
136 query->ReadBody(body); | |
137 } | |
44
f4e828607f02
Added 'SenderTransferID' option that is added as an HTTP header in outgoing requests in PushMode
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
138 |
f4e828607f02
Added 'SenderTransferID' option that is added as an HTTP header in outgoing requests in PushMode
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
139 std::map<std::string, std::string> headers; |
f4e828607f02
Added 'SenderTransferID' option that is added as an HTTP header in outgoing requests in PushMode
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
140 query->GetHttpHeaders(headers); |
0 | 141 |
142 unsigned int retry = 0; | |
143 | |
144 for (;;) | |
145 { | |
8
4c3437217518
fix for compatibility with simplified OrthancPluginCppWrapper
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5
diff
changeset
|
146 MemoryBuffer answer; |
0 | 147 |
148 bool success; | |
149 | |
150 try | |
151 { | |
152 switch (query->GetMethod()) | |
153 { | |
154 case Orthanc::HttpMethod_Get: | |
44
f4e828607f02
Added 'SenderTransferID' option that is added as an HTTP header in outgoing requests in PushMode
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
155 success = peers_.DoGet(answer, query->GetPeer(), query->GetUri(), headers); |
0 | 156 break; |
157 | |
158 case Orthanc::HttpMethod_Post: | |
44
f4e828607f02
Added 'SenderTransferID' option that is added as an HTTP header in outgoing requests in PushMode
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
159 success = peers_.DoPost(answer, query->GetPeer(), query->GetUri(), body, headers); |
0 | 160 break; |
161 | |
162 case Orthanc::HttpMethod_Put: | |
44
f4e828607f02
Added 'SenderTransferID' option that is added as an HTTP header in outgoing requests in PushMode
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
163 success = peers_.DoPut(query->GetPeer(), query->GetUri(), body, headers); |
0 | 164 break; |
165 | |
166 case Orthanc::HttpMethod_Delete: | |
44
f4e828607f02
Added 'SenderTransferID' option that is added as an HTTP header in outgoing requests in PushMode
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
167 success = peers_.DoDelete(query->GetPeer(), query->GetUri(), headers); |
0 | 168 break; |
169 | |
170 default: | |
171 throw Orthanc::OrthancException(Orthanc::ErrorCode_ParameterOutOfRange); | |
172 } | |
173 } | |
174 catch (Orthanc::OrthancException& e) | |
175 { | |
176 LOG(ERROR) << "Unhandled exception during an HTTP query to peer \"" | |
44
f4e828607f02
Added 'SenderTransferID' option that is added as an HTTP header in outgoing requests in PushMode
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
177 << query->GetPeer() << " " << query->GetUri() + "\": " << e.What(); |
0 | 178 success = false; |
179 } | |
180 | |
181 if (success) | |
182 { | |
183 size_t downloaded = 0; | |
184 size_t uploaded = 0; | |
185 | |
186 if (query->GetMethod() == Orthanc::HttpMethod_Get || | |
187 query->GetMethod() == Orthanc::HttpMethod_Post) | |
188 { | |
189 query->HandleAnswer(answer.GetData(), answer.GetSize()); | |
190 downloaded = answer.GetSize(); | |
191 } | |
192 | |
193 if (query->GetMethod() == Orthanc::HttpMethod_Put || | |
194 query->GetMethod() == Orthanc::HttpMethod_Post) | |
195 { | |
196 uploaded = body.size(); | |
197 } | |
198 | |
199 networkTraffic = downloaded + uploaded; | |
200 | |
201 { | |
202 boost::mutex::scoped_lock lock(mutex_); | |
203 downloadedSize_ += downloaded; | |
204 uploadedSize_ += uploaded; | |
205 successQueries_ ++; | |
206 | |
207 if (successQueries_ == queries_.size()) | |
208 { | |
209 completed_.notify_all(); | |
210 } | |
211 | |
212 return true; | |
213 } | |
214 } | |
215 else | |
216 { | |
217 // Error: Let's retry | |
218 retry ++; | |
219 | |
10
c9e28e31262e
new option: MaxHttpRetries
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
9
diff
changeset
|
220 if (retry <= maxRetries) |
0 | 221 { |
222 // Wait 1 second before retrying | |
223 boost::this_thread::sleep(boost::posix_time::seconds(1)); | |
224 } | |
225 else | |
226 { | |
44
f4e828607f02
Added 'SenderTransferID' option that is added as an HTTP header in outgoing requests in PushMode
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
227 if (maxRetries > 0) |
f4e828607f02
Added 'SenderTransferID' option that is added as an HTTP header in outgoing requests in PushMode
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
228 { |
f4e828607f02
Added 'SenderTransferID' option that is added as an HTTP header in outgoing requests in PushMode
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
229 LOG(ERROR) << "Reached the maximum number of retries for a HTTP query to peer " << query->GetPeer() << " " << query->GetUri(); |
f4e828607f02
Added 'SenderTransferID' option that is added as an HTTP header in outgoing requests in PushMode
Alain Mazy <am@osimis.io>
parents:
33
diff
changeset
|
230 } |
0 | 231 |
232 { | |
233 boost::mutex::scoped_lock lock(mutex_); | |
234 isFailure_ = true; | |
235 completed_.notify_all(); | |
236 } | |
237 | |
238 return false; | |
239 } | |
240 } | |
241 } | |
242 } | |
243 | |
244 | |
245 HttpQueriesQueue::Status HttpQueriesQueue::WaitComplete(unsigned int timeoutMS) | |
246 { | |
247 boost::mutex::scoped_lock lock(mutex_); | |
248 | |
249 Status status = GetStatusInternal(); | |
250 | |
251 if (status == Status_Running) | |
252 { | |
253 completed_.timed_wait(lock, boost::posix_time::milliseconds(timeoutMS)); | |
254 return GetStatusInternal(); | |
255 } | |
256 else | |
257 { | |
258 return status; | |
259 } | |
260 } | |
261 | |
262 | |
263 void HttpQueriesQueue::WaitComplete() | |
264 { | |
265 boost::mutex::scoped_lock lock(mutex_); | |
266 | |
267 while (GetStatusInternal() == Status_Running) | |
268 { | |
269 completed_.timed_wait(lock, boost::posix_time::milliseconds(200)); | |
270 } | |
271 } | |
272 | |
273 | |
274 void HttpQueriesQueue::GetStatistics(size_t& scheduledQueriesCount, | |
275 size_t& successQueriesCount, | |
276 uint64_t& downloadedSize, | |
277 uint64_t& uploadedSize) | |
278 { | |
279 boost::mutex::scoped_lock lock(mutex_); | |
280 scheduledQueriesCount = queries_.size(); | |
281 successQueriesCount = successQueries_; | |
282 downloadedSize = downloadedSize_; | |
283 uploadedSize = uploadedSize_; | |
284 } | |
285 } |