feat(server): the relay joins modems of different projects by a code (K3)
A modem that offers a connection to another project is found by six characters derived from its project and itself (modemCode): a copy of the project has another code, so no two modems offer the same one. The room names its modems of other projects to F's relay at a start and none at a stop; the relay pairs each offer with the first modem that joins its code, one to one as Java's server modem takes one client, and passes the frames between the rooms' threads. Every client hears how each modem stands (`modems`: connected, waiting, taken, unpaired, ambiguous, detached, or unavailable without F). The document keeps the connection and the code. Measured on F with two threads: a ping from one project's notebook to the other's goes through; a third project that wants the same code reads taken, and takes it when the second stops; a deleted project's modem leaves with its room. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6,6 +6,7 @@ export * from './model/link.js';
|
||||
export * from './model/node.js';
|
||||
export * from './model/virtualFileSystem.js';
|
||||
export * from './runtime/config.js';
|
||||
export * from './runtime/modemCode.js';
|
||||
export * from './runtime/dns.js';
|
||||
export * from './runtime/engine.js';
|
||||
export * from './runtime/ipv4.js';
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// A modem's code (plan FILIUS_MODEM_PLAN E1, E2): a modem that offers a
|
||||
// connection to another project is found by six characters instead of
|
||||
// Java's IP address and port. The code is derived from the project and the
|
||||
// modem, not stored: a copy of the project (handed out, duplicated) has
|
||||
// another project number and so another code, and no two modems offer the
|
||||
// same one. The editor shows it, server F pairs by it — both derive it here
|
||||
// (RemoteLinkRelay names the role; config.ts).
|
||||
//
|
||||
// The characters leave out what one misreads (0/O, 1/I); typed in, small
|
||||
// letters, spaces and dashes count as nothing.
|
||||
|
||||
export const MODEM_CODE_ALPHABET = 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789';
|
||||
export const MODEM_CODE_LENGTH = 6;
|
||||
|
||||
const FNV_OFFSET = 0xcbf29ce484222325n;
|
||||
const FNV_PRIME = 0x100000001b3n;
|
||||
|
||||
/** The code a modem of `project` offers (FNV-1a over both, its upper 30 bits in five-bit steps). */
|
||||
export function modemCode(project: string, node: string): string {
|
||||
let hash = FNV_OFFSET;
|
||||
const text = `filius-modem\n${project}\n${node}`;
|
||||
for (let index = 0; index < text.length; index += 1) {
|
||||
hash ^= BigInt(text.charCodeAt(index));
|
||||
hash = BigInt.asUintN(64, hash * FNV_PRIME);
|
||||
}
|
||||
let code = '';
|
||||
for (let index = 0; index < MODEM_CODE_LENGTH; index += 1) code += MODEM_CODE_ALPHABET[Number((hash >> BigInt(59 - 5 * index)) & 31n)];
|
||||
return code;
|
||||
}
|
||||
|
||||
const CODE_PATTERN = new RegExp(`^[${MODEM_CODE_ALPHABET}]{${MODEM_CODE_LENGTH}}$`);
|
||||
|
||||
/** A code as typed in, without spaces and dashes, in capitals; undefined when it is none. */
|
||||
export function modemCodeOf(text: string): string | undefined {
|
||||
const code = text.toUpperCase().replace(/[\s-]/g, '');
|
||||
return CODE_PATTERN.test(code) ? code : undefined;
|
||||
}
|
||||
|
||||
/** A code as people read it: two groups of three. */
|
||||
export function modemCodeText(code: string): string {
|
||||
return `${code.slice(0, 3)}-${code.slice(3)}`;
|
||||
}
|
||||
Reference in New Issue
Block a user