Add ISimulatorListener interface

This commit is contained in:
Michał Garapich
2015-01-10 02:28:01 +01:00
committed by Roland Winklmeier
parent 95829affe4
commit adc623f0e7
4 changed files with 107 additions and 31 deletions

View File

@@ -32,10 +32,8 @@ namespace BlackSimPlugin
CSimulatorCommon(CSimulatorInfo::XP(), ownAircraftProvider, remoteAircraftProvider, parent)
{
m_watcher = new QDBusServiceWatcher(this);
m_watcher->setWatchMode(QDBusServiceWatcher::WatchForRegistration | QDBusServiceWatcher::WatchForUnregistration);
m_watcher->addWatchedService(CXBusServiceProxy::InterfaceName());
m_watcher->addWatchedService(CXBusTrafficProxy::InterfaceName());
connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, this, &CSimulatorXPlane::ps_serviceRegistered);
m_watcher->setWatchMode(QDBusServiceWatcher::WatchForUnregistration);
m_watcher->addWatchedService("net.vatsim.xbus");
connect(m_watcher, &QDBusServiceWatcher::serviceUnregistered, this, &CSimulatorXPlane::ps_serviceUnregistered);
m_fastTimer = new QTimer(this);
@@ -167,7 +165,7 @@ namespace BlackSimPlugin
connect(m_service, &CXBusServiceProxy::airportsInRangeUpdated, this, &CSimulatorXPlane::ps_setAirportsInRange);
m_service->updateAirportsInRange();
m_watcher->setConnection(m_conn);
emit connectionStatusChanged(ISimulator::Connected);
emitSimulatorCombinedStatus();
return true;
}
else
@@ -189,45 +187,26 @@ namespace BlackSimPlugin
{
m_traffic->cleanup();
}
emit connectionStatusChanged(ISimulator::Disconnected);
m_conn = QDBusConnection { "default" };
m_watcher->setConnection(m_conn);
delete m_service;
delete m_traffic;
m_service = nullptr;
m_traffic = nullptr;
emitSimulatorCombinedStatus();
return true;
}
void CSimulatorXPlane::ps_serviceRegistered(const QString &serviceName)
{
if (serviceName == CXBusServiceProxy::InterfaceName())
{
delete m_service;
m_service = new CXBusServiceProxy(m_conn, this);
// FIXME duplication
connect(m_service, &CXBusServiceProxy::aircraftModelChanged, this, &CSimulatorXPlane::ps_emitOwnAircraftModelChanged);
connect(m_service, &CXBusServiceProxy::airportsInRangeUpdated, this, &CSimulatorXPlane::ps_setAirportsInRange);
m_service->updateAirportsInRange();
}
else if (serviceName == CXBusTrafficProxy::InterfaceName())
{
delete m_traffic;
m_traffic = new CXBusTrafficProxy(m_conn, this);
}
if (m_service && m_traffic)
{
emit connectionStatusChanged(ISimulator::Connected);
}
}
void CSimulatorXPlane::ps_serviceUnregistered()
{
m_conn = QDBusConnection { "default" };
m_watcher->setConnection(m_conn);
delete m_service;
delete m_traffic;
m_service = nullptr;
m_traffic = nullptr;
emit connectionStatusChanged(ISimulator::Disconnected);
emitSimulatorCombinedStatus();
}
void CSimulatorXPlane::ps_emitOwnAircraftModelChanged(const QString &path, const QString &filename, const QString &livery, const QString &icao)
@@ -435,5 +414,35 @@ namespace BlackSimPlugin
return new CSimulatorXPlane(ownAircraftProvider, renderedAircraftProvider, parent);
}
CSimulatorXPlaneListener::CSimulatorXPlaneListener(QObject* parent): ISimulatorListener(parent)
{
}
void CSimulatorXPlaneListener::start()
{
if (m_watcher) // already started
return;
m_conn = QDBusConnection::sessionBus(); // TODO make this configurable
m_watcher = new QDBusServiceWatcher("net.vatsim.xbus", m_conn, QDBusServiceWatcher::WatchForRegistration, this);
connect(m_watcher, &QDBusServiceWatcher::serviceRegistered, this, &CSimulatorXPlaneListener::ps_serviceRegistered);
}
void CSimulatorXPlaneListener::stop()
{
if (m_watcher) {
m_watcher->deleteLater();
m_watcher = nullptr;
}
}
void CSimulatorXPlaneListener::ps_serviceRegistered(const QString &serviceName)
{
if (serviceName == "net.vatsim.xbus")
emit simulatorStarted(m_simulatorInfo);
}
} // namespace
} // namespace

View File

@@ -113,7 +113,6 @@ namespace BlackSimPlugin
virtual bool isRenderedAircraft(const BlackMisc::Aviation::CCallsign &callsign) const override;
private slots:
void ps_serviceRegistered(const QString &serviceName);
void ps_serviceUnregistered();
void ps_setAirportsInRange(const QStringList &icaoCodes, const QStringList &names, const BlackMisc::CSequence<double> &lats, const BlackMisc::CSequence<double> &lons, const BlackMisc::CSequence<double> &alts);
void ps_emitOwnAircraftModelChanged(const QString &path, const QString &filename, const QString &livery, const QString &icao);
@@ -172,6 +171,31 @@ namespace BlackSimPlugin
}
};
//! Listener waits for xbus service to show up
class CSimulatorXPlaneListener : public BlackCore::ISimulatorListener
{
Q_OBJECT
public:
//! Constructor
CSimulatorXPlaneListener(QObject* parent);
//! \copydoc BlackCore::ISimulatorListener::start
virtual void start() override;
//! \copydoc BlackCore::ISimulatorListener::stop
virtual void stop() override;
private slots:
void ps_serviceRegistered(const QString &serviceName);
private:
QDBusConnection m_conn { "default" };
QDBusServiceWatcher* m_watcher { nullptr };
const BlackSim::CSimulatorInfo m_simulatorInfo = BlackSim::CSimulatorInfo::XP();
};
//! Factory for creating CSimulatorXPlane instance
class CSimulatorXPlaneFactory : public QObject, public BlackCore::ISimulatorFactory
@@ -189,6 +213,9 @@ namespace BlackSimPlugin
//! \copydoc BlackCore::ISimulatorFactory::getSimulatorInfo
virtual BlackSim::CSimulatorInfo getSimulatorInfo() const override { return BlackSim::CSimulatorInfo::XP(); }
//! \copydoc BlackCore::ISimulatorFactory::getListener
virtual BlackCore::ISimulatorListener *createListener(QObject *parent = nullptr) override { return new CSimulatorXPlaneListener(parent); }
};
}