16#include <zmq_addon.hpp>
18using json = nlohmann::json;
30 void connect(chrono::milliseconds delay = chrono::milliseconds(0)) {
32 _receiver.connect(_dealer_address);
35 void info(ostream &out = cout)
override {
37 out <<
" Dealer Address: " << style::bold << _dealer_address << style::reset << endl;
69 item = zmq::pollitem_t{_receiver.handle(), 0, ZMQ_POLLIN, 0};
71 zmq::poll(&item, 1, timeout);
72 }
catch (
const zmq::error_t &) {
75 if (!(item.revents & ZMQ_POLLIN))
return j;
78 if (!msg.recv(_receiver, ZMQ_DONTWAIT))
return j;
79 if (msg.size() == 0)
return j;
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();
96 void load_settings()
override {
98 _dealer_address = cfg[
"dealer_address"].value_or(
"tcp://localhost:9093");
103 string _dealer_address;
104 zmq::socket_t _receiver;
std::string name()
Returns the name of the agent.
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.
json pull(chrono::milliseconds timeout)
Waits up to timeout for one work item from the dealer, and returns an empty object if none arrives.
Worker(string name, string settings_path)
void info(ostream &out=cout) override
Prints information about the agent.
void connect(chrono::milliseconds delay=chrono::milliseconds(0))