# HG changeset patch # User Sebastien Jodogne # Date 1526735426 -7200 # Node ID f2b9d3256060a553f919a148f8e8376eea8ee03c # Parent 1e11b0229e0452fa1f449dd234ea53d9fb9f9c51 SystemCallOperation diff -r 1e11b0229e04 -r f2b9d3256060 CMakeLists.txt --- a/CMakeLists.txt Fri May 18 18:06:53 2018 +0200 +++ b/CMakeLists.txt Sat May 19 15:10:26 2018 +0200 @@ -96,6 +96,7 @@ OrthancServer/ServerJobs/OrthancPeerStoreJob.cpp OrthancServer/ServerJobs/StorePeerOperation.cpp OrthancServer/ServerJobs/StoreScuOperation.cpp + OrthancServer/ServerJobs/SystemCallOperation.cpp OrthancServer/ServerToolbox.cpp OrthancServer/SliceOrdering.cpp ) diff -r 1e11b0229e04 -r f2b9d3256060 OrthancServer/ServerJobs/LuaJobManager.cpp --- a/OrthancServer/ServerJobs/LuaJobManager.cpp Fri May 18 18:06:53 2018 +0200 +++ b/OrthancServer/ServerJobs/LuaJobManager.cpp Sat May 19 15:10:26 2018 +0200 @@ -37,6 +37,7 @@ #include "DeleteResourceOperation.h" #include "StorePeerOperation.h" #include "StoreScuOperation.h" +#include "SystemCallOperation.h" #include "../../Core/JobsEngine/Operations/LogJobOperation.h" #include "DicomInstanceOperationValue.h" @@ -178,7 +179,8 @@ const RemoteModalityParameters& modality) { assert(jobLock_.get() != NULL); - return jobLock_->AddOperation(new StoreScuOperation(localAet, modality, that_.connectionManager_)); + return jobLock_->AddOperation + (new StoreScuOperation(localAet, modality, that_.connectionManager_)); } @@ -189,6 +191,24 @@ } + size_t LuaJobManager::Lock::AddSystemCallOperation(const std::string& command) + { + assert(jobLock_.get() != NULL); + return jobLock_->AddOperation(new SystemCallOperation(command)); + } + + + size_t LuaJobManager::Lock::AddSystemCallOperation + (const std::string& command, + const std::vector& preArguments, + const std::vector& postArguments) + { + assert(jobLock_.get() != NULL); + return jobLock_->AddOperation + (new SystemCallOperation(command, preArguments, postArguments)); + } + + void LuaJobManager::Lock::AddNullInput(size_t operation) { assert(jobLock_.get() != NULL); diff -r 1e11b0229e04 -r f2b9d3256060 OrthancServer/ServerJobs/LuaJobManager.h --- a/OrthancServer/ServerJobs/LuaJobManager.h Fri May 18 18:06:53 2018 +0200 +++ b/OrthancServer/ServerJobs/LuaJobManager.h Sat May 19 15:10:26 2018 +0200 @@ -93,6 +93,12 @@ size_t AddStorePeerOperation(const WebServiceParameters& peer); + size_t AddSystemCallOperation(const std::string& command); + + size_t AddSystemCallOperation(const std::string& command, + const std::vector& preArguments, + const std::vector& postArguments); + void AddNullInput(size_t operation); void AddStringInput(size_t operation, diff -r 1e11b0229e04 -r f2b9d3256060 OrthancServer/ServerJobs/StorePeerOperation.cpp --- a/OrthancServer/ServerJobs/StorePeerOperation.cpp Fri May 18 18:06:53 2018 +0200 +++ b/OrthancServer/ServerJobs/StorePeerOperation.cpp Sat May 19 15:10:26 2018 +0200 @@ -70,7 +70,7 @@ << peer_.GetUrl(); } - outputs.Append(instance.Clone()); + outputs.Append(input.Clone()); } catch (OrthancException& e) { diff -r 1e11b0229e04 -r f2b9d3256060 OrthancServer/ServerJobs/StoreScuOperation.cpp --- a/OrthancServer/ServerJobs/StoreScuOperation.cpp Fri May 18 18:06:53 2018 +0200 +++ b/OrthancServer/ServerJobs/StoreScuOperation.cpp Sat May 19 15:10:26 2018 +0200 @@ -58,7 +58,8 @@ throw OrthancException(ErrorCode_BadParameterType); } - const DicomInstanceOperationValue& instance = dynamic_cast(input); + const DicomInstanceOperationValue& instance = + dynamic_cast(input); LOG(INFO) << "Lua: Sending instance " << instance.GetId() << " to modality \"" << modality_.GetApplicationEntityTitle() << "\""; @@ -68,7 +69,7 @@ std::string dicom; instance.ReadContent(dicom); resource->GetConnection().Store(dicom); - outputs.Append(instance.Clone()); + outputs.Append(input.Clone()); } catch (OrthancException& e) { diff -r 1e11b0229e04 -r f2b9d3256060 OrthancServer/ServerJobs/SystemCallOperation.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancServer/ServerJobs/SystemCallOperation.cpp Sat May 19 15:10:26 2018 +0200 @@ -0,0 +1,114 @@ +/** + * 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 . + **/ + + +#include "../PrecompiledHeadersServer.h" +#include "SystemCallOperation.h" + +#include "DicomInstanceOperationValue.h" + +#include "../../Core/JobsEngine/Operations/StringOperationValue.h" +#include "../../Core/Logging.h" +#include "../../Core/TemporaryFile.h" +#include "../../Core/Toolbox.h" + +namespace Orthanc +{ + void SystemCallOperation::Apply(JobOperationValues& outputs, + const JobOperationValue& input) + { + std::vector arguments = preArguments_; + + arguments.reserve(arguments.size() + postArguments_.size() + 1); + + std::auto_ptr tmp; + + switch (input.GetType()) + { + case JobOperationValue::Type_DicomInstance: + { + const DicomInstanceOperationValue& instance = + dynamic_cast(input); + + std::string dicom; + instance.ReadContent(dicom); + + tmp.reset(new TemporaryFile); + tmp->Write(dicom); + + arguments.push_back(tmp->GetPath()); + break; + } + + case JobOperationValue::Type_String: + { + const StringOperationValue& value = + dynamic_cast(input); + + arguments.push_back(value.GetContent()); + break; + } + + case JobOperationValue::Type_Null: + break; + + default: + throw OrthancException(ErrorCode_BadParameterType); + } + + for (size_t i = 0; i < postArguments_.size(); i++) + { + arguments.push_back(postArguments_[i]); + } + + std::string info = command_; + for (size_t i = 0; i < arguments.size(); i++) + { + info += " " + arguments[i]; + } + + LOG(INFO) << "Lua: System call: " << info; + + try + { + SystemToolbox::ExecuteSystemCommand(command_, arguments); + + // Only chain with other commands if this operation succeeds + outputs.Append(input.Clone()); + } + catch (OrthancException& e) + { + LOG(ERROR) << "Lua: Failed system call - " << info << ": " << e.What(); + } + } +} + diff -r 1e11b0229e04 -r f2b9d3256060 OrthancServer/ServerJobs/SystemCallOperation.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancServer/ServerJobs/SystemCallOperation.h Sat May 19 15:10:26 2018 +0200 @@ -0,0 +1,78 @@ +/** + * 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 . + **/ + + +#pragma once + +#include "../../Core/JobsEngine/Operations/IJobOperation.h" + +#include + +namespace Orthanc +{ + class SystemCallOperation : public IJobOperation + { + private: + std::string command_; + std::vector preArguments_; + std::vector postArguments_; + + public: + SystemCallOperation(const std::string& command) : + command_(command) + { + } + + SystemCallOperation(const std::string& command, + const std::vector& preArguments, + const std::vector& postArguments) : + command_(command), + preArguments_(preArguments), + postArguments_(postArguments) + { + } + + void AddPreArgument(const std::string& argument) + { + preArguments_.push_back(argument); + } + + void AddPostArgument(const std::string& argument) + { + postArguments_.push_back(argument); + } + + virtual void Apply(JobOperationValues& outputs, + const JobOperationValue& input); + }; +} + diff -r 1e11b0229e04 -r f2b9d3256060 UnitTestsSources/MultiThreadingTests.cpp --- a/UnitTestsSources/MultiThreadingTests.cpp Fri May 18 18:06:53 2018 +0200 +++ b/UnitTestsSources/MultiThreadingTests.cpp Sat May 19 15:10:26 2018 +0200 @@ -799,9 +799,11 @@ LuaJobManager::Lock lock(lua, engine); size_t a = lock.AddLogOperation(); size_t b = lock.AddLogOperation(); + size_t c = lock.AddSystemCallOperation("echo"); lock.AddStringInput(a, boost::lexical_cast(i)); lock.AddNullInput(a); lock.Connect(a, b); + lock.Connect(a, c); } boost::this_thread::sleep(boost::posix_time::milliseconds(2000));