ReCodEx - Task Broker
ReCodEx is complex programmer testing solution, primary targeted to technical universities. It's highly customizable and based on modern technologies.
reactor.h
1 #ifndef RECODEX_BROKER_REACTOR_H
2 #define RECODEX_BROKER_REACTOR_H
3 
4 #include <atomic>
5 #include <map>
6 #include <memory>
7 #include <zmq.hpp>
8 
9 #include "handler_interface.h"
10 #include "message_container.h"
11 #include "socket_wrapper_base.h"
12 
13 /* Forward */
14 class reactor;
15 
21 {
22 public:
27  handler_wrapper(reactor &reactor_ref, std::shared_ptr<handler_interface> handler);
28 
32  virtual ~handler_wrapper();
33 
38  virtual void operator()(const message_container &message);
39 
40 protected:
42  std::shared_ptr<handler_interface> handler_;
43 
46 };
47 
53 {
54 public:
61  asynchronous_handler_wrapper(zmq::context_t &context,
62  zmq::socket_t &async_handler_socket,
63  reactor &reactor_ref,
64  std::shared_ptr<handler_interface> handler);
65 
71 
77  virtual void operator()(const message_container &message);
78 
79 private:
83  static const std::string TERMINATE_MSG;
84 
88  zmq::context_t &context_;
89 
94  zmq::socket_t &reactor_socket_;
95 
99  std::thread worker_;
100 
105  void handler_thread();
106 
110  void send_response(zmq::socket_t &socket, const message_container &message);
111 
115  const std::string unique_id_;
116 };
117 
123 class reactor
124 {
125 public:
129  const static std::string KEY_TIMER;
130 
134  const std::string unique_id;
135 
139  reactor(std::shared_ptr<zmq::context_t> context);
140 
146  void add_socket(const std::string &name, std::shared_ptr<socket_wrapper_base> socket);
147 
154  void add_handler(const std::vector<std::string> &origins, std::shared_ptr<handler_interface> handler);
155 
162  void add_async_handler(const std::vector<std::string> &origins, std::shared_ptr<handler_interface> handler);
163 
169  void send_message(const message_container &message);
170 
176  void process_message(const message_container &message);
177 
186  void start_loop();
187 
192  void terminate();
193 
194 private:
198  std::map<std::string, std::shared_ptr<socket_wrapper_base>> sockets_;
199 
203  std::multimap<std::string, std::shared_ptr<handler_wrapper>> handlers_;
204 
208  std::shared_ptr<zmq::context_t> context_;
209 
213  zmq::socket_t async_handler_socket_;
214 
218  std::atomic<bool> termination_flag_;
219 };
220 
221 #endif // RECODEX_BROKER_REACTOR_H
virtual void operator()(const message_container &message)
Definition: reactor.cpp:146
std::shared_ptr< handler_interface > handler_
Definition: reactor.h:42
handler_wrapper(reactor &reactor_ref, std::shared_ptr< handler_interface > handler)
Definition: reactor.cpp:137
virtual ~handler_wrapper()
Definition: reactor.cpp:142
const std::string unique_id
Definition: reactor.h:134
static const std::string KEY_TIMER
Definition: reactor.h:129
reactor & reactor_
Definition: reactor.h:45