Welcome to mirror list, hosted at ThFree Co, Russian Federation.

online_absent_fetcher.cpp « routing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1e00476b821feff3c9ea864ae9021e473924949c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "routing/online_absent_fetcher.hpp"

#include "routing/online_cross_fetcher.hpp"

#include "platform/platform.hpp"
#include "platform/country_file.hpp"
#include "platform/local_country_file.hpp"

#include "base/stl_helpers.hpp"

#include "private.h"

using namespace std;

using platform::CountryFile;
using platform::LocalCountryFile;

namespace routing
{
OnlineAbsentCountriesFetcher::OnlineAbsentCountriesFetcher(
    TCountryFileFn const & countryFileFn, TCountryLocalFileFn const & countryLocalFileFn)
  : m_countryFileFn(countryFileFn), m_countryLocalFileFn(countryLocalFileFn)
{
  CHECK(m_countryFileFn, ());
  CHECK(m_countryLocalFileFn, ());
}

void OnlineAbsentCountriesFetcher::GenerateRequest(Checkpoints const & checkpoints)
{
  if (GetPlatform().ConnectionStatus() == Platform::EConnectionType::CONNECTION_NONE)
    return;

  // Single mwm case.
  if (AllPointsInSameMwm(checkpoints))
    return;

  unique_ptr<OnlineCrossFetcher> fetcher =
      make_unique<OnlineCrossFetcher>(m_countryFileFn, OSRM_ONLINE_SERVER_URL, checkpoints);
  // iOS can't reuse threads. So we need to recreate the thread.
  m_fetcherThread.reset(new threads::Thread());
  m_fetcherThread->Create(move(fetcher));
}

void OnlineAbsentCountriesFetcher::GetAbsentCountries(vector<string> & countries)
{
  countries.clear();
  // Check whether a request was scheduled to be run on the thread.
  if (!m_fetcherThread)
    return;

  m_fetcherThread->Join();
  for (auto const & point : m_fetcherThread->GetRoutineAs<OnlineCrossFetcher>()->GetMwmPoints())
  {
    string name = m_countryFileFn(point);
    ASSERT(!name.empty(), ());
    if (name.empty() || m_countryLocalFileFn(name))
      continue;

    countries.emplace_back(move(name));
  }

  m_fetcherThread.reset();
  base::SortUnique(countries);
}

bool OnlineAbsentCountriesFetcher::AllPointsInSameMwm(Checkpoints const & checkpoints) const
{
  for (size_t i = 0; i < checkpoints.GetNumSubroutes(); ++i)
  {
    if (m_countryFileFn(checkpoints.GetPoint(i)) != m_countryFileFn(checkpoints.GetPoint(i + 1)))
      return false;
  }

  return true;
}
}  // namespace routing