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
This commit is contained in:
Klaus Basan
2015-06-02 02:48:44 +02:00
parent f423f167f3
commit 91bcba6a0c
12 changed files with 57 additions and 71 deletions

View File

@@ -34,13 +34,10 @@ using namespace BlackMisc::Simulation::Settings;
namespace BlackCore namespace BlackCore
{ {
CContextSimulator::CContextSimulator(CRuntimeConfig::ContextMode mode, CRuntime *runtime) : CContextSimulator::CContextSimulator(CRuntimeConfig::ContextMode mode, CRuntime *runtime) :
IContextSimulator(mode, runtime), IContextSimulator(mode, runtime)
m_mapper(new QSignalMapper(this))
{ {
this->setObjectName("CContextSimulator"); this->setObjectName("CContextSimulator");
findSimulatorPlugins(); findSimulatorPlugins();
// Maps listener instance
connect(m_mapper, static_cast<void (QSignalMapper::*)(QObject *)>(&QSignalMapper::mapped), this, &CContextSimulator::ps_simulatorStarted);
} }
CContextSimulator::~CContextSimulator() CContextSimulator::~CContextSimulator()
@@ -489,15 +486,13 @@ namespace BlackCore
ISimulatorFactory *factory = getSimulatorFactory(simulatorInfo); ISimulatorFactory *factory = getSimulatorFactory(simulatorInfo);
Q_ASSERT(factory); Q_ASSERT(factory);
plugin->listener = factory->createListener(); plugin->listener = factory->createListener(simulatorInfo, this);
bool c = connect(plugin->listener, &ISimulatorListener::simulatorStarted, bool c = connect(plugin->listener, &ISimulatorListener::simulatorStarted, this, &CContextSimulator::ps_simulatorStarted);
m_mapper, static_cast<void (QSignalMapper::*)()> (&QSignalMapper::map));
if (!c) if (!c)
{ {
CLogMessage(this).error("Unable to use '%1'") << simulatorInfo.toQString(); CLogMessage(this).error("Unable to use '%1'") << simulatorInfo.toQString();
return; return;
} }
m_mapper->setMapping(plugin->listener, plugin->listener);
plugin->listener->moveToThread(&m_listenersThread); plugin->listener->moveToThread(&m_listenersThread);
} }
@@ -509,7 +504,9 @@ namespace BlackCore
m_listenersThread.start(QThread::LowPriority); 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); CLogMessage(this).debug() << "Listening for simulator: " << simulatorInfo.toQString(true);
} }
@@ -572,8 +569,7 @@ namespace BlackCore
void CContextSimulator::ps_removedRemoteAircraft(const CCallsign &callsign) void CContextSimulator::ps_removedRemoteAircraft(const CCallsign &callsign)
{ {
// todo: // \fixme: This was previously an assert and it should be one again in the future.
// 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. // This slot should not even be called when no simulator is available.
if (!m_simulatorPlugin) if (!m_simulatorPlugin)
{ {
@@ -695,25 +691,10 @@ namespace BlackCore
this->m_simulatorPlugin->simulator->highlightAircraft(aircraftToHighlight, enableHighlight, displayTime); 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(); stopSimulatorListeners();
loadSimulatorPlugin(plugin->info, false); loadSimulatorPlugin(info, false);
} }
void CContextSimulator::findSimulatorPlugins() void CContextSimulator::findSimulatorPlugins()

View File

@@ -162,7 +162,7 @@ namespace BlackCore
void ps_textMessagesReceived(const BlackMisc::Network::CTextMessageList &textMessages); void ps_textMessagesReceived(const BlackMisc::Network::CTextMessageList &textMessages);
//! Listener reports the simulator has started //! Listener reports the simulator has started
void ps_simulatorStarted(QObject *listener); void ps_simulatorStarted(const BlackMisc::Simulation::CSimulatorPluginInfo &info);
//! Simulator has changed cockpit //! Simulator has changed cockpit
void ps_cockpitChangedFromSimulator(const BlackMisc::Simulation::CSimulatedAircraft &ownAircraft); void ps_cockpitChangedFromSimulator(const BlackMisc::Simulation::CSimulatedAircraft &ownAircraft);
@@ -225,7 +225,6 @@ namespace BlackCore
QList<PluginData> m_simulatorPlugins; QList<PluginData> m_simulatorPlugins;
PluginData *m_simulatorPlugin = nullptr; //!< Currently loaded simulator plugin PluginData *m_simulatorPlugin = nullptr; //!< Currently loaded simulator plugin
BlackMisc::CRegularThread m_listenersThread; BlackMisc::CRegularThread m_listenersThread;
QSignalMapper *m_mapper = nullptr;
}; };
} // namespace } // namespace

View File

@@ -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 } // namespace

View File

@@ -245,11 +245,14 @@ namespace BlackCore
//! Constructor //! Constructor
//! \sa ISimulatorFactory::createListener(). //! \sa ISimulatorFactory::createListener().
//! \note msvc2015: use inherited constructor //! \note msvc2015: use inherited constructor
ISimulatorListener(QObject *parent); ISimulatorListener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent);
//! Destructor //! Destructor
virtual ~ISimulatorListener() = default; virtual ~ISimulatorListener() = default;
//! Corresponding info
const BlackMisc::Simulation::CSimulatorPluginInfo &getPluginInfo() const { return m_info; }
public slots: public slots:
//! Start listening for the simulator to start. //! Start listening for the simulator to start.
virtual void start() = 0; virtual void start() = 0;
@@ -259,7 +262,10 @@ namespace BlackCore
signals: signals:
//! Emitted when the listener discovers the simulator running. //! 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 //! Factory pattern class to create instances of ISimulator
@@ -283,7 +289,7 @@ namespace BlackCore
BlackMisc::IPluginStorageProvider *pluginStorageProvider) = 0; BlackMisc::IPluginStorageProvider *pluginStorageProvider) = 0;
//! Simulator listener instance //! Simulator listener instance
virtual ISimulatorListener *createListener(QObject *parent = nullptr) = 0; virtual ISimulatorListener *createListener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent = nullptr) = 0;
}; };
} // namespace } // namespace

View File

@@ -327,8 +327,8 @@ namespace BlackSimPlugin
return exclude; return exclude;
} }
CSimulatorFs9Listener::CSimulatorFs9Listener(QObject *parent) : CSimulatorFs9Listener::CSimulatorFs9Listener(const CSimulatorPluginInfo &info, QObject *parent) :
BlackCore::ISimulatorListener(parent), BlackCore::ISimulatorListener(info, parent),
m_timer(new QTimer(this)) m_timer(new QTimer(this))
{ {
Q_CONSTEXPR int QueryInterval = 5 * 1000; // 5 seconds Q_CONSTEXPR int QueryInterval = 5 * 1000; // 5 seconds
@@ -353,7 +353,7 @@ namespace BlackSimPlugin
if (!m_isStarted && fs9Host->isConnected()) if (!m_isStarted && fs9Host->isConnected())
{ {
emit simulatorStarted(); emit simulatorStarted(getPluginInfo());
m_isStarted = true; m_isStarted = true;
m_isConnecting = false; m_isConnecting = false;
} }
@@ -402,9 +402,9 @@ namespace BlackSimPlugin
return new CSimulatorFs9(info, ownAircraftProvider, remoteAircraftProvider, pluginStorageProvider, this); 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 } // namespace

View File

@@ -128,7 +128,7 @@ namespace BlackSimPlugin
public: public:
//! Constructor //! Constructor
CSimulatorFs9Listener(QObject *parent); CSimulatorFs9Listener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent);
public slots: public slots:
//! \copydoc BlackCore::ISimulatorListener::start //! \copydoc BlackCore::ISimulatorListener::start
@@ -167,7 +167,7 @@ namespace BlackSimPlugin
BlackMisc::IPluginStorageProvider *pluginStorageProvider) override; BlackMisc::IPluginStorageProvider *pluginStorageProvider) override;
//! \copydoc BlackCore::ISimulatorFactory::createListener //! \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 } // namespace Fs9

