From a1cb902da050ac1d4facf9f94d60e22eb98203c9 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Thu, 12 Mar 2020 21:32:38 +0100 Subject: [PATCH] Abort start in case configuration has errors --- SGSXLApp.cpp | 9 +++++++-- SGSXLConfig.cpp | 21 ++++++++++++++++----- SGSXLConfig.h | 4 ++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/SGSXLApp.cpp b/SGSXLApp.cpp index 11c32d8..92c666f 100644 --- a/SGSXLApp.cpp +++ b/SGSXLApp.cpp @@ -42,7 +42,7 @@ int main(int argc, char *argv[]) if ('-' == argv[1][0]) { printf("\nSmart Group Server Version %s (GitID #%.7s) Copyright (C) %s\n", VERSION.c_str(), gitversion, VENDOR_NAME.c_str()); - printf("Smart Group Server comes with ABSOLUTELY NO WARRANTY; see the LICENSE for details.\n"); + printf("Smart Group Server XL comes with ABSOLUTELY NO WARRANTY; see the LICENSE for details.\n"); printf("This is free software, and you are welcome to distribute it\nunder certain conditions that are discussed in the LICENSE file.\n\n"); return 0; } @@ -83,10 +83,15 @@ void CSGSXLApp::run() bool CSGSXLApp::createThread() { printf("\nSmart Group Server Version %s (GitID #%.7s) Copyright (C) %s\n", VERSION.c_str(), gitversion, VENDOR_NAME.c_str()); - printf("Smart Group Server comes with ABSOLUTELY NO WARRANTY; see the LICENSE for details.\n"); + printf("Smart Group Server XL comes with ABSOLUTELY NO WARRANTY; see the LICENSE for details.\n"); printf("This is free software, and you are welcome to distribute it\nunder certain conditions that are discussed in the LICENSE file.\n\n"); CSGSXLConfig config(m_configFile); + if(config.hasErrors()) { + printf("Configuration has one or more errors. Aborting.\n"); + return false; + } + m_thread = new CSGSXLThread(config.getLinkCount("XRF"), config.getLinkCount("DCS")); std::string CallSign, address; diff --git a/SGSXLConfig.cpp b/SGSXLConfig.cpp index 8b7cfb6..2802e36 100644 --- a/SGSXLConfig.cpp +++ b/SGSXLConfig.cpp @@ -27,8 +27,10 @@ CSGSXLConfig::CSGSXLConfig(const std::string &pathname) { + m_hasErrors = false; if (pathname.size() < 1) { printf("Configuration filename too short!\n"); + m_hasErrors = true; return; } @@ -38,17 +40,21 @@ CSGSXLConfig::CSGSXLConfig(const std::string &pathname) } catch(const FileIOException &fioex) { printf("Can't read %s\n", pathname.c_str()); + m_hasErrors = true; return; } catch(const ParseException &pex) { printf("Parse error at %s:%d - %s\n", pex.getFile(), pex.getLine(), pex.getError()); + m_hasErrors = true; return; } - if (! get_value(cfg, "gateway.callsign", m_callsign, 3, 8, "")) - return; - if (0 == m_callsign.size()) + if (! get_value(cfg, "gateway.callsign", m_callsign, 3, 8, "") + || 0 == m_callsign.size()) { + m_hasErrors = true; + printf("No Gateway callsign specified"); return; + } CUtils::ToUpper(m_callsign); get_value(cfg, "gateway.address", m_address, 0, 20, ""); printf("GATEWAY: callsign='%s' address='%s'\n", m_callsign.c_str(), m_address.c_str()); @@ -220,9 +226,9 @@ CSGSXLConfig::CSGSXLConfig(const std::string &pathname) get_value(cfg, "audio.directory", m_audioDirectory, 0, 2000, ""); m_audioDirectory = std::string(DATA_DIR) + "/" + m_audioDirectory; if(m_audioEnabled) { - printf("Audio enabled, auudio directory : %s", m_audioDirectory.c_str()); + printf("Audio enabled, auudio directory : %s\n", m_audioDirectory.c_str()); } else { - printf("Audio disabled"); + printf("Audio disabled\n"); } } @@ -258,6 +264,11 @@ unsigned int CSGSXLConfig::getLinkCount(const char *type) return count; } +bool CSGSXLConfig::hasErrors() +{ + return m_hasErrors; +} + bool CSGSXLConfig::get_value(const Config &cfg, const std::string &path, int &value, int min, int max, int default_value) { if (cfg.lookupValue(path, value)) { diff --git a/SGSXLConfig.h b/SGSXLConfig.h index 9146582..84fe4ae 100644 --- a/SGSXLConfig.h +++ b/SGSXLConfig.h @@ -62,6 +62,8 @@ public: unsigned int getLinkCount(const char *type); unsigned int getIrcDDBCount(); + bool hasErrors(); + private: bool get_value(const Config &cfg, const std::string &path, int &value, int min, int max, int default_value); bool get_value(const Config &cfg, const std::string &path, bool &value, bool default_value); @@ -79,4 +81,6 @@ private: bool m_audioEnabled; std::string m_audioDirectory; + + bool m_hasErrors; };