mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-24 07:55:35 +08:00
Delay setting the DBusConnection for a DBusObject
Previously, the DBusconnection had to be set at construction time and could not be changed over the DBusObject life time. Now a new connection can be set anytime and as often as required. The shared ownership is now also properly implemented by shared pointers. ref T291
This commit is contained in:
committed by
Klaus Basan
parent
01085f24b3
commit
59da68da5e
@@ -8,17 +8,23 @@
|
||||
*/
|
||||
|
||||
#include "dbusobject.h"
|
||||
#include <cassert>
|
||||
|
||||
namespace XSwiftBus
|
||||
{
|
||||
CDBusObject::CDBusObject(CDBusConnection *dbusConnection)
|
||||
: m_dbusConnection(dbusConnection)
|
||||
CDBusObject::CDBusObject()
|
||||
{ }
|
||||
|
||||
CDBusObject::~CDBusObject() = default;
|
||||
|
||||
void CDBusObject::setDBusConnection(const std::shared_ptr<CDBusConnection> &dbusConnection)
|
||||
{
|
||||
m_dbusConnection = dbusConnection;
|
||||
}
|
||||
|
||||
void CDBusObject::registerDBusObjectPath(const std::string &interfaceName, const std::string &objectPath)
|
||||
{
|
||||
assert(m_dbusConnection);
|
||||
m_interfaceName = interfaceName;
|
||||
m_objectPath = objectPath;
|
||||
m_dbusConnection->registerObjectPath(this, interfaceName, objectPath, m_dbusObjectPathVTable);
|
||||
@@ -26,12 +32,14 @@ namespace XSwiftBus
|
||||
|
||||
void CDBusObject::sendDBusSignal(const std::string &name)
|
||||
{
|
||||
if (! m_dbusConnection) { return; }
|
||||
CDBusMessage signal = CDBusMessage::createSignal(m_objectPath, m_interfaceName, name);
|
||||
m_dbusConnection->sendMessage(signal);
|
||||
}
|
||||
|
||||
void CDBusObject::sendDBusMessage(const CDBusMessage &message)
|
||||
{
|
||||
if (! m_dbusConnection) { return; }
|
||||
m_dbusConnection->sendMessage(message);
|
||||
}
|
||||
|
||||
@@ -71,6 +79,7 @@ namespace XSwiftBus
|
||||
(void)connection; // unused
|
||||
|
||||
auto *obj = static_cast<CDBusObject *>(data);
|
||||
|
||||
DBusError err;
|
||||
dbus_error_init(&err);
|
||||
|
||||
|
||||
@@ -22,11 +22,19 @@ namespace XSwiftBus
|
||||
{
|
||||
public:
|
||||
//! Constructor
|
||||
CDBusObject(CDBusConnection *dbusConnection);
|
||||
CDBusObject();
|
||||
|
||||
//! Destructor
|
||||
virtual ~CDBusObject();
|
||||
|
||||
//! Set the assigned DBus connection.
|
||||
//! \remark Currently one object can only manage one connection at a time
|
||||
void setDBusConnection(const std::shared_ptr<CDBusConnection> &dbusConnection);
|
||||
|
||||
//! Register itself with interfaceName and objectPath
|
||||
//! \warning Before calling this method, make sure that a valid DBus connection was set.
|
||||
void registerDBusObjectPath(const std::string &interfaceName, const std::string &objectPath);
|
||||
|
||||
//! Process DBus messages. Needs to be implemented by deriving classes
|
||||
virtual int processDBus() = 0;
|
||||
|
||||
@@ -34,9 +42,6 @@ namespace XSwiftBus
|
||||
//! DBus message handler
|
||||
virtual DBusHandlerResult dbusMessageHandler(const CDBusMessage &message) = 0;
|
||||
|
||||
//! Register itself with interfaceName and objectPath
|
||||
void registerDBusObjectPath(const std::string &interfaceName, const std::string &objectPath);
|
||||
|
||||
//! Send DBus signal
|
||||
void sendDBusSignal(const std::string &name);
|
||||
|
||||
@@ -76,7 +81,7 @@ namespace XSwiftBus
|
||||
static void dbusObjectPathUnregisterFunction(DBusConnection *connection, void *data);
|
||||
static DBusHandlerResult dbusObjectPathMessageFunction(DBusConnection *connection, DBusMessage *message, void *data);
|
||||
|
||||
CDBusConnection *m_dbusConnection;
|
||||
std::shared_ptr<CDBusConnection> m_dbusConnection;
|
||||
std::string m_interfaceName;
|
||||
std::string m_objectPath;
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ namespace XSwiftBus
|
||||
{
|
||||
|
||||
CPlugin::CPlugin()
|
||||
: m_dbusConnection(std::make_unique<CDBusConnection>()), m_menu(CMenu::mainMenu().subMenu("XSwiftBus"))
|
||||
: m_dbusConnection(std::make_shared<CDBusConnection>()), m_menu(CMenu::mainMenu().subMenu("XSwiftBus"))
|
||||
{
|
||||
m_startServerMenuItem = m_menu.item("Start XSwiftBus", [this]{ startServer(CDBusConnection::SessionBus); });
|
||||
m_toggleMessageWindowMenuItem = m_menu.item("Toggle Message Window", [this] { if(m_service) { m_service->toggleMessageBoxVisibility(); } });
|
||||
@@ -61,6 +61,12 @@ namespace XSwiftBus
|
||||
// for (auto &item : m_startServerMenuItems) { item.setEnabled(false); }
|
||||
m_startServerMenuItem.setEnabled(false);
|
||||
|
||||
m_service = std::make_unique<CService>();
|
||||
m_traffic = std::make_unique<CTraffic>();
|
||||
m_weather = std::make_unique<CWeather>();
|
||||
|
||||
m_traffic->setPlaneViewMenu(m_planeViewSubMenu);
|
||||
|
||||
// Todo: retry if it fails
|
||||
bool success = m_dbusConnection->connect(CDBusConnection::SessionBus, xswiftbusServiceName());
|
||||
|
||||
@@ -70,11 +76,12 @@ namespace XSwiftBus
|
||||
return;
|
||||
}
|
||||
|
||||
m_service = new CService(m_dbusConnection.get());
|
||||
m_traffic = new CTraffic(m_dbusConnection.get());
|
||||
m_weather = new CWeather(m_dbusConnection.get());
|
||||
|
||||
m_traffic->setPlaneViewMenu(m_planeViewSubMenu);
|
||||
m_service->setDBusConnection(m_dbusConnection);
|
||||
m_service->registerDBusObjectPath(m_service->InterfaceName(), m_service->ObjectPath());
|
||||
m_traffic->setDBusConnection(m_dbusConnection);
|
||||
m_traffic->registerDBusObjectPath(m_traffic->InterfaceName(), m_traffic->ObjectPath());
|
||||
m_weather->setDBusConnection(m_dbusConnection);
|
||||
m_weather->registerDBusObjectPath(m_weather->InterfaceName(), m_weather->ObjectPath());
|
||||
|
||||
INFO_LOG("XSwiftBus started.");
|
||||
}
|
||||
|
||||
@@ -53,10 +53,10 @@ namespace XSwiftBus
|
||||
void onAircraftRepositioned();
|
||||
|
||||
private:
|
||||
std::unique_ptr<CDBusConnection> m_dbusConnection;
|
||||
CService *m_service = nullptr;
|
||||
CTraffic *m_traffic = nullptr;
|
||||
CWeather *m_weather = nullptr;
|
||||
std::shared_ptr<CDBusConnection> m_dbusConnection;
|
||||
std::unique_ptr<CService> m_service;
|
||||
std::unique_ptr<CTraffic> m_traffic;
|
||||
std::unique_ptr<CWeather> m_weather;
|
||||
CMenu m_menu;
|
||||
CMenuItem m_startServerMenuItem;
|
||||
CMenuItem m_toggleMessageWindowMenuItem;
|
||||
|
||||
@@ -18,9 +18,8 @@
|
||||
namespace XSwiftBus
|
||||
{
|
||||
|
||||
CService::CService(CDBusConnection *connection) : CDBusObject(connection)
|
||||
CService::CService() : CDBusObject()
|
||||
{
|
||||
registerDBusObjectPath(XSWIFTBUS_SERVICE_INTERFACENAME, XSWIFTBUS_SERVICE_OBJECTPATH);
|
||||
m_messages.addMessage({ "xswiftbus started.", 0, 255, 255 });
|
||||
updateAirportsInRange();
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ namespace XSwiftBus
|
||||
{
|
||||
public:
|
||||
//! Constructor
|
||||
CService(CDBusConnection *connection);
|
||||
CService();
|
||||
|
||||
//! Destructor
|
||||
~CService() override = default;
|
||||
|
||||
@@ -43,12 +43,11 @@ namespace XSwiftBus
|
||||
surfaces.lights.timeOffset = static_cast<uint16_t>(std::rand() % 0xffff);
|
||||
}
|
||||
|
||||
CTraffic::CTraffic(CDBusConnection *dbusConnection) :
|
||||
CDBusObject(dbusConnection),
|
||||
CTraffic::CTraffic() :
|
||||
CDBusObject(),
|
||||
m_followPlaneViewNextCommand("org/swift-project/xswiftbus/follow_next_plane", "Changes plane view to follow next plane in sequence", [this] { followNextPlane(); }),
|
||||
m_followPlaneViewPreviousCommand("org/swift-project/xswiftbus/follow_previous_plane", "Changes plane view to follow previous plane in sequence", [this] { followPreviousPlane(); })
|
||||
{
|
||||
registerDBusObjectPath(XSWIFTBUS_TRAFFIC_INTERFACENAME, XSWIFTBUS_TRAFFIC_OBJECTPATH);
|
||||
XPLMRegisterDrawCallback(drawCallback, xplm_Phase_Airplanes, 1, this);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ namespace XSwiftBus
|
||||
{
|
||||
public:
|
||||
//! Constructor
|
||||
CTraffic(CDBusConnection *dbusConnection);
|
||||
CTraffic();
|
||||
|
||||
//! Destructor
|
||||
~CTraffic() override;
|
||||
|
||||
@@ -14,10 +14,8 @@
|
||||
|
||||
namespace XSwiftBus
|
||||
{
|
||||
CWeather::CWeather(CDBusConnection *dbusConnection)
|
||||
: CDBusObject(dbusConnection)
|
||||
CWeather::CWeather()
|
||||
{
|
||||
registerDBusObjectPath(InterfaceName(), ObjectPath());
|
||||
}
|
||||
|
||||
//! Set cloud layer
|
||||
|
||||
@@ -33,7 +33,7 @@ namespace XSwiftBus
|
||||
{
|
||||
public:
|
||||
//! Constructor
|
||||
CWeather(CDBusConnection *dbusConnection);
|
||||
CWeather();
|
||||
|
||||
//! DBus interface name
|
||||
static const std::string &InterfaceName()
|
||||
|
||||
Reference in New Issue
Block a user