Mercurial > hg > orthanc
annotate Core/JobsEngine/Operations/SequenceOfOperationsJob.cpp @ 2608:25225f0b4f33 jobs
simplification wrt. dicom connection manager
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Sat, 19 May 2018 15:50:09 +0200 |
parents | 988936118354 |
children | 2f3007bf0708 |
rev | line source |
---|---|
2603 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
5 * Copyright (C) 2017-2018 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 General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * In addition, as a special exception, the copyright holders of this | |
13 * program give permission to link the code of its release with the | |
14 * OpenSSL project's "OpenSSL" library (or with modified versions of it | |
15 * that use the same license as the "OpenSSL" library), and distribute | |
16 * the linked executables. You must obey the GNU General Public License | |
17 * in all respects for all of the code used other than "OpenSSL". If you | |
18 * modify file(s) with this exception, you may extend this exception to | |
19 * your version of the file(s), but you are not obligated to do so. If | |
20 * you do not wish to do so, delete this exception statement from your | |
21 * version. If you delete this exception statement from all source files | |
22 * in the program, then also delete it here. | |
23 * | |
24 * This program is distributed in the hope that it will be useful, but | |
25 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
27 * General Public License for more details. | |
28 * | |
29 * You should have received a copy of the GNU General Public License | |
30 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
31 **/ | |
32 | |
33 | |
34 #include "../../PrecompiledHeaders.h" | |
35 #include "SequenceOfOperationsJob.h" | |
36 | |
37 #include "../../Logging.h" | |
38 #include "../../OrthancException.h" | |
39 | |
40 namespace Orthanc | |
41 { | |
42 class SequenceOfOperationsJob::Operation : public boost::noncopyable | |
43 { | |
44 private: | |
45 JobOperationValues originalInputs_; | |
46 JobOperationValues workInputs_; | |
47 std::auto_ptr<IJobOperation> operation_; | |
48 std::list<Operation*> nextOperations_; | |
49 size_t currentInput_; | |
50 | |
51 public: | |
52 Operation(IJobOperation* operation) : | |
53 operation_(operation), | |
54 currentInput_(0) | |
55 { | |
56 if (operation == NULL) | |
57 { | |
58 throw OrthancException(ErrorCode_NullPointer); | |
59 } | |
60 } | |
61 | |
62 void AddOriginalInput(const JobOperationValue& value) | |
63 { | |
64 if (currentInput_ != 0) | |
65 { | |
66 // Cannot add input after processing has started | |
67 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
68 } | |
69 else | |
70 { | |
71 originalInputs_.Append(value.Clone()); | |
72 } | |
73 } | |
74 | |
75 const JobOperationValues& GetOriginalInputs() const | |
76 { | |
77 return originalInputs_; | |
78 } | |
79 | |
80 void Reset() | |
81 { | |
82 workInputs_.Clear(); | |
83 currentInput_ = 0; | |
84 } | |
85 | |
86 void AddNextOperation(Operation& other) | |
87 { | |
88 if (currentInput_ != 0) | |
89 { | |
90 // Cannot add input after processing has started | |
91 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
92 } | |
93 else | |
94 { | |
95 nextOperations_.push_back(&other); | |
96 } | |
97 } | |
98 | |
99 bool IsDone() const | |
100 { | |
101 return currentInput_ >= originalInputs_.GetSize() + workInputs_.GetSize(); | |
102 } | |
103 | |
2608
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
104 void Step(IDicomConnectionManager& connectionManager) |
2603 | 105 { |
106 if (IsDone()) | |
107 { | |
108 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
109 } | |
110 | |
111 const JobOperationValue* input; | |
112 | |
113 if (currentInput_ < originalInputs_.GetSize()) | |
114 { | |
115 input = &originalInputs_.GetValue(currentInput_); | |
116 } | |
117 else | |
118 { | |
119 input = &workInputs_.GetValue(currentInput_ - originalInputs_.GetSize()); | |
120 } | |
121 | |
122 JobOperationValues outputs; | |
2608
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
123 operation_->Apply(outputs, *input, connectionManager); |
2603 | 124 |
125 if (!nextOperations_.empty()) | |
126 { | |
127 std::list<Operation*>::iterator first = nextOperations_.begin(); | |
128 outputs.Move((*first)->workInputs_); | |
129 | |
130 std::list<Operation*>::iterator current = first; | |
131 ++current; | |
132 | |
133 while (current != nextOperations_.end()) | |
134 { | |
135 (*first)->workInputs_.Copy((*current)->workInputs_); | |
136 ++current; | |
137 } | |
138 } | |
139 | |
140 currentInput_ += 1; | |
141 } | |
142 }; | |
143 | |
144 | |
145 // Invoked from constructors | |
146 void SequenceOfOperationsJob::Setup() | |
147 { | |
148 done_ = false; | |
149 current_ = 0; | |
150 trailingTimeout_ = boost::posix_time::milliseconds(1000); | |
151 } | |
152 | |
153 | |
154 SequenceOfOperationsJob::~SequenceOfOperationsJob() | |
155 { | |
156 for (size_t i = 0; i < operations_.size(); i++) | |
157 { | |
158 if (operations_[i] != NULL) | |
159 { | |
160 delete operations_[i]; | |
161 } | |
162 } | |
163 } | |
164 | |
165 | |
166 void SequenceOfOperationsJob::Register(IObserver& observer) | |
167 { | |
168 boost::mutex::scoped_lock lock(mutex_); | |
169 observers_.push_back(&observer); | |
170 } | |
171 | |
172 | |
173 void SequenceOfOperationsJob::Lock::SetTrailingOperationTimeout(unsigned int timeout) | |
174 { | |
175 that_.trailingTimeout_ = boost::posix_time::milliseconds(timeout); | |
176 } | |
177 | |
2608
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
178 |
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
179 void SequenceOfOperationsJob::Lock::SetDicomConnectionTimeout(unsigned int timeout) |
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
180 { |
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
181 that_.connectionManager_.SetTimeout(timeout); |
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
182 } |
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
183 |
2603 | 184 |
185 size_t SequenceOfOperationsJob::Lock::AddOperation(IJobOperation* operation) | |
186 { | |
187 if (IsDone()) | |
188 { | |
189 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
190 } | |
191 | |
192 that_.operations_.push_back(new Operation(operation)); | |
193 that_.operationAdded_.notify_one(); | |
194 | |
195 return that_.operations_.size() - 1; | |
196 } | |
197 | |
198 | |
199 void SequenceOfOperationsJob::Lock::AddInput(size_t index, | |
200 const JobOperationValue& value) | |
201 { | |
202 if (IsDone()) | |
203 { | |
204 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
205 } | |
206 else if (index >= that_.operations_.size() || | |
207 index < that_.current_) | |
208 { | |
209 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
210 } | |
211 else | |
212 { | |
213 that_.operations_[index]->AddOriginalInput(value); | |
214 } | |
215 } | |
216 | |
217 | |
218 void SequenceOfOperationsJob::Lock::Connect(size_t input, | |
219 size_t output) | |
220 { | |
221 if (IsDone()) | |
222 { | |
223 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
224 } | |
225 else if (input >= output || | |
226 input >= that_.operations_.size() || | |
227 output >= that_.operations_.size() || | |
228 input < that_.current_ || | |
229 output < that_.current_) | |
230 { | |
231 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
232 } | |
233 else | |
234 { | |
235 Operation& a = *that_.operations_[input]; | |
236 Operation& b = *that_.operations_[output]; | |
237 a.AddNextOperation(b); | |
238 } | |
239 } | |
240 | |
241 | |
242 JobStepResult SequenceOfOperationsJob::ExecuteStep() | |
243 { | |
244 boost::mutex::scoped_lock lock(mutex_); | |
245 | |
246 if (current_ == operations_.size()) | |
247 { | |
248 LOG(INFO) << "Executing the trailing timeout in the sequence of operations"; | |
249 operationAdded_.timed_wait(lock, trailingTimeout_); | |
250 | |
251 if (current_ == operations_.size()) | |
252 { | |
253 // No operation was added during the trailing timeout: The | |
254 // job is over | |
255 LOG(INFO) << "The sequence of operations is over"; | |
256 done_ = true; | |
257 | |
258 for (std::list<IObserver*>::iterator it = observers_.begin(); | |
259 it != observers_.end(); ++it) | |
260 { | |
261 (*it)->SignalDone(*this); | |
262 } | |
263 | |
2608
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
264 connectionManager_.Close(); |
2603 | 265 return JobStepResult::Success(); |
266 } | |
267 else | |
268 { | |
269 LOG(INFO) << "New operation added to the sequence of operations"; | |
270 } | |
271 } | |
272 | |
273 assert(current_ < operations_.size()); | |
274 | |
275 while (current_ < operations_.size() && | |
276 operations_[current_]->IsDone()) | |
277 { | |
278 current_++; | |
279 } | |
280 | |
281 if (current_ < operations_.size()) | |
282 { | |
2608
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
283 operations_[current_]->Step(connectionManager_); |
2603 | 284 } |
285 | |
2608
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
286 connectionManager_.CheckTimeout(); |
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
287 |
2603 | 288 return JobStepResult::Continue(); |
289 } | |
290 | |
291 | |
292 void SequenceOfOperationsJob::SignalResubmit() | |
293 { | |
294 boost::mutex::scoped_lock lock(mutex_); | |
295 | |
296 current_ = 0; | |
297 done_ = false; | |
298 | |
299 for (size_t i = 0; i < operations_.size(); i++) | |
300 { | |
301 operations_[i]->Reset(); | |
302 } | |
303 } | |
304 | |
305 | |
2608
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
306 void SequenceOfOperationsJob::ReleaseResources() |
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
307 { |
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
308 boost::mutex::scoped_lock lock(mutex_); |
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
309 connectionManager_.Close(); |
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
310 } |
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
311 |
25225f0b4f33
simplification wrt. dicom connection manager
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2603
diff
changeset
|
312 |
2603 | 313 float SequenceOfOperationsJob::GetProgress() |
314 { | |
315 boost::mutex::scoped_lock lock(mutex_); | |
316 | |
317 return (static_cast<float>(current_) / | |
318 static_cast<float>(operations_.size() + 1)); | |
319 } | |
320 | |
321 | |
322 void SequenceOfOperationsJob::GetPublicContent(Json::Value& value) | |
323 { | |
324 boost::mutex::scoped_lock lock(mutex_); | |
325 | |
326 value["CountOperations"] = static_cast<unsigned int>(operations_.size()); | |
327 } | |
328 } |