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

async_router.cpp « routing - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dcb3cdb34bc59343c6026249c5b8caa3e5c648c7 (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
#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 "indexer/mercator.hpp"

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";
  default:
    {
      ASSERT(false, ("Unknown IRouter::ResultCode value ", code));
      ostringstream o;
      o << "UnknownResultCode(" << static_cast<unsigned int>(code) << ")";
      return o.str();
    }
  }
}

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::AsyncRouter(unique_ptr<IRouter> && router,
                         TRoutingStatisticsCallback const & routingStatisticsFn)
  : m_router(move(router))
  , m_routingStatisticsFn(routingStatisticsFn)
{
  m_isReadyThread.clear();
}

AsyncRouter::~AsyncRouter() { ClearState(); }

void AsyncRouter::CalculateRoute(m2::PointD const & startPoint, m2::PointD const & direction,
                                 m2::PointD const & finalPoint, TReadyCallback const & callback)
{
  ASSERT(m_router, ());
  {
    lock_guard<mutex> guard(m_paramsMutex);
    UNUSED_VALUE(guard);

    m_startPoint = startPoint;
    m_startDirection = direction;
    m_finalPoint = finalPoint;

    m_router->Cancel();
  }

  GetPlatform().RunAsync(bind(&AsyncRouter::CalculateRouteImpl, this, callback));
}

void AsyncRouter::ClearState()
{
  ASSERT(m_router, ());
  m_router->Cancel();

  lock_guard<mutex> guard(m_routeMutex);
  m_router->ClearState();
}

void AsyncRouter::CalculateRouteImpl(TReadyCallback const & callback)
{
  if (m_isReadyThread.test_and_set())
    return;

  Route route(m_router->GetName());
  IRouter::ResultCode code;

  lock_guard<mutex> guard(m_routeMutex);

  m_isReadyThread.clear();

  m2::PointD startPoint, finalPoint, startDirection;
  {
    lock_guard<mutex> params(m_paramsMutex);

    startPoint = m_startPoint;
    finalPoint = m_finalPoint;
    startDirection = m_startDirection;

    m_router->Reset();
  }

  try
  {
    LOG(LDEBUG, ("Calculating the route from", startPoint, "to", finalPoint, "startDirection", startDirection));

    my::Timer timer;
    timer.Reset();

    code = m_router->CalculateRoute(startPoint, startDirection, finalPoint, route);

    double const elapsedSec = timer.ElapsedSeconds();

    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::Cancelled:
        LOG(LINFO, ("Route calculation cancelled, elapsed seconds:", elapsedSec));
        break;
      case IRouter::NoError:
        LOG(LINFO, ("Route found, elapsed seconds:", elapsedSec));
        break;

      default:
        break;
    }

    SendStatistics(startPoint, startDirection, finalPoint, code, elapsedSec);
  }
  catch (RootException const & e)
  {
    LOG(LERROR, ("Exception happened while calculating route:", e.Msg()));
    code = IRouter::InternalError;

    SendStatistics(startPoint, startDirection, finalPoint, e.Msg());
  }

  GetPlatform().RunOnGuiThread(bind(callback, route, code));
}

void AsyncRouter::SendStatistics(m2::PointD const & startPoint, m2::PointD const & startDirection,
                                 m2::PointD const & finalPoint,
                                 IRouter::ResultCode resultCode,
                                 double elapsedSec)
{
  if (nullptr == m_routingStatisticsFn)
    return;

  map<string, string> statistics = PrepareStatisticsData(m_router->GetName(), startPoint, startDirection, finalPoint);
  statistics.emplace("result", ToString(resultCode));
  statistics.emplace("elapsed", strings::to_string(elapsedSec));

  m_routingStatisticsFn(statistics);
}

void AsyncRouter::SendStatistics(m2::PointD const & startPoint, m2::PointD const & startDirection,
                                 m2::PointD const & finalPoint,
                                 string const & exceptionMessage)
{
  if (nullptr == m_routingStatisticsFn)
    return;

  map<string, string> statistics = PrepareStatisticsData(m_router->GetName(), startPoint, startDirection, finalPoint);
  statistics.emplace("exception", exceptionMessage);

  m_routingStatisticsFn(statistics);
}

}  // namespace routing