[traffic] Remove obsolete code

Signed-off-by: mvglasow <michael -at- vonglasow.com>
This commit is contained in:
mvglasow
2025-07-09 20:15:20 +03:00
parent cc58eaa50a
commit e825753487
2 changed files with 8 additions and 474 deletions

View File

@@ -54,33 +54,6 @@ auto constexpr kStorageUpdateInterval = minutes(1);
auto constexpr kTrafficXMLFileName = "traffic.xml";
} // namespace
// TODO no longer needed
#ifdef traffic_dead_code
TrafficManager::CacheEntry::CacheEntry()
: m_isLoaded(false)
// TODO no longer needed
#ifdef traffic_dead_code
, m_dataSize(0)
#endif
, m_retriesCount(0)
, m_isWaitingForResponse(false)
, m_lastAvailability(traffic::TrafficInfo::Availability::Unknown)
{}
TrafficManager::CacheEntry::CacheEntry(time_point<steady_clock> const & requestTime)
: m_isLoaded(false)
// TODO no longer needed
#ifdef traffic_dead_code
, m_dataSize(0)
#endif
, m_lastActiveTime(requestTime)
, m_lastRequestTime(requestTime)
, m_retriesCount(0)
, m_isWaitingForResponse(true)
, m_lastAvailability(traffic::TrafficInfo::Availability::Unknown)
{}
#endif
TrafficManager::TrafficManager(DataSource & dataSource, CountryInfoGetterFn countryInfoGetter,
const CountryParentNameGetterFn &countryParentNameGetter,
GetMwmsByRectFn const & getMwmsByRectFn, size_t maxCacheSizeBytes,
@@ -92,10 +65,6 @@ TrafficManager::TrafficManager(DataSource & dataSource, CountryInfoGetterFn coun
, m_routingSession(routingSession)
, m_currentDataVersion(0)
, m_state(TrafficState::Disabled)
// TODO no longer needed
#ifdef traffic_dead_code
, m_maxCacheSizeBytes(maxCacheSizeBytes)
#endif
, m_isRunning(true)
, m_isPaused(false)
, m_thread(&TrafficManager::ThreadRoutine, this)
@@ -189,36 +158,11 @@ void TrafficManager::SetEnabled(bool enabled)
void TrafficManager::Clear()
{
// TODO what should we do about subscriptions?
{
std::lock_guard<std::mutex> lock(m_mutex);
LOG(LINFO, ("Messages in cache:", m_messageCache.size()));
LOG(LINFO, ("Feeds in queue:", m_feedQueue.size()));
LOG(LINFO, ("MWMs with coloring:", m_allMwmColoring.size()));
LOG(LINFO, ("Clearing..."));
// TODO no longer needed
#ifdef traffic_dead_code
m_currentCacheSizeBytes = 0;
#endif
m_messageCache.clear();
m_feedQueue.clear();
// TODO no longer needed
#ifdef traffic_dead_code
m_mwmCache.clear();
#endif
// TODO figure out which of the ones below we still need
m_lastDrapeMwmsByRect.clear();
m_lastPositionMwmsByRect.clear();
#ifdef traffic_dead_code
m_requestedMwms.clear();
m_trafficETags.clear();
#endif
LOG(LINFO, ("Messages in cache:", m_messageCache.size()));
LOG(LINFO, ("Feeds in queue:", m_feedQueue.size()));
LOG(LINFO, ("MWMs with coloring:", m_allMwmColoring.size()));
}
OnTrafficDataUpdate();
}
@@ -235,27 +179,7 @@ void TrafficManager::SetCurrentDataVersion(int64_t dataVersion)
void TrafficManager::OnMwmDeregistered(platform::LocalCountryFile const & countryFile)
{
if (!IsEnabled())
return;
// TODO no longer needed
#ifdef traffic_dead_code
{
std::lock_guard<std::mutex> lock(m_mutex);
MwmSet::MwmId mwmId;
for (auto const & cacheEntry : m_mwmCache)
{
if (cacheEntry.first.IsDeregistered(countryFile))
{
mwmId = cacheEntry.first;
break;
}
}
ClearCache(mwmId);
}
#endif
// TODO we dont need this any more (called by Framework::OnMapDeregistered())
}
void TrafficManager::OnDestroySurface()
@@ -772,42 +696,8 @@ void TrafficManager::ThreadRoutine()
// set new coloring for MWMs
// `m_mutex` is obtained inside the method, no need to do it here
// TODO drop the argument, use class member inside method
if (hasUpdates)
OnTrafficDataUpdate();
// TODO no longer needed
#ifdef traffic_dead_code
for (auto const & mwm : mwms)
{
if (!mwm.IsAlive())
continue;
traffic::TrafficInfo info(mwm, m_currentDataVersion);
std::string tag;
{
std::lock_guard<std::mutex> lock(m_mutex);
tag = m_trafficETags[mwm];
}
if (info.ReceiveTrafficData(tag))
{
OnTrafficDataResponse(std::move(info));
}
else
{
LOG(LWARNING, ("Traffic request failed. Mwm =", mwm));
OnTrafficRequestFailed(std::move(info));
}
{
std::lock_guard<std::mutex> lock(m_mutex);
m_trafficETags[mwm] = tag;
}
}
mwms.clear();
#endif
}
// Calling Unsubscribe() from the worker thread on exit makes thread synchronization easier
Unsubscribe();
@@ -866,62 +756,6 @@ bool TrafficManager::WaitForRequest()
return true;
}
// TODO no longer needed
#ifdef traffic_dead_code
void TrafficManager::RequestTrafficSubscription()
{
if ((m_activeDrapeMwms.empty() && m_activePositionMwms.empty() && m_activeRoutingMwms.empty())
|| !IsEnabled() || IsInvalidState() || m_isPaused)
{
return;
}
ForEachActiveMwm([this](MwmSet::MwmId const & mwmId) {
ASSERT(mwmId.IsAlive(), ());
// TODO no longer needed
#ifdef traffic_dead_code
m_mwmCache.insert(mwmId);
#endif
});
m_condition.notify_one();
// TODO no longer needed
#ifdef traffic_dead_code
UpdateState();
#endif
}
void TrafficManager::OnTrafficRequestFailed(traffic::TrafficInfo && info)
{
std::lock_guard<std::mutex> lock(m_mutex);
auto it = m_mwmCache.find(info.GetMwmId());
if (it == m_mwmCache.end())
return;
it->second.m_isWaitingForResponse = false;
it->second.m_lastAvailability = info.GetAvailability();
if (info.GetAvailability() == traffic::TrafficInfo::Availability::Unknown &&
!it->second.m_isLoaded)
{
if (m_activeDrapeMwms.find(info.GetMwmId()) != m_activeDrapeMwms.cend() ||
m_activeRoutingMwms.find(info.GetMwmId()) != m_activeRoutingMwms.cend())
{
if (it->second.m_retriesCount < kMaxRetriesCount)
RequestTrafficData(info.GetMwmId(), true /* force */);
++it->second.m_retriesCount;
}
else
{
it->second.m_retriesCount = 0;
}
}
UpdateState();
}
#endif
void TrafficManager::OnTrafficDataUpdate()
{
bool feedQueueEmpty = false;
@@ -1012,11 +846,6 @@ void TrafficManager::OnTrafficDataUpdate()
LOG(LINFO, ("Setting new coloring for", mwmId, "with", coloring.size(), "entries"));
traffic::TrafficInfo info(mwmId, std::move(coloring));
// TODO no longer needed
#ifdef traffic_dead_code
UpdateState();
#endif
if (notifyDrape)
{
/*
@@ -1061,46 +890,6 @@ void TrafficManager::OnTrafficDataUpdate()
});
}
// TODO no longer needed
#ifdef traffic_dead_code
void TrafficManager::OnTrafficDataResponse(traffic::TrafficInfo && info)
{
{
std::lock_guard<std::mutex> lock(m_mutex);
auto it = m_mwmCache.find(info.GetMwmId());
if (it == m_mwmCache.end())
return;
it->second.m_isLoaded = true;
it->second.m_lastResponseTime = steady_clock::now();
it->second.m_isWaitingForResponse = false;
it->second.m_lastAvailability = info.GetAvailability();
if (!info.GetColoring().empty())
{
// Update cache.
size_t constexpr kElementSize = sizeof(traffic::TrafficInfo::RoadSegmentId) + sizeof(traffic::SpeedGroup);
size_t const dataSize = info.GetColoring().size() * kElementSize;
m_currentCacheSizeBytes += (dataSize - it->second.m_dataSize);
it->second.m_dataSize = dataSize;
ShrinkCacheToAllowableSize();
}
UpdateState();
}
if (!info.GetColoring().empty())
{
m_drapeEngine.SafeCall(&df::DrapeEngine::UpdateTraffic,
static_cast<traffic::TrafficInfo const &>(info));
// Update traffic colors for routing.
m_observer.OnTrafficInfoAdded(std::move(info));
}
}
#endif
void TrafficManager::GetActiveMwms(std::set<MwmSet::MwmId> & activeMwms)
{
std::lock_guard<std::mutex> lock(m_mutex);
@@ -1114,60 +903,6 @@ void TrafficManager::UniteActiveMwms(std::set<MwmSet::MwmId> & activeMwms) const
activeMwms.insert(m_activeRoutingMwms.cbegin(), m_activeRoutingMwms.cend());
}
// TODO no longer needed
#ifdef traffic_dead_code
void TrafficManager::ShrinkCacheToAllowableSize()
{
// Calculating number of different active mwms.
std::set<MwmSet::MwmId> activeMwms;
UniteActiveMwms(activeMwms);
size_t const numActiveMwms = activeMwms.size();
if (m_currentCacheSizeBytes > m_maxCacheSizeBytes && m_mwmCache.size() > numActiveMwms)
{
std::multimap<time_point<steady_clock>, MwmSet::MwmId> seenTimings;
for (auto const & mwmInfo : m_mwmCache)
seenTimings.insert(std::make_pair(mwmInfo.second.m_lastActiveTime, mwmInfo.first));
auto itSeen = seenTimings.begin();
while (m_currentCacheSizeBytes > m_maxCacheSizeBytes && m_mwmCache.size() > numActiveMwms)
{
ClearCache(itSeen->second);
++itSeen;
}
}
}
void TrafficManager::ClearCache(MwmSet::MwmId const & mwmId)
{
auto const it = m_mwmCache.find(mwmId);
if (it == m_mwmCache.end())
return;
if (it->second.m_isLoaded)
{
// TODO no longer needed
#ifdef traffic_dead_code
ASSERT_GREATER_OR_EQUAL(m_currentCacheSizeBytes, it->second.m_dataSize, ());
m_currentCacheSizeBytes -= it->second.m_dataSize;
#endif
m_drapeEngine.SafeCall(&df::DrapeEngine::ClearTrafficCache, mwmId);
GetPlatform().RunTask(Platform::Thread::Gui, [this, mwmId]()
{
m_observer.OnTrafficInfoRemoved(mwmId);
});
}
m_mwmCache.erase(it);
m_trafficETags.erase(mwmId);
m_activeDrapeMwms.erase(mwmId);
m_activeRoutingMwms.erase(mwmId);
m_lastDrapeMwmsByRect.clear();
m_lastRoutingMwmsByRect.clear();
}
#endif
bool TrafficManager::IsEnabled() const
{
return m_state != TrafficState::Disabled;
@@ -1178,67 +913,6 @@ bool TrafficManager::IsInvalidState() const
return m_state == TrafficState::NetworkError;
}
// TODO no longer needed
#ifdef traffic_dead_code
void TrafficManager::UpdateState()
{
if (!IsEnabled() || IsInvalidState())
return;
auto const currentTime = steady_clock::now();
auto maxPassedTime = steady_clock::duration::zero();
bool waiting = false;
bool networkError = false;
bool expiredApp = false;
bool expiredData = false;
bool noData = false;
for (MwmSet::MwmId const & mwmId : m_activeDrapeMwms)
{
auto it = m_mwmCache.find(mwmId);
ASSERT(it != m_mwmCache.end(), ());
if (it->second.m_isWaitingForResponse)
{
waiting = true;
}
else
{
expiredApp |= it->second.m_lastAvailability == traffic::TrafficInfo::Availability::ExpiredApp;
expiredData |= it->second.m_lastAvailability == traffic::TrafficInfo::Availability::ExpiredData;
noData |= it->second.m_lastAvailability == traffic::TrafficInfo::Availability::NoData;
if (it->second.m_isLoaded)
{
auto const timeSinceLastResponse = currentTime - it->second.m_lastResponseTime;
if (timeSinceLastResponse > maxPassedTime)
maxPassedTime = timeSinceLastResponse;
}
else if (it->second.m_retriesCount >= kMaxRetriesCount)
{
networkError = true;
}
}
}
if (networkError || maxPassedTime >= kNetworkErrorTimeout)
ChangeState(TrafficState::NetworkError);
else if (waiting)
ChangeState(TrafficState::WaitingData);
else if (expiredApp)
ChangeState(TrafficState::ExpiredApp);
else if (expiredData)
ChangeState(TrafficState::ExpiredData);
else if (noData)
ChangeState(TrafficState::NoData);
else if (maxPassedTime >= kOutdatedDataTimeout)
ChangeState(TrafficState::Outdated);
else
ChangeState(TrafficState::Enabled);
}
#endif
void TrafficManager::ChangeState(TrafficState newState)
{
if (m_state == newState)

View File

@@ -46,7 +46,13 @@ public:
/**
* @brief Global state of traffic information.
*/
// TODO apart from `Disabled` and `Enabled`, all states are obsolete
/*
* TODO clean out obsolete states.
* Only `Disabled` and `Enabled` are currently used, but some might be reactivated in the future
* and platforms (android/iphone) still evaluate all states.
* `ExpiredData` is definitely obsolete, as traffic data is no longer dependent on a particular
* map version, but still evaluated by android/iphone code.
*/
enum class TrafficState
{
/** Traffic is disabled, no traffic data will be retrieved or considered for routing. */
@@ -294,64 +300,6 @@ public:
void Clear();
private:
// TODO no longer needed
#ifdef traffic_dead_code
/**
* @brief Holds information about pending or previous traffic requests pertaining to an MWM.
*/
struct CacheEntry
{
CacheEntry();
explicit CacheEntry(std::chrono::time_point<std::chrono::steady_clock> const & requestTime);
/**
* @brief Whether we have traffic data for this MWM.
*/
bool m_isLoaded;
/**
* @brief The amount of memory occupied by the coloring for this MWM.
*/
size_t m_dataSize;
/**
* @brief When the last update request occurred, not including forced updates.
*
* This timestamp is the basis for eliminating the oldest entries from the cache.
*/
std::chrono::time_point<std::chrono::steady_clock> m_lastActiveTime;
/**
* @brief When the last update request occurred, including forced updates.
*
* This timestamp is the basis for determining whether an update is needed.
*/
std::chrono::time_point<std::chrono::steady_clock> m_lastRequestTime;
/**
* @brief When the last response was received.
*
* This timestamp is the basis for determining whether a network request timed out, or if data is outdated.
*/
std::chrono::time_point<std::chrono::steady_clock> m_lastResponseTime;
/**
* @brief The number of failed traffic requests for this MWM.
*
* Reset when the MWM becomes inactive.
*/
int m_retriesCount;
/**
* @brief Whether a request is currently pending for this MWM.
*
* Set to `true` when a request is scheduled, reverted to `false` when a response is received or the request fails.
*/
bool m_isWaitingForResponse;
traffic::TrafficInfo::Availability m_lastAvailability;
};
#endif
/**
* @brief Ensures every TraFF source has a subscription covering all currently active MWMs.
@@ -453,27 +401,6 @@ private:
*/
void OnTrafficDataUpdate();
// TODO no longer needed
#ifdef traffic_dead_code
void OnTrafficDataResponse(traffic::TrafficInfo && info);
/**
* @brief Processes a failed traffic request.
*
* This method gets called when a traffic request has failed.
*
* It updates the `m_isWaitingForResponse` and `m_lastAvailability` of `info.
*
* If the MWM is no longer active, this method returns immediately after that.
*
* If the retry limit has not been reached, the MWM is re-inserted into the list by calling
* `RequestTrafficSubscription(MwmSet::MwmId, bool)` with `force` set to true. Otherwise, the retry count
* is reset and the state updated accordingly.
*
* @param info
*/
void OnTrafficRequestFailed(traffic::TrafficInfo && info);
#endif
/**
* @brief Updates `activeMwms` and requests traffic data.
*
@@ -497,49 +424,6 @@ private:
// This is a group of methods that haven't their own synchronization inside.
// TODO no longer needed
#ifdef traffic_dead_code
/**
* @brief Requests a refresh of traffic subscriptions to match all currently active MWMs.
*
* The actual call to the TraFF sources is performed asynchronously on a separate thread.
*
* The method does nothing if the `TrafficManager` instance is disabled, paused, in an invalid
* state (`NetworkError`) or if neither the rendering engine nor the routing engine have any
* active MWMs.
*
* This method is unsynchronized; the caller must lock `m_mutex` prior to calling it.
*/
void RequestTrafficSubscription();
/**
* @brief Removes traffic data for one specific MWM from the cache.
*
* This would be used when an MWM file gets deregistered and its traffic data is no longer needed.
* With the old MWM traffic architecture (pre-processed sets of segments), this method was also
* used to shrink the cache to stay below a certain size (no longer possible with TraFF, due to
* the data structures being more complex, and also due to re-fetching data being expensive in
* terms of computing time).
*
* @param mwmId The mwmId for which to remove traffic data.
*/
void ClearCache(MwmSet::MwmId const & mwmId);
void ShrinkCacheToAllowableSize();
/**
* @brief Updates the state of the traffic manager based on the state of all MWMs used by the renderer.
*
* This method cycles through the state of all MWMs used by the renderer (MWMs used by the
* routing engine but not by the rendering engine are not considered), examines their traffic
* state and sets the global state accordingly.
*
* For a description of states, see `TrafficState`. The order of states is as follows, the first
* state whose conditions are fulfilled becomes the new state: `TrafficState::NetworkError`,
* `TrafficState::WaitingData`, `TrafficState::ExpiredApp`, `TrafficState::ExpiredData`,
* `TrafficState::NoData`, `TrafficState::Outdated`, `TrafficState::Enabled`.
*/
void UpdateState();
#endif
void ChangeState(TrafficState newState);
bool IsInvalidState() const;
@@ -643,14 +527,6 @@ private:
bool m_hasSimplifiedColorScheme = true;
// TODO no longer needed
#ifdef traffic_dead_code
size_t m_maxCacheSizeBytes;
size_t m_currentCacheSizeBytes = 0;
std::map<MwmSet::MwmId, CacheEntry> m_mwmCache;
#endif
/**
* @brief The TraFF sources from which we get traffic information.
*
@@ -694,24 +570,8 @@ private:
*/
bool m_activeMwmsChanged = false;
// TODO no longer needed
#ifdef traffic_dead_code
// The ETag or entity tag is part of HTTP, the protocol for the World Wide Web.
// It is one of several mechanisms that HTTP provides for web cache validation,
// which allows a client to make conditional requests.
std::map<MwmSet::MwmId, std::string> m_trafficETags;
#endif
std::atomic<bool> m_isPaused;
// TODO no longer needed
#ifdef traffic_dead_code
/**
* @brief MWMs for which to retrieve traffic data.
*/
std::vector<MwmSet::MwmId> m_requestedMwms;
#endif
/**
* @brief Mutex for access to shared members.
*