0
|
1 /**
|
|
2 * Transfers accelerator plugin for Orthanc
|
9
|
3 * Copyright (C) 2018-2019 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
|
|
22 namespace OrthancPlugins
|
|
23 {
|
|
24 StatefulOrthancJob::JobInfo::JobInfo(StatefulOrthancJob& job) :
|
|
25 job_(job),
|
|
26 updated_(true),
|
|
27 content_(Json::objectValue)
|
|
28 {
|
|
29 }
|
|
30
|
|
31
|
|
32 void StatefulOrthancJob::JobInfo::SetContent(const std::string& key,
|
|
33 const Json::Value& value)
|
|
34 {
|
|
35 content_[key] = value;
|
|
36 updated_ = true;
|
|
37 }
|
|
38
|
|
39
|
|
40 void StatefulOrthancJob::JobInfo::Update()
|
|
41 {
|
|
42 if (updated_)
|
|
43 {
|
|
44 job_.UpdateContent(content_);
|
|
45 updated_ = false;
|
|
46 }
|
|
47 }
|
|
48
|
|
49
|
|
50 StatefulOrthancJob::StateUpdate::StateUpdate(IState* state) :
|
|
51 status_(OrthancPluginJobStepStatus_Continue),
|
|
52 state_(state)
|
|
53 {
|
|
54 if (state == NULL)
|
|
55 {
|
|
56 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
|
|
57 }
|
|
58 }
|
|
59
|
|
60
|
|
61 StatefulOrthancJob::IState* StatefulOrthancJob::StateUpdate::ReleaseOtherState()
|
|
62 {
|
|
63 if (state_.get() == NULL)
|
|
64 {
|
|
65 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
|
|
66 }
|
|
67 else
|
|
68 {
|
|
69 return state_.release();
|
|
70 }
|
|
71 }
|
|
72
|
|
73
|
|
74 StatefulOrthancJob::StatefulOrthancJob(const std::string& jobType) :
|
|
75 OrthancJob(jobType),
|
|
76 info_(*this)
|
|
77 {
|
|
78 }
|
|
79
|
|
80
|
|
81 OrthancPluginJobStepStatus StatefulOrthancJob::Step()
|
|
82 {
|
|
83 std::auto_ptr<StateUpdate> update;
|
|
84
|
|
85 if (state_.get() == NULL)
|
|
86 {
|
|
87 update.reset(CreateInitialState(info_));
|
|
88 }
|
|
89 else
|
|
90 {
|
|
91 update.reset(state_->Step());
|
|
92 }
|
|
93
|
|
94 info_.Update();
|
|
95
|
|
96 if (update.get() == NULL)
|
|
97 {
|
|
98 throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer);
|
|
99 }
|
|
100
|
|
101 if (update->IsOtherState())
|
|
102 {
|
|
103 state_.reset(update->ReleaseOtherState());
|
|
104 assert(state_.get() != NULL);
|
|
105
|
|
106 return OrthancPluginJobStepStatus_Continue;
|
|
107 }
|
|
108 else
|
|
109 {
|
|
110 OrthancPluginJobStepStatus status = update->GetStatus();
|
|
111
|
|
112 if (status == OrthancPluginJobStepStatus_Success)
|
|
113 {
|
|
114 info_.SetProgress(1);
|
|
115 }
|
|
116
|
|
117 if (status == OrthancPluginJobStepStatus_Success ||
|
|
118 status == OrthancPluginJobStepStatus_Failure)
|
|
119 {
|
|
120 state_.reset();
|
|
121 }
|
|
122
|
|
123 return status;
|
|
124 }
|
|
125 }
|
|
126
|
|
127
|
|
128 void StatefulOrthancJob::Stop(OrthancPluginJobStopReason reason)
|
|
129 {
|
|
130 if (state_.get() != NULL)
|
|
131 {
|
|
132 state_->Stop(reason);
|
|
133
|
|
134 if (reason != OrthancPluginJobStopReason_Paused)
|
|
135 {
|
|
136 // Drop the current state, so as to force going back to the
|
|
137 // initial state on resubmit
|
|
138 state_.reset();
|
|
139 }
|
|
140 }
|
|
141 }
|
|
142
|
|
143
|
|
144 void StatefulOrthancJob::Reset()
|
|
145 {
|
|
146 if (state_.get() != NULL)
|
|
147 {
|
|
148 // Error in the Orthanc core: Reset() should only be called
|
|
149 // from the "Failure" state, where no "IState*" is available
|
|
150 throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls);
|
|
151 }
|
|
152 }
|
|
153 }
|