mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-26 10:45:37 +08:00
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
This commit is contained in:
committed by
Klaus Basan
parent
9b1b39e3a4
commit
ce0b63f8f1
138
src/xswiftbus/config.cpp
Normal file
138
src/xswiftbus/config.cpp
Normal file
@@ -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 <fstream>
|
||||||
|
#include <string>
|
||||||
|
#include <cctype>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
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
|
||||||
68
src/xswiftbus/config.h
Normal file
68
src/xswiftbus/config.h
Normal file
@@ -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 <string>
|
||||||
|
|
||||||
|
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
|
||||||
@@ -36,6 +36,8 @@ namespace XSwiftBus
|
|||||||
m_planeViewSubMenu = m_menu.subMenu("Follow Plane View");
|
m_planeViewSubMenu = m_menu.subMenu("Follow Plane View");
|
||||||
planeViewOwnAircraftMenuItem = m_planeViewSubMenu.item("Own Aircraft", [this] { switchToOwnAircraftView(); });
|
planeViewOwnAircraftMenuItem = m_planeViewSubMenu.item("Own Aircraft", [this] { switchToOwnAircraftView(); });
|
||||||
|
|
||||||
|
readConfig();
|
||||||
|
|
||||||
/*m_dbusThread = std::thread([this]()
|
/*m_dbusThread = std::thread([this]()
|
||||||
{
|
{
|
||||||
while(!m_shouldStop)
|
while(!m_shouldStop)
|
||||||
@@ -55,6 +57,15 @@ namespace XSwiftBus
|
|||||||
if (m_dbusThread.joinable()) { m_dbusThread.join(); }
|
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 CPlugin::startServer(CDBusConnection::BusType bus)
|
||||||
{
|
{
|
||||||
(void) bus;
|
(void) bus;
|
||||||
@@ -67,12 +78,13 @@ namespace XSwiftBus
|
|||||||
|
|
||||||
m_traffic->setPlaneViewMenu(m_planeViewSubMenu);
|
m_traffic->setPlaneViewMenu(m_planeViewSubMenu);
|
||||||
|
|
||||||
if (m_useDBusP2P)
|
if (m_pluginConfig.getDBusMode() == CConfig::DBusP2P)
|
||||||
{
|
{
|
||||||
m_dbusP2PServer = std::make_unique<CDBusServer>();
|
m_dbusP2PServer = std::make_unique<CDBusServer>();
|
||||||
|
|
||||||
// FIXME: make listen address configurable
|
// 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->setDispatcher(&m_dbusDispatcher);
|
||||||
|
|
||||||
m_dbusP2PServer->setNewConnectionFunc([this](const std::shared_ptr<CDBusConnection> &conn)
|
m_dbusP2PServer->setNewConnectionFunc([this](const std::shared_ptr<CDBusConnection> &conn)
|
||||||
|
|||||||
@@ -27,6 +27,7 @@
|
|||||||
#include "datarefs.h"
|
#include "datarefs.h"
|
||||||
#include "XPLM/XPLMCamera.h"
|
#include "XPLM/XPLMCamera.h"
|
||||||
#include "menus.h"
|
#include "menus.h"
|
||||||
|
#include "config.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
@@ -55,6 +56,7 @@ namespace XSwiftBus
|
|||||||
void onAircraftRepositioned();
|
void onAircraftRepositioned();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
CConfig m_pluginConfig;
|
||||||
CDBusDispatcher m_dbusDispatcher;
|
CDBusDispatcher m_dbusDispatcher;
|
||||||
std::unique_ptr<CDBusServer> m_dbusP2PServer;
|
std::unique_ptr<CDBusServer> m_dbusP2PServer;
|
||||||
std::shared_ptr<CDBusConnection> m_dbusConnection;
|
std::shared_ptr<CDBusConnection> m_dbusConnection;
|
||||||
@@ -74,8 +76,7 @@ namespace XSwiftBus
|
|||||||
std::thread m_dbusThread;
|
std::thread m_dbusThread;
|
||||||
bool m_shouldStop = false;
|
bool m_shouldStop = false;
|
||||||
|
|
||||||
bool m_useDBusP2P = true;
|
void readConfig();
|
||||||
|
|
||||||
void startServer(CDBusConnection::BusType bus);
|
void startServer(CDBusConnection::BusType bus);
|
||||||
void switchToOwnAircraftView();
|
void switchToOwnAircraftView();
|
||||||
|
|
||||||
|
|||||||
8
src/xswiftbus/xswiftbus.conf
Normal file
8
src/xswiftbus/xswiftbus.conf
Normal file
@@ -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
|
||||||
@@ -10,7 +10,8 @@ INCLUDEPATH += $$EXTERNALSROOT/common/include/XPLM
|
|||||||
LIBS += -levent_core -ldbus-1
|
LIBS += -levent_core -ldbus-1
|
||||||
|
|
||||||
OTHER_FILES += \
|
OTHER_FILES += \
|
||||||
org.swift_project.xswiftbus.*.xml
|
org.swift_project.xswiftbus.*.xml \
|
||||||
|
xswiftbus.conf
|
||||||
|
|
||||||
win32 {
|
win32 {
|
||||||
equals(WORD_SIZE,64): LIBS += -lXPLM_64 -lXPWidgets_64
|
equals(WORD_SIZE,64): LIBS += -lXPLM_64 -lXPWidgets_64
|
||||||
@@ -158,7 +159,12 @@ win32-g++ {
|
|||||||
dep_target.files *= $$[QT_INSTALL_BINS]/libstdc++-6.dll
|
dep_target.files *= $$[QT_INSTALL_BINS]/libstdc++-6.dll
|
||||||
}
|
}
|
||||||
|
|
||||||
|
conf_target.path = $$PREFIX/xswiftbus
|
||||||
|
conf_target.files *= xswiftbus.conf
|
||||||
|
|
||||||
|
|
||||||
INSTALLS += dep_target
|
INSTALLS += dep_target
|
||||||
INSTALLS += legacy_data_target
|
INSTALLS += legacy_data_target
|
||||||
|
INSTALLS += conf_target
|
||||||
|
|
||||||
load(common_post)
|
load(common_post)
|
||||||
|
|||||||
Reference in New Issue
Block a user