NearbyLoom

class NearbyLoom(api: NearbyApi, serviceId: String = DEFAULT_SERVICE_ID, policy: DeliveryPolicy = DeliveryPolicy.Reliable, handshakeTimeout: Duration = DEFAULT_HANDSHAKE_TIMEOUT, maxChunkPayload: Int = ChunkCodec.MAX_CHUNK_PAYLOAD) : Loom

Loom implementation backed by Google Nearby Connections.

All logic is GMS-free and lives in commonMain. The real binding that imports play-services-nearby is supplied by androidMain at construction time.

Concurrency and virtual-time correctness

Background coroutines are launched into a scope derived from the caller's coroutine context at open()/join() time: CoroutineScope(currentCoroutineContext() + SupervisorJob()). This means background work inherits the test dispatcher (and therefore runTest's virtual clock) when tests call these methods. The scope is stored and cancelled in NearbySeam.close so no coroutines leak between tests. There is no long-lived process-global scope.

Single-loom symmetric topology

One NearbyLoom handles both the advertiser role (open) and the discoverer role (join) through the same NearbyApi. This matches the us.tractat.kuilt.core.InMemoryLoom pattern and lets the conformance suite run a "one loom, one host, one joiner" scenario.

The roster belongs to the WEAVE, not to the loom (#1878)

Each weave mints its own peer-set MutableStateFlow, seeded with that weave's us.tractat.kuilt.core.freshPeerId and thereafter written only by that weave's own handshake and departures. A single loom-wide flow — what this loom used to keep — is never pruned of a finished weave's ids: NearbySeam.close must not write it (that write was the #1850 cross-peer edit) and disconnectLoop, the only eviction, is cancelled by the very tear that would need it to run. So a closed weave's id lingered forever and seeded the next weave, whose fresh seam reported a roster containing a peer it had never met and latched us.tractat.kuilt.core.SeamState.Woven with zero connections.

Both ends of one session still converge, because each learns the other from the handshake it already completed (ConnectedLink.remotePeerId) rather than by reading a flow the counterparty wrote. That was always the honest channel — the loom-wide flow was a single-process convenience of the fake harness, and on real hardware the two peers are on different devices with different looms and have never shared one.

No loom-level selfId — identity is minted per weave

Because one loom weaves both ends of a session (above), a loom-level identity would hand the host seam and the joiner seam the same PeerId — collapsing the roster to one entry, so Weaving would never reach Woven, and making every sendTo between them fail its own peer != selfId precondition. Each weave therefore mints its own us.tractat.kuilt.core.freshPeerId, exactly as us.tractat.kuilt.core.InMemoryLoom does. That is why the uniform-Loom-construction convention's selfId knob is deliberately absent here rather than defaulted (#1430); NearbyLoomKnobsTest pins the distinctness the omission rests on.

A host accepts for exactly as long as it advertises (#2811)

One coroutine owns both: it takes the first joiner whose handshake succeeds, and advertising stops when it ends, by a link, by NearbySeam.close or by the radio's event stream failing. Nothing else ends it. In particular time does not: handshakeTimeout starts when a joiner initiates, so a host nobody has come to is Weaving and listening indefinitely, and a failed attempt costs that attempt alone. The failed endpoint is disconnected, advertising carries on untouched, and the next joiner is taken as if the first had never come.

It admits one joiner, and advertising stops once it has one, because NearbySeam relays nothing between two joiners and the conformance harness claims mesh delivery only vacuously, over one link. Accepting a second is a different feature with a different capability answer. A joiner arriving mid-handshake is not queued: it is ignored, and fails on its own clock.

A weave stops only what it started

NearbyApi.stopAdvertising and NearbyApi.stopDiscovery take no argument because the radio has one of each, and it turns down a second start (STATUS_ALREADY_ADVERTISING, STATUS_ALREADY_DISCOVERING). So a weave whose start was refused stops nothing: there is nothing of its own to stop, and a stop would land on the weave that holds the radio. A start that was cancelled rather than refused gets a stop, by radioCallMayStillBeRunning, and that is the one case this cannot tell apart: cancelling a start that the radio would have refused still stops the weave holding it. A cancelled start also keeps running, so a stop can land before it does; only a binding that cancelled its radio call could close that.

A join waits for this loom's host only while that host is mid-handshake

One loom can play both ends of a session, and then join returning means the host's roster is populated as well. So join waits for any handshake this loom's host has actually started (LocalHandshakes), which its own bound limits. It does not wait for a host that has no handshake in flight: such a host owes this join nothing, and since acceptance is long-lived, a wait for "the host's first link" would last until somebody happened to join it. That was the deferred this replaced, which also never completed if the host's wait ended any other way, so hosting a room, leaving it and joining somebody else's left the join waiting forever.

Parameters

api

NearbyApi to use (real GMS or fake for tests).

serviceId

Nearby Connections service ID. Must match on both devices.

policy

Delivery policy for every woven seam's inbound buffer.

handshakeTimeout

Ceiling on ONE connection's handshake, forwarded to ConnectStateMachine. Not a weave timeout — endpoint discovery sits outside it (see DEFAULT_HANDSHAKE_TIMEOUT).

maxChunkPayload

Per-chunk payload cap forwarded to ChunkCodec.

Constructors

Link copied to clipboard
constructor(api: NearbyApi, serviceId: String = DEFAULT_SERVICE_ID, policy: DeliveryPolicy = DeliveryPolicy.Reliable, handshakeTimeout: Duration = DEFAULT_HANDSHAKE_TIMEOUT, maxChunkPayload: Int = ChunkCodec.MAX_CHUNK_PAYLOAD)

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
Link copied to clipboard
open override fun capability(): TransportCapability
Link copied to clipboard
open suspend fun host(pattern: Pattern): Seam
Link copied to clipboard
open suspend fun join(tag: Tag): Seam
Link copied to clipboard
open suspend override fun weave(rendezvous: Rendezvous): Seam

A cancellation leaves here only if it is the caller's own, as Loom.weave requires. NearbyApi is consumer-authored and the GMS binding awaits Play services tasks, either of which can throw one without this job being cancelled, and currentCoroutineContext's ensureActive is what tells them apart: type cannot.