Use CultMesh RUDP document catalog in Odin
This commit is contained in:
parent
005e83f5d3
commit
65beba9cd9
4 changed files with 46 additions and 208 deletions
|
|
@ -96,12 +96,16 @@ The Eve/CultUI deck endpoint is a compatibility bridge:
|
|||
ws://127.0.0.1:8797/eve/deck
|
||||
```
|
||||
|
||||
The native CultNet/RUDP snapshot endpoint is:
|
||||
The native CultMesh/RUDP document catalog endpoint is:
|
||||
|
||||
```text
|
||||
127.0.0.1:17871
|
||||
```
|
||||
|
||||
That endpoint accepts typed document publication and schema/catalog requests
|
||||
through the shared CultMesh runtime. Consumers that need Odin's accepted surface
|
||||
can still request the current CultNet snapshot over the same transport.
|
||||
|
||||
Legacy browser/deck lowerers can consume the LAN bridge endpoint:
|
||||
|
||||
```text
|
||||
|
|
|
|||
|
|
@ -58,4 +58,4 @@ if ($proc.HasExited) {
|
|||
Write-Host "Odin started as PID $($proc.Id)."
|
||||
Write-Host "Health: http://127.0.0.1:$Port/health"
|
||||
Write-Host "Eve deck: ws://127.0.0.1:$Port/eve/deck"
|
||||
Write-Host "CultNet/RUDP snapshot: $CultNetRudpBind"
|
||||
Write-Host "CultMesh/RUDP document catalog: $CultNetRudpBind"
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ const fs = require("fs");
|
|||
const path = require("path");
|
||||
|
||||
const { buildConfig, loadCultRuntime } = require("./odin/config.cjs");
|
||||
const { createCultNetRudpSurfaceServer } = require("./odin/cultnet-rudp.cjs");
|
||||
const { createCultMeshRudpDocumentServer } = require("./odin/cultnet-rudp.cjs");
|
||||
const { defineOdinDocuments } = require("./odin/documents.cjs");
|
||||
const { createInterfaceDiscovery } = require("./odin/interfaces.cjs");
|
||||
const { createIdunnRudpHealthPublisher, publishIdunnRudpHealth } = require("./odin/idunn-rudp.cjs");
|
||||
|
|
@ -20,7 +20,7 @@ const idunnRudpHealthPublisher = createIdunnRudpHealthPublisher(config.idunnRudp
|
|||
|
||||
const cultRuntime = loadCultRuntime();
|
||||
if (cultRuntime.error) {
|
||||
console.error("CultMesh runtime unavailable; durable mesh snapshot disabled:", cultRuntime.error.message);
|
||||
console.error("CultMesh runtime unavailable; durable mesh catalog disabled:", cultRuntime.error.message);
|
||||
}
|
||||
|
||||
const documents = defineOdinDocuments(cultRuntime.defineDocumentType);
|
||||
|
|
@ -45,7 +45,7 @@ const stateBuilder = createStateBuilder({
|
|||
});
|
||||
|
||||
let meshNodePromise = null;
|
||||
let cultNetRudpSurfaceServer = null;
|
||||
let cultMeshRudpDocumentServer = null;
|
||||
let currentState = stateBuilder.buildPendingState("Coordinator starting");
|
||||
let lastRefresh = {
|
||||
completedAt: null,
|
||||
|
|
@ -65,12 +65,13 @@ async function main() {
|
|||
meshNodePromise = createDurableSurfaceNode();
|
||||
}
|
||||
if (config.cultnetRudpBind) {
|
||||
cultNetRudpSurfaceServer = createCultNetRudpSurfaceServer({
|
||||
cultMeshRudpDocumentServer = createCultMeshRudpDocumentServer({
|
||||
CultMesh: cultRuntime.CultMesh,
|
||||
bind: config.cultnetRudpBind,
|
||||
documents,
|
||||
getCache: async () => {
|
||||
if (!meshNodePromise) {
|
||||
throw new Error("CultMesh runtime is unavailable; Odin cannot serve a CultNet/RUDP snapshot.");
|
||||
throw new Error("CultMesh runtime is unavailable; Odin cannot serve a CultMesh/RUDP document catalog.");
|
||||
}
|
||||
return (await meshNodePromise).cache;
|
||||
},
|
||||
|
|
@ -78,8 +79,8 @@ async function main() {
|
|||
liveProviderRegistry.ingestDocument(document, document.remote);
|
||||
},
|
||||
});
|
||||
await cultNetRudpSurfaceServer.start();
|
||||
console.log(`CultNet/RUDP snapshot: ${config.cultnetRudpBind}`);
|
||||
await cultMeshRudpDocumentServer.start();
|
||||
console.log(`CultMesh/RUDP document catalog: ${config.cultnetRudpBind}`);
|
||||
}
|
||||
|
||||
const dashboardServer = createDashboardServer({
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
"use strict";
|
||||
|
||||
const dgram = require("dgram");
|
||||
const path = require("path");
|
||||
const { createRequire } = require("module");
|
||||
|
||||
|
|
@ -17,208 +16,55 @@ const requireCultNet = createRequire(path.resolve(
|
|||
|
||||
const {
|
||||
CultNetDocumentRegistry,
|
||||
CultNetRudpSession,
|
||||
cultNetBuiltinSchemaRegistry,
|
||||
decodeRudpPacket,
|
||||
defineCultNetDocumentBinding,
|
||||
encodeCultNetMessageForWire,
|
||||
encodeRudpPacket,
|
||||
parseCultNetMessage,
|
||||
} = requireCultNet("./dist/index.js");
|
||||
const { decode, encode } = requireCultNet("@msgpack/msgpack");
|
||||
|
||||
const ODIN_RUDP_CONNECTION_ID = 0x0d1d0002;
|
||||
const DEFAULT_SESSION_TIMEOUT_MS = 30_000;
|
||||
|
||||
function createCultNetRudpSurfaceServer(options) {
|
||||
function createCultMeshRudpDocumentServer(options) {
|
||||
if (!options?.bind) return null;
|
||||
if (!options.CultMesh?.createRudpDocumentServer) {
|
||||
throw new Error("Odin requires a CultMesh runtime with createRudpDocumentServer.");
|
||||
}
|
||||
if (!options.documents?.surfaceDefinition) {
|
||||
throw new Error("Odin CultNet/RUDP surface server requires a surface document definition.");
|
||||
throw new Error("Odin CultMesh/RUDP document server requires a surface document definition.");
|
||||
}
|
||||
if (typeof options.getCache !== "function") {
|
||||
throw new Error("Odin CultNet/RUDP surface server requires an async getCache function.");
|
||||
throw new Error("Odin CultMesh/RUDP document server requires an async getCache function.");
|
||||
}
|
||||
|
||||
const bind = parseEndpoint(options.bind);
|
||||
const documentRegistry = new CultNetDocumentRegistry([
|
||||
const registry = new CultNetDocumentRegistry([
|
||||
defineCultNetDocumentBinding({ definition: options.documents.surfaceDefinition }),
|
||||
]);
|
||||
const sessions = new Map();
|
||||
const socket = dgram.createSocket(bind.host.includes(":") ? "udp6" : "udp4");
|
||||
const resendPollMs = Math.max(10, Number(options.resendPollMs || 25));
|
||||
const sessionTimeoutMs = Math.max(1_000, Number(options.sessionTimeoutMs || DEFAULT_SESSION_TIMEOUT_MS));
|
||||
let resendTimer = null;
|
||||
|
||||
socket.on("message", (wire, remote) => {
|
||||
try {
|
||||
const packet = decodeRudpPacket(wire);
|
||||
handlePacket(packet, remote).catch((error) => {
|
||||
logError(options, `CultNet/RUDP packet handling failed for ${remote.address}:${remote.port}`, error);
|
||||
});
|
||||
} catch (error) {
|
||||
logError(options, "CultNet/RUDP packet decode failed", error);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on("error", (error) => {
|
||||
logError(options, "CultNet/RUDP socket error", error);
|
||||
});
|
||||
|
||||
async function start() {
|
||||
await new Promise((resolve, reject) => {
|
||||
socket.once("error", reject);
|
||||
socket.bind(bind.port, bind.host, () => {
|
||||
socket.off("error", reject);
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
resendTimer = setInterval(() => {
|
||||
try {
|
||||
const nowMs = Date.now();
|
||||
for (const [key, record] of sessions) {
|
||||
if (record.session.checkTimeout(nowMs, sessionTimeoutMs)) {
|
||||
sessions.delete(key);
|
||||
continue;
|
||||
}
|
||||
for (const packet of record.session.dueResends(nowMs)) {
|
||||
sendPacket(record.remote, packet);
|
||||
}
|
||||
const server = options.CultMesh.createRudpDocumentServer(
|
||||
"odin-cultmesh-catalog",
|
||||
ODIN_RUDP_CONNECTION_ID,
|
||||
{
|
||||
bindHost: bind.host,
|
||||
bindPort: bind.port,
|
||||
documents: registry,
|
||||
getCache: options.getCache,
|
||||
onError: (error) => {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
if (typeof options.logError === "function") {
|
||||
options.logError(`CultMesh/RUDP document server error: ${message}`);
|
||||
return;
|
||||
}
|
||||
} catch (error) {
|
||||
logError(options, "CultNet/RUDP resend loop failed", error);
|
||||
}
|
||||
}, resendPollMs);
|
||||
resendTimer.unref?.();
|
||||
}
|
||||
|
||||
function close() {
|
||||
if (resendTimer) {
|
||||
clearInterval(resendTimer);
|
||||
resendTimer = null;
|
||||
}
|
||||
sessions.clear();
|
||||
socket.close();
|
||||
}
|
||||
|
||||
async function handlePacket(packet, remote) {
|
||||
const key = remoteKey(remote);
|
||||
const nowMs = Date.now();
|
||||
let record = sessions.get(key);
|
||||
|
||||
if (packet.packetType === "connect") {
|
||||
record = {
|
||||
remote: { address: remote.address, family: remote.family, port: remote.port },
|
||||
session: new CultNetRudpSession({
|
||||
connectionId: ODIN_RUDP_CONNECTION_ID,
|
||||
initialSequence: 1,
|
||||
resendDelayMs: 100,
|
||||
}),
|
||||
};
|
||||
sessions.set(key, record);
|
||||
sendPacket(record.remote, record.session.acceptConnect(packet, nowMs, new Uint8Array()));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!record) {
|
||||
return;
|
||||
}
|
||||
|
||||
const result = record.session.receive(packet, nowMs);
|
||||
if (result.reply) {
|
||||
sendPacket(record.remote, result.reply);
|
||||
}
|
||||
for (const frame of result.delivered) {
|
||||
if (frame.channelId !== "schema") {
|
||||
continue;
|
||||
}
|
||||
await handleSchemaFrame(record, frame.payload);
|
||||
}
|
||||
if (result.disconnected) {
|
||||
sessions.delete(key);
|
||||
return;
|
||||
}
|
||||
if (packet.packetType === "accept" || result.delivered.length > 0) {
|
||||
sendPacket(record.remote, record.session.createAck());
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSchemaFrame(record, payload) {
|
||||
const message = parseCultNetMessage(decode(payload), "cultnet.schema.v0");
|
||||
switch (message.schemaVersion) {
|
||||
case "cultnet.snapshot_request.v0": {
|
||||
try {
|
||||
const cache = await options.getCache();
|
||||
const response = documentRegistry.createRawSnapshotResponse(cache, message.messageId, message);
|
||||
sendSchemaMessage(record, response);
|
||||
} catch (error) {
|
||||
sendSchemaMessage(record, {
|
||||
schemaVersion: "cultnet.error.v0",
|
||||
error: error.message,
|
||||
});
|
||||
}
|
||||
return;
|
||||
}
|
||||
case "cultnet.schema_catalog_request.v0":
|
||||
sendSchemaMessage(record, cultNetBuiltinSchemaRegistry.createCatalogResponse(message));
|
||||
return;
|
||||
case "cultnet.document_put_raw.v0": {
|
||||
console.error(`CultMesh/RUDP document server error: ${message}`);
|
||||
},
|
||||
onDocumentPutRaw: (document) => {
|
||||
if (typeof options.onDocumentPutRaw === "function") {
|
||||
try {
|
||||
options.onDocumentPutRaw(normalizeRawDocumentPut(message, record.remote));
|
||||
} catch (error) {
|
||||
logError(options, "CultNet/RUDP raw document ingest failed", error);
|
||||
}
|
||||
options.onDocumentPutRaw(document);
|
||||
}
|
||||
return;
|
||||
}
|
||||
default:
|
||||
sendSchemaMessage(record, {
|
||||
schemaVersion: "cultnet.error.v0",
|
||||
error: `Unsupported Odin CultNet/RUDP request ${message.schemaVersion}.`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function sendSchemaMessage(record, message) {
|
||||
const payload = encode(encodeCultNetMessageForWire(message, "cultnet.schema.v0"));
|
||||
for (const packet of record.session.sendMany("schema", payload, {
|
||||
reliable: true,
|
||||
ordered: true,
|
||||
nowMs: Date.now(),
|
||||
})) {
|
||||
sendPacket(record.remote, packet);
|
||||
}
|
||||
}
|
||||
|
||||
function sendPacket(remote, packet) {
|
||||
const wire = Buffer.from(encodeRudpPacket(packet));
|
||||
socket.send(wire, remote.port, remote.address);
|
||||
}
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
return {
|
||||
bind,
|
||||
start,
|
||||
close,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeRawDocumentPut(message, remote) {
|
||||
const document = message?.document;
|
||||
if (!document?.schemaId || !document?.recordKey) {
|
||||
throw new Error("raw document put is missing schemaId or recordKey");
|
||||
}
|
||||
if (document.payloadEncoding !== "messagepack") {
|
||||
throw new Error(`unsupported raw payload encoding ${document.payloadEncoding}`);
|
||||
}
|
||||
return {
|
||||
schemaId: document.schemaId,
|
||||
recordKey: document.recordKey,
|
||||
storedAt: document.storedAt || new Date().toISOString(),
|
||||
payload: decode(document.payload),
|
||||
sourceRuntimeId: document.sourceRuntimeId || null,
|
||||
sourceRole: document.sourceRole || null,
|
||||
tags: Array.isArray(document.tags) ? document.tags : [],
|
||||
remote,
|
||||
start: () => server.start(),
|
||||
close: () => server.close(),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -228,7 +74,7 @@ function parseEndpoint(value) {
|
|||
if (ipv6) return { host: ipv6[1], port: parsePort(ipv6[2]) };
|
||||
const index = text.lastIndexOf(":");
|
||||
if (index <= 0) {
|
||||
throw new Error(`CultNet/RUDP bind must be host:port, got "${value}".`);
|
||||
throw new Error(`CultMesh/RUDP bind must be host:port, got "${value}".`);
|
||||
}
|
||||
return { host: text.slice(0, index), port: parsePort(text.slice(index + 1)) };
|
||||
}
|
||||
|
|
@ -236,24 +82,11 @@ function parseEndpoint(value) {
|
|||
function parsePort(value) {
|
||||
const port = Number(value);
|
||||
if (!Number.isInteger(port) || port <= 0 || port > 65535) {
|
||||
throw new Error(`CultNet/RUDP port is invalid: ${value}`);
|
||||
throw new Error(`CultMesh/RUDP port is invalid: ${value}`);
|
||||
}
|
||||
return port;
|
||||
}
|
||||
|
||||
function remoteKey(remote) {
|
||||
return `${remote.address}:${remote.port}`;
|
||||
}
|
||||
|
||||
function logError(options, label, error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
if (typeof options?.logError === "function") {
|
||||
options.logError(`${label}: ${message}`);
|
||||
return;
|
||||
}
|
||||
console.error(`${label}: ${message}`);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
createCultNetRudpSurfaceServer,
|
||||
createCultMeshRudpDocumentServer,
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue