Listen for simulators on another thread

This commit is contained in:
Michał Garapich
2015-03-09 19:18:24 +01:00
committed by Roland Winklmeier
parent 570f4aa4ea
commit 924e094a38
6 changed files with 33 additions and 13 deletions

View File

@@ -404,13 +404,19 @@ namespace BlackCore
driver.listener = factory->createListener(); driver.listener = factory->createListener();
connect(driver.listener, &ISimulatorListener::simulatorStarted, this, &CContextSimulator::ps_simulatorStarted); connect(driver.listener, &ISimulatorListener::simulatorStarted, this, &CContextSimulator::ps_simulatorStarted);
driver.listener->moveToThread(&m_listenersThread);
} }
ISimulatorListener *listener = m_simulatorDrivers[simulatorInfo].listener; ISimulatorListener *listener = m_simulatorDrivers[simulatorInfo].listener;
Q_ASSERT(listener); Q_ASSERT(listener);
listener->start();
if (!m_listenersThread.isRunning())
{
m_listenersThread.start(QThread::LowPriority);
}
QMetaObject::invokeMethod(listener, "start");
CLogMessage(this).debug() << "Listening for simulator:" << simulatorInfo.toQString(true); CLogMessage(this).debug() << "Listening for simulator:" << simulatorInfo.toQString(true);
} }
void CContextSimulator::listenForAllSimulators() void CContextSimulator::listenForAllSimulators()
@@ -428,12 +434,15 @@ namespace BlackCore
if (plugin.isUnspecified()) if (plugin.isUnspecified())
listenForAllSimulators(); listenForAllSimulators();
else else
{
listenForSimulator(plugin); listenForSimulator(plugin);
}
} }
void CContextSimulator::unloadSimulatorPlugin() void CContextSimulator::unloadSimulatorPlugin()
{ {
if (m_simulator) { if (m_simulator)
{
// depending on shutdown order, network might already have been deleted // depending on shutdown order, network might already have been deleted
IContextNetwork *networkContext = this->getIContextNetwork(); IContextNetwork *networkContext = this->getIContextNetwork();
Q_ASSERT(networkContext); Q_ASSERT(networkContext);
@@ -541,11 +550,15 @@ namespace BlackCore
CSimulatorInfo plugin = getIContextSettings()->getSimulatorSettings().getSelectedPlugin(); CSimulatorInfo plugin = getIContextSettings()->getSimulatorSettings().getSelectedPlugin();
// no simulator loaded yet, listen // no simulator loaded yet, listen
if (!m_simulator) { if (!m_simulator)
{
stopSimulatorListeners(); stopSimulatorListeners();
if (plugin.isSameSimulator(CSimulatorInfo::UnspecifiedSim())) { if (plugin.isSameSimulator(CSimulatorInfo::UnspecifiedSim()))
{
listenForAllSimulators(); listenForAllSimulators();
} else { }
else
{
listenForSimulator(plugin); listenForSimulator(plugin);
} }
} }
@@ -599,16 +612,18 @@ namespace BlackCore
{ {
const QString path = qApp->applicationDirPath().append("/plugins/simulator"); const QString path = qApp->applicationDirPath().append("/plugins/simulator");
m_pluginsDir = QDir(path); m_pluginsDir = QDir(path);
if (!m_pluginsDir.exists()) { if (!m_pluginsDir.exists())
{
CLogMessage(this).error("No plugin directory: %1") << m_pluginsDir.currentPath(); CLogMessage(this).error("No plugin directory: %1") << m_pluginsDir.currentPath();
return; return;
} }
QStringList fileNames = m_pluginsDir.entryList(QDir::Files); QStringList fileNames = m_pluginsDir.entryList(QDir::Files);
fileNames.sort(Qt::CaseInsensitive); // give a certain order, rather than random file order fileNames.sort(Qt::CaseInsensitive); // give a certain order, rather than random file order
for (const auto& fileName: fileNames) { for (const auto& fileName: fileNames)
if (!QLibrary::isLibrary(fileName)) { {
if (!QLibrary::isLibrary(fileName))
{
continue; continue;
} }
@@ -624,7 +639,6 @@ namespace BlackCore
} }
else else
{ {
CLogMessage(this).warning("Simulator driver in %1 is invalid") << pluginPath; CLogMessage(this).warning("Simulator driver in %1 is invalid") << pluginPath;
} }
} }
@@ -634,7 +648,7 @@ namespace BlackCore
{ {
std::for_each(m_simulatorDrivers.begin(), m_simulatorDrivers.end(), [](DriverInfo& driver) { std::for_each(m_simulatorDrivers.begin(), m_simulatorDrivers.end(), [](DriverInfo& driver) {
if (driver.listener) if (driver.listener)
driver.listener->stop(); QMetaObject::invokeMethod(driver.listener, "stop");
}); });
} }

View File

@@ -14,6 +14,7 @@
#include "blackcore/context_simulator.h" #include "blackcore/context_simulator.h"
#include "blackcore/simulator.h" #include "blackcore/simulator.h"
#include "blackmisc/worker.h"
#include "blacksim/simulatorinfo.h" #include "blacksim/simulatorinfo.h"
#include "blacksim/simulatorinfolist.h" #include "blacksim/simulatorinfolist.h"
#include "blackmisc/nwtextmessagelist.h" #include "blackmisc/nwtextmessagelist.h"
@@ -216,6 +217,7 @@ namespace BlackCore
QDir m_pluginsDir; QDir m_pluginsDir;
QMap<BlackSim::CSimulatorInfo, DriverInfo> m_simulatorDrivers; QMap<BlackSim::CSimulatorInfo, DriverInfo> m_simulatorDrivers;
QFuture<bool> m_canConnectResult; QFuture<bool> m_canConnectResult;
BlackMisc::CRegularThread m_listenersThread;
}; };
} // namespace } // namespace

View File

@@ -218,7 +218,8 @@ namespace BlackCore
//! Destructor //! Destructor
virtual ~ISimulatorListener() = default; virtual ~ISimulatorListener() = default;
public slots:
//! Start listening for the simulator to start. //! Start listening for the simulator to start.
virtual void start() = 0; virtual void start() = 0;

View File

@@ -121,6 +121,7 @@ namespace BlackSimPlugin
//! Constructor //! Constructor
CSimulatorFs9Listener(const QSharedPointer<CFs9Host> &fs9Host, const QSharedPointer<CLobbyClient> &lobbyClient, QObject* parent); CSimulatorFs9Listener(const QSharedPointer<CFs9Host> &fs9Host, const QSharedPointer<CLobbyClient> &lobbyClient, QObject* parent);
public slots:
//! \copydoc BlackCore::ISimulatorListener::start //! \copydoc BlackCore::ISimulatorListener::start
virtual void start() override; virtual void start() override;

View File

@@ -209,6 +209,7 @@ namespace BlackSimPlugin
//! Constructor //! Constructor
CSimulatorFsxListener(QObject* parent); CSimulatorFsxListener(QObject* parent);
public slots:
//! \copydoc BlackCore::ISimulatorListener::start //! \copydoc BlackCore::ISimulatorListener::start
virtual void start() override; virtual void start() override;

View File

@@ -181,6 +181,7 @@ namespace BlackSimPlugin
//! Constructor //! Constructor
CSimulatorXPlaneListener(QObject* parent); CSimulatorXPlaneListener(QObject* parent);
public slots:
//! \copydoc BlackCore::ISimulatorListener::start //! \copydoc BlackCore::ISimulatorListener::start
virtual void start() override; virtual void start() override;