diff --git a/src/xbus/plugin.cpp b/src/xbus/plugin.cpp index ee97e812e..851d0130c 100644 --- a/src/xbus/plugin.cpp +++ b/src/xbus/plugin.cpp @@ -13,6 +13,9 @@ #include "weather.h" #include "utils.h" #include "blackmisc/librarypath.h" +#include "blackmisc/logmessage.h" +#include +#include 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); diff --git a/src/xbus/plugin.h b/src/xbus/plugin.h index 79898a99f..117846898 100644 --- a/src/xbus/plugin.h +++ b/src/xbus/plugin.h @@ -61,6 +61,7 @@ namespace XBus QVector m_startServerMenuItems; void startServer(const QString &address); + void tryStartServer(const QString &address); }; }