From c92a8ea32c766f6a3d70d75b60d661f2576f8765 Mon Sep 17 00:00:00 2001 From: Roland Winklmeier Date: Thu, 14 Jul 2016 15:58:00 +0200 Subject: [PATCH] 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 --- src/blackcore/context/contextsimulator.cpp | 7 ++ src/blackcore/context/contextsimulator.h | 3 + .../context/contextsimulatorimpl.cpp | 80 ++++++------------- src/blackcore/context/contextsimulatorimpl.h | 1 + 4 files changed, 37 insertions(+), 54 deletions(-) diff --git a/src/blackcore/context/contextsimulator.cpp b/src/blackcore/context/contextsimulator.cpp index 6f0c2c63a..52c976ee5 100644 --- a/src/blackcore/context/contextsimulator.cpp +++ b/src/blackcore/context/contextsimulator.cpp @@ -59,5 +59,12 @@ namespace BlackCore { return static_cast(this->getSimulatorStatus()); } + + bool IContextSimulator::isSimulatorSimulating() const + { + if (!isSimulatorAvailable() || !getSimulatorStatusEnum().testFlag(ISimulator::Simulating)) { return false; } + return true; + } + } // namespace } // namespace diff --git a/src/blackcore/context/contextsimulator.h b/src/blackcore/context/contextsimulator.h index fe1518a87..3621f4da5 100644 --- a/src/blackcore/context/contextsimulator.h +++ b/src/blackcore/context/contextsimulator.h @@ -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; diff --git a/src/blackcore/context/contextsimulatorimpl.cpp b/src/blackcore/context/contextsimulatorimpl.cpp index c5d79f968..f3227e6ce 100644 --- a/src/blackcore/context/contextsimulatorimpl.cpp +++ b/src/blackcore/context/contextsimulatorimpl.cpp @@ -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; } diff --git a/src/blackcore/context/contextsimulatorimpl.h b/src/blackcore/context/contextsimulatorimpl.h index 986628f90..ce46b91c1 100644 --- a/src/blackcore/context/contextsimulatorimpl.h +++ b/src/blackcore/context/contextsimulatorimpl.h @@ -206,6 +206,7 @@ namespace BlackCore BlackMisc::CRegularThread m_listenersThread; BlackCore::CWeatherManager m_weatherManager { this }; BlackMisc::CSetting m_enabledSimulators { this, &CContextSimulator::restoreSimulatorPlugins }; + bool m_initallyAddAircrafts = false; }; } // namespace } // namespace