ReCodEx - Task Broker
ReCodEx is complex programmer testing solution, primary targeted to technical universities. It's highly customizable and based on modern technologies.
worker.cpp
1 #include "worker.h"
2 #include "helpers/string_to_hex.h"
3 
9 {
10 public:
12  static const char delimiter = '|';
13 
18  multiple_string_matcher(std::string my_value) : header_matcher(my_value)
19  {
20  }
21 
24  {
25  }
26 
32  virtual bool match(const std::string &value)
33  {
34  size_t offset = 0;
35 
36  while (offset < value.size()) {
37  size_t end = value.find(delimiter, offset);
38 
39  if (end == std::string::npos) {
40  end = value.size();
41  }
42 
43  if (value.compare(offset, end - offset, my_value_) == 0) {
44  return true;
45  }
46 
47  offset = end + 1;
48  }
49 
50  return false;
51  }
52 };
53 
58 {
59 private:
61  size_t my_count_;
62 
63 public:
68  count_matcher(std::string my_value) : my_count_(std::stoul(my_value)), header_matcher(my_value)
69  {
70  }
71 
73  virtual ~count_matcher()
74  {
75  }
76 
82  virtual bool match(const std::string &value)
83  {
84  return my_count_ >= std::stoul(value);
85  }
86 };
87 
88 
90  const std::string &id, const std::string &hwgroup, const std::multimap<std::string, std::string> &headers)
91  : identity(id), hwgroup(hwgroup), free_(true), current_request_(nullptr), headers_copy_(headers)
92 {
93  headers_.emplace("hwgroup", std::unique_ptr<header_matcher>(new multiple_string_matcher(hwgroup)));
94 
95  for (auto it : headers) {
96  auto matcher = std::unique_ptr<header_matcher>(new header_matcher(it.second));
97 
98  if (it.first == "threads") {
99  matcher = std::unique_ptr<header_matcher>(new count_matcher(it.second));
100  }
101 
102  headers_.emplace(it.first, std::move(matcher));
103  }
104 }
105 
107 {
108 }
109 
111 {
112  request_queue_.push(request);
113 }
114 
116 {
117  free_ = true;
118  current_request_ = nullptr;
119 }
120 
122 {
123  auto request = current_request_;
124 
125  if (request != nullptr) {
126  request->failure_count += 1;
127  }
128 
130  return request;
131 }
132 
134 {
135  if (free_ && !request_queue_.empty()) {
136  current_request_ = request_queue_.front();
137  request_queue_.pop();
138  free_ = false;
139 
140  return true;
141  }
142 
143  return false;
144 }
145 
146 std::shared_ptr<const request> worker::get_current_request() const
147 {
148  return current_request_;
149 }
150 
151 std::shared_ptr<std::vector<worker::request_ptr>> worker::terminate()
152 {
153  auto result = std::make_shared<std::vector<worker::request_ptr>>();
154 
155  if (current_request_ != nullptr) {
156  current_request_->failure_count += 1;
157  result->push_back(current_request_);
158  }
159 
160  current_request_ = nullptr;
161 
162  while (!request_queue_.empty()) {
163  result->push_back(request_queue_.front());
164  request_queue_.pop();
165  }
166 
167  return result;
168 }
169 
170 bool worker::check_header(const std::string &header, const std::string &value)
171 {
172  // Find all worker headers with the right name
173  auto range = headers_.equal_range(header);
174  for (auto &worker_header = range.first; worker_header != range.second; ++worker_header) {
175  // If any of these headers has a matching value, return true
176  if (worker_header->second->match(value)) {
177  return true;
178  }
179  }
180 
181  // If we found nothing, return false
182  return false;
183 }
184 
185 
186 bool worker::headers_equal(const std::multimap<std::string, std::string> &other)
187 {
188  return other == headers_copy_;
189 }
190 
191 std::string worker::get_description() const
192 {
193  if (description == "") {
194  return helpers::string_to_hex(identity);
195  } else {
196  return helpers::string_to_hex(identity) + " (" + description + ")";
197  }
198 }
virtual bool check_header(const std::string &header, const std::string &value)
Definition: worker.cpp:170
size_t failure_count
Definition: worker.h:89
std::shared_ptr< request > request_ptr
Definition: worker.h:151
std::string description
Definition: worker.h:174
virtual void complete_request()
Definition: worker.cpp:115
virtual bool match(const std::string &value)
Definition: worker.cpp:82
static const char delimiter
Definition: worker.cpp:12
STL namespace.
count_matcher(std::string my_value)
Definition: worker.cpp:68
virtual bool next_request()
Definition: worker.cpp:133
virtual std::shared_ptr< std::vector< request_ptr > > terminate()
Definition: worker.cpp:151
multiple_string_matcher(std::string my_value)
Definition: worker.cpp:18
virtual std::shared_ptr< const request > get_current_request() const
Definition: worker.cpp:146
std::string my_value_
Definition: worker.h:117
virtual ~count_matcher()
Definition: worker.cpp:73
virtual bool match(const std::string &value)
Definition: worker.cpp:32
virtual ~multiple_string_matcher()
Definition: worker.cpp:23
virtual ~worker()
Definition: worker.cpp:106
Definition: worker.h:78
virtual void enqueue_request(request_ptr request)
Definition: worker.cpp:110
const std::string identity
Definition: worker.h:171
virtual request_ptr cancel_request()
Definition: worker.cpp:121
std::string get_description() const
Definition: worker.cpp:191
bool headers_equal(const std::multimap< std::string, std::string > &other)
Definition: worker.cpp:186
worker(const std::string &id, const std::string &hwgroup, const std::multimap< std::string, std::string > &headers)
Definition: worker.cpp:89