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

taxi_engine.cpp « partners_api - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6216c586c983a0f5350a73f4e6423e7fc8a47c01 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "partners_api/taxi_engine.hpp"
#include "partners_api/taxi_places.hpp"
#include "partners_api/uber_api.hpp"
#include "partners_api/yandex_api.hpp"

#include "base/macros.hpp"
#include "base/stl_add.hpp"

#include <algorithm>
#include <initializer_list>
#include <iterator>
#include <utility>

namespace
{
template <typename Iter>
Iter FindByProviderType(taxi::Provider::Type type, Iter first, Iter last)
{
  using IterValueType = typename std::iterator_traits<Iter>::value_type;
  return std::find_if(first, last,
                      [type](IterValueType const & item) { return item.m_type == type; });
}
}  // namespace

namespace taxi
{
// ResultMaker ------------------------------------------------------------------------------------
void ResultMaker::Reset(uint64_t requestId, size_t requestsCount,
                        SuccessCallback const & successCallback,
                        ErrorCallback const & errorCallback)
{
  ASSERT(successCallback, ());
  ASSERT(errorCallback, ());

  std::lock_guard<std::mutex> lock(m_mutex);

  m_requestId = requestId;
  m_requestsCount = static_cast<int8_t>(requestsCount);
  m_successCallback = successCallback;
  m_errorCallback = errorCallback;
  m_providers.clear();
  m_errors.clear();
}

void ResultMaker::DecrementRequestCount(uint64_t requestId)
{
  std::lock_guard<std::mutex> lock(m_mutex);

  if (m_requestId != requestId)
    return;

  DecrementRequestCount();
}

void ResultMaker::ProcessProducts(uint64_t requestId, Provider::Type type,
                                  std::vector<Product> const & products)
{
  std::lock_guard<std::mutex> lock(m_mutex);

  if (m_requestId != requestId)
    return;

  m_providers.emplace_back(type, products);

  DecrementRequestCount();
}

void ResultMaker::ProcessError(uint64_t requestId, Provider::Type type, ErrorCode code)
{
  std::lock_guard<std::mutex> lock(m_mutex);

  if (m_requestId != requestId)
    return;

  m_errors.emplace_back(type, code);

  DecrementRequestCount();
}

void ResultMaker::MakeResult(uint64_t requestId) const
{
  SuccessCallback successCallback;
  ErrorCallback errorCallback;
  ProvidersContainer providers;
  ErrorsContainer errors;

  {
    std::lock_guard<std::mutex> lock(m_mutex);

    if (m_requestId != requestId || m_requestsCount != 0)
      return;

    successCallback = m_successCallback;
    errorCallback = m_errorCallback;
    providers = m_providers;
    errors = m_errors;
  }

  if (!providers.empty() || (providers.empty() && errors.empty()))
    return successCallback(providers, requestId);

  return errorCallback(errors, requestId);
}

void ResultMaker::DecrementRequestCount()
{
  CHECK_GREATER(m_requestsCount, 0, ());
  --m_requestsCount;
}

// Engine -----------------------------------------------------------------------------------------
Engine::Engine(std::vector<ProviderUrl> urls /* = {} */)
{
  AddApi<yandex::Api>(urls, Provider::Type::Yandex, places::kYandexEnabledPlaces, {{}});
  AddApi<uber::Api>(urls, Provider::Type::Uber, {{}}, places::kUberDisabledPlaces);
}

void Engine::SetDelegate(std::unique_ptr<Delegate> delegate) { m_delegate = std::move(delegate); }

/// Requests list of available products. Returns request identificator immediately.
uint64_t Engine::GetAvailableProducts(ms::LatLon const & from, ms::LatLon const & to,
                                      SuccessCallback const & successFn,
                                      ErrorCallback const & errorFn)
{
  ASSERT(successFn, ());
  ASSERT(errorFn, ());
  ASSERT(m_delegate, ());

  auto const reqId = ++m_requestId;
  auto const maker = m_maker;

  maker->Reset(reqId, m_apis.size(), successFn, errorFn);
  for (auto const & api : m_apis)
  {
    auto type = api.m_type;

    if (!IsAvailableAtPos(type, from))
    {
      maker->DecrementRequestCount(reqId);
      maker->MakeResult(reqId);
      continue;
    }

    auto const productCallback = [type, maker, reqId](std::vector<Product> const & products)
    {
      maker->ProcessProducts(reqId, type, products);
      maker->MakeResult(reqId);
    };

    auto const errorCallback = [type, maker, reqId](ErrorCode const code)
    {
      maker->ProcessError(reqId, type, code);
      maker->MakeResult(reqId);
    };

    api.m_api->GetAvailableProducts(from, to, productCallback, errorCallback);
  }

  return reqId;
}

/// Returns link which allows you to launch some taxi app.
RideRequestLinks Engine::GetRideRequestLinks(Provider::Type type, std::string const & productId,
                                             ms::LatLon const & from, ms::LatLon const & to) const
{
  auto const it = FindByProviderType(type, m_apis.cbegin(), m_apis.cend());

  CHECK(it != m_apis.cend(), ());

  return it->m_api->GetRideRequestLinks(productId, from, to);
}

std::vector<Provider::Type> Engine::GetProvidersAtPos(ms::LatLon const & pos) const
{
  std::vector<Provider::Type> result;

  for (auto const & api : m_apis)
  {
    if (IsAvailableAtPos(api.m_type, pos))
      result.push_back(api.m_type);
  }

  return result;
}

bool Engine::IsAvailableAtPos(Provider::Type type, ms::LatLon const & pos) const
{
  if (AreAllCountriesDisabled(type, pos))
    return false;

  return IsAnyCountryEnabled(type, pos);
}

bool Engine::AreAllCountriesDisabled(Provider::Type type, ms::LatLon const & latlon) const
{
  auto const it = FindByProviderType(type, m_apis.cbegin(), m_apis.cend());

  CHECK(it != m_apis.cend(), ());

  auto const countryIds = m_delegate->GetCountryIds(latlon);
  auto const city = m_delegate->GetCityName(latlon);

  return it->AreAllCountriesDisabled(countryIds, city);
}

bool Engine::IsAnyCountryEnabled(Provider::Type type, ms::LatLon const & latlon) const
{
  auto const it = FindByProviderType(type, m_apis.cbegin(), m_apis.cend());

  CHECK(it != m_apis.cend(), ());

  auto const countryIds = m_delegate->GetCountryIds(latlon);
  auto const city = m_delegate->GetCityName(latlon);

  return it->IsAnyCountryEnabled(countryIds, city);
}

template <typename ApiType>
void Engine::AddApi(std::vector<ProviderUrl> const & urls, Provider::Type type,
                    Countries const & enabled, Countries const & disabled)
{
  auto const it = std::find_if(urls.cbegin(), urls.cend(), [type](ProviderUrl const & item)
  {
    return item.m_type == type;
  });

  if (it != urls.cend())
    m_apis.emplace_back(type, my::make_unique<ApiType>(it->m_url), enabled, disabled);
  else
    m_apis.emplace_back(type, my::make_unique<ApiType>(), enabled, disabled);
}
}  // namespace taxi