Skip to content
Snippets Groups Projects
Commit 18efd4fb authored by Nicolas Pope's avatar Nicolas Pope
Browse files

Add master to control

parent a34a6957
No related branches found
No related tags found
No related merge requests found
Pipeline #11190 failed
add_library(ftlctrl
src/slave.cpp
src/master.cpp
)
target_include_directories(ftlctrl PUBLIC
......
#ifndef _FTL_CTRL_MASTER_HPP_
#define _FTL_CTRL_MASTER_HPP_
#include <ftl/net/universe.hpp>
#include <ftl/configurable.hpp>
#include <ftl/uuid.hpp>
#include <functional>
#include <string>
#include <vector>
namespace ftl {
namespace ctrl {
struct LogEvent {
std::string preamble;
std::string message;
};
class Master {
public:
Master(ftl::Configurable *root, ftl::net::Universe *net);
~Master();
void restart();
void restart(const ftl::UUID &peer);
void shutdown();
void shutdown(const ftl::UUID &peer);
void set(const std::string &uri, ftl::config::json_t &value);
void set(const ftl::UUID &peer, const std::string &uri, ftl::config::json_t &value);
std::vector<std::string> getConfigurables();
std::vector<std::string> getConfigurables(const ftl::UUID &peer);
std::vector<ftl::config::json_t> get(const std::string &uri);
ftl::config::json_t getOne(const std::string &uri);
ftl::config::json_t get(const ftl::UUID &peer, const std::string &uri);
void watch(const std::string &uri, std::function<void()> f);
// Events
//void onError();
void onLog(std::function<void(const LogEvent &)>);
//void onFailure();
//void onStatus();
// void onChange();
private:
std::vector<std::function<void(const LogEvent&)>> log_handlers_;
ftl::Configurable *root_;
ftl::net::Universe *net_;
};
}
}
#endif // _FTL_CTRL_MASTER_HPP_
#include <ftl/master.hpp>
using ftl::ctrl::Master;
using ftl::net::Universe;
using ftl::Configurable;
using std::string;
using ftl::config::json_t;
using std::vector;
using std::function;
using ftl::ctrl::LogEvent;
Master::Master(Configurable *root, Universe *net)
: root_(root), net_(net) {
net_->bind("log", [this](const std::string &pre, const std::string &msg) {
for (auto f : log_handlers_) {
f({pre,msg});
}
});
}
Master::~Master() {
}
void Master::restart() {
net_->broadcast("restart");
}
void Master::restart(const ftl::UUID &peer) {
net_->send(peer, "restart");
}
void Master::shutdown() {
net_->broadcast("shutdown");
}
void Master::shutdown(const ftl::UUID &peer) {
net_->send(peer, "shutdown");
}
void Master::set(const string &uri, json_t &value) {
net_->broadcast("update_cfg", uri, (string)value);
}
void Master::set(const ftl::UUID &peer, const string &uri, json_t &value) {
net_->send(peer, "update_cfg", uri, (string)value);
}
vector<string> Master::getConfigurables() {
}
vector<string> Master::getConfigurables(const ftl::UUID &peer) {
}
vector<json_t> Master::get(const string &uri) {
}
json_t Master::getOne(const string &uri) {
}
json_t Master::get(const ftl::UUID &peer, const string &uri) {
}
void Master::watch(const string &uri, function<void()> f) {
}
// Events
//void onError();
void Master::onLog(function<void(const LogEvent &)> h) {
log_handlers_.push_back(h);
}
\ No newline at end of file
......@@ -21,6 +21,14 @@ Slave::Slave(Universe *net, ftl::Configurable *root) {
exit(0);
});
net->bind("update_cfg", [](const std::string &uri, const std::string &value) {
ftl::config::update(uri, nlohmann::json::parse(value));
});
net->bind("get_cfg", [](const std::string &uri) -> std::string {
return ftl::config::resolve(uri);
});
loguru::add_callback("net_log", netLog, net, loguru::Verbosity_INFO);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment