feat(editor): F4.5e a router's connections in the device sheet
SheetRun now clicks through Java's JConnectionsDialog with a router, a computer and a switch cabled in the GUIContainer: a click marks a card, the same click unmarks it, another card swaps the cables or moves the one; "+" adds 192.168.0.10/24 up to eight cards, "-" removes the last card with its cable down to none, and a router without a card keeps no gateway. The document adds, removes and swaps a router's cards (the same cables, the same devices), Java's click logic is one function for the test and the page, and the sheet names the address at a cable's far end. The page "Verbindungen verwalten" shows Java's RJ45 buttons in Java's colours with each card's far end below. Not adopted: the last card stays. A card tab or a mark whose card is undone away falls back. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -33,8 +33,12 @@ import { emptyProject, inOrder, nodeEntry, ROOTS } from './projectDoc.js';
|
||||
/** The kinds of the palette, in Java's order (GUIDesignSidebar.addComponentItemsToSidebar; the cable comes first, a tool). */
|
||||
export const PALETTE: readonly Exclude<NodeKind, 'unsupported'>[] = ['pc', 'notebook', 'networkSwitch', 'gateway', 'router', 'remoteLink'];
|
||||
|
||||
/** A router's interfaces, as Java's dialog offers them (2 to 8, 2 when it is closed). */
|
||||
export const ROUTER_INTERFACES = { minimum: 2, maximum: 8, standard: 2 } as const;
|
||||
/**
|
||||
* A router's interfaces, as Java's dialog offers them (2 to 8, 2 when it is
|
||||
* closed); its connections dialog removes them down to `fewest` here (Java:
|
||||
* none — see settings.ts).
|
||||
*/
|
||||
export const ROUTER_INTERFACES = { minimum: 2, maximum: 8, standard: 2, fewest: 1 } as const;
|
||||
|
||||
export interface NewDevice {
|
||||
readonly kind: Exclude<NodeKind, 'unsupported'>;
|
||||
|
||||
@@ -27,7 +27,10 @@
|
||||
// taken, and the sheet says why. Nor that Java's firewall table writes a
|
||||
// cell into the rule before it checks it, so that a rule holds "abc" or a
|
||||
// mask of 255.0.255.0 while the table shows the old value, and reads a port
|
||||
// that is no number as "all ports".
|
||||
// that is no number as "all ports". Nor that Java's connections dialog
|
||||
// removes a router's last card too: a router without one keeps no gateway
|
||||
// (setStandardGateway writes it into the cards; SheetRun's "connections"),
|
||||
// nor could a file hold it; here one card stays.
|
||||
|
||||
import * as Y from 'yjs';
|
||||
import {
|
||||
@@ -47,7 +50,7 @@ import {
|
||||
type NodeKind,
|
||||
} from '@mdg-filius/core';
|
||||
import { modemPair } from '@mdg-filius/fls';
|
||||
import { connect, type ConnectError } from './commands.js';
|
||||
import { connect, ROUTER_INTERFACES, type ConnectError } from './commands.js';
|
||||
import { checkField, type CheckedField, type FieldRule } from './fields.js';
|
||||
import { inOrder, ROOTS } from './projectDoc.js';
|
||||
import { fromY, toY, type PlainValue } from './values.js';
|
||||
@@ -295,6 +298,87 @@ export function setRip(doc: Y.Doc, id: Id, on: boolean, origin: unknown): void {
|
||||
}, origin);
|
||||
}
|
||||
|
||||
/** A router's ports, or none for another device. */
|
||||
function routerPorts(doc: Y.Doc, id: Id): Y.Array<Y.Map<unknown>> | undefined {
|
||||
const node = nodes(doc).get(id);
|
||||
return node === undefined || kindOf(node) !== 'router' ? undefined : portsOf(node);
|
||||
}
|
||||
|
||||
/**
|
||||
* A card added to a router after its last (JConnectionsDialog's "+":
|
||||
* hinzuAnschluss, a new NetzwerkInterface with 192.168.0.10/24 and its own
|
||||
* MAC address, here from the port's id), up to eight; says whether it was.
|
||||
*/
|
||||
export function addRouterInterface(doc: Y.Doc, id: Id, portId: Id, origin: unknown): boolean {
|
||||
const ports = routerPorts(doc, id);
|
||||
if (ports === undefined || ports.length >= ROUTER_INTERFACES.maximum) return false;
|
||||
doc.transact(() => {
|
||||
const port = new Y.Map<unknown>();
|
||||
port.set('id', portId);
|
||||
port.set('label', `rt${ports.length + 1}`);
|
||||
port.set('occupied', false);
|
||||
port.set('config', toY({ ipAddress: '192.168.0.10', subnetMask: '255.255.255.0' }));
|
||||
ports.push([port]);
|
||||
}, origin);
|
||||
return true;
|
||||
}
|
||||
|
||||
type PluggedEnd = { readonly linkId: Id; readonly link: Y.Map<unknown>; readonly end: 'sourcePort' | 'targetPort' };
|
||||
|
||||
/** The cables at a device's port, with the end that plugs into it. */
|
||||
function cablesAt(doc: Y.Doc, id: Id, portId: Id): PluggedEnd[] {
|
||||
const ends: PluggedEnd[] = [];
|
||||
for (const [linkId, link] of links(doc).entries()) {
|
||||
if (link.get('sourceNode') === id && link.get('sourcePort') === portId) ends.push({ linkId, link, end: 'sourcePort' });
|
||||
else if (link.get('targetNode') === id && link.get('targetPort') === portId) ends.push({ linkId, link, end: 'targetPort' });
|
||||
}
|
||||
return ends;
|
||||
}
|
||||
|
||||
/** A router's last card removed with its cable (JConnectionsDialog's "-"), down to one; says whether it was. */
|
||||
export function removeRouterInterface(doc: Y.Doc, id: Id, origin: unknown): boolean {
|
||||
const ports = routerPorts(doc, id);
|
||||
if (ports === undefined || ports.length <= ROUTER_INTERFACES.fewest) return false;
|
||||
const last = ports.get(ports.length - 1).get('id') as Id;
|
||||
doc.transact(() => {
|
||||
for (const { linkId } of cablesAt(doc, id, last)) links(doc).delete(linkId);
|
||||
ports.delete(ports.length - 1);
|
||||
}, origin);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Two of a router's cards trade their cables (JConnectionsDialog.swapConnection):
|
||||
* both plugged, the cables swap; one plugged, its cable moves to the other
|
||||
* card; neither, nothing changes. The cables stay the same cables, joining the
|
||||
* same devices. Says whether a cable moved.
|
||||
*/
|
||||
export function swapRouterCables(doc: Y.Doc, id: Id, first: Id, second: Id, origin: unknown): boolean {
|
||||
const ports = routerPorts(doc, id);
|
||||
const has = (portId: Id) => ports?.toArray().some((port) => port.get('id') === portId) === true;
|
||||
if (first === second || !has(first) || !has(second)) return false;
|
||||
const atFirst = cablesAt(doc, id, first);
|
||||
const atSecond = cablesAt(doc, id, second);
|
||||
if (atFirst.length === 0 && atSecond.length === 0) return false;
|
||||
doc.transact(() => {
|
||||
for (const { link, end } of atFirst) link.set(end, second);
|
||||
for (const { link, end } of atSecond) link.set(end, first);
|
||||
}, origin);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* A click on a card's button in Java's connections dialog, with the card
|
||||
* marked before (null for none): the first click marks a card, a click on it
|
||||
* again unmarks it, a click on another card swaps the two cards' cables
|
||||
* (swapRouterCables) and leaves none marked.
|
||||
*/
|
||||
export function connectionsClick(marked: number | null, clicked: number): { readonly marked: number | null; readonly swap?: readonly [number, number] } {
|
||||
if (marked === null) return { marked: clicked };
|
||||
if (marked === clicked) return { marked: null };
|
||||
return { marked: null, swap: [marked, clicked] };
|
||||
}
|
||||
|
||||
/** A column of the forwarding table's manual rows. */
|
||||
export type RouteColumn = keyof ManualRoute;
|
||||
|
||||
|
||||
@@ -88,8 +88,13 @@ export interface InterfaceSheet {
|
||||
readonly ipAddress: string;
|
||||
readonly subnetMask: string;
|
||||
readonly macAddress: string;
|
||||
/** The device at the other end of its cable (its label, as Java names it) and the cable; null without one. */
|
||||
readonly connected: { readonly device: string; readonly link: string } | null;
|
||||
/**
|
||||
* The device at the other end of its cable (its label, as Java names it),
|
||||
* the address of the card the cable plugs into there (a computer's, a
|
||||
* router's; null for a switch or a modem, which have none) and the cable;
|
||||
* null without one.
|
||||
*/
|
||||
readonly connected: { readonly device: string; readonly address: string | null; readonly link: string } | null;
|
||||
}
|
||||
|
||||
/** A router's or home router's firewall as Java's dialog shows it (JFirewallDialog). */
|
||||
@@ -142,6 +147,22 @@ const setting = (node: Y.Map<unknown>, key: string) => settingsOf(node).get(key)
|
||||
|
||||
const text = (map: Y.Map<unknown> | undefined, key: string) => (map?.get(key) as string | undefined) ?? '';
|
||||
|
||||
/** The address of a device's card at a port, as Java's NetzwerkInterface holds it; null for a device without cards (InternetKnoten). */
|
||||
function cardAddress(node: Y.Map<unknown>, portId: string): string | null {
|
||||
switch (node.get('kind') as NodeKind) {
|
||||
case 'pc':
|
||||
case 'notebook':
|
||||
return text(setting(node, 'device'), 'ipAddress');
|
||||
case 'router':
|
||||
case 'gateway': {
|
||||
const port = (node.get('ports') as Y.Array<Y.Map<unknown>>).toArray().find((each) => each.get('id') === portId);
|
||||
return text(port?.get('config') as Y.Map<unknown> | undefined, 'ipAddress');
|
||||
}
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** A router's or home router's cards, each with what its cable joins. */
|
||||
function interfacesOf(doc: Y.Doc, id: string, node: Y.Map<unknown>): InterfaceSheet[] {
|
||||
const all = doc.getMap<Y.Map<unknown>>(ROOTS.nodes);
|
||||
@@ -150,14 +171,14 @@ function interfacesOf(doc: Y.Doc, id: string, node: Y.Map<unknown>): InterfaceSh
|
||||
const portId = port.get('id') as string;
|
||||
const config = port.get('config') as Y.Map<unknown> | undefined;
|
||||
const cable = links.find(([, link]) => (link.get('sourceNode') === id && link.get('sourcePort') === portId) || (link.get('targetNode') === id && link.get('targetPort') === portId));
|
||||
const other =
|
||||
cable === undefined ? undefined : all.get((cable[1].get('sourceNode') === id && cable[1].get('sourcePort') === portId ? cable[1].get('targetNode') : cable[1].get('sourceNode')) as string);
|
||||
const near = cable !== undefined && cable[1].get('sourceNode') === id && cable[1].get('sourcePort') === portId;
|
||||
const other = cable === undefined ? undefined : all.get(cable[1].get(near ? 'targetNode' : 'sourceNode') as string);
|
||||
return {
|
||||
portId,
|
||||
ipAddress: text(config, 'ipAddress'),
|
||||
subnetMask: text(config, 'subnetMask'),
|
||||
macAddress: (port.get('mac') as string | undefined) ?? stableGeneratedMacAddress(portId),
|
||||
connected: cable === undefined || other === undefined ? null : { device: nodeLabel(other), link: cable[0] },
|
||||
connected: cable === undefined || other === undefined ? null : { device: nodeLabel(other), address: cardAddress(other, cable[1].get(near ? 'targetPort' : 'sourcePort') as string), link: cable[0] },
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user