Add CultMesh RUDP document publisher
This commit is contained in:
parent
9d5d26d3e8
commit
57c9125e10
2 changed files with 110 additions and 1 deletions
|
|
@ -148,6 +148,15 @@ export interface CultMeshRudpDocumentServer {
|
|||
close(): void;
|
||||
}
|
||||
|
||||
export interface CultMeshRudpDocumentPublishOptions extends CultMeshRudpPeerOptions {
|
||||
messageId?: string;
|
||||
sourceRuntimeId?: string;
|
||||
sourceAgentId?: string;
|
||||
sourceRole?: string;
|
||||
tags?: string[];
|
||||
flushTimeoutMs?: number;
|
||||
}
|
||||
|
||||
export interface CultMeshRudpEndpoint {
|
||||
host: string;
|
||||
port: number;
|
||||
|
|
@ -800,6 +809,38 @@ export class CultMesh {
|
|||
return { host, port, uri: `rudp://${uriHost}:${port}` };
|
||||
}
|
||||
|
||||
public static async publishRudpDocumentOnce<TDefinition extends AnyCultCacheDocumentDefinition>(
|
||||
runtimeId: string,
|
||||
connectionId: number,
|
||||
endpoint: string | CultMeshRudpEndpoint,
|
||||
binding: CultNetDocumentBinding<TDefinition>,
|
||||
recordKey: string,
|
||||
value: CultCacheDocumentValue<TDefinition>,
|
||||
options: CultMeshRudpDocumentPublishOptions = {},
|
||||
): Promise<void> {
|
||||
requireNonEmpty(runtimeId, "runtimeId");
|
||||
requireNonEmpty(recordKey, "recordKey");
|
||||
const registry = new CultNetDocumentRegistry([binding]);
|
||||
const peer = await CultMesh.createRudpPeer(runtimeId, connectionId, endpoint, options);
|
||||
try {
|
||||
peer.send(registry.createRawDocumentPutMessage(
|
||||
binding,
|
||||
options.messageId ?? `${runtimeId}:${binding.definition.type}:${recordKey}`,
|
||||
recordKey,
|
||||
value,
|
||||
{
|
||||
sourceRuntimeId: options.sourceRuntimeId ?? runtimeId,
|
||||
sourceAgentId: options.sourceAgentId,
|
||||
sourceRole: options.sourceRole,
|
||||
tags: options.tags,
|
||||
},
|
||||
));
|
||||
await new Promise((resolve) => setTimeout(resolve, Math.max(0, options.flushTimeoutMs ?? 150)));
|
||||
} finally {
|
||||
peer.close();
|
||||
}
|
||||
}
|
||||
|
||||
public static async createRudpServer(
|
||||
runtimeId: string,
|
||||
connectionId: number,
|
||||
|
|
|
|||
|
|
@ -6,7 +6,12 @@ import assert from "node:assert/strict";
|
|||
import { encode } from "@msgpack/msgpack";
|
||||
import { z } from "zod";
|
||||
import { defineDocumentType } from "cultcache-ts";
|
||||
import { CultNetDocumentRegistry, CultNetPeer, cultNetBuiltinSchemaRegistry } from "cultnet-ts";
|
||||
import {
|
||||
CultNetDocumentRegistry,
|
||||
CultNetPeer,
|
||||
cultNetBuiltinSchemaRegistry,
|
||||
defineCultNetDocumentBinding,
|
||||
} from "cultnet-ts";
|
||||
import { CultMesh } from "../src/index";
|
||||
|
||||
const noteDocument = defineDocumentType({
|
||||
|
|
@ -469,6 +474,69 @@ test("CultMesh TS RUDP document server accepts raw document puts from multiple p
|
|||
}
|
||||
});
|
||||
|
||||
test("CultMesh TS publishes one registered document to a RUDP catalog", async () => {
|
||||
const connectionId = 0x10203047;
|
||||
let received: { schemaId: string; recordKey: string; payload: unknown; sourceRuntimeId: string | null } | undefined;
|
||||
const server = CultMesh.createRudpDocumentServer(
|
||||
"cultmesh-ts-odin-catalog",
|
||||
connectionId,
|
||||
{
|
||||
documents: new CultNetDocumentRegistry(),
|
||||
bindHost: "127.0.0.1",
|
||||
bindPort: 0,
|
||||
resendDelayMs: 25,
|
||||
resendPollMs: 5,
|
||||
maxFragmentBytes: 1024,
|
||||
maxPendingReliablePackets: 16,
|
||||
onDocumentPutRaw: (document) => {
|
||||
received = {
|
||||
schemaId: document.schemaId,
|
||||
recordKey: document.recordKey,
|
||||
payload: document.payload,
|
||||
sourceRuntimeId: document.sourceRuntimeId,
|
||||
};
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
try {
|
||||
await server.start();
|
||||
await CultMesh.publishRudpDocumentOnce(
|
||||
"cultmesh-ts-muninn",
|
||||
connectionId,
|
||||
`rudp://127.0.0.1:${server.bind.port}`,
|
||||
defineCultNetDocumentBinding({ definition: noteDocument }),
|
||||
"note:odin",
|
||||
{
|
||||
noteId: "note:odin",
|
||||
body: "pay respects once",
|
||||
},
|
||||
{
|
||||
resendDelayMs: 25,
|
||||
resendPollMs: 5,
|
||||
maxFragmentBytes: 1024,
|
||||
maxPendingReliablePackets: 16,
|
||||
connectTimeoutMs: 1_000,
|
||||
flushTimeoutMs: 25,
|
||||
sourceRole: "test-provider",
|
||||
},
|
||||
);
|
||||
|
||||
await waitFor(() => received !== undefined, "published RUDP document");
|
||||
assert.deepEqual(received, {
|
||||
schemaId: "cultmesh.note.v0",
|
||||
recordKey: "note:odin",
|
||||
payload: {
|
||||
noteId: "note:odin",
|
||||
body: "pay respects once",
|
||||
},
|
||||
sourceRuntimeId: "cultmesh-ts-muninn",
|
||||
});
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
});
|
||||
|
||||
test("CultMesh TS negotiates streaming frame body transports explicitly", () => {
|
||||
const streams = CultMesh.createStreamCatalog();
|
||||
const streamUpdates: string[] = [];
|
||||
|
|
|
|||
Loading…
Reference in a new issue