SOMHunter Core
client-dres.h
Go to the documentation of this file.
1 /* This file is part of SOMHunter.
2  *
3  * Copyright (C) 2021 Frantisek Mejzlik <frankmejzlik@protonmail.com>
4  * Mirek Kratochvil <exa.exa@gmail.com>
5  * Patrik Vesely <prtrikvesely@gmail.com>
6  *
7  * SOMHunter is free software: you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License as published by the Free
9  * Software Foundation, either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * SOMHunter is distributed in the hope that it will be useful, but WITHOUT ANY
13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * SOMHunter. If not, see <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef CLIENT_DRES_H_
22 #define CLIENT_DRES_H_
23 
24 // ---
25 #include <nlohmann/json.hpp>
26 // ---
27 #include "http.h"
28 #include "settings.h"
29 
30 namespace sh {
31 struct VideoFrame;
32 
35  // *** METHODS ***
36 public:
38  : _eval_server_settings{ settings }, _do_requests{ false }, _http{}, _username{ "not-doing-requests" } {};
39  // ---
40  virtual bool login() = 0;
41  virtual bool logout() = 0;
42  virtual bool submit(const VideoFrame& frame) = 0;
43  virtual bool send_results_log(const nlohmann::json& log_JSON) = 0;
44  virtual bool send_interactions_log(const nlohmann::json& log_JSON) = 0;
45 
47  virtual nlohmann::json get_current_task() = 0;
48 
49  virtual bool is_logged_in() const { return !_username.empty(); }
50  virtual const std::string& get_user_token() const { return _username; };
51  virtual void set_do_requests(bool val) { _do_requests = val; };
52  virtual bool get_do_requests() const { return _do_requests; };
53 
54 protected:
55  virtual void set_user_token(const std::string& val) {
56  SHLOG_D("Setting `_user_token` to " << val);
57  _username = val;
58  };
59 
60  // *** MEMBER VARIABLES ***
61 protected:
65  std::string _username;
66 };
67 
73 class ClientDres final : public IServerClient {
74  // *** METHODS ***
75 public:
76  ClientDres(const EvalServerSettings& settings);
77  ~ClientDres() noexcept;
78  // ---
79  virtual bool login() override;
80  virtual bool logout() override;
81  virtual bool submit(const VideoFrame& frame) override;
82  virtual bool send_results_log(const nlohmann::json& log_JSON) override;
83  virtual bool send_interactions_log(const nlohmann::json& log_JSON) override;
84 
85  virtual UnixTimestamp get_server_ts() override;
86  virtual nlohmann::json get_current_task() override;
87 
88 private:
90  std::string log_type_to_str(LogType t) const {
91  switch (t) {
92  case LogType::LOGIN:
93  return "login";
94  break;
95 
96  case LogType::LOGOUT:
97  return "logout";
98  break;
99 
100  case LogType::SUBMIT:
101  return "submit";
102  break;
103 
104  case LogType::RESULT:
105  return "result";
106  break;
107 
109  return "interaction";
110  break;
111 
112  default:
113  std::string msg{ "Unknown log type!" };
114  throw std::runtime_error{ msg };
115  break;
116  }
117  }
118 
119  void write_log(LogType type, UnixTimestamp ts, const std::string& URL, const nlohmann::json& req, ReqCode code,
120  nlohmann::json& res) const;
121 
122  // *** MEMBER VARIABLES ***
123 protected:
124  const std::chrono::milliseconds _sync_period{ 5000ms };
125 
127  std::thread _t_sync_worker;
128 
129  bool _synced;
130  std::ptrdiff_t _diff;
131  nlohmann::json _current_task;
132 };
133 
134 }; // namespace sh
135 
136 #endif // CLIENT_DRES_H_
Specific DRES server implementation.
Definition: client-dres.h:73
const std::chrono::milliseconds _sync_period
Definition: client-dres.h:124
LogType
Definition: client-dres.h:89
virtual bool submit(const VideoFrame &frame) override
Definition: client-dres.cpp:177
std::ptrdiff_t _diff
Definition: client-dres.h:130
~ClientDres() noexcept
Definition: client-dres.cpp:68
virtual bool logout() override
Definition: client-dres.cpp:124
EvalServerSettings::ServerConfigDres _settings
Definition: client-dres.h:126
virtual bool send_results_log(const nlohmann::json &log_JSON) override
Definition: client-dres.cpp:244
virtual bool send_interactions_log(const nlohmann::json &log_JSON) override
Definition: client-dres.cpp:258
virtual nlohmann::json get_current_task() override
Definition: client-dres.cpp:282
ClientDres(const EvalServerSettings &settings)
Definition: client-dres.cpp:29
virtual bool login() override
Definition: client-dres.cpp:70
std::thread _t_sync_worker
Definition: client-dres.h:127
std::string log_type_to_str(LogType t) const
Definition: client-dres.h:90
nlohmann::json _current_task
Definition: client-dres.h:131
void write_log(LogType type, UnixTimestamp ts, const std::string &URL, const nlohmann::json &req, ReqCode code, nlohmann::json &res) const
Definition: client-dres.cpp:296
bool _synced
Definition: client-dres.h:129
virtual UnixTimestamp get_server_ts() override
Definition: client-dres.cpp:273
Definition: http.h:38
Unified interface for all remote evaluation servers.
Definition: client-dres.h:34
virtual bool get_do_requests() const
Definition: client-dres.h:52
virtual void set_do_requests(bool val)
Definition: client-dres.h:51
virtual bool send_results_log(const nlohmann::json &log_JSON)=0
virtual bool login()=0
virtual bool is_logged_in() const
Definition: client-dres.h:49
IServerClient(const EvalServerSettings &settings)
Definition: client-dres.h:37
virtual nlohmann::json get_current_task()=0
virtual UnixTimestamp get_server_ts()=0
std::string _username
Definition: client-dres.h:65
virtual const std::string & get_user_token() const
Definition: client-dres.h:50
virtual void set_user_token(const std::string &val)
Definition: client-dres.h:55
virtual bool send_interactions_log(const nlohmann::json &log_JSON)=0
bool _do_requests
Definition: client-dres.h:63
Http _http
Definition: client-dres.h:64
EvalServerSettings _eval_server_settings
Definition: client-dres.h:58
virtual bool logout()=0
virtual bool submit(const VideoFrame &frame)=0
Definition: common-types.h:33
std::size_t ReqCode
Definition: http.h:35
std::int64_t UnixTimestamp
Definition: common-types.h:54
#define SHLOG_D(x)
Definition: static-logger.hpp:179
Config for submitting to the DRES server.
Definition: settings.h:74
Definition: settings.h:59
Definition: dataset-frames.h:40