From adc623f0e7e866eded156d44e12a5b847bcb6407 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Garapich?= Date: Sat, 10 Jan 2015 02:28:01 +0100 Subject: [PATCH] Add ISimulatorListener interface --- src/blackcore/simulator.cpp | 5 ++ src/blackcore/simulator.h | 37 +++++++++- .../simulator/xplane/simulator_xplane.cpp | 67 +++++++++++-------- .../simulator/xplane/simulator_xplane.h | 29 +++++++- 4 files changed, 107 insertions(+), 31 deletions(-) diff --git a/src/blackcore/simulator.cpp b/src/blackcore/simulator.cpp index 879aefe0e..7779d6d78 100644 --- a/src/blackcore/simulator.cpp +++ b/src/blackcore/simulator.cpp @@ -23,6 +23,10 @@ namespace BlackCore { emit simulatorStatusChanged(isConnected(), isSimulating(), isPaused()); } + + ISimulatorListener::ISimulatorListener(QObject* parent) : QObject(parent) + { + } CSimulatorCommon::CSimulatorCommon(const BlackSim::CSimulatorInfo &simInfo, BlackMisc::Simulation::IOwnAircraftProvider *ownAircraftProvider, BlackMisc::Simulation::IRemoteAircraftProvider *remoteAircraftProvider, QObject *parent) : ISimulator(parent), @@ -196,6 +200,7 @@ namespace BlackCore { this->m_debugMessages = driver; Q_UNUSED(interpolator); + } int CSimulatorCommon::getInstalledModelsCount() const diff --git a/src/blackcore/simulator.h b/src/blackcore/simulator.h index 5b641e9be..3e4e3bf7b 100644 --- a/src/blackcore/simulator.h +++ b/src/blackcore/simulator.h @@ -192,7 +192,7 @@ namespace BlackCore //! Simulator started void simulatorStarted(); - //! Simulator stopped; + //! Simulator stopped void simulatorStopped(); //! A single model has been matched @@ -210,6 +210,38 @@ namespace BlackCore void emitSimulatorCombinedStatus(); }; + + /*! + * Interface to a simulator listener. + * + * The simulator listener is responsible for letting the core know when + * the corresponding simulator is started. + */ + class ISimulatorListener : public QObject + { + Q_OBJECT + + public: + + //! Constructor + //! \sa ISimulatorFactory::createListener(). + ISimulatorListener(QObject* parent); + + //! Destructor + virtual ~ISimulatorListener() = default; + + //! Start listening for the simulator to start. + virtual void start() = 0; + + //! Stops listening. + virtual void stop() = 0; + + signals: + + //! Emitted when the listener discovers the simulator running. + void simulatorStarted(BlackSim::CSimulatorInfo simulatorInfo); + + }; //! Factory pattern class to create instances of ISimulator class ISimulatorFactory @@ -233,6 +265,9 @@ namespace BlackCore //! Simulator info virtual BlackSim::CSimulatorInfo getSimulatorInfo() const = 0; + + //! Simulator listener instance + virtual ISimulatorListener *createListener(QObject *parent = nullptr) = 0; }; //! Common base class with providers, interface and some base functionality diff --git a/src/plugins/simulator/xplane/simulator_xplane.cpp b/src/plugins/simulator/xplane/simulator_xplane.cpp index 86e2d7bba..67ecc2d51 100644 --- a/src/plugins/simulator/xplane/simulator_xplane.cpp +++ b/src/plugins/simulator/xplane/simulator_xplane.cpp @@ -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 diff --git a/src/plugins/simulator/xplane/simulator_xplane.h b/src/plugins/simulator/xplane/simulator_xplane.h index a94353b9c..246b216f7 100644 --- a/src/plugins/simulator/xplane/simulator_xplane.h +++ b/src/plugins/simulator/xplane/simulator_xplane.h @@ -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 &lats, const BlackMisc::CSequence &lons, const BlackMisc::CSequence &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); } }; }