Skip to content

Commit

Permalink
Cleanup of peer components (#1253)
Browse files Browse the repository at this point in the history
Removes several bits of cruft left over from refactor.  Modifies session resumption record cleanup slightly after Discord conversation.
  • Loading branch information
lauckhart authored Oct 1, 2024
1 parent 2d9d1cb commit e7b377c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/protocol/src/common/FailsafeContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export abstract class FailsafeContext {

async updateFabric(fabric: Fabric) {
await this.#fabrics.updateFabric(fabric);
await this.#sessions.updateFabricForResumptionRecords(fabric);
await this.#sessions.deleteResumptionRecordsForFabric(fabric);
}

/**
Expand Down
6 changes: 0 additions & 6 deletions packages/protocol/src/peer/ControllerCommissioningFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,6 @@ class RecoverableCommissioningError extends CommissioningError {}

const DEFAULT_FAILSAFE_TIME_MS = 60_000; // 60 seconds

/**
* The operative connection callback may return this value to skip PASE commissioning.
*/
export const SKIP_CASE_COMMISSIONING = Symbol("skip-pase-commissioning");
export type SKIP_PASE_COMMISSIONING = typeof SKIP_CASE_COMMISSIONING;

/**
* Class to abstract the Device commission flow in a step wise way as defined in Specs. The specs are not 100%
*/
Expand Down
11 changes: 2 additions & 9 deletions packages/protocol/src/peer/PeerSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ interface RunningDiscovery {
timer?: Timer;
}

/**
* API for establishing a connection to a peer.
*/
export interface PeerConnector {
connect(address: PeerAddress, discoveryOptions: DiscoveryOptions): Promise<InteractionClient>;
}

/**
* Interfaces {@link PeerSet} with other components.
*/
Expand All @@ -97,7 +90,7 @@ export interface PeerSetContext {
/**
* Manages operational connections to peers on shared fabric.
*/
export class PeerSet implements ImmutableSet<OperationalPeer>, ObservableSet<OperationalPeer>, PeerConnector {
export class PeerSet implements ImmutableSet<OperationalPeer>, ObservableSet<OperationalPeer> {
readonly #sessions: SessionManager;
readonly #channels: ChannelManager;
readonly #exchanges: ExchangeManager;
Expand Down Expand Up @@ -280,7 +273,7 @@ export class PeerSet implements ImmutableSet<OperationalPeer>, ObservableSet<Ope
this.#peers.delete(actual);
await this.#store.deletePeer(actual.address);
await this.disconnect(actual);
await this.#sessions.removeResumptionRecord(actual.address);
await this.#sessions.deleteResumptionRecord(actual.address);
}

async close() {
Expand Down
4 changes: 2 additions & 2 deletions packages/protocol/src/protocol/DeviceCommissioner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
import { SecureChannelProtocol } from "#securechannel/SecureChannelProtocol.js";
import { PaseServer, SessionManager } from "#session/index.js";
import { CommissioningOptions, StatusCode, StatusResponseError } from "#types";
import type { ControllerCommissioningFlow } from "../peer/ControllerCommissioningFlow.js";
import type { ControllerCommissioner } from "../peer/ControllerCommissioner.js";
import { DeviceAdvertiser } from "./DeviceAdvertiser.js";

const logger = Logger.get("DeviceCommissioner");
Expand All @@ -48,7 +48,7 @@ export interface DeviceCommissionerContext {
/**
* Implements commissioning for devices.
*
* Note this implements commissioning for a *local* device; use {@link ControllerCommissioningFlow} to commission a *remote*
* Note this implements commissioning for a *local* device; use {@link ControllerCommissioner} to commission a *remote*
* device.
*/
export class DeviceCommissioner {
Expand Down
34 changes: 18 additions & 16 deletions packages/protocol/src/session/SessionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class SessionManager {

// When fabric is removed, also remove the resumption record
this.#observers.on(context.fabrics.events.deleted, async fabric =>
this.removeResumptionRecord(fabric.addressOf(fabric.rootNodeId)),
this.deleteResumptionRecordsForFabric(fabric),
);

this.#construction = Construction(this, () => this.#initialize());
Expand Down Expand Up @@ -285,11 +285,23 @@ export class SessionManager {
return session;
}

async removeResumptionRecord(address: PeerAddress) {
async deleteResumptionRecord(address: PeerAddress) {
await this.#construction;

this.#resumptionRecords.delete(address);
await this.storeResumptionRecords();
await this.#storeResumptionRecords();
}

async deleteResumptionRecordsForFabric(fabric: Fabric) {
await this.#construction;

for (const address of this.#resumptionRecords.keys()) {
if (address.fabricIndex === fabric.fabricIndex) {
this.#resumptionRecords.delete(address);
}
}

await this.#storeResumptionRecords();
}

findOldestInactiveSession() {
Expand Down Expand Up @@ -397,20 +409,10 @@ export class SessionManager {
async saveResumptionRecord(resumptionRecord: ResumptionRecord) {
await this.#construction;
this.#resumptionRecords.set(resumptionRecord.fabric.addressOf(resumptionRecord.peerNodeId), resumptionRecord);
await this.storeResumptionRecords();
}

async updateFabricForResumptionRecords(fabric: Fabric) {
await this.#construction;
const record = this.#resumptionRecords.get(fabric.addressOf(fabric.rootNodeId));
if (record === undefined) {
throw new MatterFlowError("Resumption record not found. Should never happen.");
}
this.#resumptionRecords.set(fabric.addressOf(fabric.rootNodeId), { ...record, fabric });
await this.storeResumptionRecords();
await this.#storeResumptionRecords();
}

async storeResumptionRecords() {
async #storeResumptionRecords() {
await this.#construction;
await this.#context.storage.set(
"resumptionRecords",
Expand Down Expand Up @@ -517,7 +519,7 @@ export class SessionManager {
await this.#subscriptionUpdateMutex;

this.#observers.close();
await this.storeResumptionRecords();
await this.#storeResumptionRecords();
for (const session of this.#sessions) {
await session?.end(false);
this.#sessions.delete(session);
Expand Down

0 comments on commit e7b377c

Please sign in to comment.