Mercurial > hg > orthanc
diff Plugins/Engine/PluginsJob.cpp @ 2884:497a637366b4 db-changes
integration mainline->db-changes
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 12 Oct 2018 15:18:10 +0200 |
parents | 925d8dc03a23 |
children | bfee0b9f3209 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Plugins/Engine/PluginsJob.cpp Fri Oct 12 15:18:10 2018 +0200 @@ -0,0 +1,189 @@ +/** + * Orthanc - A Lightweight, RESTful DICOM Store + * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics + * Department, University Hospital of Liege, Belgium + * Copyright (C) 2017-2018 Osimis S.A., Belgium + * + * This program is free software: you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * In addition, as a special exception, the copyright holders of this + * program give permission to link the code of its release with the + * OpenSSL project's "OpenSSL" library (or with modified versions of it + * that use the same license as the "OpenSSL" library), and distribute + * the linked executables. You must obey the GNU General Public License + * in all respects for all of the code used other than "OpenSSL". If you + * modify file(s) with this exception, you may extend this exception to + * your version of the file(s), but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from your + * version. If you delete this exception statement from all source files + * in the program, then also delete it here. + * + * 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 + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + **/ + + +#include "../../OrthancServer/PrecompiledHeadersServer.h" +#include "PluginsJob.h" + +#if ORTHANC_ENABLE_PLUGINS != 1 +#error The plugin support is disabled +#endif + + +#include "../../Core/Logging.h" +#include "../../Core/OrthancException.h" + +#include <json/reader.h> +#include <cassert> + +namespace Orthanc +{ + PluginsJob::PluginsJob(const _OrthancPluginCreateJob& parameters) : + parameters_(parameters) + { + if (parameters_.job == NULL) + { + throw OrthancException(ErrorCode_NullPointer); + } + + if (parameters_.target == NULL || + parameters_.finalize == NULL || + parameters_.type == NULL || + parameters_.getProgress == NULL || + parameters_.getContent == NULL || + parameters_.getSerialized == NULL || + parameters_.step == NULL || + parameters_.stop == NULL || + parameters_.reset == NULL) + { + parameters_.finalize(parameters.job); + throw OrthancException(ErrorCode_NullPointer); + } + + type_.assign(parameters.type); + } + + PluginsJob::~PluginsJob() + { + assert(parameters_.job != NULL); + parameters_.finalize(parameters_.job); + } + + JobStepResult PluginsJob::Step() + { + OrthancPluginJobStepStatus status = parameters_.step(parameters_.job); + + switch (status) + { + case OrthancPluginJobStepStatus_Success: + return JobStepResult::Success(); + + case OrthancPluginJobStepStatus_Failure: + return JobStepResult::Failure(ErrorCode_Plugin); + + case OrthancPluginJobStepStatus_Continue: + return JobStepResult::Continue(); + + default: + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + } + + void PluginsJob::Reset() + { + parameters_.reset(parameters_.job); + } + + void PluginsJob::Stop(JobStopReason reason) + { + switch (reason) + { + case JobStopReason_Success: + parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Success); + break; + + case JobStopReason_Failure: + parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Failure); + break; + + case JobStopReason_Canceled: + parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Canceled); + break; + + case JobStopReason_Paused: + parameters_.stop(parameters_.job, OrthancPluginJobStopReason_Paused); + break; + + default: + throw OrthancException(ErrorCode_ParameterOutOfRange); + } + } + + float PluginsJob::GetProgress() + { + return parameters_.getProgress(parameters_.job); + } + + void PluginsJob::GetPublicContent(Json::Value& value) + { + const char* content = parameters_.getContent(parameters_.job); + + if (content == NULL) + { + value = Json::objectValue; + } + else + { + Json::Reader reader; + + if (!reader.parse(content, value) || + value.type() != Json::objectValue) + { + LOG(ERROR) << "A job plugin must provide a JSON object as its public content"; + throw OrthancException(ErrorCode_Plugin); + } + } + } + + bool PluginsJob::Serialize(Json::Value& value) + { + const char* serialized = parameters_.getSerialized(parameters_.job); + + if (serialized == NULL) + { + return false; + } + else + { + Json::Reader reader; + + if (!reader.parse(serialized, value) || + value.type() != Json::objectValue) + { + LOG(ERROR) << "A job plugin must provide a JSON object as its serialized content"; + throw OrthancException(ErrorCode_Plugin); + } + + + static const char* KEY_TYPE = "Type"; + + if (value.isMember(KEY_TYPE)) + { + LOG(ERROR) << "The \"Type\" field is for reserved use for serialized job"; + throw OrthancException(ErrorCode_Plugin); + } + + value[KEY_TYPE] = type_; + return true; + } + } +}