From 7957578ae49349bb625a02f429ff9edac0a2743d Mon Sep 17 00:00:00 2001
From: "m.marn" <m.marn@gsi.de>
Date: Wed, 13 Sep 2023 07:55:01 +0000
Subject: [PATCH] Parse passed user parameters for a user provided path in
 which to search for the parameter files.

Closes #93

See merge request silecs/opensilecs!42
---
 .../interface/core/SilecsService.cpp          | 52 ++++++++++++-------
 .../interface/core/SilecsService.h            |  3 +-
 2 files changed, 34 insertions(+), 21 deletions(-)

diff --git a/silecs-communication-cpp/src/silecs-communication/interface/core/SilecsService.cpp b/silecs-communication-cpp/src/silecs-communication/interface/core/SilecsService.cpp
index 2c78900..7a714c8 100644
--- a/silecs-communication-cpp/src/silecs-communication/interface/core/SilecsService.cpp
+++ b/silecs-communication-cpp/src/silecs-communication/interface/core/SilecsService.cpp
@@ -43,9 +43,6 @@ const std::string Service::semverMinor_ = STRINGIFY(MINOR); //Minor release, bac
 const std::string Service::semverPatch_ = STRINGIFY(PATCH); //Bug fixes, backward compatible
 const std::string Service::developmentVersion_ = "DEV"; // Version which is used for unreleased code
 
-const std::string Service::paramFilesPath_ = "/dsc/data/silecs/" + Service::semverMajor_ + ".m.p/delivery/";
-const std::string Service::paramFilesPathFallback_ = "/acc/dsc/mcr/data/silecs/" + Service::semverMajor_ + ".m.p/delivery/";
-
 } // namespace
 
 /* **********************************************************************
@@ -56,6 +53,7 @@ namespace Silecs
 // static definition
 Service* Service::instance_ = NULL;
 bool Service::isShutingDown_ = false;
+std::string Service::userParamFilePath_;
 
 Service* Service::getInstance(int argc, char ** argv)
 {
@@ -101,6 +99,24 @@ void Service::setArguments(std::string usrArgs)
             throw SilecsException(__FILE__, __LINE__, PARAM_INCORRECT_ARGUMENTS);
         }
     }
+
+    std::string silecsParamStr = "-silecsParamPath";
+    pos = usrArgs.find(silecsParamStr);
+    if (pos != std::string::npos)
+    {
+        auto start = pos + silecsParamStr.size();
+        start = usrArgs.find_first_not_of(" ", start); // Move start by as many whitespaces as there are.
+        std::string temp = usrArgs.substr(start);
+        auto end = temp.find(" ");
+        if (end == std::string::npos)
+        {
+            userParamFilePath_ = temp;
+        }
+        else
+        {
+            userParamFilePath_ = temp.substr(0, end);
+        }
+    }
 }
 
 bool Service::setLogTopics(std::string topics)
@@ -229,8 +245,20 @@ const std::string Service::getBinaryFolderPath()
 
 const std::string Service::getParamFile(std::string controllerName)
 {
+    const std::string extension = ".silecsparam";
+
+    if (!userParamFilePath_.empty())
+    {
+        std::string userParamFile = userParamFilePath_ + "/" + controllerName + extension;
+        if (access(userParamFile.c_str(), F_OK) != -1)
+        {
+            LOG(DEBUG) << "Using param file: " << userParamFile;
+            return userParamFile;
+        }
+    }
+
     // directly next to the binary, used on GSI frontends
-    std::string localParameterFile1 = getBinaryFolderPath() + "/" + controllerName + ".silecsparam";
+    std::string localParameterFile1 = getBinaryFolderPath() + "/" + controllerName + extension;
 
     if (access(localParameterFile1.c_str(), F_OK) != -1)
     {
@@ -239,7 +267,7 @@ const std::string Service::getParamFile(std::string controllerName)
     }
 
     // if FESA-class is not deployed yet and started on development-system ( used on GSI development-systems )
-    std::string localParameterFile2 = getBinaryFolderPath() + "/../../../generated-silecs/client/" + controllerName + ".silecsparam";
+    std::string localParameterFile2 = getBinaryFolderPath() + "/../../../generated-silecs/client/" + controllerName + extension;
     //for debugging printf("localParameterFile2: %s",localParameterFile2.c_str());
     if (access(localParameterFile2.c_str(), F_OK) != -1)
     {
@@ -247,20 +275,6 @@ const std::string Service::getParamFile(std::string controllerName)
         return localParameterFile2;
     }
 
-    std::string globalParameterFile1 = paramFilesPath_ + controllerName + "/params/" + controllerName + ".silecsparam";
-    if (access(globalParameterFile1.c_str(), F_OK) != -1)
-    {
-        LOG(DEBUG) << "Using global param file: " << globalParameterFile1;
-        return globalParameterFile1;
-    }
-
-    std::string globalParameterFile2 = paramFilesPathFallback_ + controllerName + "/params/" + controllerName + ".silecsparam";
-    if (access(globalParameterFile2.c_str(), F_OK) != -1)
-    {
-        LOG(DEBUG) << "Using global fallback param file: " << globalParameterFile2;
-        return globalParameterFile2;
-    }
-
     std::string message = "No parameter-file found for controller '" + controllerName + "'";
     throw SilecsException(__FILE__, __LINE__, message.c_str());
 }
diff --git a/silecs-communication-cpp/src/silecs-communication/interface/core/SilecsService.h b/silecs-communication-cpp/src/silecs-communication/interface/core/SilecsService.h
index 0f106a9..eaa381e 100644
--- a/silecs-communication-cpp/src/silecs-communication/interface/core/SilecsService.h
+++ b/silecs-communication-cpp/src/silecs-communication/interface/core/SilecsService.h
@@ -193,8 +193,7 @@ private:
     static const std::string semverPatch_; //Bug fixes, backward compatible
     static const std::string developmentVersion_; // Version which is used if not released
 
-    static const std::string paramFilesPath_; // Location of all parameter-files (CERN-only)
-    static const std::string paramFilesPathFallback_; // Fallback-Location of all parameter-files (CERN-only)
+    static std::string userParamFilePath_; // User specific path, pointing to the folder in which parameter files are searched. Can be passed via application argument
 
     static bool isShutingDown_;
 };
-- 
GitLab