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: cc81485c6a8b641c71e7d1a909ce83754d8ed0bf (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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#include "partners_api/taxi_engine.hpp"
#include "partners_api/maxim_api.hpp"
#include "partners_api/rutaxi_api.hpp"
#include "partners_api/taxi_places_loader.hpp"
#include "partners_api/uber_api.hpp"
#include "partners_api/yandex_api.hpp"

#include "geometry/latlon.hpp"
#include "geometry/mercator.hpp"

#include "base/macros.hpp"

#include <algorithm>
#include <initializer_list>
#include <iterator>
#include <memory>
#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);
  AddApi<uber::Api>(urls, Provider::Type::Uber);
  AddApi<maxim::Api>(urls, Provider::Type::Maxim);
  AddApi<rutaxi::Api>(urls, Provider::Type::Rutaxi);
}

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

  auto it = std::find_if(m_apis.begin(), m_apis.end(), [](ApiItem const & item)
  {
    return item.m_type == Provider::Type::Rutaxi;
  });

  if (it != m_apis.end())
  {
    auto rutaxiPtr = dynamic_cast<rutaxi::Api *>(it->m_api.get());
    CHECK(rutaxiPtr != nullptr, ());
    rutaxiPtr->SetDelegate(m_delegate.get());
  }
}

/// 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;
  auto const pointFrom = MercatorBounds::FromLatLon(from);

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

    if (!IsAvailableAtPos(type, pointFrom))
    {
      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;
  auto const point = MercatorBounds::FromLatLon(pos);

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

  return result;
}

bool Engine::IsAvailableAtPos(Provider::Type type, m2::PointD const & point) const
{
  if (IsDisabledAtPos(type, point))
    return false;

  return IsEnabledAtPos(type, point);
}

bool Engine::IsDisabledAtPos(Provider::Type type, m2::PointD const & point) const
{
  auto const it = FindByProviderType(type, m_apis.cbegin(), m_apis.cend());

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

  if (it->IsMwmDisabled(m_delegate->GetMwmId(point)))
    return true;

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

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

bool Engine::IsEnabledAtPos(Provider::Type type, m2::PointD const & point) const
{
  auto const it = FindByProviderType(type, m_apis.cbegin(), m_apis.cend());

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

  if (it->IsMwmEnabled(m_delegate->GetMwmId(point)))
    return true;

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

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

template <typename ApiType>
void Engine::AddApi(std::vector<ProviderUrl> const & urls, Provider::Type type)
{
  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, std::make_unique<ApiType>(it->m_url));
  else
    m_apis.emplace_back(type, std::make_unique<ApiType>());
}
}  // namespace taxi