Mads
Multi-Agent Distributed System
Loading...
Searching...
No Matches
bridge.hpp
Go to the documentation of this file.
1/*
2 ____ _ _ _
3 | __ ) _ __(_) __| | __ _ ___ ___| | __ _ ___ ___
4 | _ \| '__| |/ _` |/ _` |/ _ \ / __| |/ _` / __/ __|
5 | |_) | | | | (_| | (_| | __/ | (__| | (_| \__ \__ \
6 |____/|_| |_|\__,_|\__, |\___| \___|_|\__,_|___/___/
7 |___/
8
9This class is a pure frontend class: it acts as a bridge between the output of
10an external program and the mads framework.
11
12Author(s): Paolo Bosetti
13*/
14
15#ifndef BRIDGE_HPP
16#define BRIDGE_HPP
17
18
19#include "mads.hpp"
20#include "agent.hpp"
21#include <regex>
22#include <iostream>
23
24using json = nlohmann::json;
25
26namespace Mads {
27
34class Bridge : public Agent {
35public:
42 Bridge(std::string name, std::string settings_path)
43 : Agent(name, settings_path) {
44 load_settings();
45 }
46
59 void route() {
60 zmq::multipart_t message;
61 string payload, compressed;
62 regex re("^(\\w+):\\s*(\\{.*\\})\\s*$");
63 smatch match;
64 json j;
65 while (getline(cin, payload)) {
66 if (payload == "exit") {
67 Mads::Runtime::stop_process();
68 break;
69 }
70 if (regex_search(payload, match, re) && match.size() > 1) {
71 _pub_topic = match[1];
72 payload = match[2];
73 }
74 cout << "Topic: " << _pub_topic << ", Payload: " << payload << endl;
75 try {
76 j = json::parse(payload);
78 } catch (const std::exception &e) {
79 cerr << "Failed to parse JSON: " << e.what() << endl;
80 continue;
81 }
82 }
83 }
84
85private:
92 void load_settings() override {
93 // NONE
94 }
95
96};
97
98} // namespace Mads
99
100#endif // BRIDGE_HPP
nlohmann::json json
Definition bridge.hpp:24
std::string name()
Returns the name of the agent.
std::string _pub_topic
Definition agent.hpp:1291
void publish(nlohmann::json payload, std::string topic="")
Publishes a message with the given JSON payload.
The Bridge class represents metadata for an agent.
Definition bridge.hpp:34
void route()
Routes any line received via STDIN to the mads network.
Definition bridge.hpp:59
Bridge(std::string name, std::string settings_path)
Constructs a Bridge object with the given name and settings path.
Definition bridge.hpp:42
Definition agent.hpp:67
nlohmann::json json