Refactor CContextSimulator::loadSimulatorPlugin

When a plugin is loaded, existing aircrafts should not be added
immediatly, but as soon as the simulator is simulating.
CContextSimulator also handles now the case that no simulator is
attached and all aircraft slots are now doing nothing instead of
raising an assert.

refs #549
This commit is contained in:
Roland Winklmeier
2016-07-14 15:58:00 +02:00
parent 3aee6174ac
commit c92a8ea32c
4 changed files with 37 additions and 54 deletions

View File

@@ -59,5 +59,12 @@ namespace BlackCore
{ {
return static_cast<ISimulator::SimulatorStatus>(this->getSimulatorStatus()); return static_cast<ISimulator::SimulatorStatus>(this->getSimulatorStatus());
} }
bool IContextSimulator::isSimulatorSimulating() const
{
if (!isSimulatorAvailable() || !getSimulatorStatusEnum().testFlag(ISimulator::Simulating)) { return false; }
return true;
}
} // namespace } // namespace
} // namespace } // namespace

View File

@@ -183,6 +183,9 @@ namespace BlackCore
//! Simulator avialable (driver available)? //! Simulator avialable (driver available)?
bool isSimulatorAvailable() const { return BlackConfig::CBuildConfig::isCompiledWithFlightSimulatorSupport() && !getSimulatorPluginInfo().isUnspecified(); } bool isSimulatorAvailable() const { return BlackConfig::CBuildConfig::isCompiledWithFlightSimulatorSupport() && !getSimulatorPluginInfo().isUnspecified(); }
//! Is available simulator simulating? Returns false if no simulator is available
bool isSimulatorSimulating() const;
//! Icon representing the model //! Icon representing the model
virtual BlackMisc::CPixmap iconForModel(const QString &modelString) const = 0; virtual BlackMisc::CPixmap iconForModel(const QString &modelString) const = 0;

View File

