Ref T167, allow to re-init facade

(a first approach to reconnect with DBus core)

* Changes are supposed to be backward compliant. Maybe re-init fails, but there should be no regression for the 1st init
* keep config as member to allow restart
* disconnect from DBus before connecting (allows reconnect)
* delete contexts if they are re-created
* function CCoreFacade::tryToReconnectWithDBus
This commit is contained in:
Klaus Basan
2017-09-28 21:08:28 +02:00
committed by Mathew Sutcliffe
parent 9748b5a442
commit ed51d3c5dd
2 changed files with 41 additions and 21 deletions

View File

@@ -48,12 +48,22 @@ using namespace BlackCore::Context;
namespace BlackCore namespace BlackCore
{ {
CCoreFacade::CCoreFacade(const CCoreFacadeConfig &config, QObject *parent) : QObject(parent) CCoreFacade::CCoreFacade(const CCoreFacadeConfig &config, QObject *parent) :
QObject(parent), m_config(config)
{ {
this->init(config); this->init();
} }
void CCoreFacade::init(const CCoreFacadeConfig &config) bool CCoreFacade::tryToReconnectWithDBus()
{
if (m_shuttingDown) { return false; }
if (!m_config.requiresDBusConnection()) { return false; }
m_initalized = false;
this->init();
return true;
}
void CCoreFacade::init()
{ {
if (m_initalized || m_shuttingDown) { return; } if (m_initalized || m_shuttingDown) { return; }
@@ -63,9 +73,9 @@ namespace BlackCore
// either use explicit setting or last value // either use explicit setting or last value
QString dbusAddress; QString dbusAddress;
if (config.hasDBusAddress()) if (m_config.hasDBusAddress())
{ {
dbusAddress = config.getDBusAddress(); dbusAddress = m_config.getDBusAddress();
m_launcherSetup.setProperty(CLauncherSetup::IndexDBusAddress, dbusAddress); m_launcherSetup.setProperty(CLauncherSetup::IndexDBusAddress, dbusAddress);
} }
else else
@@ -75,8 +85,8 @@ namespace BlackCore
} }
// DBus // DBus
if (config.requiresDBusSever()) { this->initDBusServer(dbusAddress); } if (m_config.requiresDBusSever()) { this->initDBusServer(dbusAddress); }
if (config.requiresDBusConnection()) if (m_config.requiresDBusConnection())
{ {
this->initDBusConnection(dbusAddress); this->initDBusConnection(dbusAddress);
if (!m_dbusConnection.isConnected()) if (!m_dbusConnection.isConnected())
@@ -91,19 +101,24 @@ namespace BlackCore
times.insert("DBus", time.restart()); times.insert("DBus", time.restart());
// contexts // contexts
m_contextApplication = IContextApplication::create(this, config.getModeApplication(), m_dbusServer, m_dbusConnection); if (m_contextApplication) { m_contextApplication->deleteLater(); }
m_contextApplication = IContextApplication::create(this, m_config.getModeApplication(), m_dbusServer, m_dbusConnection);
times.insert("Application", time.restart()); times.insert("Application", time.restart());
m_contextAudio = IContextAudio::create(this, config.getModeAudio(), m_dbusServer, m_dbusConnection); if (m_contextAudio) { m_contextAudio->deleteLater(); }
m_contextAudio = IContextAudio::create(this, m_config.getModeAudio(), m_dbusServer, m_dbusConnection);
times.insert("Audio", time.restart()); times.insert("Audio", time.restart());
m_contextOwnAircraft = IContextOwnAircraft::create(this, config.getModeOwnAircraft(), m_dbusServer, m_dbusConnection); if (m_contextOwnAircraft) { m_contextOwnAircraft->deleteLater(); }
m_contextOwnAircraft = IContextOwnAircraft::create(this, m_config.getModeOwnAircraft(), m_dbusServer, m_dbusConnection);
times.insert("Own aircraft", time.restart()); times.insert("Own aircraft", time.restart());
m_contextNetwork = IContextNetwork::create(this, config.getModeNetwork(), m_dbusServer, m_dbusConnection); if (m_contextNetwork) { m_contextNetwork->deleteLater(); }
m_contextNetwork = IContextNetwork::create(this, m_config.getModeNetwork(), m_dbusServer, m_dbusConnection);
times.insert("Network", time.restart()); times.insert("Network", time.restart());
m_contextSimulator = IContextSimulator::create(this, config.getModeSimulator(), m_dbusServer, m_dbusConnection); if (m_contextSimulator) { m_contextSimulator->deleteLater(); }
m_contextSimulator = IContextSimulator::create(this, m_config.getModeSimulator(), m_dbusServer, m_dbusConnection);
times.insert("Simulator", time.restart()); times.insert("Simulator", time.restart());
// checks -------------- // checks --------------
@@ -148,8 +163,8 @@ namespace BlackCore
void CCoreFacade::initDBusServer(const QString &dBusAddress) void CCoreFacade::initDBusServer(const QString &dBusAddress)
{ {
if (m_dbusServer) { return; }
Q_ASSERT(!dBusAddress.isEmpty()); Q_ASSERT(!dBusAddress.isEmpty());
if (m_dbusServer) { m_dbusServer->deleteLater(); } // delete if there was an existing one
m_dbusServer = new CDBusServer(dBusAddress, this); m_dbusServer = new CDBusServer(dBusAddress, this);
CLogMessage(this).info("DBus server on address: %1") << dBusAddress; CLogMessage(this).info("DBus server on address: %1") << dBusAddress;
} }
@@ -317,15 +332,19 @@ namespace BlackCore
if (m_initDBusConnection) { return; } if (m_initDBusConnection) { return; }
if (address.isEmpty() || address == CDBusServer::sessionBusAddress()) if (address.isEmpty() || address == CDBusServer::sessionBusAddress())
{ {
QDBusConnection::disconnectFromBus(m_dbusConnection.name());
m_dbusConnection = QDBusConnection::sessionBus(); m_dbusConnection = QDBusConnection::sessionBus();
} }
else if (address == CDBusServer::systemBusAddress()) else if (address == CDBusServer::systemBusAddress())
{ {
m_dbusConnection = QDBusConnection::sessionBus(); QDBusConnection::disconnectFromBus(m_dbusConnection.name());
m_dbusConnection = QDBusConnection::systemBus();
} }
else else
{ {
m_dbusConnection = QDBusConnection::connectToPeer(address, "BlackBoxRuntime"); const QString name("BlackBoxRuntime");
QDBusConnection::disconnectFromPeer(name);
m_dbusConnection = QDBusConnection::connectToPeer(address, name);
} }
} }

View File

@@ -12,6 +12,7 @@
#ifndef BLACKCORE_COREFACADE_H #ifndef BLACKCORE_COREFACADE_H
#define BLACKCORE_COREFACADE_H #define BLACKCORE_COREFACADE_H
#include "corefacadeconfig.h"
#include "blackcore/blackcoreexport.h" #include "blackcore/blackcoreexport.h"
#include "blackcore/data/launchersetup.h" #include "blackcore/data/launchersetup.h"
#include "blackcore/vatsim/vatsimsettings.h" #include "blackcore/vatsim/vatsimsettings.h"
@@ -24,12 +25,8 @@
#include <QString> #include <QString>
namespace BlackMisc { class CDBusServer; } namespace BlackMisc { class CDBusServer; }
namespace BlackCore namespace BlackCore
{ {
// forward declaration, see review
// https://dev.vatsim-germany.org/boards/22/topics/1350?r=1359#message-1359
class CCoreFacadeConfig;
namespace Context namespace Context
{ {
class CContextApplication; class CContextApplication;
@@ -66,6 +63,9 @@ namespace BlackCore
//! DBus server (if applicable) //! DBus server (if applicable)
const BlackMisc::CDBusServer *getDBusServer() const { return this->m_dbusServer; } const BlackMisc::CDBusServer *getDBusServer() const { return this->m_dbusServer; }
//! In case connection between DBus parties is lost, try to reconnect
bool tryToReconnectWithDBus();
//! DBus connection (if applicable) //! DBus connection (if applicable)
const QDBusConnection &getDBusConnection() const { return this->m_dbusConnection; } const QDBusConnection &getDBusConnection() const { return this->m_dbusConnection; }
@@ -150,7 +150,7 @@ namespace BlackCore
const Context::CContextSimulator *getCContextSimulator() const; const Context::CContextSimulator *getCContextSimulator() const;
//! Init //! Init
void init(const CCoreFacadeConfig &config); void init();
//! Remote application context, indicates distributed environment //! Remote application context, indicates distributed environment
bool hasRemoteApplicationContext() const; bool hasRemoteApplicationContext() const;
@@ -165,7 +165,8 @@ namespace BlackCore
private: private:
bool m_initalized = false; //!< flag if already initialized bool m_initalized = false; //!< flag if already initialized
bool m_shuttingDown = false; //!< flag if shutting down bool m_shuttingDown = false; //!< flag if shutting down
BlackMisc::CData<Data::TLauncherSetup> m_launcherSetup { this }; const CCoreFacadeConfig m_config; //!< used config
BlackMisc::CData<Data::TLauncherSetup> m_launcherSetup { this }; //!< updating DBus
// DBus // DBus
BlackMisc::CDBusServer *m_dbusServer = nullptr; BlackMisc::CDBusServer *m_dbusServer = nullptr;