From e821d617802ebbfff756844e4d9720342fc78a4e Mon Sep 17 00:00:00 2001 From: "Claudius \"keldu\" Holeksa" Date: Fri, 13 Nov 2026 14:12:49 +0100 Subject: Swarm POC --- poc/web_swarm/orchestrator/c++/control.hpp | 74 ++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 poc/web_swarm/orchestrator/c++/control.hpp (limited to 'poc/web_swarm/orchestrator/c++/control.hpp') diff --git a/poc/web_swarm/orchestrator/c++/control.hpp b/poc/web_swarm/orchestrator/c++/control.hpp new file mode 100644 index 0000000..9365ef9 --- /dev/null +++ b/poc/web_swarm/orchestrator/c++/control.hpp @@ -0,0 +1,74 @@ +#pragma once + +#include "orchestrator.hpp" +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace kel { +namespace lbm { +namespace swarm { + +struct control_config { + uint64_t heartbeat_ms = 2000u; + registry::thresholds limits; +}; + +/** + * The control protocol on top of the transport. + * + * Kept apart from `orchestrator` so each can be reasoned about alone: the + * transport knows nothing about hellos, and this knows nothing about ICE. + * + * Owns the heartbeat thread. libdatachannel provides no timers, so liveness is + * a plain ticker -- which is fine, since the work it does per beat is a lock, a + * vector copy and some sends. + */ +class control { +public: + control(orchestrator &orch, control_config cfg); + ~control(); + + control(const control &) = delete; + control &operator=(const control &) = delete; + + /// Installs the handlers on the orchestrator. Call before listen(). + void attach(); + + void start_heartbeat(); + void stop_heartbeat(); + + registry &workers() { return registry_; } + + /// Emitted for anything worth a line in the log. + void on_event(std::function handler); + +private: + void handle_message(const std::shared_ptr &sess, + const std::string &text); + void handle_hello(const std::shared_ptr &sess, + const std::string &text); + void heartbeat_loop(); + void emit(const std::string &line); + + orchestrator &orch_; + control_config cfg_; + registry registry_; + + std::thread ticker_; + std::mutex tick_mutex_; + std::condition_variable tick_cv_; + std::atomic running_{false}; + + std::function on_event_; +}; + +} // namespace swarm +} // namespace lbm +} // namespace kel -- cgit v1.3.1