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());
}
bool IContextSimulator::isSimulatorSimulating() const
{
if (!isSimulatorAvailable() || !getSimulatorStatusEnum().testFlag(ISimulator::Simulating)) { return false; }
return true;
}
} // namespace
} // namespace

View File

@@ -183,6 +183,9 @@ namespace BlackCore
//! Simulator avialable (driver available)?
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
virtual BlackMisc::CPixmap iconForModel(const QString &modelString) const = 0;

View File

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

View File

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