versions-db-manager.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2020 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 #include <unordered_map>
7 #include <functional>
8 #include <string>
9 #include <vector>
10 
11 #include "http-downloader.h"
12 
13 namespace rs2
14 {
15 
16  namespace sw_update
17  {
22 
23 
24  std::string to_string( const component_part_type & component );
25  std::string to_string( const update_policy_type & policy );
26  bool from_string( const std::string & component_str, component_part_type & component_val );
27  bool from_string( const std::string & policy_str, update_policy_type & policy_val );
28 
29  struct version
30  {
31  int mjor, mnor, patch, build;
32 
33  version() : mjor(0), mnor(0), patch(0), build(0) {}
34 
35  version(long long ver) : version()
36  {
37  build = ver % 10000;
38  patch = (ver / 10000) % 100;
39  mnor = (ver / 1000000) % 100;
40  mjor = (ver / 100000000) % 100;
41  }
42 
44  {
45  constexpr int MINIMAL_MATCH_SECTIONS = 4;
46  constexpr int MATCH_SECTIONS_INC_BUILD_NUM = 5;
47  std::regex rgx("^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,5})?$");
48  std::smatch match;
49 
50  if (std::regex_search(str.begin(), str.end(), match, rgx) && match.size() >= MINIMAL_MATCH_SECTIONS)
51  {
52  mjor = atoi(std::string(match[1]).c_str());
53  mnor = atoi(std::string(match[2]).c_str());
54  patch = atoi(std::string(match[3]).c_str());
55  if (match.size() == MATCH_SECTIONS_INC_BUILD_NUM)
56  build = atoi(std::string(match[4]).c_str());
57  }
58  }
59 
60  bool operator<=(const version& other) const
61  {
62  if (mjor > other.mjor) return false;
63  if ((mjor == other.mjor) && (mnor > other.mnor)) return false;
64  if ((mjor == other.mjor) && (mnor == other.mnor) && (patch > other.patch)) return false;
65  if ((mjor == other.mjor) && (mnor == other.mnor) && (patch == other.patch) && (build > other.build)) return false;
66  return true;
67  }
68  bool operator==(const version& other) const
69  {
70  return (other.mjor == mjor && other.mnor == mnor && other.patch == patch && other.build == build);
71  }
72 
73  bool operator> (const version& other) const { return !(*this <= other); }
74  bool operator!=(const version& other) const { return !(*this == other); }
75  bool operator>=(const version& other) const { return (*this == other) || (*this > other); }
76  bool operator<(const version& other) const { return !(*this >= other); }
77 
78  bool is_between(const version& from, const version& until) const
79  {
80  return (from <= *this) && (*this <= until);
81  }
82 
83  operator std::string() const
84  {
85  std::stringstream ss;
86  ss << mjor << "." << mnor << "." << patch << "." << build;
87  return ss.str();
88  }
89 
90  operator bool() const
91  {
92  return *this != version(0);
93  }
94  };
95 
96 
97  // The version_db_manager class download, parse and supply queries for the RS components versions information.
98  // The version file can be stored locally on the file system or on a HTTP server.
100  {
101  public:
102 
103  explicit versions_db_manager(const std::string &url, const bool use_url_as_local_path = false, http::user_callback_func_type download_callback = http::user_callback_func_type())
104  : _dev_info_url(url), _local_source_file(use_url_as_local_path), _server_versions_vec(), _server_versions_loaded(false), _download_cb_func(download_callback) {};
105 
107 
108  query_status_type query_versions(const std::string &device_name, component_part_type component, const update_policy_type policy, version& out_version);
109  bool get_version_download_link(const component_part_type component, const version& version, std::string& dl_link) { return get_version_data_common(component, version, "link", dl_link); };
110  bool get_version_release_notes(const component_part_type component, const version& version, std::string& version_release_notes_link) { return get_version_data_common(component, version, "release_notes_link", version_release_notes_link); };
111  bool get_version_description(const component_part_type component, const version& version, std::string& version_description) { return get_version_data_common(component, version, "description", version_description); };
112 
113  private:
115  {
124 
125 
127  policy(),
128  component(),
129  version(),
130  platform(),
131  link(),
132  device_name(),
133  rel_notes_link(),
134  desc() {}
135  };
136 
137  bool get_server_data(std::stringstream &ver_data);
138  void parse_versions_data(const std::stringstream &ver_data);
139  bool get_version_data_common(const component_part_type component, const version& version, const std::string& req_field, std::string& out);
140  bool is_device_name_equal(const std::string &str_from_db, const std::string &str_compared, bool allow_wildcard);
141 
142 
143  bool init();
144  void build_schema(std::unordered_map<std::string, std::function<bool(const std::string&)>>& verifier);
145 
146 
150  std::vector<std::unordered_map<std::string, std::string>> _server_versions_vec;
152  };
153  }
154 }
version(const std::string &str)
bool operator>(const version &other) const
Definition: cah-model.h:10
GLsizei const GLchar *const * string
bool get_version_description(const component_part_type component, const version &version, std::string &version_description)
bool operator!=(const version &other) const
bool operator<=(const version &other) const
bool operator<(const version &other) const
bool operator>=(const version &other) const
void init(void)
Definition: boing.c:180
std::vector< std::unordered_map< std::string, std::string > > _server_versions_vec
std::string to_string(const component_part_type &component)
std::function< callback_result(uint64_t dl_current_bytes, uint64_t dl_total_bytes)> user_callback_func_type
versions_db_manager(const std::string &url, const bool use_url_as_local_path=false, http::user_callback_func_type download_callback=http::user_callback_func_type())
bool get_version_download_link(const component_part_type component, const version &version, std::string &dl_link)
bool operator==(const version &other) const
bool get_version_release_notes(const component_part_type component, const version &version, std::string &version_release_notes_link)
bool is_between(const version &from, const version &until) const
bool from_string(const std::string &component_str, component_part_type &component_val)
http::user_callback_func_type _download_cb_func


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:14