Let xbus retry to connect to dbus in case it is not available

refs #615
This commit is contained in:
Roland Winklmeier
2016-04-25 13:26:47 +02:00
parent 21fe8d46ea
commit 25482f5d35
2 changed files with 26 additions and 4 deletions

View File

@@ -13,6 +13,9 @@
#include "weather.h"
#include "utils.h"
#include "blackmisc/librarypath.h"
#include "blackmisc/logmessage.h"
#include <QTimer>
#include <functional>
namespace {
inline QString xbusServiceName() {
@@ -33,21 +36,39 @@ namespace XBus
void CPlugin::startServer(const QString &address)
{
Q_ASSERT(! m_server);
for (auto &item : m_startServerMenuItems) { item.setEnabled(false); }
m_service = new CService(this);
m_traffic = new CTraffic(this);
m_weather = new CWeather(this);
// XPLM API does not like to be called from a QTimer slot, so move the recurring part into a separate method.
tryStartServer(address);
}
void CPlugin::tryStartServer(const QString &address)
{
// Make sure that there are no calls to XPLM in this method
Q_ASSERT(! m_server);
auto previousLibraryPath = BlackMisc::getCustomLibraryPath();
auto libraryPath = g_xplanePath + "Resources" + g_sep + "plugins" + g_sep + "xbus";
#if !defined (Q_OS_MAC) && defined(WORD_SIZE_64)
libraryPath = libraryPath + g_sep + "64";
#endif
BlackMisc::setCustomLibraryPath(libraryPath);
if (!BlackMisc::CDBusServer::isP2PAddress(address) && !BlackMisc::CDBusServer::isDBusAvailable(address))
{
constexpr int msec = 30000;
BlackMisc::CLogMessage(this).warning("DBus daemon not available. Trying again in %1 sec.") << msec / 1000;
QTimer::singleShot(msec, this, [&] { tryStartServer(address); });
BlackMisc::setCustomLibraryPath(previousLibraryPath);
return;
}
m_server = new BlackMisc::CDBusServer(xbusServiceName(), address, this);
BlackMisc::setCustomLibraryPath(previousLibraryPath);
m_service = new CService(this);
m_traffic = new CTraffic(this);
m_weather = new CWeather(this);
m_server->addObject(CService::ObjectPath(), m_service);
m_server->addObject(CTraffic::ObjectPath(), m_traffic);
m_server->addObject(CWeather::ObjectPath(), m_weather);

View File

@@ -61,6 +61,7 @@ namespace XBus
QVector<CMenuItem> m_startServerMenuItems;
void startServer(const QString &address);
void tryStartServer(const QString &address);
};
}