Mercurial > hg > orthanc
annotate OrthancServer/Plugins/Engine/PluginsJob.cpp @ 4983:63bf8fb86dae
removed cppcheck warning
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 25 Apr 2022 18:38:20 +0200 |
parents | 6eff25f70121 |
children | d00db9fb48fb |
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 | |
4870
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
5 * Copyright (C) 2017-2022 Osimis S.A., Belgium |
43e613a7756b
upgrade to year 2022
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4831
diff
changeset
|
6 * Copyright (C) 2021-2022 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
2810 | 7 * |
8 * This program is free software: you can redistribute it and/or | |
9 * modify it under the terms of the GNU General Public License as | |
10 * published by the Free Software Foundation, either version 3 of the | |
11 * License, or (at your option) any later version. | |
12 * | |
13 * This program is distributed in the hope that it will be useful, but | |
14 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 * General Public License for more details. | |
17 * | |
18 * You should have received a copy of the GNU General Public License | |
19 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 **/ | |
21 | |
22 | |
4045 | 23 #include "../../Sources/PrecompiledHeadersServer.h" |
2810 | 24 #include "PluginsJob.h" |
25 | |
26 #if ORTHANC_ENABLE_PLUGINS != 1 | |
27 #error The plugin support is disabled | |
28 #endif | |
29 | |
30 | |
4045 | 31 #include "../../../OrthancFramework/Sources/Logging.h" |
32 #include "../../../OrthancFramework/Sources/OrthancException.h" | |
4392
3af1d763763a
confining Json::Reader and Json::*Writer into Toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4045
diff
changeset
|
33 #include "../../../OrthancFramework/Sources/Toolbox.h" |
2810 | 34 |
35 #include <cassert> | |
36 | |
37 namespace Orthanc | |
38 { | |
2815
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
39 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
|
40 parameters_(parameters) |
2810 | 41 { |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
42 if (parameters_.job == NULL) |
2810 | 43 { |
44 throw OrthancException(ErrorCode_NullPointer); | |
45 } | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
46 |
2815
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
47 if (parameters_.target == NULL || |
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
48 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
|
49 parameters_.type == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
50 parameters_.getProgress == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
51 parameters_.getContent == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
52 parameters_.getSerialized == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
53 parameters_.step == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
54 parameters_.stop == NULL || |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
55 parameters_.reset == NULL) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
56 { |
2815
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
57 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
|
58 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
|
59 } |
2810 | 60 |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
61 type_.assign(parameters.type); |
2810 | 62 } |
63 | |
64 PluginsJob::~PluginsJob() | |
65 { | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
66 assert(parameters_.job != NULL); |
2815
925d8dc03a23
unserialization of jobs from plugins
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2814
diff
changeset
|
67 parameters_.finalize(parameters_.job); |
2810 | 68 } |
69 | |
3658
2d90dd30858c
providing job ID to the IJob::Step() methods
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
70 JobStepResult PluginsJob::Step(const std::string& jobId) |
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 OrthancPluginJobStepStatus status = parameters_.step(parameters_.job); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
73 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
74 switch (status) |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
75 { |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
76 case OrthancPluginJobStepStatus_Success: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
77 return JobStepResult::Success(); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
78 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
79 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
|
80 return JobStepResult::Failure(ErrorCode_Plugin, NULL); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
81 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
82 case OrthancPluginJobStepStatus_Continue: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
83 return JobStepResult::Continue(); |
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 default: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
86 throw OrthancException(ErrorCode_ParameterOutOfRange); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
87 } |
2810 | 88 } |
89 | |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
90 void PluginsJob::Reset() |
2810 | 91 { |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
92 parameters_.reset(parameters_.job); |
2810 | 93 } |
94 | |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
95 void PluginsJob::Stop(JobStopReason reason) |
2810 | 96 { |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
97 switch (reason) |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
98 { |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
99 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
|
100 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Success); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
101 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
102 |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
103 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
|
104 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Failure); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
105 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
106 |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
107 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
|
108 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Canceled); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
109 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
110 |
2812
ea7aea6f6a95
improved naming of methods in IJob
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2811
diff
changeset
|
111 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
|
112 parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Paused); |
2811
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
113 break; |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
114 |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
115 default: |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
116 throw OrthancException(ErrorCode_ParameterOutOfRange); |
7cfc8d266f41
reason for releasing resources in jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2810
diff
changeset
|
117 } |
2810 | 118 } |
119 | |
120 float PluginsJob::GetProgress() | |
121 { | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
122 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
|
123 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
124 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
125 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
|
126 { |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
127 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
|
128 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
129 if (content == NULL) |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
130 { |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
131 value = Json::objectValue; |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
132 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
133 else |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
134 { |
4392
3af1d763763a
confining Json::Reader and Json::*Writer into Toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4045
diff
changeset
|
135 if (!Toolbox::ReadJson(value, content) || |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
136 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
|
137 { |
2956 | 138 throw OrthancException(ErrorCode_Plugin, |
139 "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
|
140 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
141 } |
2810 | 142 } |
143 | |
144 bool PluginsJob::Serialize(Json::Value& value) | |
145 { | |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
146 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
|
147 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
148 if (serialized == NULL) |
2810 | 149 { |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
150 return false; |
2810 | 151 } |
152 else | |
153 { | |
4392
3af1d763763a
confining Json::Reader and Json::*Writer into Toolbox
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
4045
diff
changeset
|
154 if (!Toolbox::ReadJson(value, serialized) || |
2814
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
155 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
|
156 { |
2956 | 157 throw OrthancException(ErrorCode_Plugin, |
158 "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
|
159 } |
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 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
162 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
|
163 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
164 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
|
165 { |
2956 | 166 throw OrthancException(ErrorCode_Plugin, |
167 "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
|
168 } |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
169 |
7d1d3136f6cf
more generic handling of content and serialization in plugin jobs
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2812
diff
changeset
|
170 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
|
171 return true; |
2810 | 172 } |
173 } | |
174 } |