[traffic] Unsubscribe when traffic manager is disabled

Signed-off-by: mvglasow <michael -at- vonglasow.com>
This commit is contained in:
mvglasow
2025-07-22 20:30:15 +03:00
parent d988ab3326
commit 75c7d146af
2 changed files with 46 additions and 28 deletions

View File

@@ -149,12 +149,15 @@ void TrafficManager::SetEnabled(bool enabled)
if (notifyUpdate) if (notifyUpdate)
OnTrafficDataUpdate(); OnTrafficDataUpdate();
else else
RecalculateSubscription(); RecalculateSubscription(true);
m_canSetMode = false; m_canSetMode = false;
} }
else else
{
Unsubscribe();
m_routingSession.OnTrafficInfoClear(); m_routingSession.OnTrafficInfoClear();
} }
}
void TrafficManager::Clear() void TrafficManager::Clear()
{ {
@@ -256,7 +259,7 @@ void TrafficManager::OnChangeRoutingSessionState(routing::SessionState previous,
} }
} }
void TrafficManager::RecalculateSubscription() void TrafficManager::RecalculateSubscription(bool forceRenewal)
{ {
if (!IsEnabled() || m_isPaused) if (!IsEnabled() || m_isPaused)
return; return;
@@ -273,7 +276,10 @@ void TrafficManager::RecalculateSubscription()
* If UpdateViewport() or UpdateMyPosition() had changes, they would also have updated the * If UpdateViewport() or UpdateMyPosition() had changes, they would also have updated the
* routing MWMs and reset m_activeMwmsChanged. If neither of them had changes and * routing MWMs and reset m_activeMwmsChanged. If neither of them had changes and
* m_activeMwmsChanged is true, it indicates changes in route MWMs which we need to process. * m_activeMwmsChanged is true, it indicates changes in route MWMs which we need to process.
* If `forceRenewal` is true, we set `m_activeMwmsChanged` to true in order to force renewal of
* all subscriptions.
*/ */
m_activeMwmsChanged |= forceRenewal;
if (m_activeMwmsChanged) if (m_activeMwmsChanged)
{ {
if ((m_activeDrapeMwms.empty() && m_activePositionMwms.empty() && m_activeRoutingMwms.empty()) if ((m_activeDrapeMwms.empty() && m_activePositionMwms.empty() && m_activeRoutingMwms.empty())
@@ -489,6 +495,8 @@ void TrafficManager::ReceiveFeed(traffxml::TraffFeed feed)
} }
void TrafficManager::RegisterSource(std::unique_ptr<traffxml::TraffSource> source) void TrafficManager::RegisterSource(std::unique_ptr<traffxml::TraffSource> source)
{
if (IsEnabled())
{ {
std::set<MwmSet::MwmId> activeMwms; std::set<MwmSet::MwmId> activeMwms;
@@ -499,13 +507,14 @@ void TrafficManager::RegisterSource(std::unique_ptr<traffxml::TraffSource> sourc
if (!activeMwms.empty()) if (!activeMwms.empty())
source->SubscribeOrChangeSubscription(activeMwms); source->SubscribeOrChangeSubscription(activeMwms);
}
{ {
std::lock_guard<std::mutex> lock(m_trafficSourceMutex); std::lock_guard<std::mutex> lock(m_trafficSourceMutex);
m_trafficSources.push_back(std::move(source)); m_trafficSources.push_back(std::move(source));
} }
m_isPollNeeded = true; m_isPollNeeded = IsEnabled();
} }
void TrafficManager::PurgeExpiredMessages() void TrafficManager::PurgeExpiredMessages()
@@ -740,8 +749,14 @@ bool TrafficManager::WaitForRequest()
LOG(LINFO, ("nothing to do for now, waiting for timeout or notification")); LOG(LINFO, ("nothing to do for now, waiting for timeout or notification"));
bool const timeout = !m_condition.wait_for(lock, kUpdateInterval, [this] bool const timeout = !m_condition.wait_for(lock, kUpdateInterval, [this]
{ {
// return true for any condition we want to process immediately // return false to continue waiting, true for any condition we want to process immediately
return !m_isRunning || (m_activeMwmsChanged && !IsTestMode()) || !m_feedQueue.empty(); // return immediately if we got terminated
if (!m_isRunning)
return true;
// otherwise continue waiting if we are paused or disabled
if (!IsEnabled() || m_isPaused)
return false;
return (m_activeMwmsChanged && !IsTestMode()) || !m_feedQueue.empty();
}); });
// check again if we got terminated while waiting (or woken up because we got terminated) // check again if we got terminated while waiting (or woken up because we got terminated)
@@ -948,7 +963,7 @@ void TrafficManager::Resume()
return; return;
m_isPaused = false; m_isPaused = false;
RecalculateSubscription(); RecalculateSubscription(false);
} }
void TrafficManager::SetSimplifiedColorScheme(bool simplified) void TrafficManager::SetSimplifiedColorScheme(bool simplified)

View File

@@ -182,21 +182,6 @@ public:
void UpdateViewport(ScreenBase const & screen); void UpdateViewport(ScreenBase const & screen);
void UpdateMyPosition(MyPosition const & myPosition); void UpdateMyPosition(MyPosition const & myPosition);
/**
* @brief Recalculates the TraFF subscription area.
*
* The subscription area needs to be recalculated when the traffic manager goes from disabled to
* enabled, or when it is resumed after being paused, as the subscription area is not updated
* while the traffic manager is disabled or paused.
*
* If the subscription area has changed, this triggers a change of the active TraFF subscription.
*
* No traffic data is discarded, but sources will be polled for an update, which may turn out
* larger than usual if the traffic manager was in disabled/paused state for an extended period of
* time or the subscription area has changed.
*/
void RecalculateSubscription();
/** /**
* @brief Invalidates traffic information for the specified MWM. * @brief Invalidates traffic information for the specified MWM.
* *
@@ -287,6 +272,24 @@ public:
private: private:
/**
* @brief Recalculates the TraFF subscription area.
*
* The subscription area needs to be recalculated when the traffic manager goes from disabled to
* enabled, or when it is resumed after being paused, as the subscription area is not updated
* while the traffic manager is disabled or paused.
*
* If the subscription area has changed, or if `forceRenewal` is true, TraFF subscriptions are
* renewed by calling `SubscribeOrChangeSubscription()`.
*
* No traffic data is discarded, but sources will be polled for an update, which may turn out
* larger than usual if the traffic manager was in disabled/paused state for an extended period of
* time or the subscription area has changed.
*
* @param forceRenewal If true, renew subscriptions even if the subscription area has not changed.
*/
void RecalculateSubscription(bool forceRenewal);
/** /**
* @brief Ensures every TraFF source has a subscription covering all currently active MWMs. * @brief Ensures every TraFF source has a subscription covering all currently active MWMs.
* *