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

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

#include "platform/http_request.hpp"

#include "base/logging.hpp"
#include "base/string_utils.hpp"

#include "3party/jansson/myjansson.hpp"

#include "geometry/mercator.hpp"

#include "std/bind.hpp"

namespace
{
inline string LatLonToURLArgs(ms::LatLon const & point)
{
  return strings::to_string(point.lat) + ','+ strings::to_string(point.lon);
}
}  // namespace

namespace routing
{
bool ParseResponse(const string & serverResponse, vector<m2::PointD> & outPoints)
{
  try
  {
    my::Json parser(serverResponse.c_str());

    json_t const * countries = json_object_get(parser.get(), "used_mwms");
    size_t pointsCount = json_array_size(countries);
    outPoints.reserve(pointsCount);
    for (size_t i = 0; i < pointsCount; ++i)
    {
      json_t * pointArray = json_array_get(countries, i);
      outPoints.push_back({json_number_value(json_array_get(pointArray, 0)),
                           json_number_value(json_array_get(pointArray, 1))});
    }
    return !outPoints.empty();
  }
  catch (my::Json::Exception&)
  {
    return false;
  }
  return false;
}

string GenerateOnlineRequest(string const & serverURL, ms::LatLon const & startPoint,
                             ms::LatLon const & finalPoint)
{
  return serverURL + "/mapsme?loc=" + LatLonToURLArgs(startPoint) + "&loc=" +
         LatLonToURLArgs(finalPoint);
}

OnlineCrossFetcher::OnlineCrossFetcher(string const & serverURL, ms::LatLon const & startPoint,
                                       ms::LatLon const & finalPoint)
    : m_request(GenerateOnlineRequest(serverURL, startPoint, finalPoint))
{
  LOG(LINFO, ("Check mwms by URL: ", GenerateOnlineRequest(serverURL, startPoint, finalPoint)));
}

void OnlineCrossFetcher::Do()
{
  m_mwmPoints.clear();
  if (m_request.RunHttpRequest() && m_request.ErrorCode() == 200 && !m_request.WasRedirected())
    ParseResponse(m_request.ServerResponse(), m_mwmPoints);
  else
    LOG(LWARNING, ("Can't get OSRM server response. Code: ", m_request.ErrorCode()));
}
}  // namespace routing