From 788ad8d3b6bd2d1d80550763fe899c06edd778c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Fr=C3=B6jdh?= Date: Tue, 28 May 2019 18:01:26 +0200 Subject: [PATCH] Command proxy (#25) * added cmd proxy * minor * minor * WIP * clean up * added comment --- .../include/multiSlsDetectorClient.h | 13 +++ .../src/sls_detector_client.cpp | 3 - slsSupportLib/include/CmdLineParser.h | 1 + slsSupportLib/include/CmdProxy.h | 100 ++++++++++++++++++ 4 files changed, 114 insertions(+), 3 deletions(-) create mode 100644 slsSupportLib/include/CmdProxy.h diff --git a/slsDetectorSoftware/include/multiSlsDetectorClient.h b/slsDetectorSoftware/include/multiSlsDetectorClient.h index fba5f71f6..7f0eb8352 100755 --- a/slsDetectorSoftware/include/multiSlsDetectorClient.h +++ b/slsDetectorSoftware/include/multiSlsDetectorClient.h @@ -3,6 +3,7 @@ #include #include "CmdLineParser.h" +#include "CmdProxy.h" #include "container_utils.h" #include "string_utils.h" #include "multiSlsDetector.h" @@ -88,6 +89,18 @@ class multiSlsDetectorClient { return; } + + //Call CmdProxy which execute the command if it exists, on success returns an empty string + //If the command is not in CmdProxy but deprecated the new command is returned + sls::CmdProxy proxy(detPtr); + auto cmd = proxy.Call(parser.command(), parser.arguments(), parser.detector_id()); + if (cmd.empty()) + return; + else + parser.setCommand(cmd); + + + // call multi detector command line slsDetectorCommand myCmd(detPtr); std::string answer = myCmd.executeLine(parser.n_arguments()+1, parser.argv().data(), action_, parser.detector_id()); diff --git a/slsDetectorSoftware/src/sls_detector_client.cpp b/slsDetectorSoftware/src/sls_detector_client.cpp index 875950978..b1153a7fc 100755 --- a/slsDetectorSoftware/src/sls_detector_client.cpp +++ b/slsDetectorSoftware/src/sls_detector_client.cpp @@ -26,8 +26,5 @@ int main(int argc, char *argv[]) { #ifdef HELP int action = slsDetectorDefs::HELP_ACTION; #endif - - // if (argc > 1) - // argv++; multiSlsDetectorClient(argc, argv, action); } diff --git a/slsSupportLib/include/CmdLineParser.h b/slsSupportLib/include/CmdLineParser.h index af11254d3..811d88740 100755 --- a/slsSupportLib/include/CmdLineParser.h +++ b/slsSupportLib/include/CmdLineParser.h @@ -14,6 +14,7 @@ class CmdLineParser { int detector_id() const { return detector_id_; }; int n_arguments() const { return arguments_.size(); } const std::string &command() const { return command_; } + void setCommand(std::string cmd){command_ = cmd;} const std::string &executable() const { return executable_; } const std::vector &arguments() const { return arguments_; }; std::vector argv() const; diff --git a/slsSupportLib/include/CmdProxy.h b/slsSupportLib/include/CmdProxy.h new file mode 100644 index 000000000..434a829b4 --- /dev/null +++ b/slsSupportLib/include/CmdProxy.h @@ -0,0 +1,100 @@ +#pragma once + +#include +#include +#include +#include +#include + +#include "logger.h" +#include "sls_detector_exceptions.h" + +namespace sls { + +template class CmdProxy { + public: + explicit CmdProxy(T *detectorPtr) : det(detectorPtr) {} + + std::string Call(const std::string &command, + const std::vector &arguments, + int detector_id) { + cmd = command; + args = arguments; + det_id = detector_id; + + ReplaceIfDepreciated(cmd); + + auto it = functions.find(cmd); + if (it != functions.end()) { + std::cout << ((*this).*(it->second))(); + return {}; + } else { + return cmd; + } + } + + bool ReplaceIfDepreciated(std::string &command) { + auto d_it = depreciated_functions.find(command); + if (d_it != depreciated_functions.end()) { + FILE_LOG(logWARNING) + << "WARNING: " << command + << " is depreciated and will be removed. Please migrate to: " + << d_it->second; + command = d_it->second; + return true; + } + return false; + } + + size_t GetFunctionMapSize() const noexcept { return functions.size(); }; + + private: + T *det; + std::string cmd; + std::vector args; + int det_id{-1}; + + using FunctionMap = std::map; + using StringMap = std::map; + + // Initialize maps for translating name and function + FunctionMap functions{{"newfunc", &CmdProxy::NewFunction}}; + + StringMap depreciated_functions{{"oldvrfcmd", "vrf"}, + {"veryveryold", "vcp"}, + {"anothercmd", "vrs"}, + {"this_as_well", "enablefwrite"}}; + + + template std::string ResultToString(const U &ret) { + std::ostringstream os; + if (det_id != -1) + os << det_id << ":"; + os << cmd << " " << ret << "\n"; + return os.str(); + } + + void WrongNumberOfParameters(size_t expected) { + throw RuntimeError("ERROR: Expected " + std::to_string(expected) + + " parameters but got " + std::to_string(args.size()) + + "\n"); + } + + // Mapped functions + + //example + std::string NewFunction() { + if(args.size() == 0){ + std::cout << "This is the new function function\n"; + return ResultToString(det->setExposureTime(-1, true)); + }else if(args.size() == 1){ + std::cout << "Setting exposure time to " << args[0] << "s\n"; + return ResultToString(det->setExposureTime(std::stod(args[0]), true, det_id)); + }else{ + WrongNumberOfParameters(1); + return {}; + } + } +}; + +} // namespace sls