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

View File

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

View File

@@ -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.");
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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