From e74b42a84482136f6124852d55c00a9b0a3f8b42 Mon Sep 17 00:00:00 2001
From: Dinyar Rabady <dinyar.rabady@cern.ch>
Date: Sat, 24 Sep 2022 13:09:50 +0200
Subject: [PATCH] Quit more gracefully if configuration incomplete

Belongs to #15.
---
 scripts/run.sh |  4 ++++
 src/config.cc  | 10 ++++++++++
 src/config.h   |  8 +++++---
 src/scdaq.cc   |  4 ++++
 4 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/scripts/run.sh b/scripts/run.sh
index 7a2675d6..856889ab 100755
--- a/scripts/run.sh
+++ b/scripts/run.sh
@@ -15,6 +15,10 @@ while true
 do
     echo "Starting scdaq..."
     /opt/scdaq/bin/scdaq --config /etc/scdaq/scdaq.conf 2>&1 | logger --tag scdaq --id -p user.debug
+    if [[ $? == -1 ]]; then
+      echo "Unrecoverable error, won't retry."
+      exit 1
+    fi
     echo "Resetting the board..."
     ../scripts/reset-firmware.sh
     echo "Clearing caches..."
diff --git a/src/config.cc b/src/config.cc
index 5e3a1482..5edb19b3 100644
--- a/src/config.cc
+++ b/src/config.cc
@@ -45,5 +45,15 @@ void config::print()const {
   for (std::map<std::string,std::string>::const_iterator it = vmap.begin(); it!=vmap.end(); it++){
     LOG(INFO) << "key " << it->first << " value " << it->second;
   }
+}
 
+bool config::validate() const {
+  bool valid { true };
+  for (std::map<std::string,std::string>::const_iterator it = vmap.begin(); it!=vmap.end(); it++){
+    if (it->second.empty()) {
+      valid &= false;
+      LOG(ERROR) << "Configuration entry " << it->first << " has no value!";
+    }
+  }
+  return valid;
 }
diff --git a/src/config.h b/src/config.h
index e25680e5..1430efdf 100644
--- a/src/config.h
+++ b/src/config.h
@@ -10,13 +10,15 @@
 
 class config{
 public:
-  
+
   enum class InputType { WZDMA, DMA, FILEDMA, MICRONDMA, FILE };
 
   config(std::string filename);
 
   void print() const;
 
+  bool validate() const;
+
   InputType getInput() const {
     const std::string& input = vmap.at("input");
     if (input == "wzdma") {
@@ -144,9 +146,9 @@ public:
     return boost::lexical_cast<uint32_t>(v.c_str());
   }
 private:
-  
+
   std::map<std::string,std::string> vmap;
-  
+
 
 };
 #endif
diff --git a/src/scdaq.cc b/src/scdaq.cc
index 21f4c28a..def393a1 100644
--- a/src/scdaq.cc
+++ b/src/scdaq.cc
@@ -147,6 +147,10 @@ if(argc < 2){
   try {
     config conf(argv[2]);
     conf.print();
+    if (!conf.validate()) {
+      LOG(ERROR) << "Configuration invalid! Bailing out."
+      return -1;
+    }
     LOG(DEBUG) << "Configuration loaded";
     ctrl control;
     //    tbb::tick_count mainStartTime = tbb::tick_count::now();
-- 
GitLab