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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
#include "routing/async_router.hpp"
#include "platform/platform.hpp"
#include "base/logging.hpp"
#include "base/macros.hpp"
#include "base/string_utils.hpp"
#include "base/timer.hpp"
#include "geometry/mercator.hpp"
using namespace std;
namespace routing
{
namespace
{
string ToString(IRouter::ResultCode code)
{
switch (code)
{
case IRouter::NoError: return "NoError";
case IRouter::Cancelled: return "Cancelled";
case IRouter::NoCurrentPosition: return "NoCurrentPosition";
case IRouter::InconsistentMWMandRoute: return "InconsistentMWMandRoute";
case IRouter::RouteFileNotExist: return "RouteFileNotExist";
case IRouter::StartPointNotFound: return "StartPointNotFound";
case IRouter::EndPointNotFound: return "EndPointNotFound";
case IRouter::PointsInDifferentMWM: return "PointsInDifferentMWM";
case IRouter::RouteNotFound: return "RouteNotFound";
case IRouter::InternalError: return "InternalError";
case IRouter::NeedMoreMaps: return "NeedMoreMaps";
case IRouter::FileTooOld: return "FileTooOld";
case IRouter::IntermediatePointNotFound: return "IntermediatePointNotFound";
}
string const result = "Unknown IRouter::ResultCode:" + to_string(static_cast<int>(code));
ASSERT(false, (result));
return result;
}
map<string, string> PrepareStatisticsData(string const & routerName,
m2::PointD const & startPoint, m2::PointD const & startDirection,
m2::PointD const & finalPoint)
{
// Coordinates precision in 5 digits after comma corresponds to metres (0,00001degree ~ 1meter),
// therefore we round coordinates up to 5 digits after comma.
int constexpr precision = 5;
return {{"name", routerName},
{"startLon", strings::to_string_dac(MercatorBounds::XToLon(startPoint.x), precision)},
{"startLat", strings::to_string_dac(MercatorBounds::YToLat(startPoint.y), precision)},
{"startDirectionX", strings::to_string_dac(startDirection.x, precision)},
{"startDirectionY", strings::to_string_dac(startDirection.y, precision)},
{"finalLon", strings::to_string_dac(MercatorBounds::XToLon(finalPoint.x), precision)},
{"finalLat", strings::to_string_dac(MercatorBounds::YToLat(finalPoint.y), precision)}};
}
} // namespace
// ----------------------------------------------------------------------------------------------------------------------------
AsyncRouter::RouterDelegateProxy::RouterDelegateProxy(TReadyCallback const & onReady,
RouterDelegate::TPointCheckCallback const & onPointCheck,
RouterDelegate::TProgressCallback const & onProgress,
uint32_t timeoutSec)
: m_onReady(onReady), m_onPointCheck(onPointCheck), m_onProgress(onProgress)
{
m_delegate.Reset();
m_delegate.SetPointCheckCallback(bind(&RouterDelegateProxy::OnPointCheck, this, _1));
m_delegate.SetProgressCallback(bind(&RouterDelegateProxy::OnProgress, this, _1));
m_delegate.SetTimeout(timeoutSec);
}
void AsyncRouter::RouterDelegateProxy::OnReady(Route & route, IRouter::ResultCode resultCode)
{
if (!m_onReady)
return;
{
lock_guard<mutex> l(m_guard);
if (m_delegate.IsCancelled())
return;
}
m_onReady(route, resultCode);
}
void AsyncRouter::RouterDelegateProxy::Cancel()
{
lock_guard<mutex> l(m_guard);
m_delegate.Cancel();
}
void AsyncRouter::RouterDelegateProxy::OnProgress(float progress)
{
if (!m_onProgress)
return;
{
lock_guard<mutex> l(m_guard);
if (m_delegate.IsCancelled())
return;
}
m_onProgress(progress);
}
void AsyncRouter::RouterDelegateProxy::OnPointCheck(m2::PointD const & pt)
{
if (!m_onPointCheck)
return;
{
lock_guard<mutex> l(m_guard);
if (m_delegate.IsCancelled())
return;
}
m_onPointCheck(pt);
}
// ----------------------------------------------------------------------------------------------------------------------------
AsyncRouter::AsyncRouter(TRoutingStatisticsCallback const & routingStatisticsCallback,
RouterDelegate::TPointCheckCallback const & pointCheckCallback)
: m_threadExit(false), m_hasRequest(false), m_clearState(false),
m_routingStatisticsCallback(routingStatisticsCallback),
m_pointCheckCallback(pointCheckCallback)
{
m_thread = threads::SimpleThread(&AsyncRouter::ThreadFunc, this);
}
AsyncRouter::~AsyncRouter()
{
{
unique_lock<mutex> ul(m_guard);
ResetDelegate();
m_threadExit = true;
m_threadCondVar.notify_one();
}
m_thread.join();
}
void AsyncRouter::SetRouter(unique_ptr<IRouter> && router, unique_ptr<IOnlineFetcher> && fetcher)
{
unique_lock<mutex> ul(m_guard);
ResetDelegate();
m_router = move(router);
m_absentFetcher = move(fetcher);
}
void AsyncRouter::CalculateRoute(Checkpoints const & checkpoints, m2::PointD const & direction,
bool adjustToPrevRoute, TReadyCallback const & readyCallback,
RouterDelegate::TProgressCallback const & progressCallback,
uint32_t timeoutSec)
{
unique_lock<mutex> ul(m_guard);
m_checkpoints = checkpoints;
m_startDirection = direction;
m_adjustToPrevRoute = adjustToPrevRoute;
ResetDelegate();
m_delegate = make_shared<RouterDelegateProxy>(readyCallback, m_pointCheckCallback, progressCallback, timeoutSec);
m_hasRequest = true;
m_threadCondVar.notify_one();
}
void AsyncRouter::ClearState()
{
unique_lock<mutex> ul(m_guard);
m_clearState = true;
m_threadCondVar.notify_one();
ResetDelegate();
}
void AsyncRouter::LogCode(IRouter::ResultCode code, double const elapsedSec)
{
switch (code)
{
case IRouter::StartPointNotFound:
LOG(LWARNING, ("Can't find start or end node"));
break;
case IRouter::EndPointNotFound:
LOG(LWARNING, ("Can't find end point node"));
break;
case IRouter::PointsInDifferentMWM:
LOG(LWARNING, ("Points are in different MWMs"));
break;
case IRouter::RouteNotFound:
LOG(LWARNING, ("Route not found"));
break;
case IRouter::RouteFileNotExist:
LOG(LWARNING, ("There is no routing file"));
break;
case IRouter::NeedMoreMaps:
LOG(LINFO,
("Routing can find a better way with additional maps, elapsed seconds:", elapsedSec));
break;
case IRouter::Cancelled:
LOG(LINFO, ("Route calculation cancelled, elapsed seconds:", elapsedSec));
break;
case IRouter::NoError:
LOG(LINFO, ("Route found, elapsed seconds:", elapsedSec));
break;
case IRouter::NoCurrentPosition:
LOG(LINFO, ("No current position"));
break;
case IRouter::InconsistentMWMandRoute:
LOG(LINFO, ("Inconsistent mwm and route"));
break;
case IRouter::InternalError:
LOG(LINFO, ("Internal error"));
break;
case IRouter::FileTooOld:
LOG(LINFO, ("File too old"));
break;
case IRouter::IntermediatePointNotFound:
LOG(LWARNING, ("Can't find intermediate point node"));
break;
}
}
void AsyncRouter::ResetDelegate()
{
if (m_delegate)
{
m_delegate->Cancel();
m_delegate.reset();
}
}
void AsyncRouter::ThreadFunc()
{
while (true)
{
{
unique_lock<mutex> ul(m_guard);
m_threadCondVar.wait(ul, [this](){ return m_threadExit || m_hasRequest || m_clearState; });
if (m_clearState && m_router)
{
m_router->ClearState();
m_clearState = false;
}
if (m_threadExit)
break;
if (!m_hasRequest)
continue;
}
CalculateRoute();
}
}
void AsyncRouter::CalculateRoute()
{
shared_ptr<RouterDelegateProxy> delegate;
Checkpoints checkpoints;
m2::PointD startDirection;
bool adjustToPrevRoute = false;
shared_ptr<IOnlineFetcher> absentFetcher;
shared_ptr<IRouter> router;
{
unique_lock<mutex> ul(m_guard);
bool hasRequest = m_hasRequest;
m_hasRequest = false;
if (!hasRequest)
return;
if (!m_router)
return;
if (!m_delegate)
return;
checkpoints = m_checkpoints;
startDirection = m_startDirection;
adjustToPrevRoute = m_adjustToPrevRoute;
delegate = m_delegate;
router = m_router;
absentFetcher = m_absentFetcher;
}
Route route(router->GetName());
IRouter::ResultCode code;
my::Timer timer;
double elapsedSec = 0.0;
try
{
LOG(LINFO, ("Calculating the route. checkpoints:", checkpoints, "startDirection:",
startDirection, "router name:", router->GetName()));
if (absentFetcher)
absentFetcher->GenerateRequest(checkpoints);
// Run basic request.
code = router->CalculateRoute(checkpoints, startDirection, adjustToPrevRoute,
delegate->GetDelegate(), route);
elapsedSec = timer.ElapsedSeconds(); // routing time
LogCode(code, elapsedSec);
}
catch (RootException const & e)
{
code = IRouter::InternalError;
LOG(LERROR, ("Exception happened while calculating route:", e.Msg()));
SendStatistics(checkpoints.GetStart(), startDirection, checkpoints.GetFinish(), e.Msg());
delegate->OnReady(route, code);
return;
}
SendStatistics(checkpoints.GetStart(), startDirection, checkpoints.GetFinish(), code, route,
elapsedSec);
// Draw route without waiting network latency.
if (code == IRouter::NoError)
delegate->OnReady(route, code);
bool const needFetchAbsent = (code != IRouter::Cancelled);
// Check online response if we have.
vector<string> absent;
if (absentFetcher && needFetchAbsent)
{
absentFetcher->GetAbsentCountries(absent);
for (string const & country : absent)
route.AddAbsentCountry(country);
}
if (!absent.empty() && code == IRouter::NoError)
code = IRouter::NeedMoreMaps;
elapsedSec = timer.ElapsedSeconds(); // routing time + absents fetch time
LogCode(code, elapsedSec);
// Call callback only if we have some new data.
if (code != IRouter::NoError)
delegate->OnReady(route, code);
}
void AsyncRouter::SendStatistics(m2::PointD const & startPoint, m2::PointD const & startDirection,
m2::PointD const & finalPoint,
IRouter::ResultCode resultCode,
Route const & route,
double elapsedSec)
{
if (nullptr == m_routingStatisticsCallback)
return;
map<string, string> statistics = PrepareStatisticsData(m_router->GetName(), startPoint, startDirection, finalPoint);
statistics.emplace("result", ToString(resultCode));
statistics.emplace("elapsed", strings::to_string(elapsedSec));
if (IRouter::NoError == resultCode)
statistics.emplace("distance", strings::to_string(route.GetTotalDistanceMeters()));
m_routingStatisticsCallback(statistics);
}
void AsyncRouter::SendStatistics(m2::PointD const & startPoint, m2::PointD const & startDirection,
m2::PointD const & finalPoint,
string const & exceptionMessage)
{
if (nullptr == m_routingStatisticsCallback)
return;
map<string, string> statistics = PrepareStatisticsData(m_router->GetName(), startPoint, startDirection, finalPoint);
statistics.emplace("exception", exceptionMessage);
m_routingStatisticsCallback(statistics);
}
} // namespace routing
|