diff options
author | Daria Volvenkova <d.volvenkova@corp.mail.ru> | 2016-12-06 16:45:03 +0300 |
---|---|---|
committer | Daria Volvenkova <d.volvenkova@corp.mail.ru> | 2016-12-07 11:50:22 +0300 |
commit | d57302b1bfe691b748176010d5a9e717435412d1 (patch) | |
tree | 3d4294ff0a9af8cd97733dc7c9b6b2b82aa6cdc7 /map/traffic_manager.cpp | |
parent | 7cca39d8970a488ce466e97c0be634b0f804d346 (diff) |
Fixed traffic states logic.
Diffstat (limited to 'map/traffic_manager.cpp')
-rw-r--r-- | map/traffic_manager.cpp | 114 |
1 files changed, 52 insertions, 62 deletions
diff --git a/map/traffic_manager.cpp b/map/traffic_manager.cpp index 6d3110557f..797a1889b2 100644 --- a/map/traffic_manager.cpp +++ b/map/traffic_manager.cpp @@ -33,7 +33,7 @@ TrafficManager::CacheEntry::CacheEntry(time_point<steady_clock> const & requestT , m_dataSize(0) , m_lastRequestTime(requestTime) , m_retriesCount(0) - , m_isWaitingForResponse(false) + , m_isWaitingForResponse(true) , m_lastAvailability(traffic::TrafficInfo::Availability::Unknown) {} @@ -212,63 +212,54 @@ bool TrafficManager::WaitForRequest(vector<MwmSet::MwmId> & mwms) return false; if (timeout) - { - if (!IsEnabled() || IsInvalidState()) - return true; + RequestTrafficData(); - mwms.reserve(m_activeMwms.size()); - for (auto const & mwmId : m_activeMwms) - mwms.push_back(mwmId); - } - else - { - ASSERT(!m_requestedMwms.empty(), ()); + if (!m_requestedMwms.empty()) mwms.swap(m_requestedMwms); - } + return true; } -void TrafficManager::RequestTrafficData() +void TrafficManager::RequestTrafficData(MwmSet::MwmId const & mwmId) { - if (m_activeMwms.empty()) - return; - - for (auto const & mwmId : m_activeMwms) + bool needRequesting = false; + auto const currentTime = steady_clock::now(); + auto const it = m_mwmCache.find(mwmId); + if (it == m_mwmCache.end()) { - ASSERT(mwmId.IsAlive(), ()); - bool needRequesting = false; - - auto const currentTime = steady_clock::now(); - - auto it = m_mwmCache.find(mwmId); - if (it == m_mwmCache.end()) + needRequesting = true; + m_mwmCache.insert(make_pair(mwmId, CacheEntry(currentTime))); + } + else + { + it->second.m_lastSeenTime = currentTime; + auto const passedSeconds = currentTime - it->second.m_lastRequestTime; + if (passedSeconds >= kUpdateInterval) { needRequesting = true; - m_mwmCache.insert(make_pair(mwmId, CacheEntry(currentTime))); + it->second.m_isWaitingForResponse = true; + it->second.m_lastRequestTime = currentTime; } - else - { - it->second.m_lastSeenTime = currentTime; - auto const passedSeconds = currentTime - it->second.m_lastRequestTime; - if (passedSeconds >= kUpdateInterval) - { - needRequesting = true; - it->second.m_isWaitingForResponse = true; - it->second.m_lastRequestTime = currentTime; - } - } - - if (needRequesting) - RequestTrafficData(mwmId); } - UpdateState(); + if (needRequesting) + { + m_requestedMwms.push_back(mwmId); + m_condition.notify_one(); + UpdateState(); + } } -void TrafficManager::RequestTrafficData(MwmSet::MwmId const & mwmId) +void TrafficManager::RequestTrafficData() { - m_requestedMwms.push_back(mwmId); - m_condition.notify_one(); + if (m_activeMwms.empty() || !IsEnabled() || IsInvalidState()) + return; + + for (auto const & mwmId : m_activeMwms) + { + ASSERT(mwmId.IsAlive(), ()); + RequestTrafficData(mwmId); + } } void TrafficManager::OnTrafficRequestFailed(traffic::TrafficInfo && info) @@ -306,31 +297,30 @@ void TrafficManager::OnTrafficRequestFailed(traffic::TrafficInfo && info) void TrafficManager::OnTrafficDataResponse(traffic::TrafficInfo && info) { - lock_guard<mutex> lock(m_mutex); + { + lock_guard<mutex> lock(m_mutex); - auto it = m_mwmCache.find(info.GetMwmId()); - if (it == m_mwmCache.end()) - return; + 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(); + it->second.m_isLoaded = true; + it->second.m_lastResponseTime = steady_clock::now(); + it->second.m_isWaitingForResponse = false; + it->second.m_lastAvailability = info.GetAvailability(); - // Update cache. - size_t constexpr kElementSize = sizeof(traffic::TrafficInfo::RoadSegmentId) + sizeof(traffic::SpeedGroup); + // 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; - CheckCacheSize(); + size_t const dataSize = info.GetColoring().size() * kElementSize; + m_currentCacheSizeBytes += (dataSize - it->second.m_dataSize); + it->second.m_dataSize = dataSize; + CheckCacheSize(); - // Update traffic colors for drape. - df::TrafficSegmentsColoring coloring; - coloring[info.GetMwmId()] = info.GetColoring(); - m_drapeEngine->UpdateTraffic(coloring); + UpdateState(); + } - UpdateState(); + m_drapeEngine->UpdateTraffic(info); // Update traffic colors for routing. m_observer.OnTrafficInfoAdded(move(info)); |