Mercurial > hg > orthanc
annotate OrthancServer/Plugins/Engine/PluginsJob.cpp @ 4110:d99739cbe94c
make DicomInstanceHasher and ParsedDicomDir public classes
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 07 Jul 2020 10:03:59 +0200 |
parents | 05b8fd21089c |
children | 3af1d763763a |
rev | line source |
---|---|
2810 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics | |
4 * Department, University Hospital of Liege, Belgium | |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3240
diff
changeset
|
5 * Copyright (C) 2017-2020 Osimis S.A., Belgium |
2810 | 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 | |
4045 | 34 #include "../../Sources/PrecompiledHeadersServer.h" |
2810 | 35 #include "PluginsJob.h" |
36 | |
37 #if ORTHANC_ENABLE_PLUGINS != 1 | |
38 #error The plugin support is disabled | |
39 #endif | |
40 | |
41 | |
4045 | 42 #include "../../../OrthancFramework/Sources/Logging.h" |
43 #include "../../../OrthancFramework/Sources/OrthancException.h" | |
2810 | 44 |
45 #include <json/reader.h> | |
46 #include <cassert> | |
47 | |
48 namespace Orthanc | |
49 { | |
2815
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
50 PluginsJob::PluginsJob(const _OrthancPluginCreateJob& parameters) : |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
51 parameters_(parameters) |
2810 | 52 { |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
53 if (parameters_.job == NULL) |
2810 | 54 { |
55 throw OrthancException(ErrorCode_NullPointer); | |
56 } | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
57 |
2815
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
58 if (parameters_.target == NULL || |
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
59 parameters_.finalize == NULL || |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
60 parameters_.type == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
61 parameters_.getProgress == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
62 parameters_.getContent == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
63 parameters_.getSerialized == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
64 parameters_.step == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
65 parameters_.stop == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
66 parameters_.reset == NULL) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
67 { |
2815
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
68 parameters_.finalize(parameters.job); |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
69 throw OrthancException(ErrorCode_NullPointer); |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
70 } |
2810 | 71 |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
72 type_.assign(parameters.type); |
2810 | 73 } |
74 | |
75 PluginsJob::~PluginsJob() | |
76 { | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
77 assert(parameters_.job != NULL); |
2815
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
78 parameters_.finalize(parameters_.job); |
2810 | 79 } |
80 | |
3658
2d90dd30858c
providing job ID to the IJob::Step() methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
81 JobStepResult PluginsJob::Step(const std::string& jobId) |
2810 | 82 { |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
83 OrthancPluginJobStepStatus status = parameters_.step(parameters_.job); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
84 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
85 switch (status) |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
86 { |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
87 case OrthancPluginJobStepStatus_Success: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
88 return JobStepResult::Success(); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
89 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
90 case OrthancPluginJobStepStatus_Failure: |
3240
e44e0127e553
Fix issue #134 (/patient/modify gives 500, should really be 400)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
91 return JobStepResult::Failure(ErrorCode_Plugin, NULL); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
92 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
93 case OrthancPluginJobStepStatus_Continue: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
94 return JobStepResult::Continue(); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
95 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
96 default: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
97 throw OrthancException(ErrorCode_ParameterOutOfRange); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
98 } |
2810 | 99 } |
100 | |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
101 void PluginsJob::Reset() |
2810 | 102 { |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
103 parameters_.reset(parameters_.job); |
2810 | 104 } |
105 | |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
106 void PluginsJob::Stop(JobStopReason reason) |
2810 | 107 { |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
108 switch (reason) |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
109 { |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
110 case JobStopReason_Success: |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
111 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Success); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
112 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
113 |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
114 case JobStopReason_Failure: |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
115 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Failure); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
116 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
117 |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
118 case JobStopReason_Canceled: |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
119 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Canceled); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
120 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
121 |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
122 case JobStopReason_Paused: |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
123 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Paused); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
124 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
125 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
126 default: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
127 throw OrthancException(ErrorCode_ParameterOutOfRange); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
128 } |
2810 | 129 } |
130 | |
131 float PluginsJob::GetProgress() | |
132 { | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
133 return parameters_.getProgress(parameters_.job); |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
134 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
135 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
136 void PluginsJob::GetPublicContent(Json::Value& value) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
137 { |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
138 const char* content = parameters_.getContent(parameters_.job); |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
139 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
140 if (content == NULL) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
141 { |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
142 value = Json::objectValue; |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
143 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
144 else |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
145 { |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
146 Json::Reader reader; |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
147 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
148 if (!reader.parse(content, value) || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
149 value.type() != Json::objectValue) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
150 { |
2956 | 151 throw OrthancException(ErrorCode_Plugin, |
152 "A job plugin must provide a JSON object as its public content"); | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
153 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
154 } |
2810 | 155 } |
156 | |
157 bool PluginsJob::Serialize(Json::Value& value) | |
158 { | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
159 const char* serialized = parameters_.getSerialized(parameters_.job); |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
160 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
161 if (serialized == NULL) |
2810 | 162 { |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
163 return false; |
2810 | 164 } |
165 else | |
166 { | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
167 Json::Reader reader; |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
168 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
169 if (!reader.parse(serialized, value) || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
170 value.type() != Json::objectValue) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
171 { |
2956 | 172 throw OrthancException(ErrorCode_Plugin, |
173 "A job plugin must provide a JSON object as its serialized content"); | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
174 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
175 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
176 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
177 static const char* KEY_TYPE = "Type"; |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
178 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
179 if (value.isMember(KEY_TYPE)) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
180 { |
2956 | 181 throw OrthancException(ErrorCode_Plugin, |
182 "The \"Type\" field is for reserved use for serialized job"); | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
183 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
184 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
185 value[KEY_TYPE] = type_; |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
186 return true; |
2810 | 187 } |
188 } | |
189 } |