diff --git a/components/control/cpp/CMakeLists.txt b/components/control/cpp/CMakeLists.txt index 9a27600480d26175e27f29f0318f8155c5267d5e..0705bc6e094c68b04ed0d9ad75a18504009eac18 100644 --- a/components/control/cpp/CMakeLists.txt +++ b/components/control/cpp/CMakeLists.txt @@ -1,5 +1,6 @@ add_library(ftlctrl src/slave.cpp + src/master.cpp ) target_include_directories(ftlctrl PUBLIC diff --git a/components/control/cpp/include/ftl/master.hpp b/components/control/cpp/include/ftl/master.hpp new file mode 100644 index 0000000000000000000000000000000000000000..e069f12fcdfc813c27db76dbb75795b23f2570b0 --- /dev/null +++ b/components/control/cpp/include/ftl/master.hpp @@ -0,0 +1,65 @@ +#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_ diff --git a/components/control/cpp/src/master.cpp b/components/control/cpp/src/master.cpp new file mode 100644 index 0000000000000000000000000000000000000000..cba2234c2bed6dccb2572781a2eea34f6e4c6893 --- /dev/null +++ b/components/control/cpp/src/master.cpp @@ -0,0 +1,78 @@ +#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 diff --git a/components/control/cpp/src/slave.cpp b/components/control/cpp/src/slave.cpp index 6138be4676a34f6121562080b7a1d3fbd59a6037..700c1da56fb6de49bf45c36e24972a0d82be58cb 100644 --- a/components/control/cpp/src/slave.cpp +++ b/components/control/cpp/src/slave.cpp @@ -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); }