Mercurial > hg > orthanc-transfers
annotate Framework/StatefulOrthancJob.cpp @ 72:d60d1a3a7357 OrthancTransfers-1.4
fix md5
author | Alain Mazy <am@osimis.io> |
---|---|
date | Tue, 26 Mar 2024 10:16:15 +0100 |
parents | 44a0430d7899 |
children | 1e396fb509ca |
rev | line source |
---|---|
0 | 1 /** |
2 * Transfers accelerator plugin for Orthanc | |
33
44a0430d7899
upgrade to year 2021
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
25
diff
changeset
|
3 * Copyright (C) 2018-2021 Osimis S.A., Belgium |
0 | 4 * |
5 * This program is free software: you can redistribute it and/or | |
6 * modify it under the terms of the GNU Affero General Public License | |
7 * as published by the Free Software Foundation, either version 3 of | |
8 * the License, or (at your option) any later version. | |
9 * | |
10 * This program is distributed in the hope that it will be useful, but | |
11 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 * Affero General Public License for more details. | |
14 * | |
15 * You should have received a copy of the GNU Affero General Public License | |
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
17 **/ | |
18 | |
19 | |
20 #include "StatefulOrthancJob.h" | |
21 | |
25
dfc43678aecb
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
19
diff
changeset
|
22 #include <Compatibility.h> // For std::unique_ptr |
dfc43678aecb
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
19
diff
changeset
|
23 |
0 | 24 namespace OrthancPlugins |
25 { | |
26 StatefulOrthancJob::JobInfo::JobInfo(StatefulOrthancJob& job) : | |
27 job_(job), | |
28 updated_(true), | |
29 content_(Json::objectValue) | |
30 { | |
31 } | |
32 | |
33 | |
34 void StatefulOrthancJob::JobInfo::SetContent(const std::string& key, | |
35 const Json::Value& value) | |
36 { | |
37 content_[key] = value; | |
38 updated_ = true; | |
39 } | |
40 | |
41 | |
42 void StatefulOrthancJob::JobInfo::Update() | |
43 { | |
44 if (updated_) | |
45 { | |
46 job_.UpdateContent(content_); | |
47 updated_ = false; | |
48 } | |
49 } | |
50 | |
51 | |
52 StatefulOrthancJob::StateUpdate::StateUpdate(IState* state) : | |
53 status_(OrthancPluginJobStepStatus_Continue), | |
54 state_(state) | |
55 { | |
56 if (state == NULL) | |
57 { | |
58 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
59 } | |
60 } | |
61 | |
62 | |
63 StatefulOrthancJob::IState* StatefulOrthancJob::StateUpdate::ReleaseOtherState() | |
64 { | |
65 if (state_.get() == NULL) | |
66 { | |
67 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
68 } | |
69 else | |
70 { | |
71 return state_.release(); | |
72 } | |
73 } | |
74 | |
75 | |
76 StatefulOrthancJob::StatefulOrthancJob(const std::string& jobType) : | |
77 OrthancJob(jobType), | |
78 info_(*this) | |
79 { | |
80 } | |
81 | |
82 | |
83 OrthancPluginJobStepStatus StatefulOrthancJob::Step() | |
84 { | |
25
dfc43678aecb
replacing deprecated std::auto_ptr by std::unique_ptr
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
19
diff
changeset
|
85 std::unique_ptr<StateUpdate> update; |
0 | 86 |
87 if (state_.get() == NULL) | |
88 { | |
89 update.reset(CreateInitialState(info_)); | |
90 } | |
91 else | |
92 { | |
93 update.reset(state_->Step()); | |
94 } | |
95 | |
96 info_.Update(); | |
97 | |
98 if (update.get() == NULL) | |
99 { | |
100 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); | |
101 } | |
102 | |
103 if (update->IsOtherState()) | |
104 { | |
105 state_.reset(update->ReleaseOtherState()); | |
106 assert(state_.get() != NULL); | |
107 | |
108 return OrthancPluginJobStepStatus_Continue; | |
109 } | |
110 else | |
111 { | |
112 OrthancPluginJobStepStatus status = update->GetStatus(); | |
113 | |
114 if (status == OrthancPluginJobStepStatus_Success) | |
115 { | |
116 info_.SetProgress(1); | |
117 } | |
118 | |
119 if (status == OrthancPluginJobStepStatus_Success || | |
120 status == OrthancPluginJobStepStatus_Failure) | |
121 { | |
122 state_.reset(); | |
123 } | |
124 | |
125 return status; | |
126 } | |
127 } | |
128 | |
129 | |
130 void StatefulOrthancJob::Stop(OrthancPluginJobStopReason reason) | |
131 { | |
132 if (state_.get() != NULL) | |
133 { | |
134 state_->Stop(reason); | |
135 | |
136 if (reason != OrthancPluginJobStopReason_Paused) | |
137 { | |
138 // Drop the current state, so as to force going back to the | |
139 // initial state on resubmit | |
140 state_.reset(); | |
141 } | |
142 } | |
143 } | |
144 | |
145 | |
146 void StatefulOrthancJob::Reset() | |
147 { | |
148 if (state_.get() != NULL) | |
149 { | |
150 // Error in the Orthanc core: Reset() should only be called | |
151 // from the "Failure" state, where no "IState*" is available | |
152 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); | |
153 } | |
154 } | |
155 } |