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 | |
| parent | 0c0087ef9482634273ec1a23d3a915754f94926d (diff) | |
| download | libs-lbm-e821d617802ebbfff756844e4d9720342fc78a4e.tar.gz | |
Swarm POCdev
Diffstat (limited to 'poc/web_swarm/worker')
| -rw-r--r-- | poc/web_swarm/worker/.nix/derivation.nix | 80 | ||||
| -rw-r--r-- | poc/web_swarm/worker/SConstruct | 120 | ||||
| -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 | ||||
| -rw-r--r-- | poc/web_swarm/worker/selftest/SConscript | 35 | ||||
| -rw-r--r-- | poc/web_swarm/worker/selftest/selftest.cpp | 146 | ||||
| -rw-r--r-- | poc/web_swarm/worker/web/index.html | 87 | ||||
| -rw-r--r-- | poc/web_swarm/worker/web/worker.js | 286 |
9 files changed, 1048 insertions, 0 deletions
diff --git a/poc/web_swarm/worker/.nix/derivation.nix b/poc/web_swarm/worker/.nix/derivation.nix new file mode 100644 index 0000000..694d951 --- /dev/null +++ b/poc/web_swarm/worker/.nix/derivation.nix @@ -0,0 +1,80 @@ +{ lib +, stdenv +, scons +, clang-tools +, emscripten +, python3 +, nodejs +, pname +, version +, kel +}: + +stdenv.mkDerivation { + pname = "${pname}-web-swarm-worker"; + inherit version; + src = ./..; + + nativeBuildInputs = [ + scons + clang-tools + emscripten + python3 + nodejs + ]; + + # Deliberately empty. em++ is not a nix cc-wrapper, so it never reads + # NIX_CFLAGS_COMPILE and cannot consume buildInputs the way the native + # builds do. Include roots are handed to scons explicitly below, which also + # has the virtue of making the dependency visible rather than ambient. + buildInputs = [ ]; + + sconsFlags = [ + ("kel_include=" + + "${kel.lbm.core.dev}/include:" + + "${kel.lbm.swarm.dev}/include:" + + "${kel.lbm.webgl.dev}/include") + ]; + + # Same class of problem as the AdaptiveCpp HOME hack in + # modules/sycl/.nix/derivation.nix: a toolchain that insists on a writable + # cache inside a read-only build. Seed it from the cache the emscripten + # derivation already precompiled, or every build rebuilds the sysroot. + preBuild = '' + export HOME="$TMPDIR" + export EM_CACHE="$TMPDIR/em_cache" + cp -r ${emscripten}/share/emscripten/cache "$EM_CACHE" + chmod -R u+w "$EM_CACHE" + ''; + + # The real worker needs a browser. Its protocol half does not, so that part + # is built for node and run here; only the GL probe is left uncovered. + doCheck = true; + + checkPhase = '' + runHook preCheck + + node ./bin/kel_lbm_worker_selftest.js + + runHook postCheck + ''; + + postInstall = '' + cat > $out/share/kel-lbm/worker/README <<'EOT' + Serve this directory over HTTP and open index.html. + + python3 -m http.server 8000 + + file:// will not work: wasm is fetched and file:// is a null origin. + + The orchestrator is a separate process on its own port -- a WebSocket + server is not an HTTP server, so it does not serve this page. Start it + with kel_lbm_orchestrator and point the page at it, either in the form + on the page or with ?orchestrator=ws://host:port/ + EOT + ''; + + preferLocalBuild = true; + + outputs = [ "out" ]; +} diff --git a/poc/web_swarm/worker/SConstruct b/poc/web_swarm/worker/SConstruct new file mode 100644 index 0000000..ae0f068 --- /dev/null +++ b/poc/web_swarm/worker/SConstruct @@ -0,0 +1,120 @@ +#!/usr/bin/env python3 + +import sys +import os +import os.path +import glob +import re + + +if sys.version_info < (3,): + def isbasestring(s): + return isinstance(s,basestring) +else: + def isbasestring(s): + return isinstance(s, (str,bytes)) + +def add_kel_source_files(self, sources, filetype, lib_env=None, shared=False, target_post=""): + + if isbasestring(filetype): + dir_path = self.Dir('.').abspath + filetype = sorted(glob.glob(dir_path+"/"+filetype)) + + for path in filetype: + target_name = re.sub( r'(.*?)(\.cpp|\.c\+\+)', r'\1' + target_post, path ) + if shared: + target_name+='.os' + sources.append( self.SharedObject( target=target_name, source=path ) ) + else: + target_name+='.o' + sources.append( self.StaticObject( target=target_name, source=path ) ) + pass + +def isAbsolutePath(key, dirname, env): + assert os.path.isabs(dirname), "%r must have absolute path syntax" % (key,) + +# em++ emits a .js loader plus a .wasm sidecar, but SCons only learns about the +# target it was asked for, so the .wasm is invisible to the dependency graph: +# `scons -c` leaves it behind and an incremental build can reuse a stale one. +# +# Program's emitter is an EmitterProxy rather than a dict, so add_emitter() is +# not available on it. Declaring the sidecar explicitly is the supported route: +# SideEffect keeps parallel builds from racing on it, Clean makes -c remove it. +def em_program(env, target, objects): + js = env.Program(target, objects) + wasm = env.File(str(js[0])[:-3] + '.wasm') + env.SideEffect(wasm, js) + env.Clean(js, wasm) + return [js[0], wasm] + +env_vars = Variables( + args=ARGUMENTS +) + +env_vars.Add('prefix', + help='Installation target location of build results and headers', + default='/usr/local/', + validator=isAbsolutePath +) + +# em++ is not a nix cc-wrapper, so it never sees NIX_CFLAGS_COMPILE and cannot +# pick up buildInputs the way every other module here does. The derivation +# passes the include roots explicitly instead; see .nix/derivation.nix. +env_vars.Add('kel_include', + help='Colon separated include roots for kel-lbm headers', + default='' +) + +env=Environment(ENV=os.environ, variables=env_vars, + CPPPATH=[p for p in ARGUMENTS.get('kel_include', '').split(':') if p], + CPPDEFINES=[], + CXX = 'em++', + CC = 'emcc', + AR = 'emar', + RANLIB = 'emranlib', + LINK = 'em++', + CXXFLAGS=[ + '-std=c++20', + '-O2', + '-Wall', + '-Wextra', + # kel-lbm's string_literal provides operator""_sl as a string literal + # operator template, which is a GNU extension the library leans on + # deliberately. Without this it warns once per translation unit from a + # core header and drowns out warnings that are actually ours. + '-Wno-gnu-string-literal-operator-template' + ], + LINKFLAGS=[ + # WebGL2 only, and the GLES3 entry points resolved through + # getProcAddress so extension functions work. + '-sFULL_ES3=1', + '-sWASM=1', + '-sMODULARIZE=1', + '-sENVIRONMENT=web', + '-sALLOW_MEMORY_GROWTH=1', + '-sMIN_WEBGL_VERSION=2', + '-sMAX_WEBGL_VERSION=2', + # The page drives the module through these rather than through main. + '-sEXPORTED_FUNCTIONS=_main,_kel_worker_init,_kel_worker_hello,' + '_kel_worker_handle,_kel_worker_pongs,_kel_worker_budget_bytes', + '-sEXPORTED_RUNTIME_METHODS=ccall,cwrap,UTF8ToString' + ], + LIBS=[], + PROGSUFFIX='.js' +); +env.__class__.add_source_files = add_kel_source_files +env.__class__.em_program = em_program + +env.objects = []; +env.sources = []; +env.headers = []; +env.targets = []; + +Export('env') +SConscript('c++/SConscript') +SConscript('selftest/SConscript') + +env.Alias('all', [env.targets]); +env.Default('all'); + +env.Alias('install', '$prefix') 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; +} diff --git a/poc/web_swarm/worker/selftest/SConscript b/poc/web_swarm/worker/selftest/SConscript new file mode 100644 index 0000000..f60d79a --- /dev/null +++ b/poc/web_swarm/worker/selftest/SConscript @@ -0,0 +1,35 @@ +#!/bin/false + +import os +import os.path +import glob + + +Import('env') + +dir_path = Dir('.').abspath + +# Built for node rather than the web, so a check phase can run it. Same +# protocol code as the real worker; only the entry point and the environment +# differ. +test_env = env.Clone(); +test_env['LINKFLAGS'] = [ + '-sWASM=1', + '-sENVIRONMENT=node', + '-sALLOW_MEMORY_GROWTH=1', + # The probe is compiled in and must link, but with no context it returns + # cleanly rather than running any of it. + '-sMIN_WEBGL_VERSION=2', + '-sMAX_WEBGL_VERSION=2', + '-sFULL_ES3=1', +] + +test_env.sources = sorted(glob.glob(dir_path + "/*.cpp")); + +env.sources += test_env.sources; + +objects = [] +test_env.add_source_files(objects, test_env.sources, shared=False); +env.selftest = test_env.em_program('#bin/kel_lbm_worker_selftest', [objects]); + +env.targets += [env.selftest]; diff --git a/poc/web_swarm/worker/selftest/selftest.cpp b/poc/web_swarm/worker/selftest/selftest.cpp new file mode 100644 index 0000000..ee13bbf --- /dev/null +++ b/poc/web_swarm/worker/selftest/selftest.cpp @@ -0,0 +1,146 @@ +#include "../c++/protocol.hpp" + +#include <kel/lbm/swarm/message.hpp> +#include <kel/lbm/swarm/wire.hpp> + +#include <cstdio> +#include <string> + +/** + * Node-hosted self test for the browser worker's protocol half. + * + * A check phase has no browser, so the real worker cannot be tested there. + * But only the GL probe actually needs one -- the part that decides what to + * say, and says it in the format the orchestrator parses, is ordinary code. + * Built for node and run in the derivation's check phase, this covers that + * part and leaves only the probe genuinely untested. + * + * Running with no WebGL context is not a limitation here, it is the point: it + * is the same path a browser takes when the device cannot give one, and the + * worker has to register anyway so the orchestrator learns why. + */ +namespace { + +using namespace kel::lbm; + +int failures = 0; + +void check(bool ok, const char *what){ + if(ok){ + std::printf(" ok %s\n", what); + return; + } + std::printf(" FAIL %s\n", what); + ++failures; +} + +} // namespace + +int main(){ + std::printf("worker protocol self test\n"); + + swarm::worker_protocol worker; + + swarm::worker_protocol::config cfg; + cfg.worker_id = "selftest"; + cfg.user_agent = "node"; + + // No WebGL under node, so the probe must fail -- cleanly, without taking + // the rest of the worker down with it. + const bool usable = worker.init(cfg, 1000u); + check(not usable, "probe reports the device unusable without a context"); + check(not worker.probe_ok(), "and remembers that it did"); + + // Registration still happens. An orchestrator that knows why a device is + // unusable is better off than one that never heard from it. + const std::string hello = worker.hello(2000u); + check(not hello.empty(), "an unusable device still produces a hello"); + + { + auto eo = swarm::decode_message<sch::MsgHelloWebGl2>(hello); + check(not eo.is_error(), "the hello decodes as a webgl2 hello"); + if(not eo.is_error()){ + const auto &cap = eo.get_value().get<"capability">(); + check(cap.get<"kind">().stl_view() == wkind::webgl2, + "it registers as a webgl2 worker"); + check(cap.get<"worker_id">().stl_view() == "selftest", + "it carries the configured id"); + check(cap.get<"schema_version">().stl_view() == schema_version, + "it states the schema version the orchestrator checks"); + check(not cap.get<"webgl2">().get<"features">() + .get<"color_buffer_float">().get(), + "and honestly reports no float render target"); + } + } + + // The routing decode the orchestrator does first has to work on it too. + { + auto eo = swarm::decode_message<sch::MsgHello>(hello); + check(not eo.is_error(), "the orchestrator's routing decode accepts it"); + } + + // A ping must come back as a pong echoing the sequence and the timestamp. + { + auto ping = swarm::make_message<sch::MsgPing>(msg::ping, "orch", 42u); + ping.get<"sent_at_ms">().set(123456u); + + auto eo_ping = swarm::encode_message<sch::MsgPing>(ping); + check(not eo_ping.is_error(), "a ping encodes"); + + const std::string reply = worker.handle(eo_ping.get_value(), 3000u); + check(not reply.empty(), "a ping is answered"); + + auto eo_pong = swarm::decode_message<sch::MsgPong>(reply); + check(not eo_pong.is_error(), "the answer is a well formed pong"); + if(not eo_pong.is_error()){ + const auto &pong = eo_pong.get_value(); + check(pong.get<"type">().stl_view() == msg::pong, "typed as a pong"); + check(pong.get<"seq">().get() == 42u, + "echoing the sequence, so a late pong can be told apart"); + check(pong.get<"sent_at_ms">().get() == 123456u, + "echoing the timestamp, so rtt needs no clock agreement"); + check(pong.get<"worker_now_ms">().get() == 3000u, + "and carrying the worker's own clock"); + check(pong.get<"live">().get<"state">().stl_view() == wstate::error, + "reporting error state, since the probe failed"); + } + + check(worker.pongs() == 1u, "the pong is counted"); + } + + // A welcome must produce a ready. Readiness is a claim only the worker + // makes; the orchestrator never assumes it. + { + auto welcome = swarm::make_message<sch::MsgWelcome>( + msg::welcome, "w1", 0u); + welcome.get<"heartbeat_ms">().set(2000u); + welcome.get<"schema_version">().set(std::string{schema_version}); + + auto eo_wel = swarm::encode_message<sch::MsgWelcome>(welcome); + check(not eo_wel.is_error(), "a welcome encodes"); + + const std::string reply = worker.handle(eo_wel.get_value(), 4000u); + check(not reply.empty(), "a welcome is answered"); + + auto eo_ready = swarm::decode_message<sch::MsgReady>(reply); + check(not eo_ready.is_error(), "the answer is a well formed ready"); + if(not eo_ready.is_error()){ + check(eo_ready.get_value().get<"type">().stl_view() == msg::ready, + "typed as ready"); + } + } + + // Things the worker has no business answering must produce nothing rather + // than an empty message on the wire. + { + check(worker.handle("", 5000u).empty(), "garbage is not answered"); + check(worker.handle("{", 5000u).empty(), "malformed json is not answered"); + check(worker.handle(R"({"type":"pong","worker_id":"x","seq":1})", 5000u) + .empty(), + "a message meant for the orchestrator is not answered"); + check(worker.pongs() == 1u, "and none of that counted as a pong"); + } + + std::printf("%s\n", failures == 0 ? "PASS" : "FAIL"); + return failures == 0 ? 0 : 1; +} diff --git a/poc/web_swarm/worker/web/index.html b/poc/web_swarm/worker/web/index.html new file mode 100644 index 0000000..dddf4a1 --- /dev/null +++ b/poc/web_swarm/worker/web/index.html @@ -0,0 +1,87 @@ +<!DOCTYPE html> +<html lang="en"> +<head> +<meta charset="utf-8"> +<meta name="viewport" content="width=device-width, initial-scale=1"> +<title>kel-lbm swarm worker</title> +<style> + body { + margin: 0; + padding: 1.5rem; + background: #14161a; + color: #d8dee9; + font-family: system-ui, sans-serif; + line-height: 1.5; + } + h1 { font-size: 1.1rem; margin: 0 0 1rem; font-weight: 600; } + .row { display: flex; gap: 0.5rem; margin-bottom: 0.5rem; flex-wrap: wrap; } + label { min-width: 7rem; align-self: center; font-size: 0.85rem; color: #8c95a3; } + input { + flex: 1; min-width: 14rem; padding: 0.4rem 0.6rem; + background: #1d2027; color: #d8dee9; + border: 1px solid #333842; border-radius: 4px; + font-family: ui-monospace, monospace; font-size: 0.85rem; + } + button { + padding: 0.4rem 1rem; border-radius: 4px; cursor: pointer; + background: #2d4f74; color: #e8eef5; + border: 1px solid #3b6694; font-size: 0.85rem; + } + button:disabled { opacity: 0.4; cursor: default; } + #status { + margin: 1rem 0; padding: 0.75rem 1rem; + background: #1d2027; border-left: 3px solid #3b6694; border-radius: 3px; + } + #state { font-size: 1.05rem; font-weight: 600; } + #detail { color: #8c95a3; font-size: 0.85rem; font-family: ui-monospace, monospace; } + #log { + height: 16rem; overflow-y: auto; white-space: pre-wrap; + background: #0f1115; border: 1px solid #262b33; border-radius: 4px; + padding: 0.75rem; font-family: ui-monospace, monospace; font-size: 0.78rem; + color: #9aa5b1; + } + /* + * The probe needs a real canvas to get a WebGL2 context from, but nothing is + * ever drawn for display. Kept at one pixel and out of the way rather than + * display:none, which some browsers treat as a reason to skip the context. + */ + #kel-probe { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; } + .note { color: #6b7684; font-size: 0.78rem; margin-top: 1rem; } +</style> +</head> +<body> + +<h1>kel-lbm swarm worker</h1> + +<div class="row"> + <label for="url">orchestrator</label> + <input id="url" type="text" spellcheck="false"> +</div> +<div class="row"> + <label for="worker-id">worker id</label> + <input id="worker-id" type="text" spellcheck="false"> +</div> +<div class="row"> + <button id="connect" disabled>connect</button> + <button id="disconnect" disabled>disconnect</button> +</div> + +<div id="status"> + <div id="state">loading</div> + <div id="detail"></div> +</div> + +<div id="log"></div> + +<p class="note"> + Open this page from an HTTP server, not a file:// URL — wasm is fetched, + and file:// is a null origin. The orchestrator is a separate process on its own + port; append <code>?orchestrator=ws://host:port/</code> to override. +</p> + +<canvas id="kel-probe" width="1" height="1"></canvas> + +<script src="worker.js"></script> +<script src="kel_lbm_worker.js"></script> +</body> +</html> diff --git a/poc/web_swarm/worker/web/worker.js b/poc/web_swarm/worker/web/worker.js new file mode 100644 index 0000000..04d4e10 --- /dev/null +++ b/poc/web_swarm/worker/web/worker.js @@ -0,0 +1,286 @@ +// The browser worker, transport half. +// +// This file owns the WebSocket, the RTCPeerConnection, the data channel and +// nothing else. Every decision about what a message means lives in the wasm +// module, so the wire format has exactly one definition -- the schemas in +// modules/swarm -- rather than one in C++ and a hand maintained copy here. +// +// The rule to keep: if you find yourself about to read a field out of a parsed +// message in this file, it belongs on the other side of the boundary. + +'use strict'; + +const ui = { + state: null, + detail: null, + log: null, +}; + +function setState(text, detail) { + if (ui.state) ui.state.textContent = text; + if (ui.detail && detail !== undefined) ui.detail.textContent = detail; +} + +function log(line) { + console.log(line); + if (!ui.log) return; + const stamp = new Date().toLocaleTimeString(); + ui.log.textContent += `[${stamp}] ${line}\n`; + ui.log.scrollTop = ui.log.scrollHeight; +} + +// Normalises an ICE candidate into the shape modules/swarm declares. The +// browser hands out {candidate, sdpMid, sdpMLineIndex}; the wire carries +// {type, candidate, mid}. Doing the mapping here keeps every message on the +// wire uniform -- all of them are objects with a "type". +function candidateMessage(candidate) { + return JSON.stringify({ + type: 'candidate', + candidate: candidate.candidate, + mid: candidate.sdpMid === null ? '' : candidate.sdpMid, + }); +} + +function descriptionMessage(description) { + return JSON.stringify({ + type: description.type, + sdp: description.sdp, + }); +} + +class Worker { + constructor(mod, url, workerId) { + this.mod = mod; + this.url = url; + this.workerId = workerId; + this.ws = null; + this.pc = null; + this.control = null; + + // Bound once so the wasm exports are called by name rather than through + // ccall at every message, which would re-resolve them each time. + this.hello = mod.cwrap('kel_worker_hello', 'string', []); + this.handle = mod.cwrap('kel_worker_handle', 'string', ['string']); + this.pongs = mod.cwrap('kel_worker_pongs', 'number', []); + } + + connect() { + setState('connecting', this.url); + log(`connecting to ${this.url}`); + + this.ws = new WebSocket(this.url); + + this.ws.onopen = () => { + setState('signalling'); + log('signalling socket open'); + }; + + this.ws.onerror = () => { + setState('error', 'signalling socket failed'); + log('signalling socket error'); + }; + + this.ws.onclose = () => { + setState('disconnected'); + log('signalling socket closed'); + }; + + this.ws.onmessage = (event) => this.onSignal(event.data); + + this.startPeerConnection(); + } + + startPeerConnection() { + // No ICE servers: host candidates are enough on localhost or a LAN, and + // a STUN server is only needed to get through NAT. + this.pc = new RTCPeerConnection({ iceServers: [] }); + + this.pc.onicecandidate = (event) => { + if (!event.candidate) return; + this.send(candidateMessage(event.candidate)); + }; + + this.pc.onconnectionstatechange = () => { + log(`peer connection ${this.pc.connectionState}`); + if (this.pc.connectionState === 'failed') { + setState('error', 'peer connection failed'); + } + }; + + // The orchestrator creates the channel, so it arrives rather than being + // made here. That fixes the ordering: it is already in the offer, so + // there is no race between it opening and the first send. + this.pc.ondatachannel = (event) => { + if (event.channel.label !== 'control') return; + this.attachControl(event.channel); + }; + } + + attachControl(channel) { + this.control = channel; + + channel.onopen = () => { + setState('registering'); + log('control channel open'); + + const hello = this.hello(); + if (!hello) { + setState('error', 'could not build hello'); + return; + } + log(`-> hello (${hello.length} bytes)`); + channel.send(hello); + }; + + channel.onmessage = (event) => { + if (typeof event.data !== 'string') return; + + // The one place the transport looks inside a message, and only to say + // what happened in the log. The reply itself is decided by wasm. + const reply = this.handle(event.data); + + let kind = '?'; + try { + kind = JSON.parse(event.data).type; + } catch (err) { + kind = 'unparseable'; + } + + if (kind === 'welcome') { + setState('registered', `pongs 0`); + log('<- welcome, registered'); + } else if (kind === 'ping') { + setState('registered', `pongs ${this.pongs()}`); + } else { + log(`<- ${kind}`); + } + + if (reply && channel.readyState === 'open') { + channel.send(reply); + } + }; + + channel.onclose = () => { + setState('disconnected', 'control channel closed'); + log('control channel closed'); + }; + } + + onSignal(text) { + let message; + try { + message = JSON.parse(text); + } catch (err) { + log('malformed signalling message'); + return; + } + + if (message.type === 'offer' || message.type === 'answer') { + this.pc + .setRemoteDescription({ type: message.type, sdp: message.sdp }) + .then(() => { + if (message.type !== 'offer') return null; + return this.pc.createAnswer(); + }) + .then((answer) => { + if (!answer) return null; + return this.pc.setLocalDescription(answer).then(() => { + this.send(descriptionMessage(this.pc.localDescription)); + }); + }) + .catch((err) => log(`signalling failed: ${err}`)); + return; + } + + if (message.type === 'candidate') { + this.pc + .addIceCandidate({ + candidate: message.candidate, + sdpMid: message.mid === '' ? null : message.mid, + }) + .catch((err) => log(`bad candidate: ${err}`)); + return; + } + + log(`unknown signalling type: ${message.type}`); + } + + send(text) { + if (this.ws && this.ws.readyState === WebSocket.OPEN) { + this.ws.send(text); + } + } + + disconnect() { + if (this.control) this.control.close(); + if (this.pc) this.pc.close(); + if (this.ws) this.ws.close(); + setState('disconnected'); + } +} + +function defaultUrl() { + const params = new URLSearchParams(window.location.search); + const fromQuery = params.get('orchestrator'); + if (fromQuery) return fromQuery; + // The page is served by something else -- a WebSocket server is not an HTTP + // server -- so the orchestrator is not on this port. 8080 is its default. + return `ws://${window.location.hostname || '127.0.0.1'}:8080/`; +} + +window.addEventListener('DOMContentLoaded', () => { + ui.state = document.getElementById('state'); + ui.detail = document.getElementById('detail'); + ui.log = document.getElementById('log'); + + const urlInput = document.getElementById('url'); + const idInput = document.getElementById('worker-id'); + const connectButton = document.getElementById('connect'); + const disconnectButton = document.getElementById('disconnect'); + + urlInput.value = defaultUrl(); + idInput.value = `browser-${Math.floor(Math.random() * 10000)}`; + + setState('loading', 'instantiating wasm'); + + Module().then((mod) => { + const init = mod.cwrap('kel_worker_init', 'number', ['string', 'string']); + const budget = mod.cwrap('kel_worker_budget_bytes', 'number', []); + + log('wasm ready, probing device'); + + const usable = init(idInput.value, navigator.userAgent); + const mib = Math.round(budget() / (1024 * 1024)); + + if (usable) { + setState('probed', `${mib} MiB usable`); + log(`probe: usable, ${mib} MiB safe budget`); + } else { + // Still worth connecting. An orchestrator that knows why a device is + // unusable is better off than one that never heard from it. + setState('probed', 'device cannot run a solver'); + log('probe: device unusable (no float render target, or no context)'); + } + + let worker = null; + + connectButton.disabled = false; + connectButton.onclick = () => { + if (worker) worker.disconnect(); + worker = new Worker(mod, urlInput.value, idInput.value); + worker.connect(); + connectButton.disabled = true; + disconnectButton.disabled = false; + }; + + disconnectButton.onclick = () => { + if (worker) worker.disconnect(); + worker = null; + connectButton.disabled = false; + disconnectButton.disabled = true; + }; + }).catch((err) => { + setState('error', 'wasm failed to load'); + log(`wasm load failed: ${err}`); + }); +}); |
