SeamRoomFactory

class SeamRoomFactory(loom: Loom, scope: CoroutineScope, clock: () -> Instant, deviceKeyStore: DeviceKeyStore, heartbeatConfig: HeartbeatConfig = HeartbeatConfig(), admitTimeout: Duration = DEFAULT_ADMIT_TIMEOUT, memberInboxCapacity: Int = DEFAULT_MEMBER_INBOX_CAPACITY, reconnectControllerFactory: JoinerReconnectControllerFactory? = null) : RoomFactory

Loom-backed implementation of RoomFactory.

Each call to host or join weaves a Seam via loom (new on most looms; one holding a live session may answer a repeat with it), wraps it in a SeamRoom, and drives the admit/identify handshake:

scope is used to launch the per-Room admit loop coroutines. Callers should use a scope whose lifetime matches the room's intended lifetime (e.g. backgroundScope in tests, a structured session scope in production).

SeamRoom's internal membership state is guarded by a reentrant lock and is safe under any dispatcher, including multi-threaded ones such as Dispatchers.Default. Suspend calls (sends, broadcasts) are always performed outside the lock.

clock is required (not defaulted) so callers must make an explicit choice: use SeamRoomFactory.systemClock for production wall-clock time or supply a virtual clock in tests. An accidental epoch-zero default would ship wrong timestamps silently.

deviceKeyStore holds this device's key, and is required for the same reason: whether a device is remembered across restarts is the caller's choice, not a default. Pass a platform store to be recognised when the app comes back, or InMemoryDeviceKeyStore() to be a new device every run (tests, and apps that choose not to be remembered). Every room this factory makes shares it, since they are all the same device. Its key travels in every Hello and on every Member. A host admits a joiner only once the joiner has signed a proof under that key, so on a host every member's key is verified; on a joiner the host's own entry is verified (the host signed its challenge under that key) and every other member's key is host-asserted (#3211; see Member). host, join and adopt each read the key from it once, before the room exists, and throw to their caller when the store cannot produce one; a room never reads the store for its key (see RoomDevice).

A host signs its room id into every challenge, and a joiner signs its device id into every proof, so host, join and adopt throw IllegalArgumentException for a roomId or deviceId no statement could carry — one holding an unpaired surrogate, or whose UTF-8 is longer than 65 535 bytes — before they touch the loom. Left to the exchange, it would fail every attempt.

heartbeatConfig controls partition-detection timing.

admitTimeout bounds the joiner's admit handshake: if no AdmitMessage.Welcome arrives within this window after the joiner's Hello, the room surfaces a terminal MembershipEvent.AdmissionFailed with AdmissionFailure.TimedOut instead of waiting forever (#1178). A host uses it too, as how long a challenge it sent stays open for the joiner's proof (#3211).

See also

for the production convenience constructor.

Constructors

Link copied to clipboard
constructor(loom: Loom, scope: CoroutineScope, clock: () -> Instant, deviceKeyStore: DeviceKeyStore, heartbeatConfig: HeartbeatConfig = HeartbeatConfig(), admitTimeout: Duration = DEFAULT_ADMIT_TIMEOUT, memberInboxCapacity: Int = DEFAULT_MEMBER_INBOX_CAPACITY, reconnectControllerFactory: JoinerReconnectControllerFactory? = null)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
suspend fun adopt(seam: Seam, role: AdoptRole, memberName: String? = null, roomKey: String? = null, reweave: suspend () -> Seam? = null, deviceId: String? = null): Room

Adopt an already-woven seam into a Room with an explicit role — no re-weave.

Link copied to clipboard
suspend fun electLobby(pattern: Pattern): ElectionLobby

Symmetric lobby entry both peers call identically: weave the mesh via us.tractat.kuilt.core.Rendezvous.New (a constant session name), then return an ElectionLobby over the woven seam. Every peer elects the same host (min(peers)); on Start the elected host runs the freeze round and each peer adopts a Room once (#1439).

Link copied to clipboard
open suspend override fun host(pattern: Pattern, memberName: String? = null, roomId: RoomId? = null, admission: HostAdmission): Room

Host a new room. The caller's peer becomes the SessionRole.Host.

Link copied to clipboard
open suspend override fun join(tag: Tag, memberName: String? = null, deviceId: String? = null): Room

Join an existing room. The caller's peer becomes a SessionRole.Joiner.