mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-04 08:36:52 +08:00
refs #520 Moved CDBusServer from BlackCore to BlackMisc.
This commit is contained in:
@@ -25,7 +25,7 @@ using namespace BlackMisc;
|
||||
namespace BlackCore
|
||||
{
|
||||
|
||||
IContextApplication *IContextApplication::create(CRuntime *parent, CRuntimeConfig::ContextMode mode, CDBusServer *server, QDBusConnection &connection)
|
||||
IContextApplication *IContextApplication::create(CRuntime *parent, CRuntimeConfig::ContextMode mode, BlackMisc::CDBusServer *server, QDBusConnection &connection)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
@@ -33,7 +33,7 @@ namespace BlackCore
|
||||
case CRuntimeConfig::LocalInDbusServer:
|
||||
return (new CContextApplication(mode, parent))->registerWithDBus(server);
|
||||
case CRuntimeConfig::Remote:
|
||||
return new BlackCore::CContextApplicationProxy(BlackCore::CDBusServer::ServiceName(), connection, mode, parent);
|
||||
return new BlackCore::CContextApplicationProxy(BlackMisc::CDBusServer::ServiceName(), connection, mode, parent);
|
||||
case CRuntimeConfig::NotUsed:
|
||||
default:
|
||||
return new CContextApplicationEmpty(parent);
|
||||
|
||||
@@ -72,7 +72,7 @@ namespace BlackCore
|
||||
virtual QString getPathAndContextId() const { return this->buildPathAndContextId(ObjectPath()); }
|
||||
|
||||
//! Factory method
|
||||
static IContextApplication *create(CRuntime *parent, CRuntimeConfig::ContextMode mode, CDBusServer *server, QDBusConnection &conn);
|
||||
static IContextApplication *create(CRuntime *parent, CRuntimeConfig::ContextMode mode, BlackMisc::CDBusServer *server, QDBusConnection &conn);
|
||||
|
||||
//! Destructor
|
||||
virtual ~IContextApplication() {}
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace BlackCore
|
||||
IContextApplication(mode, runtime)
|
||||
{}
|
||||
|
||||
CContextApplication *CContextApplication::registerWithDBus(CDBusServer *server)
|
||||
CContextApplication *CContextApplication::registerWithDBus(BlackMisc::CDBusServer *server)
|
||||
{
|
||||
if (!server || this->m_mode != CRuntimeConfig::LocalInDbusServer) { return this; }
|
||||
server->addObject(IContextApplication::ObjectPath(), this);
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include "blackcoreexport.h"
|
||||
#include "contextapplication.h"
|
||||
#include "contextruntime.h"
|
||||
#include "dbusserver.h"
|
||||
#include "blackmisc/dbusserver.h"
|
||||
#include "blackmisc/identifierlist.h"
|
||||
|
||||
namespace BlackCore
|
||||
@@ -95,7 +95,7 @@ namespace BlackCore
|
||||
CContextApplication(CRuntimeConfig::ContextMode mode, CRuntime *runtime);
|
||||
|
||||
//! Register myself in DBus, fail safe
|
||||
CContextApplication *registerWithDBus(CDBusServer *server);
|
||||
CContextApplication *registerWithDBus(BlackMisc::CDBusServer *server);
|
||||
|
||||
private:
|
||||
BlackMisc::CIdentifierList m_registeredApplications;
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
namespace BlackCore
|
||||
{
|
||||
|
||||
IContextAudio *IContextAudio::create(CRuntime *runtime, CRuntimeConfig::ContextMode mode, CDBusServer *server, QDBusConnection &conn)
|
||||
IContextAudio *IContextAudio::create(CRuntime *runtime, CRuntimeConfig::ContextMode mode, BlackMisc::CDBusServer *server, QDBusConnection &conn)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
@@ -23,7 +23,7 @@ namespace BlackCore
|
||||
case CRuntimeConfig::LocalInDbusServer:
|
||||
return (new CContextAudio(mode, runtime))->registerWithDBus(server);
|
||||
case CRuntimeConfig::Remote:
|
||||
return new CContextAudioProxy(BlackCore::CDBusServer::ServiceName(), conn, mode, runtime);
|
||||
return new CContextAudioProxy(BlackMisc::CDBusServer::ServiceName(), conn, mode, runtime);
|
||||
default:
|
||||
return new CContextAudioEmpty(runtime); // audio not mandatory
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ namespace BlackCore
|
||||
virtual QString getPathAndContextId() const { return this->buildPathAndContextId(ObjectPath()); }
|
||||
|
||||
//! Factory method
|
||||
static IContextAudio *create(CRuntime *runtime, CRuntimeConfig::ContextMode mode, CDBusServer *server, QDBusConnection &conn);
|
||||
static IContextAudio *create(CRuntime *runtime, CRuntimeConfig::ContextMode mode, BlackMisc::CDBusServer *server, QDBusConnection &conn);
|
||||
|
||||
//! Destructor
|
||||
virtual ~IContextAudio() {}
|
||||
|
||||
@@ -14,8 +14,8 @@
|
||||
|
||||
#include "blackcoreexport.h"
|
||||
#include "contextaudio.h"
|
||||
#include "dbusserver.h"
|
||||
#include "voice.h"
|
||||
#include "blackmisc/dbusserver.h"
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
#include "blackcore/blackcoreexport.h"
|
||||
#include "blackcore/contextaudio.h"
|
||||
#include "blackcore/contextruntime.h"
|
||||
#include "blackcore/dbusserver.h"
|
||||
#include "blackcore/voice.h"
|
||||
#include "blackcore/voicechannel.h"
|
||||
#include "blackcore/audiodevice.h"
|
||||
@@ -23,6 +22,7 @@
|
||||
#include "blackcore/actionbind.h"
|
||||
#include "blackinput/keyboard.h"
|
||||
#include "blackcore/settings/audio.h"
|
||||
#include "blackmisc/dbusserver.h"
|
||||
#include "blackmisc/audio/voiceroomlist.h"
|
||||
|
||||
#include <QThread>
|
||||
@@ -122,7 +122,7 @@ namespace BlackCore
|
||||
CContextAudio(CRuntimeConfig::ContextMode mode, CRuntime *runtime);
|
||||
|
||||
//! Register myself in DBus
|
||||
CContextAudio *registerWithDBus(CDBusServer *server)
|
||||
CContextAudio *registerWithDBus(BlackMisc::CDBusServer *server)
|
||||
{
|
||||
if (!server || this->m_mode != CRuntimeConfig::LocalInDbusServer) { return this; }
|
||||
server->addObject(IContextAudio::ObjectPath(), this);
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
namespace BlackCore
|
||||
{
|
||||
|
||||
IContextNetwork *IContextNetwork::create(CRuntime *runtime, CRuntimeConfig::ContextMode mode, CDBusServer *server, QDBusConnection &connection)
|
||||
IContextNetwork *IContextNetwork::create(CRuntime *runtime, CRuntimeConfig::ContextMode mode, BlackMisc::CDBusServer *server, QDBusConnection &connection)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
@@ -23,7 +23,7 @@ namespace BlackCore
|
||||
case CRuntimeConfig::LocalInDbusServer:
|
||||
return (new CContextNetwork(mode, runtime))->registerWithDBus(server);
|
||||
case CRuntimeConfig::Remote:
|
||||
return new CContextNetworkProxy(BlackCore::CDBusServer::ServiceName(), connection, mode, runtime);
|
||||
return new CContextNetworkProxy(BlackMisc::CDBusServer::ServiceName(), connection, mode, runtime);
|
||||
case CRuntimeConfig::NotUsed:
|
||||
default:
|
||||
return new CContextNetworkEmpty(runtime);
|
||||
|
||||
@@ -65,7 +65,7 @@ namespace BlackCore
|
||||
virtual QString getPathAndContextId() const { return this->buildPathAndContextId(ObjectPath()); }
|
||||
|
||||
//! Factory method
|
||||
static IContextNetwork *create(CRuntime *parent, CRuntimeConfig::ContextMode mode, CDBusServer *server, QDBusConnection &connection);
|
||||
static IContextNetwork *create(CRuntime *parent, CRuntimeConfig::ContextMode mode, BlackMisc::CDBusServer *server, QDBusConnection &connection);
|
||||
|
||||
//! Destructor
|
||||
virtual ~IContextNetwork() {}
|
||||
|
||||
@@ -78,7 +78,7 @@ namespace BlackCore
|
||||
connect(this->m_airspace, &CAirspaceMonitor::addedAircraft, this, &CContextNetwork::addedAircraft);
|
||||
}
|
||||
|
||||
CContextNetwork *CContextNetwork::registerWithDBus(CDBusServer *server)
|
||||
CContextNetwork *CContextNetwork::registerWithDBus(BlackMisc::CDBusServer *server)
|
||||
{
|
||||
if (!server || this->m_mode != CRuntimeConfig::LocalInDbusServer) return this;
|
||||
server->addObject(IContextNetwork::ObjectPath(), this);
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
#include "blackcoreexport.h"
|
||||
#include "blackcore/contextnetwork.h"
|
||||
#include "blackcore/contextruntime.h"
|
||||
#include "blackcore/dbusserver.h"
|
||||
#include "blackcore/network.h"
|
||||
#include "blackmisc/dbusserver.h"
|
||||
#include "blackmisc/simulation/remoteaircraftprovider.h"
|
||||
#include "blackmisc/aviation/atcstationlist.h"
|
||||
#include "blackmisc/aviation/aircraftsituationlist.h"
|
||||
@@ -233,7 +233,7 @@ namespace BlackCore
|
||||
CContextNetwork(CRuntimeConfig::ContextMode, CRuntime *runtime);
|
||||
|
||||
//! Register myself in DBus
|
||||
CContextNetwork *registerWithDBus(CDBusServer *server);
|
||||
CContextNetwork *registerWithDBus(BlackMisc::CDBusServer *server);
|
||||
|
||||
private:
|
||||
CAirspaceMonitor *m_airspace = nullptr;
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
IContextOwnAircraft *IContextOwnAircraft::create(CRuntime *parent, CRuntimeConfig::ContextMode mode, CDBusServer *server, QDBusConnection &conn)
|
||||
IContextOwnAircraft *IContextOwnAircraft::create(CRuntime *parent, CRuntimeConfig::ContextMode mode, BlackMisc::CDBusServer *server, QDBusConnection &conn)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
@@ -22,7 +22,7 @@ namespace BlackCore
|
||||
case CRuntimeConfig::LocalInDbusServer:
|
||||
return (new CContextOwnAircraft(mode, parent))->registerWithDBus(server);
|
||||
case CRuntimeConfig::Remote:
|
||||
return new CContextOwnAircraftProxy(BlackCore::CDBusServer::ServiceName(), conn, mode, parent);
|
||||
return new CContextOwnAircraftProxy(BlackMisc::CDBusServer::ServiceName(), conn, mode, parent);
|
||||
case CRuntimeConfig::NotUsed:
|
||||
default:
|
||||
return new CContextOwnAircraftEmpty(parent);
|
||||
|
||||
@@ -59,7 +59,7 @@ namespace BlackCore
|
||||
virtual QString getPathAndContextId() const { return this->buildPathAndContextId(ObjectPath()); }
|
||||
|
||||
//! Factory method
|
||||
static IContextOwnAircraft *create(CRuntime *parent, CRuntimeConfig::ContextMode mode, CDBusServer *server, QDBusConnection &conn);
|
||||
static IContextOwnAircraft *create(CRuntime *parent, CRuntimeConfig::ContextMode mode, BlackMisc::CDBusServer *server, QDBusConnection &conn);
|
||||
|
||||
//! Destructor
|
||||
virtual ~IContextOwnAircraft() {}
|
||||
|
||||
@@ -15,8 +15,8 @@
|
||||
#include "blackcoreexport.h"
|
||||
#include "blackcore/contextownaircraft.h"
|
||||
#include "blackcore/contextruntime.h"
|
||||
#include "blackcore/dbusserver.h"
|
||||
#include "blackcore/settings/network.h"
|
||||
#include "blackmisc/dbusserver.h"
|
||||
#include "blackmisc/aviation/atcstation.h"
|
||||
#include "blackmisc/simulation/ownaircraftprovider.h"
|
||||
#include "blackmisc/identifiable.h"
|
||||
@@ -123,7 +123,7 @@ namespace BlackCore
|
||||
CContextOwnAircraft(CRuntimeConfig::ContextMode, CRuntime *runtime);
|
||||
|
||||
//! Register myself in DBus
|
||||
CContextOwnAircraft *registerWithDBus(CDBusServer *server)
|
||||
CContextOwnAircraft *registerWithDBus(BlackMisc::CDBusServer *server)
|
||||
{
|
||||
if (!server || this->m_mode != CRuntimeConfig::LocalInDbusServer) return this;
|
||||
server->addObject(IContextOwnAircraft::ObjectPath(), this);
|
||||
|
||||
@@ -24,11 +24,12 @@
|
||||
#include <QMultiMap>
|
||||
#include <QReadWriteLock>
|
||||
|
||||
namespace BlackMisc { class CDBusServer; }
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
// forward declaration, see review
|
||||
// https://dev.vatsim-germany.org/boards/22/topics/1350?r=1359#message-1359
|
||||
class CDBusServer;
|
||||
class CContextApplication;
|
||||
class CContextAudio;
|
||||
class CContextNetwork;
|
||||
@@ -53,7 +54,7 @@ namespace BlackCore
|
||||
virtual ~CRuntime() { this->gracefulShutdown(); }
|
||||
|
||||
//! DBus server (if applicable)
|
||||
const CDBusServer *getDBusServer() const { return this->m_dbusServer; }
|
||||
const BlackMisc::CDBusServer *getDBusServer() const { return this->m_dbusServer; }
|
||||
|
||||
//! DBus connection (if applicable)
|
||||
const QDBusConnection &getDBusConnection() const { return this->m_dbusConnection; }
|
||||
@@ -153,7 +154,7 @@ namespace BlackCore
|
||||
CSetting<Settings::Network::DBusServerAddress> m_dbusServerAddress { this };
|
||||
|
||||
// DBus
|
||||
CDBusServer *m_dbusServer = nullptr;
|
||||
BlackMisc::CDBusServer *m_dbusServer = nullptr;
|
||||
QDBusConnection m_dbusConnection = QDBusConnection("default");
|
||||
bool m_initDBusConnection = false;
|
||||
|
||||
|
||||
@@ -34,7 +34,7 @@ namespace BlackCore
|
||||
return t;
|
||||
}
|
||||
|
||||
IContextSimulator *IContextSimulator::create(CRuntime *parent, CRuntimeConfig::ContextMode mode, CDBusServer *server, QDBusConnection &conn)
|
||||
IContextSimulator *IContextSimulator::create(CRuntime *parent, CRuntimeConfig::ContextMode mode, BlackMisc::CDBusServer *server, QDBusConnection &conn)
|
||||
{
|
||||
switch (mode)
|
||||
{
|
||||
@@ -42,7 +42,7 @@ namespace BlackCore
|
||||
case CRuntimeConfig::LocalInDbusServer:
|
||||
return (new CContextSimulator(mode, parent))->registerWithDBus(server);
|
||||
case CRuntimeConfig::Remote:
|
||||
return new CContextSimulatorProxy(BlackCore::CDBusServer::ServiceName(), conn, mode, parent);
|
||||
return new CContextSimulatorProxy(BlackMisc::CDBusServer::ServiceName(), conn, mode, parent);
|
||||
case CRuntimeConfig::NotUsed:
|
||||
default:
|
||||
return new CContextSimulatorEmpty(parent);
|
||||
|
||||
@@ -26,8 +26,8 @@
|
||||
#include "blackcoreexport.h"
|
||||
#include "context.h"
|
||||
#include "blackcore/simulator.h"
|
||||
#include "blackcore/dbusserver.h"
|
||||
#include "blackcore/contextruntime.h"
|
||||
#include "blackmisc/dbusserver.h"
|
||||
#include "blackmisc/simulation/aircraftmodellist.h"
|
||||
#include "blackmisc/simulation/simulatorplugininfo.h"
|
||||
#include "blackmisc/simulation/simulatorsetup.h"
|
||||
@@ -61,7 +61,7 @@ namespace BlackCore
|
||||
virtual QString getPathAndContextId() const { return this->buildPathAndContextId(ObjectPath()); }
|
||||
|
||||
//! Factory method
|
||||
static IContextSimulator *create(CRuntime *parent, CRuntimeConfig::ContextMode mode, CDBusServer *server, QDBusConnection &conn);
|
||||
static IContextSimulator *create(CRuntime *parent, CRuntimeConfig::ContextMode mode, BlackMisc::CDBusServer *server, QDBusConnection &conn);
|
||||
|
||||
//! Destructor
|
||||
virtual ~IContextSimulator() {}
|
||||
|
||||
@@ -128,7 +128,7 @@ namespace BlackCore
|
||||
CContextSimulator(CRuntimeConfig::ContextMode, CRuntime *runtime);
|
||||
|
||||
//! Register myself in DBus
|
||||
CContextSimulator *registerWithDBus(CDBusServer *server)
|
||||
CContextSimulator *registerWithDBus(BlackMisc::CDBusServer *server)
|
||||
{
|
||||
if (!server || this->m_mode != CRuntimeConfig::LocalInDbusServer) return this;
|
||||
server->addObject(CContextSimulator::ObjectPath(), this);
|
||||
|
||||
@@ -1,397 +0,0 @@
|
||||
/* Copyright (C) 2014
|
||||
* 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 "blackmisc/logmessage.h"
|
||||
#include "blackmisc/network/networkutils.h"
|
||||
#include "dbusserver.h"
|
||||
#include <QProcess>
|
||||
#include <QMetaClassInfo>
|
||||
|
||||
using namespace BlackMisc;
|
||||
using namespace BlackMisc::Network;
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
/*
|
||||
* Constructor
|
||||
* Remark, without the default "unix:tmpdir=/tmp" any refereal to address crashes
|
||||
* see http://download.froglogic.com/public/qt5-squishcoco-report/QtBase/source_241_preprocessed.html
|
||||
* DBus config: http://dbus.freedesktop.org/doc/dbus-daemon.1.html
|
||||
*/
|
||||
CDBusServer::CDBusServer(const QString &service, const QString &address, QObject *parent) : QObject(parent)
|
||||
{
|
||||
ServerMode m = CDBusServer::addressToDBusMode(address);
|
||||
switch (m)
|
||||
{
|
||||
case SERVERMODE_SESSIONBUS:
|
||||
{
|
||||
// we use a session bus connection instead of a real P2P connection
|
||||
this->m_serverMode = CDBusServer::SERVERMODE_SESSIONBUS;
|
||||
QDBusConnection connection = QDBusConnection::connectToBus(QDBusConnection::SessionBus, ServiceName());
|
||||
if (!connection.isConnected())
|
||||
{
|
||||
launchDbusDaemon();
|
||||
connection = QDBusConnection::connectToBus(QDBusConnection::SessionBus, ServiceName());
|
||||
}
|
||||
|
||||
if (!connection.registerService(service))
|
||||
{
|
||||
// registration fails can either mean something wrong with DBus or service already exists
|
||||
CLogMessage(this).warning("DBus registration: %1") << connection.lastError().message();
|
||||
CLogMessage(this).warning("Cannot register DBus service, server started? dbus-daemon.exe --session --address=tcp:host=192.168.0.133,port=45000");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SERVERMODE_SYSTEMBUS:
|
||||
{
|
||||
// we use a system bus connection instead of a real P2P connection
|
||||
this->m_serverMode = CDBusServer::SERVERMODE_SYSTEMBUS;
|
||||
QDBusConnection connection = QDBusConnection::systemBus();
|
||||
if (!connection.isConnected())
|
||||
{
|
||||
launchDbusDaemon();
|
||||
connection = QDBusConnection::systemBus();
|
||||
}
|
||||
|
||||
if (!connection.registerService(service))
|
||||
{
|
||||
// registration fails can either mean something wrong with DBus or service already exists
|
||||
CLogMessage(this).warning("DBus registration: %1") << connection.lastError().message();
|
||||
CLogMessage(this).warning("Cannot register DBus service, server started? dbus-daemon.exe --session --address=tcp:host=192.168.0.133,port=45000");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SERVERMODE_P2P:
|
||||
default:
|
||||
{
|
||||
QString dbusAddress(CDBusServer::isQtDBusAddress(address) ? address : "tcp:host=127.0.0.1,port=45000");
|
||||
dbusAddress = dbusAddress.toLower().trimmed().replace(' ', "");
|
||||
if (!dbusAddress.contains("bind=")) { dbusAddress = dbusAddress.append(",bind=*"); }
|
||||
this->m_serverMode = CDBusServer::SERVERMODE_P2P;
|
||||
this->m_busServer.reset(
|
||||
new QDBusServer(
|
||||
dbusAddress, // "unix:tmpdir=/tmp"
|
||||
parent)
|
||||
);
|
||||
m_busServer->setAnonymousAuthenticationAllowed(true);
|
||||
|
||||
// Note: P2P has no service name
|
||||
if (!m_busServer->isConnected())
|
||||
{
|
||||
CLogMessage(this).warning("DBus P2P connection failed: %1") << this->lastQDBusServerError().message();
|
||||
}
|
||||
else
|
||||
{
|
||||
CLogMessage(this).debug() << "Server listening on address: " << m_busServer->address();
|
||||
}
|
||||
connect(m_busServer.data(), &QDBusServer::newConnection, this, &CDBusServer::ps_registerObjectsWithP2PConnection);
|
||||
}
|
||||
break;
|
||||
} // switch
|
||||
}
|
||||
|
||||
const QString &CDBusServer::ServiceName()
|
||||
{
|
||||
static const QString sn(BLACKCORE_RUNTIME_SERVICENAME);
|
||||
return sn;
|
||||
}
|
||||
|
||||
void CDBusServer::launchDbusDaemon()
|
||||
{
|
||||
const QString program = QStringLiteral("dbus-daemon");
|
||||
const QStringList arguments = { QStringLiteral("--config-file=../share/dbus-1/session.conf") };
|
||||
bool success = QProcess::startDetached(program, arguments);
|
||||
if (!success) { CLogMessage(this).warning("Failed to launch dbus-daemon!"); }
|
||||
}
|
||||
|
||||
bool CDBusServer::isP2PAddress(const QString &address)
|
||||
{
|
||||
return CDBusServer::addressToDBusMode(address) == SERVERMODE_P2P;
|
||||
}
|
||||
|
||||
bool CDBusServer::splitDBusAddressIntoHostAndPort(const QString &dbusAddress, QString &host, int &port)
|
||||
{
|
||||
bool ok = false;
|
||||
QString dbus(dbusAddress.trimmed().toLower().replace(' ', ""));
|
||||
if (dbus.contains("host=") || dbus.contains("port="))
|
||||
{
|
||||
// "tcp:host=foo.com,port=123"
|
||||
QStringList parts(dbus.split(','));
|
||||
for (const QString &p : parts)
|
||||
{
|
||||
if (p.contains("host="))
|
||||
{
|
||||
host = p.mid(p.lastIndexOf("=") + 1).trimmed();
|
||||
}
|
||||
else if (p.contains("port="))
|
||||
{
|
||||
bool ok;
|
||||
port = p.mid(p.lastIndexOf("=") + 1).trimmed().toInt(&ok);
|
||||
if (!ok) { port = -1; }
|
||||
}
|
||||
}
|
||||
if (port < 0) { port = 45000; }
|
||||
if (host.isEmpty()) { host = "127.0.0.1"; }
|
||||
ok = true;
|
||||
}
|
||||
|
||||
if (!ok)
|
||||
{
|
||||
host = "";
|
||||
port = -1;
|
||||
}
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool CDBusServer::isQtDBusAddress(const QString &address)
|
||||
{
|
||||
return
|
||||
(address.contains("tcp:") ||
|
||||
address.contains("unix:")
|
||||
);
|
||||
}
|
||||
|
||||
bool CDBusServer::isSessionOrSystemAddress(const QString &address)
|
||||
{
|
||||
return address == sessionDBusServer() || address == systemDBusServer();
|
||||
}
|
||||
|
||||
const QString CDBusServer::getClassInfo(QObject *object)
|
||||
{
|
||||
if (!object) return "";
|
||||
const QMetaObject *mo = object->metaObject();
|
||||
if (mo->classInfoCount() < 1) return "";
|
||||
for (int i = 0; i < mo->classInfoCount(); i++)
|
||||
{
|
||||
QMetaClassInfo ci = mo->classInfo(i);
|
||||
QString name(ci.name());
|
||||
if (name == "D-Bus Interface") { return QString(ci.value()); }
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
bool CDBusServer::ps_registerObjectsWithP2PConnection(const QDBusConnection &connection)
|
||||
{
|
||||
Q_ASSERT(!this->m_objects.isEmpty());
|
||||
QDBusConnection newConnection(connection); // copy, because object will be registered on this connection
|
||||
// insert or replace connection
|
||||
m_DBusConnections.insert(newConnection.name(), newConnection);
|
||||
bool success = true;
|
||||
CLogMessage(this).debug() << "New Connection from: " << newConnection.name();
|
||||
QMap<QString, QObject *>::ConstIterator i = m_objects.begin();
|
||||
while (i != m_objects.end())
|
||||
{
|
||||
CLogMessage(this).debug() << "Adding " << i.key() << CDBusServer::getClassInfo(i.value()) << "to the new connection.";
|
||||
bool ok = newConnection.registerObject(i.key(), i.value(), CDBusServer::RegisterOptions());
|
||||
Q_ASSERT_X(ok, "CDBusServer::newConnection", "Registration failed");
|
||||
if (!ok) { success = false; }
|
||||
++i;
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
void CDBusServer::addObject(const QString &path, QObject *object)
|
||||
{
|
||||
if (!object) { return; }
|
||||
m_objects.insert(path, object); // For P2P: registered when P2P connection is established
|
||||
|
||||
// P2P
|
||||
if (this->m_serverMode == CDBusServer::SERVERMODE_P2P)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
bool success = false;
|
||||
if (this->m_serverMode == CDBusServer::SERVERMODE_SESSIONBUS)
|
||||
{
|
||||
QDBusConnection connection = QDBusConnection::connectToBus(QDBusConnection::SessionBus, ServiceName());
|
||||
success = connection.registerObject(path, object, CDBusServer::RegisterOptions());
|
||||
if (success)
|
||||
{
|
||||
CLogMessage(this).debug() << "Adding " << path << CDBusServer::getClassInfo(object) << " to session bus.";
|
||||
}
|
||||
else
|
||||
{
|
||||
CLogMessage(this).error("Error, no success with session bus registration");
|
||||
}
|
||||
}
|
||||
else if (this->m_serverMode == CDBusServer::SERVERMODE_SYSTEMBUS)
|
||||
{
|
||||
QDBusConnection connection = QDBusConnection::connectToBus(QDBusConnection::SystemBus, ServiceName());
|
||||
success = connection.registerObject(path, object, CDBusServer::RegisterOptions());
|
||||
if (success)
|
||||
{
|
||||
CLogMessage(this).debug() << "Adding " << path << CDBusServer::getClassInfo(object) << " to system bus.";
|
||||
}
|
||||
else
|
||||
{
|
||||
CLogMessage(this).error("Error, no success with system bus registration");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Q_ASSERT_X(false, "CDBusServer::addObject", "Wrong server mode");
|
||||
}
|
||||
}
|
||||
|
||||
QDBusError CDBusServer::lastQDBusServerError() const
|
||||
{
|
||||
if (!hasQDBusServer()) { return QDBusError(); }
|
||||
return this->m_busServer->lastError();
|
||||
}
|
||||
|
||||
const QDBusServer *CDBusServer::qDBusServer() const
|
||||
{
|
||||
return this->m_busServer.data();
|
||||
}
|
||||
|
||||
bool CDBusServer::hasQDBusServer() const
|
||||
{
|
||||
return !this->m_busServer.isNull();
|
||||
}
|
||||
|
||||
void CDBusServer::unregisterAllObjects()
|
||||
{
|
||||
if (this->m_objects.isEmpty()) return;
|
||||
foreach(QString path, this->m_objects.keys())
|
||||
{
|
||||
switch (this->m_serverMode)
|
||||
{
|
||||
case CDBusServer::SERVERMODE_SESSIONBUS:
|
||||
QDBusConnection::sessionBus().unregisterObject(path);
|
||||
break;
|
||||
case CDBusServer::SERVERMODE_SYSTEMBUS:
|
||||
QDBusConnection::systemBus().unregisterObject(path);
|
||||
break;
|
||||
case CDBusServer::SERVERMODE_P2P:
|
||||
{
|
||||
foreach(QDBusConnection connection, this->m_DBusConnections)
|
||||
{
|
||||
connection.unregisterObject(path);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
} // all paths
|
||||
}
|
||||
|
||||
const QDBusConnection &CDBusServer::defaultConnection()
|
||||
{
|
||||
static QDBusConnection defaultConnection("default");
|
||||
return defaultConnection;
|
||||
}
|
||||
|
||||
const QString &CDBusServer::sessionDBusServer()
|
||||
{
|
||||
static QString session("session");
|
||||
return session;
|
||||
}
|
||||
|
||||
const QString &CDBusServer::systemDBusServer()
|
||||
{
|
||||
static QString system("system");
|
||||
return system;
|
||||
}
|
||||
|
||||
QString CDBusServer::p2pAddress(const QString &host, const QString &port)
|
||||
{
|
||||
QString h = host.isEmpty() ? "127.0.0.1" : host.trimmed();
|
||||
QString p = port;
|
||||
|
||||
// can handle host and port separately or combined
|
||||
// such as "myHost::1234"
|
||||
if (port.isEmpty())
|
||||
{
|
||||
if (h.contains(":"))
|
||||
{
|
||||
QStringList parts = h.split(":");
|
||||
// todo: Replace assert with input validation
|
||||
Q_ASSERT_X(parts.length() == 2, "p2pAdress", "Wrong IP string split");
|
||||
h = parts.at(0).trimmed();
|
||||
p = parts.at(1).trimmed();
|
||||
}
|
||||
else
|
||||
{
|
||||
p = "45000";
|
||||
}
|
||||
}
|
||||
|
||||
// todo: Replace assert with input validation
|
||||
Q_ASSERT_X(CNetworkUtils::isValidIPv4Address(p), "p2pAdress", "Wrong IP in String");
|
||||
QString p2p = QString("tcp:host=%1,port=%2").arg(h).arg(p);
|
||||
return p2p;
|
||||
}
|
||||
|
||||
QString CDBusServer::fixAddressToDBusAddress(const QString &address)
|
||||
{
|
||||
if (address.isEmpty() || address == sessionDBusServer() || address == systemDBusServer()) { return address; }
|
||||
if (address.startsWith("tcp:") || address.startsWith("unix:")) return address;
|
||||
return p2pAddress(address);
|
||||
}
|
||||
|
||||
CDBusServer::ServerMode CDBusServer::addressToDBusMode(const QString &address)
|
||||
{
|
||||
QString a = address.toLower();
|
||||
if (a == CDBusServer::systemDBusServer()) { return SERVERMODE_SYSTEMBUS; }
|
||||
else if (a == CDBusServer::sessionDBusServer()) { return SERVERMODE_SESSIONBUS; }
|
||||
else { return SERVERMODE_P2P; }
|
||||
}
|
||||
|
||||
bool CDBusServer::isDBusAvailable(const QString &address, int port, int timeoutMs)
|
||||
{
|
||||
QString m;
|
||||
return CNetworkUtils::canConnect(address, port, m, timeoutMs);
|
||||
}
|
||||
|
||||
bool CDBusServer::isDBusAvailable(const QString &address, int port, QString &message, int timeoutMs)
|
||||
{
|
||||
return CNetworkUtils::canConnect(address, port, message, timeoutMs);
|
||||
}
|
||||
|
||||
bool CDBusServer::isDBusAvailable(const QString &dbusAddress, QString &message, int timeoutMs)
|
||||
{
|
||||
if (dbusAddress.isEmpty()) { message = "no address"; return false; }
|
||||
if (isP2PAddress(dbusAddress))
|
||||
{
|
||||
QString host;
|
||||
int port = -1;
|
||||
if (splitDBusAddressIntoHostAndPort(dbusAddress, host, port))
|
||||
{
|
||||
return isDBusAvailable(host, port, message, timeoutMs);
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
QDBusConnection connection(
|
||||
(dbusAddress == systemDBusServer()) ?
|
||||
QDBusConnection::systemBus() :
|
||||
QDBusConnection::connectToBus(QDBusConnection::SessionBus, ServiceName())
|
||||
);
|
||||
bool success = connection.isConnected();
|
||||
|
||||
// further checks would need to go here
|
||||
// failing session bus not detected yet
|
||||
|
||||
message = connection.lastError().message();
|
||||
connection.disconnectFromBus(ServiceName());
|
||||
return success;
|
||||
}
|
||||
}
|
||||
|
||||
bool CDBusServer::isDBusAvailable(const QString &dbusAddress, int timeoutMs)
|
||||
{
|
||||
QString m;
|
||||
return isDBusAvailable(dbusAddress, m, timeoutMs);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -1,145 +0,0 @@
|
||||
/* Copyright (C) 2014
|
||||
* 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.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKCORE_DBUSSERVER_H
|
||||
#define BLACKCORE_DBUSSERVER_H
|
||||
|
||||
#include "blackcoreexport.h"
|
||||
#include "blackmisc/valueobject.h" // for qHash overload, include before Qt stuff due GCC issue
|
||||
#include <QObject>
|
||||
#include <QtDBus/QDBusServer>
|
||||
#include <QtDBus/QDBusError>
|
||||
#include <QtDBus/QDBusConnection>
|
||||
#include <QStringList>
|
||||
#include <QMap>
|
||||
|
||||
//! Service name of DBus service
|
||||
#define BLACKCORE_RUNTIME_SERVICENAME "org.swift-project"
|
||||
|
||||
namespace BlackCore
|
||||
{
|
||||
|
||||
/*!
|
||||
* Custom DBusServer
|
||||
* \details This class implements a custom DBusServer for DBus peer connections, but can also be used
|
||||
* with session or system bus. For session / system bus this class represents no real server,
|
||||
* but more a wrapper for \sa QDBusConnection and the registered objects
|
||||
*/
|
||||
class BLACKCORE_EXPORT CDBusServer : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
Q_CLASSINFO("D-Bus Interface", BLACKCORE_RUNTIME_SERVICENAME)
|
||||
|
||||
public:
|
||||
//! Service name of DBus server
|
||||
static const QString &ServiceName();
|
||||
|
||||
//! Server mode, normally P2P, but can be changed for debugging / testing
|
||||
enum ServerMode
|
||||
{
|
||||
SERVERMODE_P2P,
|
||||
SERVERMODE_SESSIONBUS,
|
||||
SERVERMODE_SYSTEMBUS
|
||||
};
|
||||
|
||||
//! Construct a server for the BlackCore runtime
|
||||
//! \remarks We are using address and not ServerMode, as on some systems we need to pass in some specific configuration string
|
||||
//! \sa QDBusServer
|
||||
CDBusServer(const QString &address, QObject *parent = nullptr) : CDBusServer(CDBusServer::ServiceName(), address, parent) {}
|
||||
|
||||
//! Construct a server for some arbitrary service
|
||||
CDBusServer(const QString &service, const QString &address, QObject *parent = nullptr);
|
||||
|
||||
//!! Adds a QObject to be exposed to DBus
|
||||
void addObject(const QString &name, QObject *object);
|
||||
|
||||
//! Last error
|
||||
QDBusError lastQDBusServerError() const;
|
||||
|
||||
//! DBus server (if avaialable)
|
||||
const QDBusServer *qDBusServer() const;
|
||||
|
||||
//! With (P2P) DBus server
|
||||
bool hasQDBusServer() const;
|
||||
|
||||
//! Unregister all objects
|
||||
void unregisterAllObjects();
|
||||
|
||||
//! Default connection
|
||||
static const QDBusConnection &defaultConnection();
|
||||
|
||||
//! Denotes a session DBus server
|
||||
static const QString &sessionDBusServer();
|
||||
|
||||
//! Denotes a session DBus server
|
||||
static const QString &systemDBusServer();
|
||||
|
||||
//! Denotes a P2P DBus server, e.g. "tcp:host=192.168.3.3,port=45000"
|
||||
//! \remarks it is valid to pass only one string as host:port
|
||||
static QString p2pAddress(const QString &host = "127.0.0.1", const QString &port = "");
|
||||
|
||||
//! Turn something like 127.0.0.1:45000 into "tcp:host=127.0.0.1,port=45000"
|
||||
static QString fixAddressToDBusAddress(const QString &address);
|
||||
|
||||
//! address to DBus server mode
|
||||
static ServerMode addressToDBusMode(const QString &address);
|
||||
|
||||
//! Qt DBus address, e.g. "unix:tmpdir=/tmp", "tcp:host=127.0.0.1,port=45000"
|
||||
static bool isQtDBusAddress(const QString &address);
|
||||
|
||||
//! Session or system DBus address
|
||||
static bool isSessionOrSystemAddress(const QString &address);
|
||||
|
||||
//! Check if address means a real server with P2P connection
|
||||
static bool isP2PAddress(const QString &address);
|
||||
|
||||
//! Split DBus address into host and port
|
||||
static bool splitDBusAddressIntoHostAndPort(const QString &dbusAddress, QString &host, int &port);
|
||||
|
||||
//! Is DBus available?
|
||||
static bool isDBusAvailable(const QString &address, int port, int timeoutMs = 1500);
|
||||
|
||||
//! Is DBus available?
|
||||
static bool isDBusAvailable(const QString &address, int port, QString &message, int timeoutMs = 1500);
|
||||
|
||||
//! Is DBus available?
|
||||
static bool isDBusAvailable(const QString &dbusAddress, QString &message, int timeoutMs = 1500);
|
||||
|
||||
//! Is DBus available?
|
||||
static bool isDBusAvailable(const QString &dbusAddress, int timeoutMs = 1500);
|
||||
|
||||
private:
|
||||
ServerMode m_serverMode = SERVERMODE_P2P;
|
||||
QScopedPointer<QDBusServer> m_busServer; //!< QDBusServer implementation
|
||||
QMap<QString, QObject *> m_objects; //!< Mapping of all exposed objects, for P2P registration when connection establishes, also to later unregister objects
|
||||
QMap<QString, QDBusConnection> m_DBusConnections; //!< Mapping of all DBusConnection objects
|
||||
|
||||
//! Manually launch our shipped dbus daemon
|
||||
void launchDbusDaemon();
|
||||
|
||||
//! Get the class info
|
||||
static const QString getClassInfo(QObject *object);
|
||||
|
||||
//! Register options with connection
|
||||
static const QDBusConnection::RegisterOptions &RegisterOptions()
|
||||
{
|
||||
static QDBusConnection::RegisterOptions opt = QDBusConnection::ExportAdaptors | QDBusConnection::ExportAllSignals | QDBusConnection::ExportAllSlots;
|
||||
return opt;
|
||||
}
|
||||
|
||||
private slots:
|
||||
|
||||
//! Called when a new DBus client has connected in P2P mode
|
||||
bool ps_registerObjectsWithP2PConnection(const QDBusConnection &connection);
|
||||
};
|
||||
}
|
||||
|
||||
#endif // guard
|
||||
Reference in New Issue
Block a user