From 91bcba6a0c712d2e3f7fcad0b8eeaa2ccb79f43e Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Tue, 2 Jun 2015 02:48:44 +0200 Subject: [PATCH] refs #438, added plug info to listeners * listener signal can be directly used without mapping * (during debugging) it is much easier to identify a listener --- src/blackcore/context_simulator_impl.cpp | 37 +++++-------------- src/blackcore/context_simulator_impl.h | 3 +- src/blackcore/simulator.cpp | 3 +- src/blackcore/simulator.h | 12 ++++-- src/plugins/simulator/fs9/simulator_fs9.cpp | 10 ++--- src/plugins/simulator/fs9/simulator_fs9.h | 4 +- src/plugins/simulator/fsx/simulator_fsx.cpp | 24 ++++++------ src/plugins/simulator/fsx/simulator_fsx.h | 2 +- .../simulator/fsx/simulator_fsxfactory.cpp | 4 +- .../simulator/fsx/simulator_fsxfactory.h | 6 ++- .../simulator/xplane/simulator_xplane.cpp | 18 ++++----- .../simulator/xplane/simulator_xplane.h | 5 +-- 12 files changed, 57 insertions(+), 71 deletions(-) diff --git a/src/blackcore/context_simulator_impl.cpp b/src/blackcore/context_simulator_impl.cpp index ae074e027..ad32d220d 100644 --- a/src/blackcore/context_simulator_impl.cpp +++ b/src/blackcore/context_simulator_impl.cpp @@ -34,13 +34,10 @@ using namespace BlackMisc::Simulation::Settings; namespace BlackCore { CContextSimulator::CContextSimulator(CRuntimeConfig::ContextMode mode, CRuntime *runtime) : - IContextSimulator(mode, runtime), - m_mapper(new QSignalMapper(this)) + IContextSimulator(mode, runtime) { this->setObjectName("CContextSimulator"); findSimulatorPlugins(); - // Maps listener instance - connect(m_mapper, static_cast(&QSignalMapper::mapped), this, &CContextSimulator::ps_simulatorStarted); } CContextSimulator::~CContextSimulator() @@ -489,15 +486,13 @@ namespace BlackCore ISimulatorFactory *factory = getSimulatorFactory(simulatorInfo); Q_ASSERT(factory); - plugin->listener = factory->createListener(); - bool c = connect(plugin->listener, &ISimulatorListener::simulatorStarted, - m_mapper, static_cast (&QSignalMapper::map)); + plugin->listener = factory->createListener(simulatorInfo, this); + bool c = connect(plugin->listener, &ISimulatorListener::simulatorStarted, this, &CContextSimulator::ps_simulatorStarted); if (!c) { CLogMessage(this).error("Unable to use '%1'") << simulatorInfo.toQString(); return; } - m_mapper->setMapping(plugin->listener, plugin->listener); plugin->listener->moveToThread(&m_listenersThread); } @@ -509,7 +504,9 @@ namespace BlackCore m_listenersThread.start(QThread::LowPriority); } - QMetaObject::invokeMethod(listener, "start"); + bool s = QMetaObject::invokeMethod(listener, "start"); + Q_ASSERT_X(s, Q_FUNC_INFO, "cannot invoke method"); + Q_UNUSED(s); CLogMessage(this).debug() << "Listening for simulator: " << simulatorInfo.toQString(true); } @@ -572,8 +569,7 @@ namespace BlackCore void CContextSimulator::ps_removedRemoteAircraft(const CCallsign &callsign) { - // todo: - // This was previously an assert and it should be one again in the future. + // \fixme: This was previously an assert and it should be one again in the future. // This slot should not even be called when no simulator is available. if (!m_simulatorPlugin) { @@ -695,25 +691,10 @@ namespace BlackCore this->m_simulatorPlugin->simulator->highlightAircraft(aircraftToHighlight, enableHighlight, displayTime); } - void CContextSimulator::ps_simulatorStarted(QObject *listener) + void CContextSimulator::ps_simulatorStarted(const CSimulatorPluginInfo &info) { - Q_ASSERT(listener); - Q_ASSERT(listener->inherits("BlackCore::ISimulatorListener")); - - /* Find caller */ - PluginData *plugin = [this](QObject * listener) - { - auto it = std::find_if(m_simulatorPlugins.begin(), m_simulatorPlugins.end(), [listener](const PluginData & plugin) - { - return plugin.listener == listener; - }); - return &(*it); - }(listener); - Q_ASSERT(plugin); - - CLogMessage(this).debug() << plugin->info.toQString() << "started"; stopSimulatorListeners(); - loadSimulatorPlugin(plugin->info, false); + loadSimulatorPlugin(info, false); } void CContextSimulator::findSimulatorPlugins() diff --git a/src/blackcore/context_simulator_impl.h b/src/blackcore/context_simulator_impl.h index 28fe819cd..8ef513e2d 100644 --- a/src/blackcore/context_simulator_impl.h +++ b/src/blackcore/context_simulator_impl.h @@ -162,7 +162,7 @@ namespace BlackCore void ps_textMessagesReceived(const BlackMisc::Network::CTextMessageList &textMessages); //! Listener reports the simulator has started - void ps_simulatorStarted(QObject *listener); + void ps_simulatorStarted(const BlackMisc::Simulation::CSimulatorPluginInfo &info); //! Simulator has changed cockpit void ps_cockpitChangedFromSimulator(const BlackMisc::Simulation::CSimulatedAircraft &ownAircraft); @@ -225,7 +225,6 @@ namespace BlackCore QList m_simulatorPlugins; PluginData *m_simulatorPlugin = nullptr; //!< Currently loaded simulator plugin BlackMisc::CRegularThread m_listenersThread; - QSignalMapper *m_mapper = nullptr; }; } // namespace diff --git a/src/blackcore/simulator.cpp b/src/blackcore/simulator.cpp index f938d2ffa..d13aa34a7 100644 --- a/src/blackcore/simulator.cpp +++ b/src/blackcore/simulator.cpp @@ -59,7 +59,8 @@ namespace BlackCore } } - ISimulatorListener::ISimulatorListener(QObject *parent) : QObject(parent) + ISimulatorListener::ISimulatorListener(const CSimulatorPluginInfo &info, QObject *parent) : + QObject(parent), m_info(info) { } } // namespace diff --git a/src/blackcore/simulator.h b/src/blackcore/simulator.h index 206d65da2..a61dce181 100644 --- a/src/blackcore/simulator.h +++ b/src/blackcore/simulator.h @@ -245,11 +245,14 @@ namespace BlackCore //! Constructor //! \sa ISimulatorFactory::createListener(). //! \note msvc2015: use inherited constructor - ISimulatorListener(QObject *parent); + ISimulatorListener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent); //! Destructor virtual ~ISimulatorListener() = default; + //! Corresponding info + const BlackMisc::Simulation::CSimulatorPluginInfo &getPluginInfo() const { return m_info; } + public slots: //! Start listening for the simulator to start. virtual void start() = 0; @@ -259,7 +262,10 @@ namespace BlackCore signals: //! Emitted when the listener discovers the simulator running. - void simulatorStarted(); + void simulatorStarted(const BlackMisc::Simulation::CSimulatorPluginInfo &info); + + private: + BlackMisc::Simulation::CSimulatorPluginInfo m_info; }; //! Factory pattern class to create instances of ISimulator @@ -283,7 +289,7 @@ namespace BlackCore BlackMisc::IPluginStorageProvider *pluginStorageProvider) = 0; //! Simulator listener instance - virtual ISimulatorListener *createListener(QObject *parent = nullptr) = 0; + virtual ISimulatorListener *createListener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent = nullptr) = 0; }; } // namespace diff --git a/src/plugins/simulator/fs9/simulator_fs9.cpp b/src/plugins/simulator/fs9/simulator_fs9.cpp index 3797689d6..231eaf277 100644 --- a/src/plugins/simulator/fs9/simulator_fs9.cpp +++ b/src/plugins/simulator/fs9/simulator_fs9.cpp @@ -327,8 +327,8 @@ namespace BlackSimPlugin return exclude; } - CSimulatorFs9Listener::CSimulatorFs9Listener(QObject *parent) : - BlackCore::ISimulatorListener(parent), + CSimulatorFs9Listener::CSimulatorFs9Listener(const CSimulatorPluginInfo &info, QObject *parent) : + BlackCore::ISimulatorListener(info, parent), m_timer(new QTimer(this)) { Q_CONSTEXPR int QueryInterval = 5 * 1000; // 5 seconds @@ -353,7 +353,7 @@ namespace BlackSimPlugin if (!m_isStarted && fs9Host->isConnected()) { - emit simulatorStarted(); + emit simulatorStarted(getPluginInfo()); m_isStarted = true; m_isConnecting = false; } @@ -402,9 +402,9 @@ namespace BlackSimPlugin return new CSimulatorFs9(info, ownAircraftProvider, remoteAircraftProvider, pluginStorageProvider, this); } - BlackCore::ISimulatorListener *CSimulatorFs9Factory::createListener(QObject *parent) + BlackCore::ISimulatorListener *CSimulatorFs9Factory::createListener(const CSimulatorPluginInfo &info, QObject *parent) { - return new CSimulatorFs9Listener(parent); + return new CSimulatorFs9Listener(info, parent); } } // namespace diff --git a/src/plugins/simulator/fs9/simulator_fs9.h b/src/plugins/simulator/fs9/simulator_fs9.h index 9a58c6f7a..2e6f83a45 100644 --- a/src/plugins/simulator/fs9/simulator_fs9.h +++ b/src/plugins/simulator/fs9/simulator_fs9.h @@ -128,7 +128,7 @@ namespace BlackSimPlugin public: //! Constructor - CSimulatorFs9Listener(QObject *parent); + CSimulatorFs9Listener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent); public slots: //! \copydoc BlackCore::ISimulatorListener::start @@ -167,7 +167,7 @@ namespace BlackSimPlugin BlackMisc::IPluginStorageProvider *pluginStorageProvider) override; //! \copydoc BlackCore::ISimulatorFactory::createListener - virtual BlackCore::ISimulatorListener *createListener(QObject *parent = nullptr) override; + virtual BlackCore::ISimulatorListener *createListener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent = nullptr) override; }; } // namespace Fs9 diff --git a/src/plugins/simulator/fsx/simulator_fsx.cpp b/src/plugins/simulator/fsx/simulator_fsx.cpp index f283668ea..b249e4c92 100644 --- a/src/plugins/simulator/fsx/simulator_fsx.cpp +++ b/src/plugins/simulator/fsx/simulator_fsx.cpp @@ -40,10 +40,10 @@ namespace BlackSimPlugin namespace Fsx { CSimulatorFsx::CSimulatorFsx(const CSimulatorPluginInfo &info, - IOwnAircraftProvider *ownAircraftProvider, - IRemoteAircraftProvider *remoteAircraftProvider, - IPluginStorageProvider *pluginStorageProvider, - QObject *parent) : + IOwnAircraftProvider *ownAircraftProvider, + IRemoteAircraftProvider *remoteAircraftProvider, + IPluginStorageProvider *pluginStorageProvider, + QObject *parent) : CSimulatorFsCommon(info, ownAircraftProvider, remoteAircraftProvider, pluginStorageProvider, simObjectsDir(), excludeDirectories(), parent) { @@ -54,11 +54,11 @@ namespace BlackSimPlugin m_useFsuipc = false; // do not use FSUIPC at the moment with FSX this->m_interpolator = new CInterpolatorLinear(remoteAircraftProvider, this); m_modelMatcher.setDefaultModel(CAircraftModel( - "Boeing 737-800 Paint1", - CAircraftModel::TypeModelMatchingDefaultModel, - "B737-800 default model", - CAircraftIcaoData(CAircraftIcaoCode("B738", "L2J"), CAirlineIcaoCode(), "FFFFFF") - )); + "Boeing 737-800 Paint1", + CAircraftModel::TypeModelMatchingDefaultModel, + "B737-800 default model", + CAircraftIcaoData(CAircraftIcaoCode("B738", "L2J"), CAirlineIcaoCode(), "FFFFFF") + )); } CSimulatorFsx::~CSimulatorFsx() @@ -860,7 +860,8 @@ namespace BlackSimPlugin return exclude; } - CSimulatorFsxListener::CSimulatorFsxListener(QObject *parent) : ISimulatorListener(parent), + CSimulatorFsxListener::CSimulatorFsxListener(const CSimulatorPluginInfo &info, QObject *parent) : + ISimulatorListener(info, parent), m_timer(new QTimer(this)) { Q_CONSTEXPR int QueryInterval = 5 * 1000; // 5 seconds @@ -873,8 +874,7 @@ namespace BlackSimPlugin HANDLE hSimConnect; HRESULT result = SimConnect_Open(&hSimConnect, BlackMisc::CProject::systemNameAndVersionChar(), nullptr, 0, 0, 0); SimConnect_Close(hSimConnect); - - if (result == S_OK) { emit simulatorStarted(); } + if (result == S_OK) { emit simulatorStarted(this->getPluginInfo()); } }); } diff --git a/src/plugins/simulator/fsx/simulator_fsx.h b/src/plugins/simulator/fsx/simulator_fsx.h index c2ccdcb09..cb005da7f 100644 --- a/src/plugins/simulator/fsx/simulator_fsx.h +++ b/src/plugins/simulator/fsx/simulator_fsx.h @@ -217,7 +217,7 @@ namespace BlackSimPlugin public: //! Constructor - CSimulatorFsxListener(QObject *parent); + CSimulatorFsxListener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent); public slots: //! \copydoc BlackCore::ISimulatorListener::start diff --git a/src/plugins/simulator/fsx/simulator_fsxfactory.cpp b/src/plugins/simulator/fsx/simulator_fsxfactory.cpp index 72c509543..82e6cb4c1 100644 --- a/src/plugins/simulator/fsx/simulator_fsxfactory.cpp +++ b/src/plugins/simulator/fsx/simulator_fsxfactory.cpp @@ -27,9 +27,9 @@ namespace BlackSimPlugin return new CSimulatorFsx(info, ownAircraftProvider, renderedAircraftProvider, pluginStorageProvider, this); } - BlackCore::ISimulatorListener *CSimulatorFsxFactory::createListener(QObject *parent) + BlackCore::ISimulatorListener *CSimulatorFsxFactory::createListener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent) { - return new CSimulatorFsxListener(parent); + return new CSimulatorFsxListener(info, parent); } } // namespace diff --git a/src/plugins/simulator/fsx/simulator_fsxfactory.h b/src/plugins/simulator/fsx/simulator_fsxfactory.h index a31638508..1fb90e4dc 100644 --- a/src/plugins/simulator/fsx/simulator_fsxfactory.h +++ b/src/plugins/simulator/fsx/simulator_fsxfactory.h @@ -24,7 +24,9 @@ namespace BlackSimPlugin namespace Fsx { //! Factory implementation to create CSimulatorFsx instances - class CSimulatorFsxFactory : public QObject, public BlackCore::ISimulatorFactory + class CSimulatorFsxFactory : + public QObject, + public BlackCore::ISimulatorFactory { Q_OBJECT Q_PLUGIN_METADATA(IID "org.swift-project.blackcore.simulatorinterface" FILE "simulator_fsx.json") @@ -38,7 +40,7 @@ namespace BlackSimPlugin BlackMisc::IPluginStorageProvider *pluginStorageProvider) override; //! \copydoc BlackCore::ISimulatorFactory::getListener - virtual BlackCore::ISimulatorListener *createListener(QObject *parent = nullptr) override; + virtual BlackCore::ISimulatorListener *createListener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent = nullptr) override; }; } // namespace diff --git a/src/plugins/simulator/xplane/simulator_xplane.cpp b/src/plugins/simulator/xplane/simulator_xplane.cpp index 14035f66e..a5f82d497 100644 --- a/src/plugins/simulator/xplane/simulator_xplane.cpp +++ b/src/plugins/simulator/xplane/simulator_xplane.cpp @@ -430,7 +430,7 @@ namespace BlackSimPlugin return true; } - BlackCore::ISimulator *CSimulatorXPlaneFactory::create(const BlackMisc::Simulation::CSimulatorPluginInfo &info, + BlackCore::ISimulator *CSimulatorXPlaneFactory::create(const CSimulatorPluginInfo &info, IOwnAircraftProvider *ownAircraftProvider, IRemoteAircraftProvider *renderedAircraftProvider, IPluginStorageProvider *pluginStorageProvider) @@ -438,19 +438,15 @@ namespace BlackSimPlugin return new CSimulatorXPlane(info, ownAircraftProvider, renderedAircraftProvider, pluginStorageProvider, this); } - CSimulatorXPlaneListener::CSimulatorXPlaneListener(QObject *parent): ISimulatorListener(parent) - { - - } + CSimulatorXPlaneListener::CSimulatorXPlaneListener(const CSimulatorPluginInfo &info, QObject *parent): ISimulatorListener(info, parent) + { } void CSimulatorXPlaneListener::start() { - if (m_watcher) // already started - return; - + if (m_watcher) { return; } // already started if (isXBusRunning()) { - emit simulatorStarted(); + emit simulatorStarted(getPluginInfo()); } else { @@ -486,7 +482,9 @@ namespace BlackSimPlugin void CSimulatorXPlaneListener::ps_serviceRegistered(const QString &serviceName) { if (serviceName == xbusServiceName()) - emit simulatorStarted(); + { + emit simulatorStarted(getPluginInfo()); + } } } // namespace diff --git a/src/plugins/simulator/xplane/simulator_xplane.h b/src/plugins/simulator/xplane/simulator_xplane.h index 824121db9..2569bde5f 100644 --- a/src/plugins/simulator/xplane/simulator_xplane.h +++ b/src/plugins/simulator/xplane/simulator_xplane.h @@ -186,7 +186,7 @@ namespace BlackSimPlugin public: //! Constructor - CSimulatorXPlaneListener(QObject *parent); + CSimulatorXPlaneListener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent); public slots: //! \copydoc BlackCore::ISimulatorListener::start @@ -205,7 +205,6 @@ namespace BlackSimPlugin private: QDBusConnection m_conn { "default" }; QDBusServiceWatcher *m_watcher { nullptr }; - }; //! Factory for creating CSimulatorXPlane instance @@ -223,7 +222,7 @@ namespace BlackSimPlugin BlackMisc::IPluginStorageProvider *pluginStorageProvider) override; //! \copydoc BlackCore::ISimulatorFactory::createListener - virtual BlackCore::ISimulatorListener *createListener(QObject *parent = nullptr) override { return new CSimulatorXPlaneListener(parent); } + virtual BlackCore::ISimulatorListener *createListener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent = nullptr) override { return new CSimulatorXPlaneListener(info, parent); } }; } // ns