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/web | |
| parent | 0c0087ef9482634273ec1a23d3a915754f94926d (diff) | |
| download | libs-lbm-dev.tar.gz | |
Swarm POCdev
Diffstat (limited to 'poc/web_swarm/worker/web')
| -rw-r--r-- | poc/web_swarm/worker/web/index.html | 87 | ||||
| -rw-r--r-- | poc/web_swarm/worker/web/worker.js | 286 |
2 files changed, 373 insertions, 0 deletions
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}`); + }); +}); |
