Launch shipped dbus-daemon if first connection attempt was unsuccessful

This commit is contained in:
Roland Winklmeier
2015-10-25 23:30:17 +01:00
committed by Mathew Sutcliffe
parent c14c25d278
commit 6ff9df5c05
3 changed files with 104 additions and 1 deletions

View File

@@ -10,7 +10,7 @@
#include "blackmisc/logmessage.h"
#include "blackmisc/network/networkutils.h"
#include "dbus_server.h"
#include <QDebug>
#include <QProcess>
#include <QMetaClassInfo>
using namespace BlackMisc;
@@ -35,6 +35,12 @@ namespace BlackCore
// 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
@@ -48,6 +54,12 @@ namespace BlackCore
// 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
@@ -91,6 +103,14 @@ namespace BlackCore
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!"); }
}
/*
* Check for P2P address
*/

View File

@@ -113,6 +113,9 @@ namespace BlackCore
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();
//! Check if address means a real server with P2P connection
static bool isP2P(const QString &address);