[XSwiftBus] Allow to write an updated xswiftbus.conf file

In a distributed core/UI swift we need to write on XPlane side
This commit is contained in:
Klaus Basan
2020-03-18 23:15:43 +01:00
committed by Mat Sutcliffe
parent 80e97f749f
commit fd3ad207bd
6 changed files with 80 additions and 1 deletions

View File

@@ -14,6 +14,8 @@
#include <string>
#include <cctype>
#include <algorithm>
#include <chrono>
#include <iomanip>
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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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()

View File

@@ -50,5 +50,11 @@ namespace XSwiftBus
{
return m_provider->getConfig();
}
bool CSettingsAware::writeConfig(bool tcas, bool debug)
{
return m_provider->writeConfig(tcas, debug);
}
} // ns

View File

@@ -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;
};