Skip to content

Commit

Permalink
svs: decouple Component from StateVector codec
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Mar 10, 2024
1 parent cb323fa commit dd79029
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 28 deletions.
28 changes: 23 additions & 5 deletions pkg/svs/src/state-vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export class StateVector {
return o;
}

/** Encode TLV-VALUE of name component. */
/** Encode TLV-VALUE only. */
public encodeTo(encoder: Encoder): void {
const list = Array.from(this);
list.sort(([a], [b]) => -a.compare(b));
Expand All @@ -95,12 +95,15 @@ export class StateVector {
}
}

/** Encode to name component. */
/**
* Encode to name component.
* @deprecated No longer supported.
*/
public toComponent(): Component {
return new Component(TT.StateVector, Encoder.encode(this));
}

/** Decode TLV-VALUE of name component. */
/** Decode TLV-VALUE only. */
public static decodeFrom(decoder: Decoder): StateVector {
const vv = new StateVector();
while (!decoder.eof) {
Expand All @@ -115,7 +118,10 @@ export class StateVector {
return vv;
}

/** Decode from name component. */
/**
* Decode from name component.
* @deprecated No longer supported.
*/
public static fromComponent(comp: Component): StateVector {
if (comp.type !== TT.StateVector) {
throw new Error("unexpected NameComponent TLV-TYPE");
Expand All @@ -125,7 +131,19 @@ export class StateVector {
}

export namespace StateVector {
/** TLV-TYPE of name component. */
/**
* StateVector TLV-TYPE.
*
* @remarks
* SVS v1 encodes StateVector as a name component of this type.
* SVS v2 encodes StateVector as a sub-element of this type within AppParameters.
*/
export const Type = TT.StateVector;

/**
* TLV-TYPE of name component.
* @deprecated Use {@link Type}.
*/
export const NameComponentType = TT.StateVector;

export interface DiffEntry {
Expand Down
46 changes: 23 additions & 23 deletions pkg/svs/src/sync.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { consume, type ConsumerOptions, type Endpoint, produce, type Producer, type ProducerHandler } from "@ndn/endpoint";
import { Forwarder } from "@ndn/fw";
import { Interest, Name, type NameLike, nullSigner, type Signer, type Verifier } from "@ndn/packet";
import { Component, Interest, Name, type NameLike, nullSigner, type Signer, type Verifier } from "@ndn/packet";
import { type SyncNode, type SyncProtocol, SyncUpdate } from "@ndn/sync-api";
import { CustomEvent, randomJitter, trackEventListener } from "@ndn/util";
import { Decoder, Encoder } from "@ndn/tlv";
import { assert, CustomEvent, randomJitter, trackEventListener } from "@ndn/util";
import type { Promisable } from "type-fest";
import { TypedEventTarget } from "typescript-event-target";

Expand All @@ -25,25 +26,22 @@ type EventMap = SyncProtocol.EventMap<Name> & {
/** StateVectorSync participant. */
export class SvSync extends TypedEventTarget<EventMap> implements SyncProtocol<Name> {
public static async create({
fw,
describe,
syncPrefix,
endpoint, // eslint-disable-line etc/no-deprecated
fw = endpoint?.fw ?? Forwarder.getDefault(),
describe = `SvSync(${syncPrefix})`,
initialStateVector = new StateVector(),
initialize,
syncPrefix,
syncInterestLifetime = 1000,
steadyTimer = [30000, 0.1],
suppressionTimer = [200, 0.5],
signer = nullSigner,
verifier,
}: SvSync.Options): Promise<SvSync> {
fw ??= endpoint?.fw ?? Forwarder.getDefault();
describe ??= `SvSync(${syncPrefix})`;

const sync = new SvSync(
syncPrefix,
describe,
initialStateVector,
syncPrefix,
Interest.makeModifyFunc({
canBePrefix: true,
mustBeFresh: true,
Expand All @@ -57,18 +55,18 @@ export class SvSync extends TypedEventTarget<EventMap> implements SyncProtocol<N
);

await initialize?.(sync);
sync.producer = produce(sync.syncPrefix, sync.handleSyncInterest, {
sync.producer = produce(syncPrefix, sync.handleSyncInterest, {
fw,
describe: `${sync.describe}[p]`,
describe: `${describe}[p]`,
routeCapture: false,
});
return sync;
}

private constructor(
public readonly syncPrefix: Name,
public readonly describe: string,
private readonly own: StateVector,
public readonly syncPrefix: Name,
private readonly modifyInterest: Interest.ModifyFunc,
private readonly cOpts: ConsumerOptions,
private readonly steadyTimer: () => number,
Expand Down Expand Up @@ -153,7 +151,9 @@ export class SvSync extends TypedEventTarget<EventMap> implements SyncProtocol<N

private readonly handleSyncInterest: ProducerHandler = async (interest) => {
await this.verifier?.verify(interest);
const recv = StateVector.fromComponent(interest.name.at(this.syncPrefix.length));
const vComp = interest.name.at(this.syncPrefix.length);
assert(vComp.type === StateVector.Type, "name component is not a StateVector");
const recv = new Decoder(vComp.value).decode(StateVector);

const ourOlder = this.own.listOlderThan(recv);
const ourNewer = recv.listOlderThan(this.own);
Expand Down Expand Up @@ -208,7 +208,7 @@ export class SvSync extends TypedEventTarget<EventMap> implements SyncProtocol<N
this.debug("send");

const interest = new Interest();
interest.name = this.syncPrefix.append(this.own.toComponent());
interest.name = this.syncPrefix.append(new Component(StateVector.Type, Encoder.encode(this.own)));
this.modifyInterest(interest);
await this.signer.sign(interest);

Expand All @@ -232,6 +232,15 @@ export namespace SvSync {

/** {@link SvSync.create} options. */
export interface Options {
/** Sync group prefix. */
syncPrefix: Name;

/**
* Endpoint for communication.
* @deprecated Specify `.fw`.
*/
endpoint?: Endpoint;

/**
* Use the specified logical forwarder.
* @defaultValue `Forwarder.getDefault()`
Expand All @@ -241,12 +250,6 @@ export namespace SvSync {
/** Description for debugging purpose. */
describe?: string;

/**
* Endpoint for communication.
* @deprecated Specify `.fw`.
*/
endpoint?: Endpoint;

/**
* Initial state vector.
* @defaultValue empty state vector
Expand All @@ -264,9 +267,6 @@ export namespace SvSync {
*/
initialize?: (sync: SvSync) => Promisable<void>;

/** Sync group prefix. */
syncPrefix: Name;

/**
* Sync Interest lifetime in milliseconds.
* @defaultValue 1000
Expand Down

0 comments on commit dd79029

Please sign in to comment.