Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
dealer.hpp
Go to the documentation of this file.
1/*
2 ____ _
3 | _ \ ___ __ _| | ___ _ __
4 | | | |/ _ \/ _` | |/ _ \ '__|
5 | |_| | __/ (_| | | __/ |
6 |____/ \___|\__,_|_|\___|_|
7
8An agent that collects messages from the mads network and sends them to
9listening workers, in a round-robin fashion.
10*/
11#ifndef DEALER_HPP
12#define DEALER_HPP
13
14#include "agent.hpp"
15#include "mads.hpp"
16#include <zmq.hpp>
17#include <zmq_addon.hpp>
18
19using json = nlohmann::json;
20
21namespace Mads {
22
23class Dealer : public Agent {
24public:
25 Dealer(string name, string settings_path) :
26 Agent(name, settings_path),
27 _sender(_context, zmq::socket_type::push) {
28 load_settings();
29 }
30
31 void connect(chrono::milliseconds delay = chrono::milliseconds(0)) {
32 Agent::connect(delay);
33 _sender.bind(_dealer_address);
34 }
35
36 void info(ostream &out = cout) override {
37 Agent::info(out);
38 out << " Dealer Address: " << style::bold << _dealer_address << style::reset << endl;
39 }
40
41 void push(string message) {
42 _sender.send(zmq::buffer(message), zmq::send_flags::none);
43 }
44
45 void push(json j) {
46 const string payload = j.dump();
47 _sender.send(zmq::buffer(payload), zmq::send_flags::none);
48 }
49
50private:
51 void load_settings() override {
52 auto cfg = _config[_name];
53 _dealer_address = cfg["dealer_address"].value_or("tcp://*:9093");
54 };
55
56
57private:
58 string _dealer_address;
59 zmq::socket_t _sender;
60
61};
62
63} // namespace Mads
64#endif // DEALER_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.
zmq::context_t _context
Definition agent.hpp:1299
void push(string message)
Definition dealer.hpp:41
Dealer(string name, string settings_path)
Definition dealer.hpp:25
void connect(chrono::milliseconds delay=chrono::milliseconds(0))
Definition dealer.hpp:31
void info(ostream &out=cout) override
Prints information about the agent.
Definition dealer.hpp:36
void push(json j)
Definition dealer.hpp:45
Definition agent.hpp:67
nlohmann::json json