View File

@@ -40,10 +40,10 @@ namespace BlackSimPlugin
namespace Fsx namespace Fsx
{ {
CSimulatorFsx::CSimulatorFsx(const CSimulatorPluginInfo &info, CSimulatorFsx::CSimulatorFsx(const CSimulatorPluginInfo &info,
IOwnAircraftProvider *ownAircraftProvider, IOwnAircraftProvider *ownAircraftProvider,
IRemoteAircraftProvider *remoteAircraftProvider, IRemoteAircraftProvider *remoteAircraftProvider,
IPluginStorageProvider *pluginStorageProvider, IPluginStorageProvider *pluginStorageProvider,
QObject *parent) : QObject *parent) :
CSimulatorFsCommon(info, ownAircraftProvider, remoteAircraftProvider, pluginStorageProvider, CSimulatorFsCommon(info, ownAircraftProvider, remoteAircraftProvider, pluginStorageProvider,
simObjectsDir(), excludeDirectories(), parent) simObjectsDir(), excludeDirectories(), parent)
{ {
@@ -54,11 +54,11 @@ namespace BlackSimPlugin
m_useFsuipc = false; // do not use FSUIPC at the moment with FSX m_useFsuipc = false; // do not use FSUIPC at the moment with FSX
this->m_interpolator = new CInterpolatorLinear(remoteAircraftProvider, this); this->m_interpolator = new CInterpolatorLinear(remoteAircraftProvider, this);
m_modelMatcher.setDefaultModel(CAircraftModel( m_modelMatcher.setDefaultModel(CAircraftModel(
"Boeing 737-800 Paint1", "Boeing 737-800 Paint1",
CAircraftModel::TypeModelMatchingDefaultModel, CAircraftModel::TypeModelMatchingDefaultModel,
"B737-800 default model", "B737-800 default model",
CAircraftIcaoData(CAircraftIcaoCode("B738", "L2J"), CAirlineIcaoCode(), "FFFFFF") CAircraftIcaoData(CAircraftIcaoCode("B738", "L2J"), CAirlineIcaoCode(), "FFFFFF")
)); ));
} }
CSimulatorFsx::~CSimulatorFsx() CSimulatorFsx::~CSimulatorFsx()
@@ -860,7 +860,8 @@ namespace BlackSimPlugin
return exclude; return exclude;
} }
CSimulatorFsxListener::CSimulatorFsxListener(QObject *parent) : ISimulatorListener(parent), CSimulatorFsxListener::CSimulatorFsxListener(const CSimulatorPluginInfo &info, QObject *parent) :
ISimulatorListener(info, parent),
m_timer(new QTimer(this)) m_timer(new QTimer(this))
{ {
Q_CONSTEXPR int QueryInterval = 5 * 1000; // 5 seconds Q_CONSTEXPR int QueryInterval = 5 * 1000; // 5 seconds
@@ -873,8 +874,7 @@ namespace BlackSimPlugin
HANDLE hSimConnect; HANDLE hSimConnect;
HRESULT result = SimConnect_Open(&hSimConnect, BlackMisc::CProject::systemNameAndVersionChar(), nullptr, 0, 0, 0); HRESULT result = SimConnect_Open(&hSimConnect, BlackMisc::CProject::systemNameAndVersionChar(), nullptr, 0, 0, 0);
SimConnect_Close(hSimConnect); SimConnect_Close(hSimConnect);
if (result == S_OK) { emit simulatorStarted(this->getPluginInfo()); }
if (result == S_OK) { emit simulatorStarted(); }
}); });
} }

View File

@@ -217,7 +217,7 @@ namespace BlackSimPlugin
public: public:
//! Constructor //! Constructor
CSimulatorFsxListener(QObject *parent); CSimulatorFsxListener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent);
public slots: public slots:
//! \copydoc BlackCore::ISimulatorListener::start //! \copydoc BlackCore::ISimulatorListener::start

View File

