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:
Roland Winklmeier
2018-07-26 21:52:45 +02:00
committed by Klaus Basan
parent 01085f24b3
commit 59da68da5e
10 changed files with 45 additions and 28 deletions

View File

@@ -8,17 +8,23 @@
*/ */
#include "dbusobject.h" #include "dbusobject.h"
#include <cassert>
namespace XSwiftBus namespace XSwiftBus
{ {
CDBusObject::CDBusObject(CDBusConnection *dbusConnection) CDBusObject::CDBusObject()
: m_dbusConnection(dbusConnection)
{ } { }
CDBusObject::~CDBusObject() = default; 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) void CDBusObject::registerDBusObjectPath(const std::string &interfaceName, const std::string &objectPath)
{ {
assert(m_dbusConnection);
m_interfaceName = interfaceName; m_interfaceName = interfaceName;
m_objectPath = objectPath; m_objectPath = objectPath;
m_dbusConnection->registerObjectPath(this, interfaceName, objectPath, m_dbusObjectPathVTable); m_dbusConnection->registerObjectPath(this, interfaceName, objectPath, m_dbusObjectPathVTable);
@@ -26,12 +32,14 @@ namespace XSwiftBus
void CDBusObject::sendDBusSignal(const std::string &name) void CDBusObject::sendDBusSignal(const std::string &name)
{ {
if (! m_dbusConnection) { return; }
CDBusMessage signal = CDBusMessage::createSignal(m_objectPath, m_interfaceName, name); CDBusMessage signal = CDBusMessage::createSignal(m_objectPath, m_interfaceName, name);
m_dbusConnection->sendMessage(signal); m_dbusConnection->sendMessage(signal);
} }
void CDBusObject::sendDBusMessage(const CDBusMessage &message) void CDBusObject::sendDBusMessage(const CDBusMessage &message)
{ {
if (! m_dbusConnection) { return; }
m_dbusConnection->sendMessage(message); m_dbusConnection->sendMessage(message);
} }
@@ -71,6 +79,7 @@ namespace XSwiftBus
(void)connection; // unused (void)connection; // unused
auto *obj = static_cast<CDBusObject *>(data); auto *obj = static_cast<CDBusObject *>(data);
DBusError err; DBusError err;
dbus_error_init(&err); dbus_error_init(&err);

View File

@@ -22,11 +22,19 @@ namespace XSwiftBus
{ {
public: public:
//! Constructor //! Constructor
CDBusObject(CDBusConnection *dbusConnection); CDBusObject();
//! Destructor //! Destructor
virtual ~CDBusObject(); 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 //! Process DBus messages. Needs to be implemented by deriving classes
virtual int processDBus() = 0; virtual int processDBus() = 0;
@@ -34,9 +42,6 @@ namespace XSwiftBus
//! DBus message handler //! DBus message handler
virtual DBusHandlerResult dbusMessageHandler(const CDBusMessage &message) = 0; 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 //! Send DBus signal
void sendDBusSignal(const std::string &name); void sendDBusSignal(const std::string &name);
@@ -76,7 +81,7 @@ namespace XSwiftBus
static void dbusObjectPathUnregisterFunction(DBusConnection *connection, void *data); static void dbusObjectPathUnregisterFunction(DBusConnection *connection, void *data);
static DBusHandlerResult dbusObjectPathMessageFunction(DBusConnection *connection, DBusMessage *message, 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_interfaceName;
std::string m_objectPath; std::string m_objectPath;

View File

@@ -27,7 +27,7 @@ namespace XSwiftBus
{ {
CPlugin::CPlugin() 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_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(); } }); 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); } // for (auto &item : m_startServerMenuItems) { item.setEnabled(false); }
m_startServerMenuItem.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 // Todo: retry if it fails
bool success = m_dbusConnection->connect(CDBusConnection::SessionBus, xswiftbusServiceName()); bool success = m_dbusConnection->connect(CDBusConnection::SessionBus, xswiftbusServiceName());
@@ -70,11 +76,12 @@ namespace XSwiftBus
return; return;
} }
m_service = new CService(m_dbusConnection.get()); m_service->setDBusConnection(m_dbusConnection);
m_traffic = new CTraffic(m_dbusConnection.get()); m_service->registerDBusObjectPath(m_service->InterfaceName(), m_service->ObjectPath());
m_weather = new CWeather(m_dbusConnection.get()); m_traffic->setDBusConnection(m_dbusConnection);
m_traffic->registerDBusObjectPath(m_traffic->InterfaceName(), m_traffic->ObjectPath());
m_traffic->setPlaneViewMenu(m_planeViewSubMenu); m_weather->setDBusConnection(m_dbusConnection);
m_weather->registerDBusObjectPath(m_weather->InterfaceName(), m_weather->ObjectPath());
INFO_LOG("XSwiftBus started."); INFO_LOG("XSwiftBus started.");
} }

