RoomHubSeam

class RoomHubSeam(channelName: String, val selfId: PeerId, authorizer: RoomAuthorizer) : Seam, PrincipalRoster

Server-side hub Seam for one named room — the structural per-room isolation primitive.

A RoomHubSeam is the server's view of a single room. A frame broadcast here reaches only the connections that have been admitted to this room; a non-member is never in the fanout list, so a cross-room leak is structurally unrepresentable — isolation by construction, not by guard.

Deterministic delivery

Frames are pushed into the room by MuxServerLoom, which performs a single collection of each connection's underlying seam and demultiplexes by channel name inline. A room never collects a per-channel flow itself — so registration and forwarding do not depend on the replay-0 subscription timing of a NamedMux channel view. Inbound frames land in a bounded Spool (a buffered channel), so a frame delivered before the room's consumer subscribes is retained rather than dropped. This is what makes the path deterministic under virtual time.

Membership / registration

A connection joins a room via two gates, both applied by deliver on the first frame:

  1. Authorizationauthorizer is invoked with the peer's id and this room's channelName. A false return structurally excludes the connection: it is never added to peers, the fanout, or the inbound stream.

  2. First-frame admission — only if the authorizer returns true is the connection registered. All subsequent frames from that connection on this channel are then forwarded to incoming and the connection appears in peers.

A connection is deregistered via deregister when its underlying link tears.

Reconnect / resume

Registration is keyed by PeerId. A returning peer (same id, fresh connection) replaces the stale entry; the stale connection's later deregister is a no-op because its OutboundSender is no longer the registered one — the resumed membership survives the old connection's teardown.

The one replacement that is refused is an unattested link claiming an id whose live link the host verified (#2357) — a peer id is self-asserted and public, so without that guard anyone could take both the roster entry and the unicast route of a verified peer, presenting no credential. See principals. Attested → attested replacement is unaffected, and so is a deployment that attests nothing at all.

Thread safety

All mutable state (registered and the attestedPrincipals roster) is guarded by a reentrant lock. Suspend calls (authorizer, sends, spool delivery) are always performed outside the lock. The terminal lifecycle runs through a SeamStateGate: close latches Torn single-shot (no more non-CAS if (_state is Torn) return), and the roster-resurrection hazard — an in-flight deliver re-registering a peer after close() collapsed the roster — is closed by folding the closed check into the same critical section that mutates registered/_peers/principals, so a post-close deliver can never republish membership. The check is that marker and not a read of state because Seam.peers requires the roster collapse to be published before the Torn latch, so mid-close there is an instant at which the roster is collapsed and state is not yet terminal.

Parameters

channelName

the room name, matching the NamedMux channel tag clients use.

selfId

this server peer's own PeerId.

authorizer

required authorization policy — invoked on first frame from each connection. Use RoomAuthorizer.AllowAll for open-access rooms and in tests.

Constructors

Link copied to clipboard
constructor(channelName: String, selfId: PeerId, authorizer: RoomAuthorizer)

Properties

Link copied to clipboard
open override val attestedPrincipals: StateFlow<Map<PeerId, Principal>>

Host-verified principals of currently-linked peers, keyed by the PeerId each was verified against at admission. Peers with no attestation are absent. An entry is removed when its peer's link drops; the map empties when the seam tears down.

Link copied to clipboard
open val capability: StateFlow<TransportCapability>

Live capability of the fabric carrying this session — its role(s) and whether it is usable right now. Updates as radios, permissions, and network paths change.

Link copied to clipboard
open override val incoming: Flow<Swatch>

Frames received from peers, in send order, delivered to a single collector. Cold/single-collection semantics: collect once per Seam; fan-out consumers wrap with shareIn. A second concurrent collector is unsupported and will race.

Link copied to clipboard
open val maxPayloadBytes: Int?

The largest payload a single broadcast or sendTo may carry, or null when this seam cannot tell.

Link copied to clipboard
open override val peers: StateFlow<Set<PeerId>>

Live set of peers currently connected. Includes selfId.

Link copied to clipboard
open val plies: StateFlow<Map<PlyId, SeamState>>

Per-ply lifecycle breakdown. Single-ply fabrics report a one-entry map keyed by PlyId.Sole. Invariant: state.value equals the rollup of plies.value.values under "any ply Woven ⇒ Woven".

Link copied to clipboard
open override val selfId: PeerId
Link copied to clipboard
open override val state: StateFlow<SeamState>

The fabric's lifecycle as observed by this peer.

Functions

Link copied to clipboard
open suspend override fun broadcast(payload: ByteArray)

Send to all other peers. Suspends until accepted by the local transport.

Link copied to clipboard
open suspend override fun close(reason: CloseReason = CloseReason.Normal)

Disconnect from the session. Idempotent.

Link copied to clipboard
suspend fun <T> Seam.raceCollapse(abortWhen: (Set<PeerId>) -> Boolean = { it.size < 2 }, body: suspend () -> T): T

Run body but abort it with a SeamCollapsedException the instant this Seam collapses mid-operation — either the fabric latches SeamState.Torn (transport tear) OR the live peer set satisfies abortWhen (membership drain). Whichever of body, the tear, or the drain resolves first wins; the losers are cancelled.

Link copied to clipboard
open suspend override fun sendTo(peer: PeerId, payload: ByteArray)

Send to one peer. Suspends until accepted by the local transport.