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

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

#include "drape_frontend/custom_symbol.hpp"

#include "drape/pointers.hpp"

#include "geometry/rect2d.hpp"
#include "geometry/screenbase.hpp"

#include "indexer/index.hpp"
#include "indexer/mwm_set.hpp"

#include "base/thread.hpp"

#include <chrono>
#include <map>
#include <mutex>
#include <string>
#include <vector>

namespace df
{
class DrapeEngine;
}  // namespace df

// Hyper Local Ads (HLA) manager.
class HLAManager final
{
public:
  using GetMwmsByRectFn = function<std::vector<MwmSet::MwmId>(m2::RectD const &)>;
  using GetMwmIdByName = function<MwmSet::MwmId(std::string const &)>;

  HLAManager(GetMwmsByRectFn const & getMwmsByRectFn, GetMwmIdByName const & getMwmIdByName);
  HLAManager(HLAManager && /* hlaManager */) = default;
  ~HLAManager();

  void Teardown();
  void SetDrapeEngine(ref_ptr<df::DrapeEngine> engine);
  void UpdateViewport(ScreenBase const & screen);

  void OnDownloadCountry(std::string const & countryName);
  void OnDeleteCountry(std::string const & countryName);

private:
  enum class RequestType
  {
    Download,
    Delete
  };
  using Request = std::pair<MwmSet::MwmId, RequestType>;

  void ThreadRoutine();
  bool WaitForRequest(std::vector<Request> & campaignMwms);

  std::string MakeRemoteURL(MwmSet::MwmId const & mwmId) const;
  std::vector<uint8_t> DownloadCampaign(MwmSet::MwmId const & mwmId) const;
  df::CustomSymbols DeserializeCampaign(std::vector<uint8_t> && rawData,
                                        std::chrono::steady_clock::time_point timestamp);
  void SendSymbolsToRendering(df::CustomSymbols && symbols);
  void DeleteSymbolsFromRendering(MwmSet::MwmId const & mwmId);

  void ReadExpirationFile(std::string const & expirationFile);
  void ReadCampaignFile(std::string const & campaignFile);

  GetMwmsByRectFn m_getMwmsByRectFn;
  GetMwmIdByName m_getMwmIdByNameFn;

  ref_ptr<df::DrapeEngine> m_drapeEngine;

  std::map<std::string, bool> m_campaigns;
  std::map<std::string, std::chrono::steady_clock::time_point> m_expiration;

  df::CustomSymbols m_symbolsCache;
  std::mutex m_symbolsCacheMutex;

  bool m_isRunning = true;
  std::condition_variable m_condition;
  std::vector<Request> m_requestedCampaigns;
  std::mutex m_mutex;
  threads::SimpleThread m_thread;
};