Mercurial > hg > orthanc-transfers
diff Framework/StatefulOrthancJob.cpp @ 0:95226b754d9e
initial release
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Mon, 17 Sep 2018 11:34:55 +0200 |
parents | |
children | 7e207ade2f1a |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Framework/StatefulOrthancJob.cpp Mon Sep 17 11:34:55 2018 +0200 @@ -0,0 +1,153 @@ +/** + * Transfers accelerator plugin for Orthanc + * Copyright (C) 2018 Osimis, Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU Affero General Public License + * as published by the Free Software Foundation, either version 3 of + * the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + **/ + + +#include "StatefulOrthancJob.h" + +namespace OrthancPlugins +{ + StatefulOrthancJob::JobInfo::JobInfo(StatefulOrthancJob& job) : + job_(job), + updated_(true), + content_(Json::objectValue) + { + } + + + void StatefulOrthancJob::JobInfo::SetContent(const std::string& key, + const Json::Value& value) + { + content_[key] = value; + updated_ = true; + } + + + void StatefulOrthancJob::JobInfo::Update() + { + if (updated_) + { + job_.UpdateContent(content_); + updated_ = false; + } + } + + + StatefulOrthancJob::StateUpdate::StateUpdate(IState* state) : + status_(OrthancPluginJobStepStatus_Continue), + state_(state) + { + if (state == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); + } + } + + + StatefulOrthancJob::IState* StatefulOrthancJob::StateUpdate::ReleaseOtherState() + { + if (state_.get() == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + else + { + return state_.release(); + } + } + + + StatefulOrthancJob::StatefulOrthancJob(const std::string& jobType) : + OrthancJob(jobType), + info_(*this) + { + } + + + OrthancPluginJobStepStatus StatefulOrthancJob::Step() + { + std::auto_ptr<StateUpdate> update; + + if (state_.get() == NULL) + { + update.reset(CreateInitialState(info_)); + } + else + { + update.reset(state_->Step()); + } + + info_.Update(); + + if (update.get() == NULL) + { + throw Orthanc::OrthancException(Orthanc::ErrorCode_NullPointer); + } + + if (update->IsOtherState()) + { + state_.reset(update->ReleaseOtherState()); + assert(state_.get() != NULL); + + return OrthancPluginJobStepStatus_Continue; + } + else + { + OrthancPluginJobStepStatus status = update->GetStatus(); + + if (status == OrthancPluginJobStepStatus_Success) + { + info_.SetProgress(1); + } + + if (status == OrthancPluginJobStepStatus_Success || + status == OrthancPluginJobStepStatus_Failure) + { + state_.reset(); + } + + return status; + } + } + + + void StatefulOrthancJob::Stop(OrthancPluginJobStopReason reason) + { + if (state_.get() != NULL) + { + state_->Stop(reason); + + if (reason != OrthancPluginJobStopReason_Paused) + { + // Drop the current state, so as to force going back to the + // initial state on resubmit + state_.reset(); + } + } + } + + + void StatefulOrthancJob::Reset() + { + if (state_.get() != NULL) + { + // Error in the Orthanc core: Reset() should only be called + // from the "Failure" state, where no "IState*" is available + throw Orthanc::OrthancException(Orthanc::ErrorCode_BadSequenceOfCalls); + } + } +}