@@ -27,9 +27,9 @@ namespace BlackSimPlugin
return new CSimulatorFsx(info, ownAircraftProvider, renderedAircraftProvider, pluginStorageProvider, this); 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 } // namespace

View File

@@ -24,7 +24,9 @@ namespace BlackSimPlugin
namespace Fsx namespace Fsx
{ {
//! Factory implementation to create CSimulatorFsx instances //! Factory implementation to create CSimulatorFsx instances
class CSimulatorFsxFactory : public QObject, public BlackCore::ISimulatorFactory class CSimulatorFsxFactory :
public QObject,
public BlackCore::ISimulatorFactory
{ {
Q_OBJECT Q_OBJECT
Q_PLUGIN_METADATA(IID "org.swift-project.blackcore.simulatorinterface" FILE "simulator_fsx.json") Q_PLUGIN_METADATA(IID "org.swift-project.blackcore.simulatorinterface" FILE "simulator_fsx.json")
@@ -38,7 +40,7 @@ namespace BlackSimPlugin
BlackMisc::IPluginStorageProvider *pluginStorageProvider) override; BlackMisc::IPluginStorageProvider *pluginStorageProvider) override;
//! \copydoc BlackCore::ISimulatorFactory::getListener //! \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 } // namespace

View File

@@ -430,7 +430,7 @@ namespace BlackSimPlugin
return true; return true;
} }
BlackCore::ISimulator *CSimulatorXPlaneFactory::create(const BlackMisc::Simulation::CSimulatorPluginInfo &info, BlackCore::ISimulator *CSimulatorXPlaneFactory::create(const CSimulatorPluginInfo &info,
IOwnAircraftProvider *ownAircraftProvider, IOwnAircraftProvider *ownAircraftProvider,
IRemoteAircraftProvider *renderedAircraftProvider, IRemoteAircraftProvider *renderedAircraftProvider,
IPluginStorageProvider *pluginStorageProvider) IPluginStorageProvider *pluginStorageProvider)
@@ -438,19 +438,15 @@ namespace BlackSimPlugin
return new CSimulatorXPlane(info, ownAircraftProvider, renderedAircraftProvider, pluginStorageProvider, this); 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() void CSimulatorXPlaneListener::start()
{ {
if (m_watcher) // already started if (m_watcher) { return; } // already started
return;
if (isXBusRunning()) if (isXBusRunning())
{ {
emit simulatorStarted(); emit simulatorStarted(getPluginInfo());
} }
else else
{ {
@@ -486,7 +482,9 @@ namespace BlackSimPlugin
void CSimulatorXPlaneListener::ps_serviceRegistered(const QString &serviceName) void CSimulatorXPlaneListener::ps_serviceRegistered(const QString &serviceName)
{ {
if (serviceName == xbusServiceName()) if (serviceName == xbusServiceName())
emit simulatorStarted(); {
emit simulatorStarted(getPluginInfo());
}
} }
} // namespace } // namespace

View File

@@ -186,7 +186,7 @@ namespace BlackSimPlugin
public: public:
//! Constructor //! Constructor
CSimulatorXPlaneListener(QObject *parent); CSimulatorXPlaneListener(const BlackMisc::Simulation::CSimulatorPluginInfo &info, QObject *parent);
public slots: public slots:
//! \copydoc BlackCore::ISimulatorListener::start //! \copydoc BlackCore::ISimulatorListener::start
@@ -205,7 +205,6 @@ namespace BlackSimPlugin
private: private:
QDBusConnection m_conn { "default" }; QDBusConnection m_conn { "default" };
QDBusServiceWatcher *m_watcher { nullptr }; QDBusServiceWatcher *m_watcher { nullptr };
}; };
//! Factory for creating CSimulatorXPlane instance //! Factory for creating CSimulatorXPlane instance
@@ -223,7 +222,7 @@ namespace BlackSimPlugin
BlackMisc::IPluginStorageProvider *pluginStorageProvider) override; BlackMisc::IPluginStorageProvider *pluginStorageProvider) override;
//! \copydoc BlackCore::ISimulatorFactory::createListener //! \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 } // ns