mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-15 01:05:35 +08:00
Refactor listening for and connecting to XSwiftBus
- Use common function for Session and P2P connections - Don't use the DBus service watcher as it did not allow to test in regular intervals - Add a XSwiftBus version check to X-Plane listener - Try to initialize and acquire multiplayer planes in checkConnection() already and warn the user if any of those failed. The plugin will be loaded only if everything succeeded.
This commit is contained in:
committed by
Klaus Basan
parent
49bd38d722
commit
db7ed76bc5
@@ -336,36 +336,32 @@ namespace BlackSimPlugin
|
|||||||
m_trafficProxy = new CXSwiftBusTrafficProxy(m_dBusConnection, this);
|
m_trafficProxy = new CXSwiftBusTrafficProxy(m_dBusConnection, this);
|
||||||
m_weatherProxy = new CXSwiftBusWeatherProxy(m_dBusConnection, this);
|
m_weatherProxy = new CXSwiftBusWeatherProxy(m_dBusConnection, this);
|
||||||
|
|
||||||
bool ok = false;
|
|
||||||
bool s = m_dBusConnection.connect(QString(), DBUS_PATH_LOCAL, DBUS_INTERFACE_LOCAL,
|
bool s = m_dBusConnection.connect(QString(), DBUS_PATH_LOCAL, DBUS_INTERFACE_LOCAL,
|
||||||
"Disconnected", this, SLOT(serviceUnregistered()));
|
"Disconnected", this, SLOT(serviceUnregistered()));
|
||||||
Q_ASSERT(s);
|
Q_ASSERT(s);
|
||||||
|
if (!m_serviceProxy->isValid() || !m_trafficProxy->isValid() || !m_weatherProxy->isValid())
|
||||||
if (m_serviceProxy->isValid() && m_trafficProxy->isValid() && m_weatherProxy->isValid() && m_trafficProxy->initialize())
|
|
||||||
{
|
|
||||||
emitOwnAircraftModelChanged(m_serviceProxy->getAircraftModelPath(), m_serviceProxy->getAircraftModelFilename(), m_serviceProxy->getAircraftLivery(),
|
|
||||||
m_serviceProxy->getAircraftIcaoCode(), m_serviceProxy->getAircraftModelString(), m_serviceProxy->getAircraftName(), m_serviceProxy->getAircraftDescription());
|
|
||||||
QString xplaneVersion = QString("%1.%2").arg(m_serviceProxy->getXPlaneVersionMajor()).arg(m_serviceProxy->getXPlaneVersionMinor());
|
|
||||||
setSimulatorDetails("X-Plane", {}, xplaneVersion);
|
|
||||||
connect(m_serviceProxy, &CXSwiftBusServiceProxy::aircraftModelChanged, this, &CSimulatorXPlane::emitOwnAircraftModelChanged);
|
|
||||||
connect(m_serviceProxy, &CXSwiftBusServiceProxy::airportsInRangeUpdated, this, &CSimulatorXPlane::setAirportsInRange);
|
|
||||||
m_serviceProxy->updateAirportsInRange();
|
|
||||||
connect(m_trafficProxy, &CXSwiftBusTrafficProxy::simFrame, this, &CSimulatorXPlane::updateRemoteAircraft);
|
|
||||||
connect(m_trafficProxy, &CXSwiftBusTrafficProxy::remoteAircraftAdded, this, &CSimulatorXPlane::onRemoteAircraftAdded);
|
|
||||||
connect(m_trafficProxy, &CXSwiftBusTrafficProxy::remoteAircraftAddingFailed, this, &CSimulatorXPlane::onRemoteAircraftAddingFailed);
|
|
||||||
if (m_watcher) { m_watcher->setConnection(m_dBusConnection); }
|
|
||||||
m_trafficProxy->removeAllPlanes();
|
|
||||||
this->loadCslPackages();
|
|
||||||
this->emitSimulatorCombinedStatus();
|
|
||||||
ok = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
this->disconnectFrom();
|
this->disconnectFrom();
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ok) { this->initSimulatorInternals(); }
|
emitOwnAircraftModelChanged(m_serviceProxy->getAircraftModelPath(), m_serviceProxy->getAircraftModelFilename(), m_serviceProxy->getAircraftLivery(),
|
||||||
return ok;
|
m_serviceProxy->getAircraftIcaoCode(), m_serviceProxy->getAircraftModelString(), m_serviceProxy->getAircraftName(), m_serviceProxy->getAircraftDescription());
|
||||||
|
QString xplaneVersion = QString("%1.%2").arg(m_serviceProxy->getXPlaneVersionMajor()).arg(m_serviceProxy->getXPlaneVersionMinor());
|
||||||
|
setSimulatorDetails("X-Plane", {}, xplaneVersion);
|
||||||
|
connect(m_serviceProxy, &CXSwiftBusServiceProxy::aircraftModelChanged, this, &CSimulatorXPlane::emitOwnAircraftModelChanged);
|
||||||
|
connect(m_serviceProxy, &CXSwiftBusServiceProxy::airportsInRangeUpdated, this, &CSimulatorXPlane::setAirportsInRange);
|
||||||
|
m_serviceProxy->updateAirportsInRange();
|
||||||
|
connect(m_trafficProxy, &CXSwiftBusTrafficProxy::simFrame, this, &CSimulatorXPlane::updateRemoteAircraft);
|
||||||
|
connect(m_trafficProxy, &CXSwiftBusTrafficProxy::remoteAircraftAdded, this, &CSimulatorXPlane::onRemoteAircraftAdded);
|
||||||
|
connect(m_trafficProxy, &CXSwiftBusTrafficProxy::remoteAircraftAddingFailed, this, &CSimulatorXPlane::onRemoteAircraftAddingFailed);
|
||||||
|
if (m_watcher) { m_watcher->setConnection(m_dBusConnection); }
|
||||||
|
m_trafficProxy->removeAllPlanes();
|
||||||
|
this->loadCslPackages();
|
||||||
|
this->emitSimulatorCombinedStatus();
|
||||||
|
|
||||||
|
this->initSimulatorInternals();
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CSimulatorXPlane::disconnectFrom()
|
bool CSimulatorXPlane::disconnectFrom()
|
||||||
@@ -1134,35 +1130,16 @@ namespace BlackSimPlugin
|
|||||||
constexpr int QueryInterval = 5 * 1000; // 5 seconds
|
constexpr int QueryInterval = 5 * 1000; // 5 seconds
|
||||||
m_timer.setInterval(QueryInterval);
|
m_timer.setInterval(QueryInterval);
|
||||||
m_timer.setObjectName(this->objectName().append(":m_timer"));
|
m_timer.setObjectName(this->objectName().append(":m_timer"));
|
||||||
connect(&m_timer, &QTimer::timeout, this, &CSimulatorXPlaneListener::checkConnectionViaPeer);
|
connect(&m_timer, &QTimer::timeout, this, &CSimulatorXPlaneListener::checkConnection);
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSimulatorXPlaneListener::startImpl()
|
void CSimulatorXPlaneListener::startImpl()
|
||||||
{
|
{
|
||||||
QString dbusAddress = m_xswiftbusServerSetting.getThreadLocal();
|
m_timer.start();
|
||||||
|
|
||||||
if (CDBusServer::isSessionOrSystemAddress(dbusAddress))
|
|
||||||
{
|
|
||||||
checkConnectionViaBus(dbusAddress);
|
|
||||||
}
|
|
||||||
else if (CDBusServer::isQtDBusAddress(dbusAddress))
|
|
||||||
{
|
|
||||||
m_timer.start();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CLogMessage(this).warning("Invalid DBus address. Not listening for X-Plane connections!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSimulatorXPlaneListener::stopImpl()
|
void CSimulatorXPlaneListener::stopImpl()
|
||||||
{
|
{
|
||||||
if (m_watcher)
|
|
||||||
{
|
|
||||||
delete m_watcher;
|
|
||||||
m_watcher = nullptr;
|
|
||||||
}
|
|
||||||
m_timer.stop();
|
m_timer.stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1176,56 +1153,84 @@ namespace BlackSimPlugin
|
|||||||
QTimer::singleShot(0, this, [ = ]
|
QTimer::singleShot(0, this, [ = ]
|
||||||
{
|
{
|
||||||
if (!myself) { return; }
|
if (!myself) { return; }
|
||||||
this->checkConnectionViaPeer();
|
checkConnection();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CSimulatorXPlaneListener::checkConnection()
|
||||||
|
{
|
||||||
|
if (this->isShuttingDown()) { return; }
|
||||||
|
Q_ASSERT_X(!CThreadUtils::isCurrentThreadApplicationThread(), Q_FUNC_INFO, "Expect to run in background");
|
||||||
|
|
||||||
|
QString dbusAddress = m_xswiftbusServerSetting.getThreadLocal();
|
||||||
|
if (CDBusServer::isSessionOrSystemAddress(dbusAddress))
|
||||||
|
{
|
||||||
|
checkConnectionViaBus(dbusAddress);
|
||||||
|
}
|
||||||
|
else if (CDBusServer::isQtDBusAddress(dbusAddress))
|
||||||
|
{
|
||||||
|
checkConnectionViaPeer(dbusAddress);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void CSimulatorXPlaneListener::checkConnectionViaBus(const QString &address)
|
void CSimulatorXPlaneListener::checkConnectionViaBus(const QString &address)
|
||||||
{
|
{
|
||||||
if (m_watcher) { return; }
|
|
||||||
m_conn = CSimulatorXPlane::connectionFromString(address);
|
m_conn = CSimulatorXPlane::connectionFromString(address);
|
||||||
CXSwiftBusServiceProxy *service = new CXSwiftBusServiceProxy(m_conn);
|
if (!m_conn.isConnected())
|
||||||
CXSwiftBusTrafficProxy *traffic = new CXSwiftBusTrafficProxy(m_conn);
|
|
||||||
|
|
||||||
bool result = service->isValid() && traffic->isValid();
|
|
||||||
|
|
||||||
service->deleteLater();
|
|
||||||
traffic->deleteLater();
|
|
||||||
|
|
||||||
if (result)
|
|
||||||
{
|
{
|
||||||
emit simulatorStarted(getPluginInfo());
|
|
||||||
m_conn.disconnectFromBus(m_conn.name());
|
m_conn.disconnectFromBus(m_conn.name());
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else
|
checkConnectionCommon();
|
||||||
{
|
m_conn.disconnectFromBus(m_conn.name());
|
||||||
m_watcher = new QDBusServiceWatcher(xswiftbusServiceName(), m_conn, QDBusServiceWatcher::WatchForRegistration, this);
|
|
||||||
connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, this, &CSimulatorXPlaneListener::serviceRegistered);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void CSimulatorXPlaneListener::checkConnectionViaPeer()
|
void CSimulatorXPlaneListener::checkConnectionViaPeer(const QString &address)
|
||||||
{
|
{
|
||||||
m_conn = QDBusConnection::connectToPeer(m_xswiftbusServerSetting.getThreadLocal(), "xswiftbus");
|
m_conn = QDBusConnection::connectToPeer(address, "xswiftbus");
|
||||||
if (!m_conn.isConnected())
|
if (!m_conn.isConnected())
|
||||||
{
|
{
|
||||||
// This is required to cleanup the connection in QtDBus
|
// This is required to cleanup the connection in QtDBus
|
||||||
m_conn.disconnectFromPeer(m_conn.name());
|
m_conn.disconnectFromPeer(m_conn.name());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
checkConnectionCommon();
|
||||||
CXSwiftBusServiceProxy *service = new CXSwiftBusServiceProxy(m_conn);
|
|
||||||
CXSwiftBusTrafficProxy *traffic = new CXSwiftBusTrafficProxy(m_conn);
|
|
||||||
|
|
||||||
bool result = service->isValid() && traffic->isValid();
|
|
||||||
|
|
||||||
service->deleteLater();
|
|
||||||
traffic->deleteLater();
|
|
||||||
|
|
||||||
if (result) { emit simulatorStarted(getPluginInfo()); }
|
|
||||||
m_conn.disconnectFromPeer(m_conn.name());
|
m_conn.disconnectFromPeer(m_conn.name());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CSimulatorXPlaneListener::checkConnectionCommon()
|
||||||
|
{
|
||||||
|
CXSwiftBusServiceProxy service(m_conn);
|
||||||
|
CXSwiftBusTrafficProxy traffic(m_conn);
|
||||||
|
CXSwiftBusWeatherProxy weather(m_conn);
|
||||||
|
|
||||||
|
bool result = service.isValid() && traffic.isValid() && weather.isValid();
|
||||||
|
if (! result) { return; }
|
||||||
|
|
||||||
|
QString swiftVersion = BlackConfig::CBuildConfig::getVersionString();
|
||||||
|
QString xswiftbusVersion = service.getVersionNumber();
|
||||||
|
if (! swiftVersion.contains(xswiftbusVersion))
|
||||||
|
{
|
||||||
|
CLogMessage(this).error("You are using an incorrect version of XSwiftBus. Make sure to install %1 into X-Plane plugins!") << xswiftbusVersion;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(! traffic.initialize())
|
||||||
|
{
|
||||||
|
CLogMessage(this).error("Connection to XSwiftBus successful, but could not initialize XSwiftBus. Check X-Plane Log.txt.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
MultiplayerAcquireInfo info = traffic.acquireMultiplayerPlanes();
|
||||||
|
if (! info.hasAcquired)
|
||||||
|
{
|
||||||
|
CLogMessage(this).error("Connection to XSwiftBus successful, but could not acquire multiplayer planes. %1 has acquired them already. Disable %2 or remove it if not required and reload XSwiftBus.") << info.owner << info.owner;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
emit simulatorStarted(getPluginInfo());
|
||||||
|
}
|
||||||
|
|
||||||
void CSimulatorXPlaneListener::serviceRegistered(const QString &serviceName)
|
void CSimulatorXPlaneListener::serviceRegistered(const QString &serviceName)
|
||||||
{
|
{
|
||||||
if (serviceName == xswiftbusServiceName())
|
if (serviceName == xswiftbusServiceName())
|
||||||
@@ -1237,12 +1242,8 @@ namespace BlackSimPlugin
|
|||||||
|
|
||||||
void CSimulatorXPlaneListener::xSwiftBusServerSettingChanged()
|
void CSimulatorXPlaneListener::xSwiftBusServerSettingChanged()
|
||||||
{
|
{
|
||||||
// user changed settings, restart the listener
|
this->stop();
|
||||||
if (m_watcher)
|
this->start();
|
||||||
{
|
|
||||||
this->stop();
|
|
||||||
this->start();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -277,18 +277,16 @@ namespace BlackSimPlugin
|
|||||||
virtual void checkImpl() override;
|
virtual void checkImpl() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//! Check if XSwiftBus service is already registered on the bus
|
void checkConnection();
|
||||||
void checkConnectionViaBus(const QString &address);
|
void checkConnectionViaBus(const QString &address);
|
||||||
|
void checkConnectionViaPeer(const QString &address);
|
||||||
//! Check if XSwiftBus service is available via P2P address
|
void checkConnectionCommon();
|
||||||
void checkConnectionViaPeer();
|
|
||||||
|
|
||||||
void serviceRegistered(const QString &serviceName);
|
void serviceRegistered(const QString &serviceName);
|
||||||
void xSwiftBusServerSettingChanged();
|
void xSwiftBusServerSettingChanged();
|
||||||
|
|
||||||
QTimer m_timer { this };
|
QTimer m_timer { this };
|
||||||
QDBusConnection m_conn { "default" };
|
QDBusConnection m_conn { "default" };
|
||||||
QDBusServiceWatcher *m_watcher { nullptr };
|
|
||||||
BlackMisc::CSettingReadOnly<BlackMisc::Simulation::Settings::TXSwiftBusServer> m_xswiftbusServerSetting { this, &CSimulatorXPlaneListener::xSwiftBusServerSettingChanged };
|
BlackMisc::CSettingReadOnly<BlackMisc::Simulation::Settings::TXSwiftBusServer> m_xswiftbusServerSetting { this, &CSimulatorXPlaneListener::xSwiftBusServerSettingChanged };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user