View File

@@ -53,10 +53,10 @@ namespace XSwiftBus
void onAircraftRepositioned(); void onAircraftRepositioned();
private: private:
std::unique_ptr<CDBusConnection> m_dbusConnection; std::shared_ptr<CDBusConnection> m_dbusConnection;
CService *m_service = nullptr; std::unique_ptr<CService> m_service;
CTraffic *m_traffic = nullptr; std::unique_ptr<CTraffic> m_traffic;
CWeather *m_weather = nullptr; std::unique_ptr<CWeather> m_weather;
CMenu m_menu; CMenu m_menu;
CMenuItem m_startServerMenuItem; CMenuItem m_startServerMenuItem;
CMenuItem m_toggleMessageWindowMenuItem; CMenuItem m_toggleMessageWindowMenuItem;

View File

@@ -18,9 +18,8 @@
namespace XSwiftBus 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 }); m_messages.addMessage({ "xswiftbus started.", 0, 255, 255 });
updateAirportsInRange(); updateAirportsInRange();
} }

View File

@@ -39,7 +39,7 @@ namespace XSwiftBus
{ {
public: public:
//! Constructor //! Constructor
CService(CDBusConnection *connection); CService();
//! Destructor //! Destructor
~CService() override = default; ~CService() override = default;

View File

@@ -43,12 +43,11 @@ namespace XSwiftBus
surfaces.lights.timeOffset = static_cast<uint16_t>(std::rand() % 0xffff); surfaces.lights.timeOffset = static_cast<uint16_t>(std::rand() % 0xffff);
} }
CTraffic::CTraffic(CDBusConnection *dbusConnection) : CTraffic::CTraffic() :
CDBusObject(dbusConnection), CDBusObject(),
m_followPlaneViewNextCommand("org/swift-project/xswiftbus/follow_next_plane", "Changes plane view to follow next plane in sequence", [this] { followNextPlane(); }), 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(); }) 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); XPLMRegisterDrawCallback(drawCallback, xplm_Phase_Airplanes, 1, this);
} }

View File

@@ -37,7 +37,7 @@ namespace XSwiftBus
{ {
public: public:
//! Constructor //! Constructor
CTraffic(CDBusConnection *dbusConnection); CTraffic();
//! Destructor //! Destructor
~CTraffic() override; ~CTraffic() override;

View File

@@ -14,10 +14,8 @@
namespace XSwiftBus namespace XSwiftBus
{ {
CWeather::CWeather(CDBusConnection *dbusConnection) CWeather::CWeather()
: CDBusObject(dbusConnection)
{ {
registerDBusObjectPath(InterfaceName(), ObjectPath());
} }
//! Set cloud layer //! Set cloud layer

View File

@@ -33,7 +33,7 @@ namespace XSwiftBus
{ {
public: public:
//! Constructor //! Constructor
CWeather(CDBusConnection *dbusConnection); CWeather();
//! DBus interface name //! DBus interface name
static const std::string &InterfaceName() static const std::string &InterfaceName()