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

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

#include "online_absent_fetcher.hpp"
#include "route.hpp"
#include "router.hpp"
#include "router_delegate.hpp"

#include "base/thread.hpp"

#include "std/condition_variable.hpp"
#include "std/map.hpp"
#include "std/mutex.hpp"
#include "std/shared_ptr.hpp"
#include "std/string.hpp"
#include "std/unique_ptr.hpp"

namespace routing
{

/// Dispatches a route calculation on a worker thread
class AsyncRouter final
{
public:
  /// Callback takes ownership of passed route.
  using TReadyCallback = function<void(Route &, IRouter::ResultCode)>;

  /// Callback on routing statistics
  using TRoutingStatisticsCallback = function<void(map<string, string> const &)>;

  /// AsyncRouter is a wrapper class to run routing routines in the different thread
  AsyncRouter(TRoutingStatisticsCallback const & routingStatisticsCallback,
              RouterDelegate::TPointCheckCallback const & pointCheckCallback);
  ~AsyncRouter();

  /// Sets a synchronous router, current route calculation will be cancelled
  /// @param router pointer to a router implementation
  /// @param fetcher pointer to a online fetcher
  void SetRouter(unique_ptr<IRouter> && router, unique_ptr<IOnlineFetcher> && fetcher);

  /// Main method to calulate new route from startPt to finalPt with start direction
  /// Processed result will be passed to callback. Callback will called at GUI thread.
  ///
  /// @param startPoint point to start routing
  /// @param direction start direction for routers with high cost of the turnarounds
  /// @param finalPoint target point for route
  /// @param readyCallback function to return routing result
  /// @param progressCallback function to update the router progress
  /// @param timeoutSec timeout to cancel routing. 0 is infinity.
  void CalculateRoute(m2::PointD const & startPoint, m2::PointD const & direction,
                      m2::PointD const & finalPoint, TReadyCallback const & readyCallback,
                      RouterDelegate::TProgressCallback const & progressCallback,
                      uint32_t timeoutSec);

  /// Interrupt routing and clear buffers
  void ClearState();

private:
  /// Worker thread function
  void ThreadFunc();

  /// This function is called in worker thread
  void CalculateRoute();

  void ResetDelegate();

  /// These functions are called to send statistics about the routing
  void SendStatistics(m2::PointD const & startPoint, m2::PointD const & startDirection,
                      m2::PointD const & finalPoint,
                      IRouter::ResultCode resultCode,
                      Route const & route,
                      double elapsedSec);
  void SendStatistics(m2::PointD const & startPoint, m2::PointD const & startDirection,
                      m2::PointD const & finalPoint,
                      string const & exceptionMessage);

  void LogCode(IRouter::ResultCode code, double const elapsedSec);

  /// Blocks callbacks when routing has been cancelled
  class RouterDelegateProxy
  {
  public:
    RouterDelegateProxy(TReadyCallback const & onReady,
                        RouterDelegate::TPointCheckCallback const & onPointCheck,
                        RouterDelegate::TProgressCallback const & onProgress,
                        uint32_t timeoutSec);

    void OnReady(Route & route, IRouter::ResultCode resultCode);
    void Cancel();

    RouterDelegate const & GetDelegate() const { return m_delegate; }

  private:
    void OnProgress(float progress);
    void OnPointCheck(m2::PointD const & pt);

    mutex m_guard;
    TReadyCallback const m_onReady;
    RouterDelegate::TPointCheckCallback const m_onPointCheck;
    RouterDelegate::TProgressCallback const m_onProgress;
    RouterDelegate m_delegate;
  };

private:
  mutex m_guard;

  /// Thread which executes routing calculation
  threads::SimpleThread m_thread;
  condition_variable m_threadCondVar;
  bool m_threadExit;
  bool m_hasRequest;

  /// Current request parameters
  bool m_clearState;
  m2::PointD m_startPoint;
  m2::PointD m_finalPoint;
  m2::PointD m_startDirection;
  shared_ptr<RouterDelegateProxy> m_delegate;
  shared_ptr<IOnlineFetcher> m_absentFetcher;
  shared_ptr<IRouter> m_router;

  TRoutingStatisticsCallback const m_routingStatisticsCallback;
  RouterDelegate::TPointCheckCallback const m_pointCheckCallback;
};

}  // namespace routing