comparison OrthancServer/Plugins/Engine/PluginsJob.cpp @ 5138:d00db9fb48fb

added OrthancPluginCreateJob2() in the plugin SDK
author Sebastien Jodogne <s.jodogne@gmail.com>
date Thu, 19 Jan 2023 19:04:13 +0100
parents 6eff25f70121
children 0ea402b4d901
comparison
equal deleted inserted replaced
5137:15109c3f0f7d 5138:d00db9fb48fb
34 34
35 #include <cassert> 35 #include <cassert>
36 36
37 namespace Orthanc 37 namespace Orthanc
38 { 38 {
39 PluginsJob::PluginsJob(const _OrthancPluginCreateJob& parameters) : 39 void PluginsJob::Setup()
40 parameters_(parameters)
41 { 40 {
42 if (parameters_.job == NULL) 41 if (parameters_.job == NULL)
43 { 42 {
44 throw OrthancException(ErrorCode_NullPointer); 43 throw OrthancException(ErrorCode_NullPointer);
45 } 44 }
46 45
47 if (parameters_.target == NULL || 46 if (parameters_.target == NULL ||
48 parameters_.finalize == NULL || 47 parameters_.finalize == NULL ||
49 parameters_.type == NULL || 48 parameters_.type == NULL ||
50 parameters_.getProgress == NULL || 49 parameters_.getProgress == NULL ||
51 parameters_.getContent == NULL || 50 (parameters_.getContent == NULL && deprecatedGetContent_ == NULL) ||
52 parameters_.getSerialized == NULL || 51 (parameters_.getSerialized == NULL && deprecatedGetSerialized_ == NULL) ||
53 parameters_.step == NULL || 52 parameters_.step == NULL ||
54 parameters_.stop == NULL || 53 parameters_.stop == NULL ||
55 parameters_.reset == NULL) 54 parameters_.reset == NULL)
56 { 55 {
57 parameters_.finalize(parameters.job); 56 parameters_.finalize(parameters_.job);
58 throw OrthancException(ErrorCode_NullPointer); 57 throw OrthancException(ErrorCode_NullPointer);
59 } 58 }
60 59
61 type_.assign(parameters.type); 60 type_.assign(parameters_.type);
61 }
62
63 PluginsJob::PluginsJob(const _OrthancPluginCreateJob2& parameters) :
64 parameters_(parameters),
65 deprecatedGetContent_(NULL),
66 deprecatedGetSerialized_(NULL)
67 {
68 Setup();
69 }
70
71 PluginsJob::PluginsJob(const _OrthancPluginCreateJob& parameters)
72 {
73 LOG(WARNING) << "Your plugin is using the deprecated OrthancPluginCreateJob() function";
74
75 memset(&parameters_, 0, sizeof(parameters_));
76 parameters_.target = parameters.target;
77 parameters_.job = parameters.job;
78 parameters_.finalize = parameters.finalize;
79 parameters_.type = parameters.type;
80 parameters_.getProgress = parameters.getProgress;
81 parameters_.getContent = NULL;
82 parameters_.getSerialized = NULL;
83 parameters_.step = parameters.step;
84 parameters_.stop = parameters.stop;
85 parameters_.reset = parameters.reset;
86
87 deprecatedGetContent_ = parameters.getContent;
88 deprecatedGetSerialized_ = parameters.getSerialized;
89
90 Setup();
62 } 91 }
63 92
64 PluginsJob::~PluginsJob() 93 PluginsJob::~PluginsJob()
65 { 94 {
66 assert(parameters_.job != NULL); 95 assert(parameters_.job != NULL);
120 float PluginsJob::GetProgress() 149 float PluginsJob::GetProgress()
121 { 150 {
122 return parameters_.getProgress(parameters_.job); 151 return parameters_.getProgress(parameters_.job);
123 } 152 }
124 153
154
155 namespace
156 {
157 class MemoryBufferRaii : public boost::noncopyable
158 {
159 private:
160 OrthancPluginMemoryBuffer buffer_;
161
162 public:
163 MemoryBufferRaii()
164 {
165 buffer_.size = 0;
166 buffer_.data = NULL;
167 }
168
169 ~MemoryBufferRaii()
170 {
171 if (buffer_.size != 0)
172 {
173 free(buffer_.data);
174 }
175 }
176
177 OrthancPluginMemoryBuffer* GetObject()
178 {
179 return &buffer_;
180 }
181
182 void ToJsonObject(Json::Value& target) const
183 {
184 if ((buffer_.data == NULL && buffer_.size != 0) ||
185 (buffer_.data != NULL && buffer_.size == 0) ||
186 !Toolbox::ReadJson(target, buffer_.data, buffer_.size) ||
187 target.type() != Json::objectValue)
188 {
189 throw OrthancException(ErrorCode_Plugin,
190 "A job plugin must provide a JSON object as its public content and as its serialization");
191 }
192 }
193 };
194 }
195
125 void PluginsJob::GetPublicContent(Json::Value& value) 196 void PluginsJob::GetPublicContent(Json::Value& value)
126 { 197 {
127 const char* content = parameters_.getContent(parameters_.job); 198 if (parameters_.getContent != NULL)
128 199 {
129 if (content == NULL) 200 MemoryBufferRaii target;
130 { 201
131 value = Json::objectValue; 202 OrthancPluginErrorCode code = parameters_.getContent(target.GetObject(), parameters_.job);
203
204 if (code != OrthancPluginErrorCode_Success)
205 {
206 throw OrthancException(static_cast<ErrorCode>(code));
207 }
208 else
209 {
210 target.ToJsonObject(value);
211 }
132 } 212 }
133 else 213 else
134 { 214 {
135 if (!Toolbox::ReadJson(value, content) || 215 // This was the source code in Orthanc <= 1.11.2
136 value.type() != Json::objectValue) 216 const char* content = deprecatedGetContent_(parameters_.job);
137 { 217
138 throw OrthancException(ErrorCode_Plugin, 218 if (content == NULL)
139 "A job plugin must provide a JSON object as its public content"); 219 {
220 value = Json::objectValue;
221 }
222 else
223 {
224 if (!Toolbox::ReadJson(value, content) ||
225 value.type() != Json::objectValue)
226 {
227 throw OrthancException(ErrorCode_Plugin,
228 "A job plugin must provide a JSON object as its public content");
229 }
140 } 230 }
141 } 231 }
142 } 232 }
143 233
144 bool PluginsJob::Serialize(Json::Value& value) 234 bool PluginsJob::Serialize(Json::Value& value)
145 { 235 {
146 const char* serialized = parameters_.getSerialized(parameters_.job); 236 if (parameters_.getSerialized != NULL)
147 237 {
148 if (serialized == NULL) 238 MemoryBufferRaii target;
149 { 239
150 return false; 240 int32_t code = parameters_.getContent(target.GetObject(), parameters_.job);
241
242 if (code < 0)
243 {
244 throw OrthancException(ErrorCode_Plugin, "Error during the serialization of a job");
245 }
246 else if (code == 0)
247 {
248 return false; // Serialization is not implemented
249 }
250 else
251 {
252 target.ToJsonObject(value);
253
254 static const char* KEY_TYPE = "Type";
255
256 if (value.isMember(KEY_TYPE))
257 {
258 throw OrthancException(ErrorCode_Plugin,
259 "The \"Type\" field is for reserved use for serialized job");
260 }
261
262 value[KEY_TYPE] = type_;
263 return true;
264 }
151 } 265 }
152 else 266 else
153 { 267 {
154 if (!Toolbox::ReadJson(value, serialized) || 268 // This was the source code in Orthanc <= 1.11.2
155 value.type() != Json::objectValue) 269 const char* serialized = deprecatedGetSerialized_(parameters_.job);
156 { 270
157 throw OrthancException(ErrorCode_Plugin, 271 if (serialized == NULL)
158 "A job plugin must provide a JSON object as its serialized content"); 272 {
159 } 273 return false;
160 274 }
161 275 else
162 static const char* KEY_TYPE = "Type"; 276 {
277 if (!Toolbox::ReadJson(value, serialized) ||
278 value.type() != Json::objectValue)
279 {
280 throw OrthancException(ErrorCode_Plugin,
281 "A job plugin must provide a JSON object as its serialized content");
282 }
283
284
285 static const char* KEY_TYPE = "Type";
163 286
164 if (value.isMember(KEY_TYPE)) 287 if (value.isMember(KEY_TYPE))
165 { 288 {
166 throw OrthancException(ErrorCode_Plugin, 289 throw OrthancException(ErrorCode_Plugin,
167 "The \"Type\" field is for reserved use for serialized job"); 290 "The \"Type\" field is for reserved use for serialized job");
168 } 291 }
169 292
170 value[KEY_TYPE] = type_; 293 value[KEY_TYPE] = type_;
171 return true; 294 return true;
295 }
172 } 296 }
173 } 297 }
174 } 298 }