Mercurial > hg > orthanc
changeset 2621:83ac5a05ce84 jobs
primitives for unserializing jobs
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 22 May 2018 17:37:16 +0200 |
parents | 1232922c8793 |
children | bd6e0b70e915 |
files | CMakeLists.txt Core/JobsEngine/GenericJobUnserializer.cpp Core/JobsEngine/GenericJobUnserializer.h Core/JobsEngine/IJobUnserializer.cpp Core/JobsEngine/IJobUnserializer.h Core/JobsEngine/Operations/JobOperationValues.cpp Core/JobsEngine/Operations/JobOperationValues.h Core/JobsEngine/Operations/SequenceOfOperationsJob.cpp OrthancServer/ServerJobs/OrthancJobUnserializer.cpp OrthancServer/ServerJobs/OrthancJobUnserializer.h Resources/CMake/OrthancFrameworkConfiguration.cmake UnitTestsSources/MultiThreadingTests.cpp |
diffstat | 12 files changed, 479 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/CMakeLists.txt Tue May 22 14:08:57 2018 +0200 +++ b/CMakeLists.txt Tue May 22 17:37:16 2018 +0200 @@ -86,6 +86,7 @@ OrthancServer/ServerJobs/DicomModalityStoreJob.cpp OrthancServer/ServerJobs/LuaJobManager.cpp OrthancServer/ServerJobs/ModifyInstanceOperation.cpp + OrthancServer/ServerJobs/OrthancJobUnserializer.cpp OrthancServer/ServerJobs/OrthancPeerStoreJob.cpp OrthancServer/ServerJobs/StorePeerOperation.cpp OrthancServer/ServerJobs/StoreScuOperation.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Core/JobsEngine/GenericJobUnserializer.cpp Tue May 22 17:37:16 2018 +0200 @@ -0,0 +1,90 @@ +/** + * 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 "../PrecompiledHeaders.h" +#include "GenericJobUnserializer.h" + +#include "../Logging.h" +#include "../OrthancException.h" + +#include "Operations/LogJobOperation.h" +#include "Operations/NullOperationValue.h" +#include "Operations/StringOperationValue.h" + +namespace Orthanc +{ + IJob* GenericJobUnserializer::UnserializeJob(const Json::Value& source) + { + const std::string type = GetString(source, "Type"); + + LOG(ERROR) << "Cannot unserialize job of type: " << type; + throw OrthancException(ErrorCode_BadFileFormat); + } + + + IJobOperation* GenericJobUnserializer::UnserializeOperation(const Json::Value& source) + { + const std::string type = GetString(source, "Type"); + + if (type == "Log") + { + return new LogJobOperation; + } + else + { + LOG(ERROR) << "Cannot unserialize operation of type: " << type; + throw OrthancException(ErrorCode_BadFileFormat); + } + } + + + JobOperationValue* GenericJobUnserializer::UnserializeValue(const Json::Value& source) + { + const std::string type = GetString(source, "Type"); + + if (type == "String") + { + return new StringOperationValue(GetString(source, "Content")); + } + else if (type == "Null") + { + return new NullOperationValue; + } + else + { + LOG(ERROR) << "Cannot unserialize value of type: " << type; + throw OrthancException(ErrorCode_BadFileFormat); + } + } +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Core/JobsEngine/GenericJobUnserializer.h Tue May 22 17:37:16 2018 +0200 @@ -0,0 +1,49 @@ +/** + * 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/>. + **/ + + +#pragma once + +#include "IJobUnserializer.h" + +namespace Orthanc +{ + class GenericJobUnserializer : public IJobUnserializer + { + public: + virtual IJob* UnserializeJob(const Json::Value& source); + + virtual IJobOperation* UnserializeOperation(const Json::Value& source); + + virtual JobOperationValue* UnserializeValue(const Json::Value& source); + }; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Core/JobsEngine/IJobUnserializer.cpp Tue May 22 17:37:16 2018 +0200 @@ -0,0 +1,103 @@ +/** + * 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 "../PrecompiledHeaders.h" +#include "IJobUnserializer.h" + +#include "../OrthancException.h" + +namespace Orthanc +{ + void IJobUnserializer::CheckType(const Json::Value& source, + const std::string& expectedType) + { + static const char* TYPE = "Type"; + + if (source.type() != Json::objectValue || + !source.isMember(TYPE) || + source[TYPE].type() != Json::stringValue || + source[TYPE].asString() != expectedType) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + } + + + std::string IJobUnserializer::GetString(const Json::Value& source, + const std::string& name) + { + if (source.type() != Json::objectValue || + !source.isMember(name.c_str()) || + source[name.c_str()].type() != Json::stringValue) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + else + { + return source[name.c_str()].asString(); + } + } + + + int IJobUnserializer::GetInteger(const Json::Value& source, + const std::string& name) + { + if (source.type() != Json::objectValue || + !source.isMember(name.c_str()) || + (source[name.c_str()].type() != Json::intValue && + source[name.c_str()].type() != Json::uintValue)) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + else + { + return source[name.c_str()].asInt(); + } + } + + + unsigned int IJobUnserializer::GetUnsignedInteger(const Json::Value& source, + const std::string& name) + { + int tmp = GetInteger(source, name); + + if (tmp < 0) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + else + { + return static_cast<unsigned int>(tmp); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Core/JobsEngine/IJobUnserializer.h Tue May 22 17:37:16 2018 +0200 @@ -0,0 +1,67 @@ +/** + * 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/>. + **/ + + +#pragma once + +#include "IJob.h" +#include "Operations/JobOperationValue.h" +#include "Operations/IJobOperation.h" + +namespace Orthanc +{ + class IJobUnserializer : public boost::noncopyable + { + public: + virtual ~IJobUnserializer() + { + } + + virtual IJob* UnserializeJob(const Json::Value& source) = 0; + + virtual IJobOperation* UnserializeOperation(const Json::Value& source) = 0; + + virtual JobOperationValue* UnserializeValue(const Json::Value& source) = 0; + + static void CheckType(const Json::Value& source, + const std::string& expectedType); + + static std::string GetString(const Json::Value& source, + const std::string& name); + + static int GetInteger(const Json::Value& source, + const std::string& name); + + static unsigned int GetUnsignedInteger(const Json::Value& source, + const std::string& name); + }; +}
--- a/Core/JobsEngine/Operations/JobOperationValues.cpp Tue May 22 14:08:57 2018 +0200 +++ b/Core/JobsEngine/Operations/JobOperationValues.cpp Tue May 22 17:37:16 2018 +0200 @@ -34,9 +34,11 @@ #include "../../PrecompiledHeaders.h" #include "JobOperationValues.h" +#include "../IJobUnserializer.h" #include "../../OrthancException.h" #include <cassert> +#include <memory> namespace Orthanc { @@ -117,4 +119,25 @@ target.append(tmp); } } + + + JobOperationValues* Unserialize(IJobUnserializer& unserializer, + const Json::Value& source) + { + if (source.type() != Json::arrayValue) + { + throw OrthancException(ErrorCode_BadFileFormat); + } + + std::auto_ptr<JobOperationValues> result(new JobOperationValues); + + result->Reserve(source.size()); + + for (Json::Value::ArrayIndex i = 0; i < source.size(); i++) + { + result->Append(unserializer.UnserializeValue(source[i])); + } + + return result.release(); + } }
--- a/Core/JobsEngine/Operations/JobOperationValues.h Tue May 22 14:08:57 2018 +0200 +++ b/Core/JobsEngine/Operations/JobOperationValues.h Tue May 22 17:37:16 2018 +0200 @@ -39,6 +39,8 @@ namespace Orthanc { + class IJobUnserializer; + class JobOperationValues : public boost::noncopyable { private: @@ -80,5 +82,8 @@ JobOperationValue& GetValue(size_t index) const; void Serialize(Json::Value& target) const; + + static JobOperationValues* Unserialize(IJobUnserializer& unserializer, + const Json::Value& source); }; }
--- a/Core/JobsEngine/Operations/SequenceOfOperationsJob.cpp Tue May 22 14:08:57 2018 +0200 +++ b/Core/JobsEngine/Operations/SequenceOfOperationsJob.cpp Tue May 22 17:37:16 2018 +0200 @@ -365,12 +365,17 @@ { boost::mutex::scoped_lock lock(mutex_); - value = Json::arrayValue; + Json::Value tmp = Json::arrayValue; for (size_t i = 0; i < operations_.size(); i++) { Json::Value operation = Json::objectValue; operations_[i]->Serialize(operation); - value.append(operation); + tmp.append(operation); } + + value["Operations"] = tmp; + value["TrailingTimeout"] = static_cast<unsigned int>(trailingTimeout_.total_milliseconds()); + value["DicomTimeout"] = connectionManager_.GetTimeout(); + value["Current"] = static_cast<unsigned int>(current_); } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancServer/ServerJobs/OrthancJobUnserializer.cpp Tue May 22 17:37:16 2018 +0200 @@ -0,0 +1,73 @@ +/** + * 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 "../PrecompiledHeadersServer.h" +#include "OrthancJobUnserializer.h" + +#include "../../Core/Logging.h" +#include "../../Core/OrthancException.h" + +#include "DicomInstanceOperationValue.h" + +namespace Orthanc +{ + IJob* OrthancJobUnserializer::UnserializeJob(const Json::Value& source) + { + const std::string type = GetString(source, "Type"); + + return GenericJobUnserializer::UnserializeJob(source); + } + + + IJobOperation* OrthancJobUnserializer::UnserializeOperation(const Json::Value& source) + { + const std::string type = GetString(source, "Type"); + + return GenericJobUnserializer::UnserializeOperation(source); + } + + + JobOperationValue* OrthancJobUnserializer::UnserializeValue(const Json::Value& source) + { + const std::string type = GetString(source, "Type"); + + if (type == "DicomInstance") + { + return new DicomInstanceOperationValue(context_, GetString(source, "ID")); + } + else + { + return GenericJobUnserializer::UnserializeValue(source); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancServer/ServerJobs/OrthancJobUnserializer.h Tue May 22 17:37:16 2018 +0200 @@ -0,0 +1,58 @@ +/** + * 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/>. + **/ + + +#pragma once + +#include "../ServerContext.h" +#include "../../Core/JobsEngine/GenericJobUnserializer.h" + +namespace Orthanc +{ + class OrthancJobUnserializer : public GenericJobUnserializer + { + private: + ServerContext& context_; + + public: + OrthancJobUnserializer(ServerContext& context) : + context_(context) + { + } + + virtual IJob* UnserializeJob(const Json::Value& source); + + virtual IJobOperation* UnserializeOperation(const Json::Value& source); + + virtual JobOperationValue* UnserializeValue(const Json::Value& source); + }; +}
--- a/Resources/CMake/OrthancFrameworkConfiguration.cmake Tue May 22 14:08:57 2018 +0200 +++ b/Resources/CMake/OrthancFrameworkConfiguration.cmake Tue May 22 17:37:16 2018 +0200 @@ -497,6 +497,8 @@ list(APPEND ORTHANC_CORE_SOURCES_INTERNAL ${ORTHANC_ROOT}/Core/Cache/SharedArchive.cpp ${ORTHANC_ROOT}/Core/FileStorage/FilesystemStorage.cpp + ${ORTHANC_ROOT}/Core/JobsEngine/GenericJobUnserializer.cpp + ${ORTHANC_ROOT}/Core/JobsEngine/IJobUnserializer.cpp ${ORTHANC_ROOT}/Core/JobsEngine/JobInfo.cpp ${ORTHANC_ROOT}/Core/JobsEngine/JobStatus.cpp ${ORTHANC_ROOT}/Core/JobsEngine/JobStepResult.cpp