Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
worker.hpp
Go to the documentation of this file.
1/*
2 __ __ _
3 \ \ / /__ _ __| | _____ _ __
4 \ \ /\ / / _ \| '__| |/ / _ \ '__|
5 \ V V / (_) | | | < __/ |
6 \_/\_/ \___/|_| |_|\_\___|_|
7
8An agent that works on PUSH-PULL messages received by a Dealer agent.
9*/
10#ifndef WORKER_HPP
11#define WORKER_HPP
12
13#include "agent.hpp"
14#include "mads.hpp"
15#include <zmq.hpp>
16#include <zmq_addon.hpp>
17
18using json = nlohmann::json;
19
20namespace Mads {
21
22class Worker : public Agent {
23public:
24 Worker(string name, string settings_path) :
25 Agent(name, settings_path),
26 _receiver(_context, zmq::socket_type::pull) {
27 load_settings();
28 }
29
30 void connect(chrono::milliseconds delay = chrono::milliseconds(0)) {
31 Agent::connect(delay);
32 _receiver.connect(_dealer_address);
33 }
34
35 void info(ostream &out = cout) override {
36 Agent::info(out);
37 out << " Dealer Address: " << style::bold << _dealer_address << style::reset << endl;
38 }
39
66 json pull(chrono::milliseconds timeout) {
67 json j;
68 zmq::pollitem_t item;
69 item = zmq::pollitem_t{_receiver.handle(), 0, ZMQ_POLLIN, 0};
70 try {
71 zmq::poll(&item, 1, timeout);
72 } catch (const zmq::error_t &) {
73 return j; // context terminating under us: no work, and none coming
74 }
75 if (!(item.revents & ZMQ_POLLIN)) return j;
76
77 zmq::multipart_t msg;
78 if (!msg.recv(_receiver, ZMQ_DONTWAIT)) return j;
79 if (msg.size() == 0) return j;
80
81 try {
82 j = json::parse(msg.at(0).to_string());
83 } catch (const std::exception &e) {
84 cerr << fg::red << "Error parsing JSON: " << e.what() << fg::reset << endl;
85 j["error"] = e.what();
86 }
87 return j;
88 }
89
92 json pull() { return pull(chrono::milliseconds(receive_timeout())); }
93
94
95private:
96 void load_settings() override {
97 auto cfg = _config[_name];
98 _dealer_address = cfg["dealer_address"].value_or("tcp://localhost:9093");
99 };
100
101
102private:
103 string _dealer_address;
104 zmq::socket_t _receiver;
105
106};
107
108} // namespace Mads
109#endif // WORKER_HPP
nlohmann::json json
Definition bridge.hpp:24
toml::table _config
Definition agent.hpp:1289
std::string name()
Returns the name of the agent.
std::string _name
Definition agent.hpp:1286
void connect(std::chrono::milliseconds delay=std::chrono::milliseconds(250))
Connects the agent to the publish and subscribe endpoints.
virtual void info(std::ostream &out=std::cout)
Prints information about the agent.
int receive_timeout()
Returns the value of timeout in receiving messages.
zmq::context_t _context
Definition agent.hpp:1299
json pull(chrono::milliseconds timeout)
Waits up to timeout for one work item from the dealer, and returns an empty object if none arrives.
Definition worker.hpp:66
json pull()
Definition worker.hpp:92
Worker(string name, string settings_path)
Definition worker.hpp:24
void info(ostream &out=cout) override
Prints information about the agent.
Definition worker.hpp:35
void connect(chrono::milliseconds delay=chrono::milliseconds(0))
Definition worker.hpp:30
Definition agent.hpp:67
nlohmann::json json