@@ -368,24 +368,14 @@ namespace BlackCore
Q_ASSERT(c); Q_ASSERT(c);
Q_UNUSED(c); Q_UNUSED(c);
// use network to initally add aircraft // Once the simulator signaled it is ready to simulate, add all known aircrafts.
IContextNetwork *networkContext = this->getIContextNetwork(); m_initallyAddAircrafts = true;
Q_ASSERT(networkContext); // try to connect to simulator
Q_ASSERT(networkContext->isLocalObject()); simulator->connectTo();
// initially add aircraft
for (const CSimulatedAircraft &simulatedAircraft : networkContext->getAircraftInRange())
{
Q_ASSERT(!simulatedAircraft.getCallsign().isEmpty());
simulator->logicallyAddRemoteAircraft(simulatedAircraft);
}
// when everything is set up connected, update the current plugin info // when everything is set up connected, update the current plugin info
m_simulatorPlugin.first = simulatorInfo; m_simulatorPlugin.first = simulatorInfo;
m_simulatorPlugin.second = simulator; m_simulatorPlugin.second = simulator;
// try to connect to simulator
simulator->connectTo();
emit simulatorPluginChanged(simulatorInfo); emit simulatorPluginChanged(simulatorInfo);
CLogMessage(this).info("Simulator plugin loaded: %1") << simulatorInfo.toQString(true); CLogMessage(this).info("Simulator plugin loaded: %1") << simulatorInfo.toQString(true);
@@ -481,14 +471,7 @@ namespace BlackCore
void CContextSimulator::ps_addRemoteAircraft(const CSimulatedAircraft &remoteAircraft) void CContextSimulator::ps_addRemoteAircraft(const CSimulatedAircraft &remoteAircraft)
{ {
//! \todo 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 (!isSimulatorSimulating()) { return; }
if (m_simulatorPlugin.first.isUnspecified())
{
// Do something if no simulator is running
return;
}
Q_ASSERT(m_simulatorPlugin.second);
Q_ASSERT(!remoteAircraft.getCallsign().isEmpty()); Q_ASSERT(!remoteAircraft.getCallsign().isEmpty());
m_simulatorPlugin.second->logicallyAddRemoteAircraft(remoteAircraft); m_simulatorPlugin.second->logicallyAddRemoteAircraft(remoteAircraft);
@@ -496,21 +479,29 @@ namespace BlackCore
void CContextSimulator::ps_removedRemoteAircraft(const CCallsign &callsign) void CContextSimulator::ps_removedRemoteAircraft(const CCallsign &callsign)
{ {
// \fixme: This was previously an assert and it should be one again in the future. if (!isSimulatorSimulating()) { return; }
// This slot should not even be called when no simulator is available.
if (m_simulatorPlugin.first.isUnspecified())
{
// Do something if no simulator is running
return;
}
Q_ASSERT(m_simulatorPlugin.second);
m_simulatorPlugin.second->logicallyRemoveRemoteAircraft(callsign); m_simulatorPlugin.second->logicallyRemoveRemoteAircraft(callsign);
} }
void CContextSimulator::ps_onSimulatorStatusChanged(int status) void CContextSimulator::ps_onSimulatorStatusChanged(int status)
{ {
ISimulator::SimulatorStatus statusEnum = ISimulator::statusToEnum(status); ISimulator::SimulatorStatus statusEnum = ISimulator::statusToEnum(status);
if (m_initallyAddAircrafts && statusEnum.testFlag(ISimulator::Simulating))
{
// use network to initally add aircraft
IContextNetwork *networkContext = this->getIContextNetwork();
Q_ASSERT(networkContext);
Q_ASSERT(networkContext->isLocalObject());
// initially add aircraft
const CSimulatedAircraftList aircrafts = networkContext->getAircraftInRange();
for (const CSimulatedAircraft &simulatedAircraft : aircrafts)
{
Q_ASSERT(!simulatedAircraft.getCallsign().isEmpty());
m_simulatorPlugin.second->logicallyAddRemoteAircraft(simulatedAircraft);
}
m_initallyAddAircrafts = false;
}
if (!statusEnum.testFlag(ISimulator::Connected)) if (!statusEnum.testFlag(ISimulator::Connected))
{ {
// we got disconnected, plugin no longer needed // we got disconnected, plugin no longer needed
@@ -521,16 +512,7 @@ namespace BlackCore
void CContextSimulator::ps_textMessagesReceived(const Network::CTextMessageList &textMessages) void CContextSimulator::ps_textMessagesReceived(const Network::CTextMessageList &textMessages)
{ {
// todo: if (!isSimulatorSimulating()) { return; }
// 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.first.isUnspecified())
{
// Do something if no simulator is running
return;
}
Q_ASSERT(m_simulatorPlugin.second);
for (const auto &tm : textMessages) for (const auto &tm : textMessages)
{ {
m_simulatorPlugin.second->displayTextMessage(tm); m_simulatorPlugin.second->displayTextMessage(tm);
@@ -545,29 +527,19 @@ namespace BlackCore
void CContextSimulator::ps_changedRemoteAircraftModel(const CSimulatedAircraft &aircraft) void CContextSimulator::ps_changedRemoteAircraftModel(const CSimulatedAircraft &aircraft)
{ {
Q_ASSERT(m_simulatorPlugin.second); if (!isSimulatorSimulating()) { return; }
m_simulatorPlugin.second->changeRemoteAircraftModel(aircraft); m_simulatorPlugin.second->changeRemoteAircraftModel(aircraft);
} }
void CContextSimulator::ps_changedRemoteAircraftEnabled(const CSimulatedAircraft &aircraft) void CContextSimulator::ps_changedRemoteAircraftEnabled(const CSimulatedAircraft &aircraft)
{ {
Q_ASSERT(m_simulatorPlugin.second); if (!isSimulatorSimulating()) { return; }
m_simulatorPlugin.second->changeRemoteAircraftEnabled(aircraft); m_simulatorPlugin.second->changeRemoteAircraftEnabled(aircraft);
} }
void CContextSimulator::ps_updateSimulatorCockpitFromContext(const CSimulatedAircraft &ownAircraft, const CIdentifier &originator) void CContextSimulator::ps_updateSimulatorCockpitFromContext(const CSimulatedAircraft &ownAircraft, const CIdentifier &originator)
{ {
// todo: if (!isSimulatorSimulating()) { return; }
// 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.first.isUnspecified())
{
// Do something if no simulator is running
return;
}
Q_ASSERT(m_simulatorPlugin.second);
// avoid loops // avoid loops
if (originator.getName().isEmpty() || originator == IContextSimulator::InterfaceName()) { return; } if (originator.getName().isEmpty() || originator == IContextSimulator::InterfaceName()) { return; }

View File

@@ -206,6 +206,7 @@ namespace BlackCore
BlackMisc::CRegularThread m_listenersThread; BlackMisc::CRegularThread m_listenersThread;
BlackCore::CWeatherManager m_weatherManager { this }; BlackCore::CWeatherManager m_weatherManager { this };
BlackMisc::CSetting<BlackCore::Application::TEnabledSimulators> m_enabledSimulators { this, &CContextSimulator::restoreSimulatorPlugins }; BlackMisc::CSetting<BlackCore::Application::TEnabledSimulators> m_enabledSimulators { this, &CContextSimulator::restoreSimulatorPlugins };
bool m_initallyAddAircrafts = false;
}; };
} // namespace } // namespace
} // namespace } // namespace