From e9f52bb82902172e367bf6e0048772fd004cc338 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 16 Nov 2016 03:22:36 +0100 Subject: [PATCH] refs #806, use token based approach for mapping component Allows to respond quickly without overloading (DBus) --- src/blackgui/components/mappingcomponent.cpp | 71 ++++++++++--------- src/blackgui/components/mappingcomponent.h | 29 ++++---- .../components/settingsviewupdatetimes.ui | 18 ++++- 3 files changed, 70 insertions(+), 48 deletions(-) diff --git a/src/blackgui/components/mappingcomponent.cpp b/src/blackgui/components/mappingcomponent.cpp index 5255cfa25..f1eadf4d3 100644 --- a/src/blackgui/components/mappingcomponent.cpp +++ b/src/blackgui/components/mappingcomponent.cpp @@ -78,7 +78,7 @@ namespace BlackGui connect(ui->tvp_RenderedAircraft, &CSimulatedAircraftView::modelDataChanged, this, &CMappingComponent::ps_onRowCountChanged); connect(ui->tvp_RenderedAircraft, &CSimulatedAircraftView::clicked, this, &CMappingComponent::ps_onAircraftSelectedInView); - connect(ui->tvp_RenderedAircraft, &CSimulatedAircraftView::requestUpdate, this, &CMappingComponent::ps_markRenderedViewForUpdate); + connect(ui->tvp_RenderedAircraft, &CSimulatedAircraftView::requestUpdate, this, &CMappingComponent::ps_tokenBucketUpdate); connect(ui->tvp_RenderedAircraft, &CSimulatedAircraftView::requestTextMessageWidget, this, &CMappingComponent::requestTextMessageWidget); connect(ui->tvp_RenderedAircraft, &CSimulatedAircraftView::requestEnableAircraft, this, &CMappingComponent::ps_onMenuToggleEnableAircraft); connect(ui->tvp_RenderedAircraft, &CSimulatedAircraftView::requestFastPositionUpdates, this, &CMappingComponent::ps_onMenuChangeFastPositionUpdates); @@ -102,17 +102,17 @@ namespace BlackGui // Updates ui->tvp_AircraftModels->setDisplayAutomatically(false); this->ps_settingsChanged(); - connect(&m_updateTimer, &QTimer::timeout, this, &CMappingComponent::ps_backgroundUpdate); + connect(&m_updateTimer, &QTimer::timeout, this, &CMappingComponent::ps_timerUpdate); connect(sGui->getIContextSimulator(), &IContextSimulator::modelSetChanged, this, &CMappingComponent::ps_onModelSetChanged); - connect(sGui->getIContextSimulator(), &IContextSimulator::modelMatchingCompleted, this, &CMappingComponent::ps_markRenderedAircraftForUpdate); - connect(sGui->getIContextSimulator(), &IContextSimulator::aircraftRenderingChanged, this, &CMappingComponent::ps_markRenderedAircraftForUpdate); - connect(sGui->getIContextSimulator(), &IContextSimulator::airspaceSnapshotHandled, this, &CMappingComponent::ps_markRenderedViewForUpdate); + connect(sGui->getIContextSimulator(), &IContextSimulator::modelMatchingCompleted, this, &CMappingComponent::ps_tokenBucketUpdateAircraft); + connect(sGui->getIContextSimulator(), &IContextSimulator::aircraftRenderingChanged, this, &CMappingComponent::ps_tokenBucketUpdateAircraft); + connect(sGui->getIContextSimulator(), &IContextSimulator::airspaceSnapshotHandled, this, &CMappingComponent::ps_tokenBucketUpdate); connect(sGui->getIContextSimulator(), &IContextSimulator::addingRemoteModelFailed, this, &CMappingComponent::ps_addingRemoteAircraftFailed); connect(sGui->getIContextNetwork(), &IContextNetwork::changedRemoteAircraftModel, this, &CMappingComponent::ps_onRemoteAircraftModelChanged); - connect(sGui->getIContextNetwork(), &IContextNetwork::changedRemoteAircraftEnabled, this, &CMappingComponent::ps_markRenderedAircraftForUpdate); - connect(sGui->getIContextNetwork(), &IContextNetwork::changedFastPositionUpdates, this, &CMappingComponent::ps_markRenderedAircraftForUpdate); - connect(sGui->getIContextNetwork(), &IContextNetwork::removedAircraft, this, &CMappingComponent::ps_markRenderedViewForUpdate); + connect(sGui->getIContextNetwork(), &IContextNetwork::changedRemoteAircraftEnabled, this, &CMappingComponent::ps_tokenBucketUpdateAircraft); + connect(sGui->getIContextNetwork(), &IContextNetwork::changedFastPositionUpdates, this, &CMappingComponent::ps_tokenBucketUpdateAircraft); + connect(sGui->getIContextNetwork(), &IContextNetwork::removedAircraft, this, &CMappingComponent::ps_tokenBucketUpdate); connect(sGui->getIContextNetwork(), &IContextNetwork::connectionStatusChanged, this, &CMappingComponent::ps_onConnectionStatusChanged); // requires simulator context @@ -177,7 +177,7 @@ namespace BlackGui if (!saFromBackend.hasValidCallsign()) { return; } // obviously deleted if (index.contains(CSimulatedAircraft::IndexEnabled)) { - bool enabled = sa.propertyByIndex(index).toBool(); + const bool enabled = sa.propertyByIndex(index).toBool(); if (saFromBackend.isEnabled() == enabled) { return; } CLogMessage(this).info("Request to %1 aircraft %2") << (enabled ? "enable" : "disable") << saFromBackend.getCallsign().toQString(); sGui->getIContextNetwork()->updateAircraftEnabled(saFromBackend.getCallsign(), enabled); @@ -207,7 +207,7 @@ namespace BlackGui ui->lbl_AircraftIconDisplayed->setText(""); ui->lbl_AircraftIconDisplayed->setToolTip(model.getDescription()); const QString modelString(model.getModelString()); - CPixmap pm = sGui->getIContextSimulator()->iconForModel(modelString); + const CPixmap pm = sGui->getIContextSimulator()->iconForModel(modelString); if (pm.isNull()) { ui->lbl_AircraftIconDisplayed->setPixmap(CIcons::crossWhite16()); @@ -350,7 +350,7 @@ namespace BlackGui void CMappingComponent::ps_onRemoteAircraftModelChanged(const CSimulatedAircraft &aircraft, const CIdentifier &originator) { if (CIdentifiable::isMyIdentifier(originator)) { return; } - this->ps_markRenderedAircraftForUpdate(aircraft); + this->ps_tokenBucketUpdateAircraft(aircraft); } void CMappingComponent::ps_onConnectionStatusChanged(INetwork::ConnectionStatus from, INetwork::ConnectionStatus to) @@ -358,7 +358,7 @@ namespace BlackGui Q_UNUSED(from); if (INetwork::isDisconnectedStatus(to)) { - this->ps_markRenderedViewForUpdate(); + this->tokenBucketUpdate(true); ui->tvp_RenderedAircraft->clear(); } } @@ -379,22 +379,9 @@ namespace BlackGui } } - void CMappingComponent::ps_markRenderedAircraftForUpdate(const CSimulatedAircraft &aircraft) - { - Q_UNUSED(aircraft); - m_missedRenderedAircraftUpdate = true; - } - - void CMappingComponent::ps_markRenderedViewForUpdate() - { - m_missedRenderedAircraftUpdate = true; - m_updateTimer.start(); - this->ps_backgroundUpdate(); - } - void CMappingComponent::ps_addingRemoteAircraftFailed(const CSimulatedAircraft &aircraft, const CStatusMessage &message) { - m_missedRenderedAircraftUpdate = true; + this->tokenBucketUpdate(true); Q_UNUSED(aircraft); Q_UNUSED(message); } @@ -418,6 +405,7 @@ namespace BlackGui void CMappingComponent::updateRenderedAircraftView(bool forceUpdate) { + m_updateTimer.start(); // restart if (!forceUpdate && !this->isVisibleWidget()) { m_missedRenderedAircraftUpdate = true; @@ -436,14 +424,31 @@ namespace BlackGui } } - void CMappingComponent::ps_backgroundUpdate() + void CMappingComponent::ps_tokenBucketUpdateAircraft(const CSimulatedAircraft &aircraft) { - if (this->m_missedRenderedAircraftUpdate) - { - // update, normally when view is invisible, - // but we want an update from time to have some data when user switches to view - this->updateRenderedAircraftView(true); - } + Q_UNUSED(aircraft); + this->tokenBucketUpdate(true); + } + + void CMappingComponent::ps_timerUpdate() + { + this->tokenBucketUpdate(false); + } + + void CMappingComponent::ps_tokenBucketUpdate() + { + this->tokenBucketUpdate(true); + } + + void CMappingComponent::tokenBucketUpdate(bool markForUpdate) + { + if (markForUpdate) { this->m_missedRenderedAircraftUpdate = true; } + if (!this->m_missedRenderedAircraftUpdate) { return; } + if (!this->m_bucket.tryConsume()) { return; } + + // update, normally when view is invisible, + // but we want an update from time to have some data when user switches to view + this->updateRenderedAircraftView(true); } void CMappingComponent::ps_settingsChanged() diff --git a/src/blackgui/components/mappingcomponent.h b/src/blackgui/components/mappingcomponent.h index 5e8afd214..5d82370d0 100644 --- a/src/blackgui/components/mappingcomponent.h +++ b/src/blackgui/components/mappingcomponent.h @@ -13,6 +13,7 @@ #define BLACKGUI_COMPONENTS_MAPPINGCOMPONENT_H #include "blackcore/network.h" +#include "blackcore/tokenbucket.h" #include "blackgui/blackguiexport.h" #include "blackgui/components/enablefordockwidgetinfoarea.h" #include "blackgui/settings/viewupdatesettings.h" @@ -116,17 +117,17 @@ namespace BlackGui //! Highlight in simulator void ps_onMenuHighlightInSimulator(const BlackMisc::Simulation::CSimulatedAircraft &aircraft); - //! Update with next cycle - void ps_markRenderedAircraftForUpdate(const BlackMisc::Simulation::CSimulatedAircraft &aircraft); - - //! Update with next cycle - void ps_markRenderedViewForUpdate(); - //! Adding a remote aircraft failed void ps_addingRemoteAircraftFailed(const BlackMisc::Simulation::CSimulatedAircraft &aircraft, const BlackMisc::CStatusMessage &message); - //! Updated by timer - void ps_backgroundUpdate(); + //! Timer update + void ps_timerUpdate(); + + //! Token bucket based update + void ps_tokenBucketUpdate(); + + //! Token bucket based update + void ps_tokenBucketUpdateAircraft(const BlackMisc::Simulation::CSimulatedAircraft &aircraft); //! Settings have been changed void ps_settingsChanged(); @@ -135,6 +136,9 @@ namespace BlackGui void ps_connectionStatusChanged(BlackCore::INetwork::ConnectionStatus from, BlackCore::INetwork::ConnectionStatus to); private: + //! Token bucket based update + void tokenBucketUpdate(bool markForUpdate); + //! Identifier for data send from this component BlackMisc::CIdentifier mappingIdentifier(); @@ -146,10 +150,11 @@ namespace BlackGui QScopedPointer ui; BlackMisc::CSettingReadOnly m_settings { this, &CMappingComponent::ps_settingsChanged }; //!< settings changed - bool m_missedRenderedAircraftUpdate = true; //! Rendered aircraft need update - QTimer m_updateTimer { this }; - BlackGui::Views::CCheckBoxDelegate *m_currentMappingsViewDelegate = nullptr; //! checkbox in view - BlackMisc::CIdentifier m_identifier; + bool m_missedRenderedAircraftUpdate = true; //! Rendered aircraft need update + QTimer m_updateTimer { this }; + BlackCore::CTokenBucket m_bucket { 3, BlackMisc::PhysicalQuantities::CTime(5.0, BlackMisc::PhysicalQuantities::CTimeUnit::s()), 1}; + BlackGui::Views::CCheckBoxDelegate *m_currentMappingsViewDelegate = nullptr; //! checkbox in view + BlackMisc::CIdentifier m_identifier; }; } // namespace } // namespace diff --git a/src/blackgui/components/settingsviewupdatetimes.ui b/src/blackgui/components/settingsviewupdatetimes.ui index ef66ff32d..4ff966711 100644 --- a/src/blackgui/components/settingsviewupdatetimes.ui +++ b/src/blackgui/components/settingsviewupdatetimes.ui @@ -6,8 +6,8 @@ 0 0 - 184 - 159 + 168 + 123 @@ -19,7 +19,19 @@ QFrame::Raised - + + + 1 + + + 1 + + + 1 + + + 1 +