Split listening for simulators and loading their plugins

Previously loadSimulatorPlugin was doing two things. First starting
a listener and second loading the plugin itself. This was confusing
since the sequence is to listen first and load not before a simulator
is available.
This change splits this up properly into two methods. listenForSimulator()
is called when the plugin is started and as soon as a simulator is started,
its plugin will be loaded.

refs #909
This commit is contained in:
Roland Winklmeier
2017-03-25 19:03:24 +01:00
committed by Mathew Sutcliffe
parent aafe211b5c
commit 9dbb984e1a
2 changed files with 18 additions and 42 deletions

View File

@@ -92,7 +92,7 @@ namespace BlackCore
bool CContextSimulator::startSimulatorPlugin(const CSimulatorPluginInfo &simulatorInfo) bool CContextSimulator::startSimulatorPlugin(const CSimulatorPluginInfo &simulatorInfo)
{ {
return this->loadSimulatorPlugin(simulatorInfo, true); return this->listenForSimulator(simulatorInfo);
} }
void CContextSimulator::stopSimulatorPlugin(const CSimulatorPluginInfo &simulatorInfo) void CContextSimulator::stopSimulatorPlugin(const CSimulatorPluginInfo &simulatorInfo)
@@ -241,35 +241,22 @@ namespace BlackCore
return m_simulatorPlugin.second->getTimeSynchronizationOffset(); return m_simulatorPlugin.second->getTimeSynchronizationOffset();
} }
bool CContextSimulator::loadSimulatorPlugin(const CSimulatorPluginInfo &simulatorPluginInfo, bool withListener) bool CContextSimulator::loadSimulatorPlugin(const CSimulatorPluginInfo &simulatorPluginInfo)
{ {
Q_ASSERT(getIContextApplication()); Q_ASSERT(getIContextApplication());
Q_ASSERT(getIContextApplication()->isUsingImplementingObject()); Q_ASSERT(getIContextApplication()->isUsingImplementingObject());
Q_ASSERT(!simulatorPluginInfo.isUnspecified()); Q_ASSERT(!simulatorPluginInfo.isUnspecified());
Q_ASSERT(CThreadUtils::isCurrentThreadApplicationThread()); // only run in main thread Q_ASSERT(CThreadUtils::isCurrentThreadApplicationThread()); // only run in main thread
if (!simulatorPluginInfo.isValid()) // Is a plugin already loaded?
if (!m_simulatorPlugin.first.isUnspecified())
{ {
CLogMessage(this).error("Illegal plugin"); // This can happen, if a listener emitted simulatorStarted twice or two different simulators
// are running at the same time. In this case, we leave the loaded plugin and just return.
return false; return false;
} }
// Is the plugin already loaded? if (!simulatorPluginInfo.isValid())
if (!m_simulatorPlugin.first.isUnspecified())
{
return true;
}
unloadSimulatorPlugin(); // old plugin unloaded
// now we have a state where no driver is loaded
if (withListener)
{
this->listenForSimulator(simulatorPluginInfo);
return false; // not a plugin yet, just listener
}
if (!simulatorPluginInfo.isValid() || simulatorPluginInfo.isUnspecified())
{ {
CLogMessage(this).error("Illegal plugin"); CLogMessage(this).error("Illegal plugin");
return false; return false;
@@ -330,25 +317,12 @@ namespace BlackCore
return true; return true;
} }
void CContextSimulator::listenForSimulator(const CSimulatorPluginInfo &simulatorInfo) bool CContextSimulator::listenForSimulator(const CSimulatorPluginInfo &simulatorInfo)
{ {
Q_ASSERT(this->getIContextApplication()); Q_ASSERT(this->getIContextApplication());
Q_ASSERT(this->getIContextApplication()->isUsingImplementingObject()); Q_ASSERT(this->getIContextApplication()->isUsingImplementingObject());
Q_ASSERT(!simulatorInfo.isUnspecified()); Q_ASSERT(!simulatorInfo.isUnspecified());
Q_ASSERT(m_simulatorPlugin.first.isUnspecified());
const ISimulator::SimulatorStatus simStatus = getSimulatorStatusEnum();
if (!m_simulatorPlugin.first.isUnspecified() &&
m_simulatorPlugin.first == simulatorInfo && simStatus.testFlag(ISimulator::Connected))
{
// the simulator is already connected and running
return;
}
if (!m_simulatorPlugin.first.isUnspecified())
{
// wrong or disconnected plugin, we start from the scratch
this->unloadSimulatorPlugin();
}
if (!m_listenersThread.isRunning()) if (!m_listenersThread.isRunning())
{ {
@@ -357,7 +331,7 @@ namespace BlackCore
} }
ISimulatorListener *listener = m_plugins->createListener(simulatorInfo.getIdentifier()); ISimulatorListener *listener = m_plugins->createListener(simulatorInfo.getIdentifier());
if (!listener) { return; } if (!listener) { return false; }
if (listener->thread() != &m_listenersThread) if (listener->thread() != &m_listenersThread)
{ {
@@ -365,7 +339,7 @@ namespace BlackCore
if (!c) if (!c)
{ {
CLogMessage(this).error("Unable to use '%1'") << simulatorInfo.toQString(); CLogMessage(this).error("Unable to use '%1'") << simulatorInfo.toQString();
return; return false;
} }
Q_ASSERT_X(!listener->parent(), Q_FUNC_INFO, "Objects with parent cannot be moved to thread"); Q_ASSERT_X(!listener->parent(), Q_FUNC_INFO, "Objects with parent cannot be moved to thread");
@@ -378,6 +352,7 @@ namespace BlackCore
Q_UNUSED(s); Q_UNUSED(s);
CLogMessage(this).info("Listening for simulator '%1'") << simulatorInfo.getIdentifier(); CLogMessage(this).info("Listening for simulator '%1'") << simulatorInfo.getIdentifier();
return true;
} }
void CContextSimulator::listenForAllSimulators() void CContextSimulator::listenForAllSimulators()
@@ -571,8 +546,9 @@ namespace BlackCore
void CContextSimulator::restoreSimulatorPlugins() void CContextSimulator::restoreSimulatorPlugins()
{ {
stopSimulatorListeners(); if (!m_simulatorPlugin.first.isUnspecified()) { return; }
stopSimulatorListeners();
const auto enabledSimulators = m_enabledSimulators.getThreadLocal(); const auto enabledSimulators = m_enabledSimulators.getThreadLocal();
const auto allSimulators = m_plugins->getAvailableSimulatorPlugins(); const auto allSimulators = m_plugins->getAvailableSimulatorPlugins();
for (const CSimulatorPluginInfo &s : allSimulators) for (const CSimulatorPluginInfo &s : allSimulators)
@@ -680,7 +656,7 @@ namespace BlackCore
void CContextSimulator::ps_simulatorStarted(const CSimulatorPluginInfo &info) void CContextSimulator::ps_simulatorStarted(const CSimulatorPluginInfo &info)
{ {
stopSimulatorListeners(); stopSimulatorListeners();
loadSimulatorPlugin(info, false); loadSimulatorPlugin(info);
} }
void CContextSimulator::stopSimulatorListeners() void CContextSimulator::stopSimulatorListeners()

View File

@@ -178,14 +178,14 @@ namespace BlackCore
//! Reads list of enabled simulators, starts listeners //! Reads list of enabled simulators, starts listeners
void restoreSimulatorPlugins(); void restoreSimulatorPlugins();
//! Load plugin, if required start listeners before //! Load plugin and connect
bool loadSimulatorPlugin(const BlackMisc::Simulation::CSimulatorPluginInfo &simulatorPluginInfo, bool withListeners); bool loadSimulatorPlugin(const BlackMisc::Simulation::CSimulatorPluginInfo &simulatorPluginInfo);
//! Unload plugin, if desired restart listeners //! Unload plugin, if desired restart listeners
void unloadSimulatorPlugin(); void unloadSimulatorPlugin();
//! Listen for single simulator //! Listen for single simulator
void listenForSimulator(const BlackMisc::Simulation::CSimulatorPluginInfo &simulatorInfo); bool listenForSimulator(const BlackMisc::Simulation::CSimulatorPluginInfo &simulatorInfo);
//! Listen for all simulators //! Listen for all simulators
void listenForAllSimulators(); void listenForAllSimulators();