From fd3ad207bd717cfa330cd2c96ebf04f70c13e0f8 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 18 Mar 2020 23:15:43 +0100 Subject: [PATCH] [XSwiftBus] Allow to write an updated xswiftbus.conf file In a distributed core/UI swift we need to write on XPlane side --- src/xswiftbus/config.cpp | 52 ++++++++++++++++++++++++++++++++++++++ src/xswiftbus/config.h | 12 ++++++++- src/xswiftbus/plugin.h | 3 +++ src/xswiftbus/service.cpp | 2 ++ src/xswiftbus/settings.cpp | 6 +++++ src/xswiftbus/settings.h | 6 +++++ 6 files changed, 80 insertions(+), 1 deletion(-) diff --git a/src/xswiftbus/config.cpp b/src/xswiftbus/config.cpp index 30ebcb908..04d48ec20 100644 --- a/src/xswiftbus/config.cpp +++ b/src/xswiftbus/config.cpp @@ -14,6 +14,8 @@ #include #include #include +#include +#include using namespace BlackMisc::Simulation::XPlane::QtFreeUtils; @@ -79,6 +81,50 @@ namespace XSwiftBus DEBUG_LOG("DBus server port: " + std::to_string(m_dbusPort)); } + bool CConfig::writeConfig(bool tcas, bool debug) + { + setTcasEnabled(tcas); + setDebugMode(debug); + return writeConfigFile(); + } + + bool CConfig::writeConfigFile() const + { + std::ofstream configFile(m_filePath, std::ofstream::out | std::ofstream::trunc); + if (!configFile.is_open()) { return false; } + + // this code should be similar to CXSwiftBusConfigWriter + configFile << "# DBus Mode - Options: p2p, session" << std::endl; + configFile << "dbusMode = " << m_dbusMode << std::endl; + configFile << std::endl; + configFile << "# DBus server address - relevant for P2P mode only" << std::endl; + configFile << "dbusAddress = " << m_dbusAddress << std::endl; + configFile << std::endl; + configFile << "# DBus server port - relevant for P2P mode only" << std::endl; + configFile << "dbusPort = " << m_dbusPort << std::endl; + configFile << std::endl; + configFile << "# Render phase debugging - to help diagnose crashes" << std::endl; + configFile << "debug = " << boolToOnOff(m_debug) << std::endl; + configFile << std::endl; + configFile << "# TCAS traffic - to disable in case of crashes" << std::endl; + configFile << "tcas = " << boolToOnOff(m_tcas) << std::endl; + + // for info + const auto clockNow = std::chrono::system_clock::now(); + const time_t now = std::chrono::system_clock::to_time_t(clockNow); + struct tm tms; +#if defined (IBM) + localtime_s(&tms, &now); +#else + localtime_r(&now, &tms); +#endif + configFile << std::endl; + configFile << "# Updated by XSwiftBus plugin " << std::put_time(&tms, "%T"); + configFile << std::endl; + configFile.close(); + return true; + } + bool CConfig::parseDBusMode(const std::string &value) { if (stringCompareCaseInsensitive(value, "session")) { m_dbusMode = CConfig::DBusSession; return true; } @@ -137,4 +183,10 @@ namespace XSwiftBus } return {}; } + + std::string CConfig::boolToOnOff(bool on) + { + if (on) { return "on"; } + return "off"; + } } // ns diff --git a/src/xswiftbus/config.h b/src/xswiftbus/config.h index e5f4026dc..bc6272364 100644 --- a/src/xswiftbus/config.h +++ b/src/xswiftbus/config.h @@ -53,9 +53,18 @@ namespace XSwiftBus //! Get debug on/off bool getDebugMode() const { return m_debug; } - //! Get tcas traffic on/off + //! Set debug mode + void setDebugMode(bool on) { m_debug = on; } + + //! Get TCAS traffic on/off bool getTcasEnabled() const { return m_tcas; } + //! Set TCAS traffic on/off + void setTcasEnabled(bool on) { m_tcas = on; } + + //! Update and write config file + bool writeConfig(bool tcas, bool debug); + private: bool parseDBusMode (const std::string &value); bool parseDBusAddress(const std::string &value); @@ -65,6 +74,7 @@ namespace XSwiftBus bool writeConfigFile () const; static std::string dbusModeToString(DBusMode mode); + static std::string boolToOnOff(bool on); std::string m_filePath; DBusMode m_dbusMode = DBusP2P; diff --git a/src/xswiftbus/plugin.h b/src/xswiftbus/plugin.h index 223f40ca7..73b4c3aa2 100644 --- a/src/xswiftbus/plugin.h +++ b/src/xswiftbus/plugin.h @@ -65,6 +65,9 @@ namespace XSwiftBus //! \copydoc XSwiftBus::CSettingsProvider::getConfig virtual const CConfig &getConfig() const override { return m_pluginConfig; } + //! \copydoc XSwiftBus::CSettingsProvider::writeConfig + virtual bool writeConfig(bool tcas, bool debug) override { return m_pluginConfig.writeConfig(tcas, debug); } + private: CConfig m_pluginConfig; CDBusDispatcher m_dbusDispatcher; diff --git a/src/xswiftbus/service.cpp b/src/xswiftbus/service.cpp index 0873743d1..f2c1bd20e 100644 --- a/src/xswiftbus/service.cpp +++ b/src/xswiftbus/service.cpp @@ -258,8 +258,10 @@ namespace XSwiftBus CSettings s; s.parseXSwiftBusString(jsonString); this->setSettings(s); + const bool w = this->writeConfig(s.isTcasEnabled(), s.isLogRenderPhases()); this->updateMessageBoxFromSettings(); INFO_LOG("Received settings " + s.convertToString()); + if (w) { INFO_LOG("Written new config file"); } } void CService::readAirportsDatabase() diff --git a/src/xswiftbus/settings.cpp b/src/xswiftbus/settings.cpp index c4e1c65f9..be6a087e4 100644 --- a/src/xswiftbus/settings.cpp +++ b/src/xswiftbus/settings.cpp @@ -50,5 +50,11 @@ namespace XSwiftBus { return m_provider->getConfig(); } + + bool CSettingsAware::writeConfig(bool tcas, bool debug) + { + return m_provider->writeConfig(tcas, debug); + } + } // ns diff --git a/src/xswiftbus/settings.h b/src/xswiftbus/settings.h index d4fcbf19b..b9690cdc1 100644 --- a/src/xswiftbus/settings.h +++ b/src/xswiftbus/settings.h @@ -45,6 +45,9 @@ namespace XSwiftBus //! Get settings from xswiftbus.conf (needed during plugin initialization) virtual const CConfig &getConfig() const = 0; + //! Write a config file with these new values + virtual bool writeConfig(bool tcas, bool debug) = 0; + protected: //! Destructor ~CSettingsProvider() = default; @@ -73,6 +76,9 @@ namespace XSwiftBus //! \copydoc CSettingsProvider::getConfig const CConfig &getConfig() const; + //! \copydoc CSettingsProvider::writeConfig + bool writeConfig(bool tcas, bool debug); + private: CSettingsProvider *m_provider = nullptr; };