ReCodEx - Task Broker
ReCodEx is complex programmer testing solution, primary targeted to technical universities. It's highly customizable and based on modern technologies.
worker.h
1 #ifndef RECODEX_BROKER_WORKER_H
2 #define RECODEX_BROKER_WORKER_H
3 
4 #include <map>
5 #include <memory>
6 #include <queue>
7 #include <vector>
8 
9 
15 {
16 private:
18  std::string job_id_;
20  std::vector<std::string> data_;
22  bool complete_;
23 
24 public:
30  job_request_data(const std::string &job_id, const std::vector<std::string> &additional) : complete_(true)
31  {
32  job_id_ = job_id;
33  data_ = {"eval", job_id};
34  for (auto &i : additional) {
35  data_.push_back(i);
36  }
37  }
38 
43  job_request_data(const std::string &job_id) : job_id_(job_id), complete_(false)
44  {
45  }
46 
51  bool is_complete() const
52  {
53  return complete_;
54  }
55 
60  const std::string &get_job_id() const
61  {
62  return job_id_;
63  }
64 
69  std::vector<std::string> get() const
70  {
71  return data_;
72  }
73 };
74 
78 struct request {
80  typedef std::multimap<std::string, std::string> headers_t;
81 
83  const headers_t headers;
84 
87 
89  size_t failure_count = 0;
90 
96  request(const headers_t &headers, const job_request_data &data) : headers(headers), data(data)
97  {
98  }
99 
104  request(const job_request_data &data) : data(data)
105  {
106  }
107 };
108 
114 {
115 protected:
117  std::string my_value_;
118 
119 public:
124  header_matcher(std::string my_value) : my_value_(my_value)
125  {
126  }
127 
129  virtual ~header_matcher()
130  {
131  }
132 
138  virtual bool match(const std::string &value)
139  {
140  return value == my_value_;
141  }
142 };
143 
147 class worker
148 {
149 public:
151  typedef std::shared_ptr<request> request_ptr;
152 
153 private:
155  std::multimap<std::string, std::unique_ptr<header_matcher>> headers_;
156 
158  const std::multimap<std::string, std::string> headers_copy_;
159 
161  bool free_;
162 
164  std::queue<request_ptr> request_queue_;
165 
167  request_ptr current_request_;
168 
169 public:
171  const std::string identity;
172 
174  std::string description = "";
175 
177  const std::string hwgroup;
178 
180  size_t liveness;
181 
187  worker(const std::string &id, const std::string &hwgroup, const std::multimap<std::string, std::string> &headers);
188 
192  virtual ~worker();
193 
198  bool headers_equal(const std::multimap<std::string, std::string> &other);
199 
205  virtual bool check_header(const std::string &header, const std::string &value);
206 
211  virtual void enqueue_request(request_ptr request);
212 
217  virtual void complete_request();
218 
224  virtual request_ptr cancel_request();
225 
230  virtual bool next_request();
231 
236  virtual std::shared_ptr<const request> get_current_request() const;
237 
243  virtual std::shared_ptr<std::vector<request_ptr>> terminate();
244 
249  std::string get_description() const;
250 };
251 
252 #endif // RECODEX_BROKER_WORKER_H
std::shared_ptr< request > request_ptr
Definition: worker.h:151
job_request_data(const std::string &job_id)
Definition: worker.h:43
size_t liveness
Definition: worker.h:180
job_request_data(const std::string &job_id, const std::vector< std::string > &additional)
Definition: worker.h:30
const std::string hwgroup
Definition: worker.h:177
const std::string & get_job_id() const
Definition: worker.h:60
request(const job_request_data &data)
Definition: worker.h:104
header_matcher(std::string my_value)
Definition: worker.h:124
virtual ~header_matcher()
Definition: worker.h:129
const headers_t headers
Definition: worker.h:83
std::multimap< std::string, std::string > headers_t
Definition: worker.h:80
virtual bool match(const std::string &value)
Definition: worker.h:138
Definition: worker.h:147
bool is_complete() const
Definition: worker.h:51
std::string my_value_
Definition: worker.h:117
Definition: worker.h:78
request(const headers_t &headers, const job_request_data &data)
Definition: worker.h:96
const std::string identity
Definition: worker.h:171
const job_request_data data
Definition: worker.h:86