Mercurial > hg > orthanc
comparison Core/JobsEngine/SetOfCommandsJob.cpp @ 2860:8b00e4cb4a6b
SetOfCommandsJob
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 05 Oct 2018 16:07:34 +0200 |
parents | |
children | 437e6ba20a5e |
comparison
equal
deleted
inserted
replaced
2859:dedc2befbf41 | 2860:8b00e4cb4a6b |
---|---|
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 "SetOfCommandsJob.h" | |
36 | |
37 #include "../Logging.h" | |
38 #include "../OrthancException.h" | |
39 #include "../SerializationToolbox.h" | |
40 | |
41 #include <cassert> | |
42 | |
43 namespace Orthanc | |
44 { | |
45 SetOfCommandsJob::SetOfCommandsJob() : | |
46 started_(false), | |
47 permissive_(false), | |
48 position_(0) | |
49 { | |
50 } | |
51 | |
52 | |
53 SetOfCommandsJob::~SetOfCommandsJob() | |
54 { | |
55 for (size_t i = 0; i < commands_.size(); i++) | |
56 { | |
57 assert(commands_[i] != NULL); | |
58 delete commands_[i]; | |
59 } | |
60 } | |
61 | |
62 | |
63 void SetOfCommandsJob::Reserve(size_t size) | |
64 { | |
65 if (started_) | |
66 { | |
67 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
68 } | |
69 else | |
70 { | |
71 commands_.reserve(size); | |
72 } | |
73 } | |
74 | |
75 | |
76 void SetOfCommandsJob::AddCommand(ICommand* command) | |
77 { | |
78 if (command == NULL) | |
79 { | |
80 throw OrthancException(ErrorCode_NullPointer); | |
81 } | |
82 else if (started_) | |
83 { | |
84 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
85 } | |
86 else | |
87 { | |
88 commands_.push_back(command); | |
89 } | |
90 } | |
91 | |
92 | |
93 void SetOfCommandsJob::SetPermissive(bool permissive) | |
94 { | |
95 if (started_) | |
96 { | |
97 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
98 } | |
99 else | |
100 { | |
101 permissive_ = permissive; | |
102 } | |
103 } | |
104 | |
105 | |
106 void SetOfCommandsJob::Reset() | |
107 { | |
108 if (started_) | |
109 { | |
110 position_ = 0; | |
111 } | |
112 else | |
113 { | |
114 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
115 } | |
116 } | |
117 | |
118 | |
119 float SetOfCommandsJob::GetProgress() | |
120 { | |
121 if (commands_.empty()) | |
122 { | |
123 return 1; | |
124 } | |
125 else | |
126 { | |
127 return (static_cast<float>(position_) / | |
128 static_cast<float>(commands_.size())); | |
129 } | |
130 } | |
131 | |
132 | |
133 const SetOfCommandsJob::ICommand& SetOfCommandsJob::GetCommand(size_t index) const | |
134 { | |
135 if (index >= commands_.size()) | |
136 { | |
137 throw OrthancException(ErrorCode_ParameterOutOfRange); | |
138 } | |
139 else | |
140 { | |
141 assert(commands_[index] != NULL); | |
142 return *commands_[index]; | |
143 } | |
144 } | |
145 | |
146 | |
147 JobStepResult SetOfCommandsJob::Step() | |
148 { | |
149 if (!started_) | |
150 { | |
151 throw OrthancException(ErrorCode_InternalError); | |
152 } | |
153 | |
154 if (commands_.empty() && | |
155 position_ == 0) | |
156 { | |
157 // No command to handle: We're done | |
158 position_ = 1; | |
159 return JobStepResult::Success(); | |
160 } | |
161 | |
162 if (position_ >= commands_.size()) | |
163 { | |
164 // Already done | |
165 throw OrthancException(ErrorCode_BadSequenceOfCalls); | |
166 } | |
167 | |
168 try | |
169 { | |
170 // Not at the trailing step: Handle the current command | |
171 if (!commands_[position_]->Execute()) | |
172 { | |
173 // Error | |
174 if (!permissive_) | |
175 { | |
176 return JobStepResult::Failure(ErrorCode_InternalError); | |
177 } | |
178 } | |
179 } | |
180 catch (OrthancException& e) | |
181 { | |
182 if (permissive_) | |
183 { | |
184 LOG(WARNING) << "Ignoring an error in a permissive job: " << e.What(); | |
185 } | |
186 else | |
187 { | |
188 return JobStepResult::Failure(e.GetErrorCode()); | |
189 } | |
190 } | |
191 | |
192 position_ += 1; | |
193 | |
194 if (position_ == commands_.size()) | |
195 { | |
196 // We're done | |
197 return JobStepResult::Success(); | |
198 } | |
199 else | |
200 { | |
201 return JobStepResult::Continue(); | |
202 } | |
203 } | |
204 | |
205 | |
206 | |
207 static const char* KEY_DESCRIPTION = "Description"; | |
208 static const char* KEY_PERMISSIVE = "Permissive"; | |
209 static const char* KEY_POSITION = "Position"; | |
210 static const char* KEY_TYPE = "Type"; | |
211 static const char* KEY_COMMANDS = "Commands"; | |
212 | |
213 | |
214 void SetOfCommandsJob::GetPublicContent(Json::Value& value) | |
215 { | |
216 value[KEY_DESCRIPTION] = GetDescription(); | |
217 } | |
218 | |
219 | |
220 bool SetOfCommandsJob::Serialize(Json::Value& target) | |
221 { | |
222 target = Json::objectValue; | |
223 | |
224 std::string type; | |
225 GetJobType(type); | |
226 target[KEY_TYPE] = type; | |
227 | |
228 target[KEY_PERMISSIVE] = permissive_; | |
229 target[KEY_POSITION] = static_cast<unsigned int>(position_); | |
230 target[KEY_DESCRIPTION] = description_; | |
231 | |
232 target[KEY_COMMANDS] = Json::arrayValue; | |
233 Json::Value& tmp = target[KEY_COMMANDS]; | |
234 | |
235 for (size_t i = 0; i < commands_.size(); i++) | |
236 { | |
237 assert(commands_[i] != NULL); | |
238 | |
239 Json::Value command; | |
240 commands_[i]->Serialize(command); | |
241 tmp.append(command); | |
242 } | |
243 | |
244 return true; | |
245 } | |
246 | |
247 | |
248 SetOfCommandsJob::SetOfCommandsJob(ICommandUnserializer* unserializer, | |
249 const Json::Value& source) : | |
250 started_(false) | |
251 { | |
252 std::auto_ptr<ICommandUnserializer> raii(unserializer); | |
253 | |
254 permissive_ = SerializationToolbox::ReadBoolean(source, KEY_PERMISSIVE); | |
255 position_ = SerializationToolbox::ReadUnsignedInteger(source, KEY_POSITION); | |
256 description_ = SerializationToolbox::ReadString(source, KEY_DESCRIPTION); | |
257 | |
258 if (!source.isMember(KEY_COMMANDS) || | |
259 source[KEY_COMMANDS].type() != Json::arrayValue) | |
260 { | |
261 throw OrthancException(ErrorCode_BadFileFormat); | |
262 } | |
263 else | |
264 { | |
265 const Json::Value& tmp = source[KEY_COMMANDS]; | |
266 commands_.resize(tmp.size()); | |
267 | |
268 for (Json::Value::ArrayIndex i = 0; i < tmp.size(); i++) | |
269 { | |
270 try | |
271 { | |
272 commands_[i] = unserializer->Unserialize(tmp[i]); | |
273 } | |
274 catch (OrthancException&) | |
275 { | |
276 } | |
277 | |
278 if (commands_[i] == NULL) | |
279 { | |
280 for (size_t j = 0; j < i; j++) | |
281 { | |
282 delete commands_[j]; | |
283 } | |
284 | |
285 throw OrthancException(ErrorCode_BadFileFormat); | |
286 } | |
287 } | |
288 } | |
289 | |
290 if (commands_.empty()) | |
291 { | |
292 if (position_ > 1) | |
293 { | |
294 throw OrthancException(ErrorCode_BadFileFormat); | |
295 } | |
296 } | |
297 else if (position_ > commands_.size()) | |
298 { | |
299 throw OrthancException(ErrorCode_BadFileFormat); | |
300 } | |
301 } | |
302 } |