diff options
| author | Claudius "keldu" Holeksa <mail@keldu.de> | 2026-11-13 14:12:49 +0100 |
|---|---|---|
| committer | Claudius "keldu" Holeksa <mail@keldu.de> | 2026-11-13 14:12:49 +0100 |
| commit | e821d617802ebbfff756844e4d9720342fc78a4e (patch) | |
| tree | 1dcf33b84698b5074218e44ea491b77a9c23ee66 /poc/web_swarm/worker/c++ | |
| parent | 0c0087ef9482634273ec1a23d3a915754f94926d (diff) | |
| download | libs-lbm-e821d617802ebbfff756844e4d9720342fc78a4e.tar.gz | |
Swarm POCdev
Diffstat (limited to 'poc/web_swarm/worker/c++')
| -rw-r--r-- | poc/web_swarm/worker/c++/SConscript | 32 | ||||
| -rw-r--r-- | poc/web_swarm/worker/c++/protocol.hpp | 160 | ||||
| -rw-r--r-- | poc/web_swarm/worker/c++/worker.cpp | 102 |
3 files changed, 294 insertions, 0 deletions
diff --git a/poc/web_swarm/worker/c++/SConscript b/poc/web_swarm/worker/c++/SConscript new file mode 100644 index 0000000..a678b7b --- /dev/null +++ b/poc/web_swarm/worker/c++/SConscript @@ -0,0 +1,32 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +worker_env = env.Clone(); + +worker_env.sources = sorted(glob.glob(dir_path + "/*.cpp")); +worker_env.headers = sorted(glob.glob(dir_path + "/*.hpp")); + +env.sources += worker_env.sources; +env.headers += worker_env.headers; + +objects = [] +worker_env.add_source_files(objects, worker_env.sources, shared=False); +env.worker = worker_env.em_program('#bin/kel_lbm_worker', [objects]); + +env.targets += [env.worker]; + +# The page and its glue live beside the module: index.html loads worker.js for +# the transport and kel_lbm_worker.js for the emscripten loader. +web_dir = Dir('#web').abspath +web_files = sorted(glob.glob(web_dir + "/*")) + +env.Install('$prefix/share/kel-lbm/worker/', env.worker); +env.Install('$prefix/share/kel-lbm/worker/', web_files); diff --git a/poc/web_swarm/worker/c++/protocol.hpp b/poc/web_swarm/worker/c++/protocol.hpp new file mode 100644 index 0000000..16d379f --- /dev/null +++ b/poc/web_swarm/worker/c++/protocol.hpp @@ -0,0 +1,160 @@ +#pragma once + +#include <kel/lbm/webgl/probe.hpp> + +#include <kel/lbm/swarm/message.hpp> +#include <kel/lbm/swarm/wire.hpp> + +#include <cstdint> +#include <string> + +/** + * What the browser worker does, separated from how JavaScript calls it. + * + * worker.cpp is a thin extern "C" shim over this; keeping the behaviour in a + * header means the self test can drive it directly under node, without a + * browser and without going through ccall. The GL probe is the only part that + * genuinely needs a browser, and it fails cleanly when there is no context -- + * which is exactly the state node is in, so everything else stays exercisable. + */ +namespace kel { +namespace lbm { +namespace swarm { + +class worker_protocol { +public: + struct config { + std::string worker_id = "browser"; + std::string user_agent = "unknown"; + }; + + /** + * Runs the capability probe. + * + * Returns false when this device cannot run a solver. That is not a reason + * to stay silent: the worker still registers, so the orchestrator learns + * the device exists and why it is unusable, rather than never hearing from + * it at all. + */ + bool init(config cfg, uint64_t now_ms){ + cfg_ = std::move(cfg); + started_at_ms_ = now_ms; + + webgl::probe_config pcfg; + probe_ok_ = webgl::probe_webgl2(pcfg, capability_); + + return probe_ok_; + } + + /// The hello to send once the control channel opens. + std::string hello(uint64_t now_ms) const { + auto message = make_message<sch::MsgHelloWebGl2>( + msg::hello, cfg_.worker_id, 0u); + + auto &cap = message.get<"capability">(); + cap.get<"worker_id">().set(cfg_.worker_id); + cap.get<"kind">().set(std::string{wkind::webgl2}); + cap.get<"schema_version">().set(std::string{schema_version}); + cap.get<"reported_at_ms">().set(now_ms); + cap.get<"user_agent">().set(cfg_.user_agent); + cap.get<"webgl2">() = capability_; + + auto eo = encode_message<sch::MsgHelloWebGl2>(message); + if(eo.is_error()){ + return {}; + } + return eo.get_value(); + } + + /** + * Handles one control message, returning the reply or an empty string. + * + * The transport never has to know that a ping wants a pong -- only that a + * non-empty return goes back down the channel. + */ + std::string handle(const std::string &text, uint64_t now_ms){ + auto eo_hdr = peek_header(text); + if(eo_hdr.is_error()){ + return {}; + } + + const std::string type = eo_hdr.get_value().get<"type">().stl_string(); + + if(type == msg::ping){ + return handle_ping(text, now_ms); + } + if(type == msg::welcome){ + return handle_welcome(now_ms); + } + + // Anything else is not ours to answer. + return {}; + } + + uint64_t pongs() const { return pongs_; } + bool probe_ok() const { return probe_ok_; } + + const data<sch::WebGl2Capability> &capability() const { return capability_; } + +private: + data<sch::WorkerLive> make_live(uint64_t now_ms) const { + data<sch::WorkerLive> live; + live.get<"state">().set(std::string{ + probe_ok_ ? wstate::ready : wstate::error}); + live.get<"uptime_ms">().set( + (now_ms > started_at_ms_) ? (now_ms - started_at_ms_) : 0u); + // Nothing is being simulated yet; the field exists so the monitor does + // not need a protocol change once something is. + live.get<"steps_done">().set(0u); + live.get<"last_error">().set(std::string{""}); + return live; + } + + std::string handle_ping(const std::string &text, uint64_t now_ms){ + auto eo = decode_message<sch::MsgPing>(text); + if(eo.is_error()){ + return {}; + } + + auto pong = make_message<sch::MsgPong>( + msg::pong, cfg_.worker_id, eo.get_value().get<"seq">().get()); + + // Echo the orchestrator's own timestamp so it can measure the round + // trip against its clock. The two machines never have to agree on what + // time it is. + pong.get<"sent_at_ms">().set(eo.get_value().get<"sent_at_ms">().get()); + pong.get<"worker_now_ms">().set(now_ms); + pong.get<"live">() = make_live(now_ms); + + auto eo_out = encode_message<sch::MsgPong>(pong); + if(eo_out.is_error()){ + return {}; + } + + ++pongs_; + return eo_out.get_value(); + } + + std::string handle_welcome(uint64_t now_ms){ + // Registration accepted. Announce readiness, which is a claim only the + // worker can make -- the orchestrator will not assume it. + auto ready = make_message<sch::MsgReady>(msg::ready, cfg_.worker_id, 0u); + ready.get<"live">() = make_live(now_ms); + + auto eo = encode_message<sch::MsgReady>(ready); + if(eo.is_error()){ + return {}; + } + return eo.get_value(); + } + + config cfg_; + data<sch::WebGl2Capability> capability_; + bool probe_ok_ = false; + uint64_t started_at_ms_ = 0u; + uint64_t pongs_ = 0u; +}; + +} // namespace swarm +} // namespace lbm +} // namespace kel diff --git a/poc/web_swarm/worker/c++/worker.cpp b/poc/web_swarm/worker/c++/worker.cpp new file mode 100644 index 0000000..db6d2cd --- /dev/null +++ b/poc/web_swarm/worker/c++/worker.cpp @@ -0,0 +1,102 @@ +#include "protocol.hpp" + +#include <emscripten.h> + +#include <string> + +/** + * The browser worker, wasm half: a thin shim over worker_protocol. + * + * The division of labour with worker.js is deliberate and worth stating, + * because the obvious alternative is tempting and wrong. JS owns the + * transport: the WebSocket, the RTCPeerConnection, the data channel and the + * timer. It moves strings and nothing else. Everything that decides what a + * string *means* is on this side. + * + * The reason is single sourcing. The wire format is defined once, as schemas + * in modules/swarm, and both the orchestrator and this worker encode and + * decode through the same codec. A pure JS worker would have been quicker to + * write and would have put a second, hand maintained copy of the format in a + * language where nothing checks it against the first. + * + * Every entry point takes and returns plain C strings. Returned pointers point + * into a static buffer owned here and stay valid until the next call that + * returns a string, which is all JS needs since it copies immediately via + * UTF8ToString. + */ +namespace { + +kel::lbm::swarm::worker_protocol g_worker; + +/// Backing store for whatever was last handed to JS. +std::string g_out; + +const char *hand_over(std::string text){ + g_out = std::move(text); + return g_out.c_str(); +} + +/** + * Monotonic milliseconds since the page started. Deliberately performance.now + * rather than wall clock: the live block reports an uptime, and round trips + * are measured by echoing the orchestrator's own stamp, so nothing here needs + * to agree with another machine's idea of the time. + */ +EM_JS(double, js_now_ms, (), { + return performance.now(); +}); + +uint64_t now_ms(){ + return static_cast<uint64_t>(js_now_ms()); +} + +} // namespace + +extern "C" { + +/// Runs the capability probe. Call once, before connecting. 1 if this device +/// can actually run a solver, 0 otherwise. +EMSCRIPTEN_KEEPALIVE +int kel_worker_init(const char *worker_id, const char *user_agent){ + kel::lbm::swarm::worker_protocol::config cfg; + if(worker_id != nullptr and *worker_id != '\0'){ + cfg.worker_id = worker_id; + } + if(user_agent != nullptr){ + cfg.user_agent = user_agent; + } + + return g_worker.init(std::move(cfg), now_ms()) ? 1 : 0; +} + +EMSCRIPTEN_KEEPALIVE +const char *kel_worker_hello(){ + return hand_over(g_worker.hello(now_ms())); +} + +EMSCRIPTEN_KEEPALIVE +const char *kel_worker_handle(const char *text){ + if(text == nullptr){ + return hand_over(std::string{}); + } + return hand_over(g_worker.handle(std::string{text}, now_ms())); +} + +EMSCRIPTEN_KEEPALIVE +double kel_worker_pongs(){ + return static_cast<double>(g_worker.pongs()); +} + +EMSCRIPTEN_KEEPALIVE +double kel_worker_budget_bytes(){ + return static_cast<double>( + g_worker.capability().get<"budget">().get<"probed_bytes_safe">().get()); +} + +} // extern "C" + +int main(){ + // Nothing to do. The page drives everything through the exports above; + // main exists because emscripten wants an entry point. + return 0; +} |
