From ce0b63f8f12079e1fe0a0ce95ecbba182ed22a11 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Mon, 30 Jul 2018 11:01:19 +0200 Subject: [PATCH] Add simple config file support to XSwiftBus This adds a simple configuration file to XSwiftbus. So far, only the xswiftbus DBus server is configured. ref T291 --- src/xswiftbus/config.cpp | 138 +++++++++++++++++++++++++++++++++++ src/xswiftbus/config.h | 68 +++++++++++++++++ src/xswiftbus/plugin.cpp | 16 +++- src/xswiftbus/plugin.h | 5 +- src/xswiftbus/xswiftbus.conf | 8 ++ src/xswiftbus/xswiftbus.pro | 8 +- 6 files changed, 238 insertions(+), 5 deletions(-) create mode 100644 src/xswiftbus/config.cpp create mode 100644 src/xswiftbus/config.h create mode 100644 src/xswiftbus/xswiftbus.conf diff --git a/src/xswiftbus/config.cpp b/src/xswiftbus/config.cpp new file mode 100644 index 000000000..1e58b4a31 --- /dev/null +++ b/src/xswiftbus/config.cpp @@ -0,0 +1,138 @@ +/* Copyright (C) 2018 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#include "config.h" +#include "utils.h" +#include +#include +#include +#include + +namespace XSwiftBus +{ + + //! Case insensitive string compare + bool stringCompare(const std::string & str1, const std::string &str2) + { + if (str1.size() != str2.size()) { return false; } + return std::equal(str1.begin(), str1.end(), str2.begin(), [](const char & c1, const char & c2) + { + return (c1 == c2 || std::toupper(c1) == std::toupper(c2)); + }); + } + + CConfig::CConfig() + {} + + CConfig::~CConfig() {} + + void CConfig::parse() + { + std::ifstream configFile(m_filePath); + if (! configFile.is_open()) { return; } + + std::string line; + int lineNo = 1; + + while (std::getline(configFile, line)) + { + line.erase(std::remove_if(line.begin(), line.end(), isspace), line.end()); + if (line.empty() || line[0] == '#') { continue; } + + auto delimiterPos = line.find("="); + if (delimiterPos == std::string::npos) + { + WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Skipping invalid line!"); + lineNo++; + continue; + } + + std::string key = line.substr(0, delimiterPos); + std::string value = line.substr(delimiterPos + 1); + + if (key.empty() || value.empty()) + { + WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Skipping invalid line!"); + lineNo++; + continue; + } + + bool valid = true; + if (stringCompare(key, "dbusMode")) { valid = parseDBusMode(value); } + else if (stringCompare(key, "dbusAddress")) { valid = parseDBusAddress(value); } + else if (stringCompare(key, "dbusPort")) { valid = parseDBusPort(value); } + else + { + WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Unknown variable " + value + "!"); + lineNo++; + continue; + } + + if (! valid) + { + WARNING_LOG("xswiftbus.conf line " + std::to_string(lineNo) + ": Skipping invalid line!"); + lineNo++; + continue; + } + + lineNo++; + } + } + + void CConfig::print() + { + DEBUG_LOG("XSwiftBus configuration:"); + DEBUG_LOG("DBus mode: " + dbusModeToString(m_dbusMode)); + DEBUG_LOG("DBus server address: " + m_dbusAddress); + DEBUG_LOG("DBus server port: " + std::to_string(m_dbusPort)); + } + + bool CConfig::parseDBusMode(const std::string &value) + { + if (stringCompare(value, "session")) { m_dbusMode = CConfig::DBusSession; return true; } + else if (stringCompare(value, "P2P")) { m_dbusMode = CConfig::DBusP2P; return true; } + else { return false; } + } + + bool CConfig::parseDBusAddress(const std::string &value) + { + + m_dbusAddress = value; + return true; + } + + bool CConfig::parseDBusPort(const std::string &value) + { + int port = 0; + try + { + port = std::stoi(value); + } + catch (...) + { + return false; + } + + if (port < 0 || port > 65535) { return false; } + m_dbusPort = port; + return true; + } + + std::string CConfig::dbusModeToString(DBusMode mode) + { + switch (mode) + { + case DBusSession: return "Session"; + case DBusP2P: return "P2P"; + } + + return {}; + } + +} // ns diff --git a/src/xswiftbus/config.h b/src/xswiftbus/config.h new file mode 100644 index 000000000..2b7ef28e7 --- /dev/null +++ b/src/xswiftbus/config.h @@ -0,0 +1,68 @@ +/* Copyright (C) 2018 + * swift Project Community / Contributors + * + * This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level + * directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project, + * including this file, may be copied, modified, propagated, or distributed except according to the terms + * contained in the LICENSE file. + */ + +#ifndef BLACKSIM_XSWIFTBUS_CONFIG_H +#define BLACKSIM_XSWIFTBUS_CONFIG_H + +#include + +namespace XSwiftBus +{ + /*! + * XSwiftBus configuration class + */ + class CConfig + { + public: + //! DBus Server Mode + enum DBusMode + { + DBusSession, + DBusP2P + }; + + //! Constructor. + CConfig(); + + //! Destructor; + ~CConfig(); + + //! Set config file path + void setFilePath(const std::string &filePath) { m_filePath = filePath; } + + //! Load and parse config file + void parse(); + + //! Print the current configuration to the X-Plane log + void print(); + + //! Get current DBus mode + DBusMode getDBusMode() const { return m_dbusMode; } + + //! Get current DBus server address + std::string getDBusAddress() const { return m_dbusAddress; } + + //! Get current DBus server port + int getDBusPort() const { return m_dbusPort; } + + private: + bool parseDBusMode(const std::string &value); + bool parseDBusAddress(const std::string &value); + bool parseDBusPort(const std::string &value); + + static std::string dbusModeToString(DBusMode mode); + + std::string m_filePath; + DBusMode m_dbusMode = DBusP2P; + std::string m_dbusAddress = "127.0.0.1"; + int m_dbusPort = 45001; + }; +} // ns + +#endif diff --git a/src/xswiftbus/plugin.cpp b/src/xswiftbus/plugin.cpp index f8c9fc504..3a8610031 100644 --- a/src/xswiftbus/plugin.cpp +++ b/src/xswiftbus/plugin.cpp @@ -36,6 +36,8 @@ namespace XSwiftBus m_planeViewSubMenu = m_menu.subMenu("Follow Plane View"); planeViewOwnAircraftMenuItem = m_planeViewSubMenu.item("Own Aircraft", [this] { switchToOwnAircraftView(); }); + readConfig(); + /*m_dbusThread = std::thread([this]() { while(!m_shouldStop) @@ -55,6 +57,15 @@ namespace XSwiftBus if (m_dbusThread.joinable()) { m_dbusThread.join(); } } + void CPlugin::readConfig() + { + initXPlanePath(); + auto configFilePath = g_xplanePath + "Resources" + g_sep + "plugins" + g_sep + "xswiftbus" + g_sep + "xswiftbus.conf"; + m_pluginConfig.setFilePath(configFilePath); + m_pluginConfig.parse(); + m_pluginConfig.print(); + } + void CPlugin::startServer(CDBusConnection::BusType bus) { (void) bus; @@ -67,12 +78,13 @@ namespace XSwiftBus m_traffic->setPlaneViewMenu(m_planeViewSubMenu); - if (m_useDBusP2P) + if (m_pluginConfig.getDBusMode() == CConfig::DBusP2P) { m_dbusP2PServer = std::make_unique(); // FIXME: make listen address configurable - m_dbusP2PServer->listen("tcp:host=127.0.0.1,port=45000"); + std::string listenAddress = "tcp:host=" + m_pluginConfig.getDBusAddress() + ",port=" + std::to_string(m_pluginConfig.getDBusPort()); + m_dbusP2PServer->listen(listenAddress); m_dbusP2PServer->setDispatcher(&m_dbusDispatcher); m_dbusP2PServer->setNewConnectionFunc([this](const std::shared_ptr &conn) diff --git a/src/xswiftbus/plugin.h b/src/xswiftbus/plugin.h index 9913a45a7..5c57bdee8 100644 --- a/src/xswiftbus/plugin.h +++ b/src/xswiftbus/plugin.h @@ -27,6 +27,7 @@ #include "datarefs.h" #include "XPLM/XPLMCamera.h" #include "menus.h" +#include "config.h" #include #include @@ -55,6 +56,7 @@ namespace XSwiftBus void onAircraftRepositioned(); private: + CConfig m_pluginConfig; CDBusDispatcher m_dbusDispatcher; std::unique_ptr m_dbusP2PServer; std::shared_ptr m_dbusConnection; @@ -74,8 +76,7 @@ namespace XSwiftBus std::thread m_dbusThread; bool m_shouldStop = false; - bool m_useDBusP2P = true; - + void readConfig(); void startServer(CDBusConnection::BusType bus); void switchToOwnAircraftView(); diff --git a/src/xswiftbus/xswiftbus.conf b/src/xswiftbus/xswiftbus.conf new file mode 100644 index 000000000..ca5753f21 --- /dev/null +++ b/src/xswiftbus/xswiftbus.conf @@ -0,0 +1,8 @@ +# DBus Mode - Options: p2p, session +dbusMode = p2p + +# DBus server address - relevant for P2P mode only +dbusAddress = 127.0.0.1 + +# DBus server port - relevant for P2P mode only +dbusPort = 45001 diff --git a/src/xswiftbus/xswiftbus.pro b/src/xswiftbus/xswiftbus.pro index 02aa0b95c..37c36ec02 100644 --- a/src/xswiftbus/xswiftbus.pro +++ b/src/xswiftbus/xswiftbus.pro @@ -10,7 +10,8 @@ INCLUDEPATH += $$EXTERNALSROOT/common/include/XPLM LIBS += -levent_core -ldbus-1 OTHER_FILES += \ - org.swift_project.xswiftbus.*.xml + org.swift_project.xswiftbus.*.xml \ + xswiftbus.conf win32 { equals(WORD_SIZE,64): LIBS += -lXPLM_64 -lXPWidgets_64 @@ -158,7 +159,12 @@ win32-g++ { dep_target.files *= $$[QT_INSTALL_BINS]/libstdc++-6.dll } +conf_target.path = $$PREFIX/xswiftbus +conf_target.files *= xswiftbus.conf + + INSTALLS += dep_target INSTALLS += legacy_data_target +INSTALLS += conf_target load(common_post)