summaryrefslogtreecommitdiff
path: root/poc/web_swarm/orchestrator/c++/control.hpp
diff options
context:
space:
mode:
authorClaudius "keldu" Holeksa <mail@keldu.de>2026-11-13 14:12:49 +0100
committerClaudius "keldu" Holeksa <mail@keldu.de>2026-11-13 14:12:49 +0100
commite821d617802ebbfff756844e4d9720342fc78a4e (patch)
tree1dcf33b84698b5074218e44ea491b77a9c23ee66 /poc/web_swarm/orchestrator/c++/control.hpp
parent0c0087ef9482634273ec1a23d3a915754f94926d (diff)
downloadlibs-lbm-dev.tar.gz
Swarm POCdev
Diffstat (limited to 'poc/web_swarm/orchestrator/c++/control.hpp')
-rw-r--r--poc/web_swarm/orchestrator/c++/control.hpp74
1 files changed, 74 insertions, 0 deletions
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 <kel/lbm/swarm/registry.hpp>
+
+#include <atomic>
+#include <condition_variable>
+#include <cstdint>
+#include <functional>
+#include <mutex>
+#include <string>
+#include <thread>
+
+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<void(const std::string &)> handler);
+
+private:
+ void handle_message(const std::shared_ptr<session> &sess,
+ const std::string &text);
+ void handle_hello(const std::shared_ptr<session> &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<bool> running_{false};
+
+ std::function<void(const std::string &)> on_event_;
+};
+
+} // namespace swarm
+} // namespace lbm
+} // namespace kel