Skip to content

Commit

Permalink
nfdmgmt: use invokeGeneric in NfdPrefixReg
Browse files Browse the repository at this point in the history
Don't depend on ControlParameters, to reduce browser bundle size.
  • Loading branch information
yoursunny committed Dec 30, 2023
1 parent f92bd68 commit 74a79aa
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 143 deletions.
89 changes: 89 additions & 0 deletions packages/nfdmgmt/src/an-nfd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-disable @typescript-eslint/no-duplicate-enum-values --
* TLV-TYPE numbers can have duplicates
**/
export const enum TT {
BaseCongestionMarkingInterval = 0x87,
Capacity = 0x83,
ChannelStatus = 0x82,
ControlParameters = 0x68,
ControlResponse = 0x65,
Cost = 0x6A,
Count = 0x84,
CsInfo = 0x80,
CurrentTimestamp = 0x82,
DefaultCongestionThreshold = 0x88,
ExpirationPeriod = 0x6D,
FaceEventKind = 0xC1,
FaceEventNotification = 0xC0,
FaceId = 0x69,
FacePersistency = 0x85,
FaceQueryFilter = 0x96,
FaceScope = 0x84,
FaceStatus = 0x80,
Flags = 0x6C,
LinkType = 0x86,
LocalUri = 0x81,
Mask = 0x70,
Mtu = 0x89,
NCsEntries = 0x87,
NfdVersion = 0x80,
NFibEntries = 0x84,
NHits = 0x81,
NInBytes = 0x94,
NInData = 0x91,
NInInterests = 0x90,
NInNacks = 0x97,
NMeasurementsEntries = 0x86,
NMisses = 0x82,
NNameTreeEntries = 0x83,
NOutBytes = 0x95,
NOutData = 0x93,
NOutInterests = 0x92,
NOutNacks = 0x98,
NPitEntries = 0x85,
NSatisfiedInterests = 0x99,
NUnsatisfiedInterests = 0x9A,
Origin = 0x6F,
RibEntry = 0x80,
Route = 0x81,
StartTimestamp = 0x81,
StatusCode = 0x66,
StatusText = 0x67,
Strategy = 0x6B,
StrategyChoice = 0x80,
Uri = 0x72,
UriScheme = 0x83,
}

export enum FaceScope {
NonLocal = 0,
Local = 1,
}

export enum LinkType {
PointToPoint = 0,
MultiAccess = 1,
AdHocWireless = 2,
}

export enum FacePersistency {
OnDemand = 0,
Persistent = 1,
Permanent = 2,
}

export const FaceFlags = {
LocalFields: 1 << 0,
LpReliability: 1 << 1,
CongestionMarking: 1 << 2,
} as const;

export const CsFlags = {
EnableAdmit: 1 << 0,
EnableServe: 1 << 1,
} as const;

export const RouteFlags = {
ChildInherit: 1 << 0,
Capture: 1 << 1,
} as const;
36 changes: 18 additions & 18 deletions packages/nfdmgmt/src/control-command-nfd.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
import { StructFieldName, StructFieldNameNested, TT } from "@ndn/packet";
import { StructFieldName, StructFieldNameNested, TT as l3TT } from "@ndn/packet";
import { Decoder, EvDecoder, StructBuilder, StructFieldEnum, StructFieldNNI, type StructFields, StructFieldText } from "@ndn/tlv";

import { CsFlags, FaceFlags, FacePersistency, RouteFlags, TT } from "./an-nfd";
import { type ControlCommandOptions, invokeGeneric } from "./control-command-generic";
import { type ControlResponse } from "./control-response";
import { CsFlags, FaceFlags, FacePersistency, RouteFlags } from "./enum-nfd";

const flagBits = { ...FaceFlags, ...CsFlags, ...RouteFlags };

const buildControlParameters = new StructBuilder("ControlParameters", 0x68)
.add(TT.Name, "name", StructFieldName)
.add(0x69, "faceId", StructFieldNNI)
.add(0x72, "uri", StructFieldText)
.add(0x81, "localUri", StructFieldText)
.add(0x6F, "origin", StructFieldNNI)
.add(0x6A, "cost", StructFieldNNI)
.add(0x83, "capacity", StructFieldNNI)
.add(0x84, "count", StructFieldNNI)
.add(0x87, "baseCongestionMarkingInterval", StructFieldNNI)
.add(0x88, "defaultCongestionThreshold", StructFieldNNI)
.add(0x89, "mtu", StructFieldNNI)
.add(0x6C, "flags", StructFieldNNI, { flagPrefix: "flag", flagBits: flagBits })
.add(0x70, "mask", StructFieldNNI, { flagBits: flagBits })
.add(0x6B, "strategy", StructFieldNameNested)
.add(0x6D, "expirationPeriod", StructFieldNNI)
.add(0x85, "facePersistency", StructFieldEnum<FacePersistency>(FacePersistency))
.add(l3TT.Name, "name", StructFieldName)
.add(TT.FaceId, "faceId", StructFieldNNI)
.add(TT.Uri, "uri", StructFieldText)
.add(TT.LocalUri, "localUri", StructFieldText)
.add(TT.Origin, "origin", StructFieldNNI)
.add(TT.Cost, "cost", StructFieldNNI)
.add(TT.Capacity, "capacity", StructFieldNNI)
.add(TT.Count, "count", StructFieldNNI)
.add(TT.BaseCongestionMarkingInterval, "baseCongestionMarkingInterval", StructFieldNNI)
.add(TT.DefaultCongestionThreshold, "defaultCongestionThreshold", StructFieldNNI)
.add(TT.Mtu, "mtu", StructFieldNNI)
.add(TT.Flags, "flags", StructFieldNNI, { flagPrefix: "flag", flagBits: flagBits })
.add(TT.Mask, "mask", StructFieldNNI, { flagBits: flagBits })
.add(TT.Strategy, "strategy", StructFieldNameNested)
.add(TT.ExpirationPeriod, "expirationPeriod", StructFieldNNI)
.add(TT.FacePersistency, "facePersistency", StructFieldEnum<FacePersistency>(FacePersistency))
.setIsCritical(EvDecoder.neverCritical);
/** NFD Management ControlParameters struct. */
export class ControlParameters extends buildControlParameters.baseClass<ControlParameters>() {
Expand Down
32 changes: 0 additions & 32 deletions packages/nfdmgmt/src/enum-nfd.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/nfdmgmt/src/mod.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
export * from "./an-nfd";
export * from "./control-command-generic";
export * from "./control-command-nfd";
export * from "./control-response";
export { localhopPrefix, localhostPrefix, getPrefix } from "./common";
export * from "./enum-nfd";
export * from "./prefix-reg";
export * from "./sign-interest-02";
export * from "./status-dataset-generic";
Expand Down
82 changes: 40 additions & 42 deletions packages/nfdmgmt/src/prefix-reg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@ import { Endpoint, type Producer } from "@ndn/endpoint";
import { type FwFace, ReadvertiseDestination, TapFace } from "@ndn/fw";
import { Certificate, type KeyChain } from "@ndn/keychain";
import { Interest, type Name, NameMap } from "@ndn/packet";
import { type Encodable, NNI } from "@ndn/tlv";
import { Closers } from "@ndn/util";
import map from "obliterator/map.js";
import type { Except } from "type-fest";
import type { Except, Promisable } from "type-fest";

import { RouteFlags, TT } from "./an-nfd";
import { getPrefix } from "./common";
import type { ControlCommandOptions } from "./control-command-generic";
import { ControlParameters, invoke } from "./control-command-nfd";
import type { RouteFlags } from "./enum-nfd";
import { type ControlCommandOptions, invokeGeneric } from "./control-command-generic";
import type { ControlParameters } from "./control-command-nfd";
import type { ControlResponse } from "./control-response";

type CommandOptions = Except<ControlCommandOptions, "endpoint" | "prefix">;
type RouteOptions = Pick<ControlParameters.Fields, "origin" | "cost" | "flags" | `flag${keyof typeof RouteFlags}`>;
type RouteOptions = Pick<ControlParameters.Fields, "origin" | "cost" | `flag${keyof typeof RouteFlags}`>;
type Options = CommandOptions & RouteOptions & {
retry?: ReadvertiseDestination.RetryOptions;

Expand All @@ -35,7 +37,7 @@ interface State {

class NfdPrefixReg extends ReadvertiseDestination<State> {
private readonly commandOptions: Except<ControlCommandOptions, "endpoint">;
private readonly routeOptions: RouteOptions;
private readonly routeOptions: [origin: Encodable, cost: Encodable, flags: Encodable];
private readonly refreshInterval: number | false;
private readonly preloadCertName?: Name;
private readonly preloadFromKeyChain?: KeyChain;
Expand All @@ -50,12 +52,20 @@ class NfdPrefixReg extends ReadvertiseDestination<State> {
...opts,
};

this.routeOptions = new ControlParameters({
origin: 65,
cost: 0,
flagCapture: true,
...opts,
});
const {
origin = 65,
cost = 0,
flagChildInherit = false,
flagCapture = true,
} = opts;
this.routeOptions = [
[TT.Origin, NNI(origin)],
[TT.Cost, NNI(cost)],
[TT.Flags, NNI(
(Number(flagChildInherit) * RouteFlags.ChildInherit) |
(Number(flagCapture) * RouteFlags.Capture),
)],
];

this.refreshInterval = opts.refreshInterval ?? 300000;
this.preloadCertName = opts.preloadCertName;
Expand All @@ -71,7 +81,7 @@ class NfdPrefixReg extends ReadvertiseDestination<State> {
super.disable();
}

private async tap(): Promise<[opts: ControlCommandOptions, untap: () => void]> {
private async tap<R>(f: (opts: ControlCommandOptions) => Promisable<R>): Promise<R> {
const tapFace = TapFace.create(this.face);
tapFace.addRoute("/");
const endpoint = new Endpoint({
Expand All @@ -83,10 +93,11 @@ class NfdPrefixReg extends ReadvertiseDestination<State> {

const closers = new Closers();
closers.push(...map(preloadProducers, ([, p]) => p), tapFace);
return [
{ ...this.commandOptions, endpoint },
closers.close,
];
try {
return await f({ ...this.commandOptions, endpoint });
} finally {
closers.close();
}
}

private async preload(endpoint: Endpoint) {
Expand Down Expand Up @@ -131,23 +142,13 @@ class NfdPrefixReg extends ReadvertiseDestination<State> {
};

protected override async doAdvertise(name: Name, state: State) {
const [opts, untap] = await this.tap();
try {
const cr = await invoke("rib/register", {
name,
origin: this.routeOptions.origin,
cost: this.routeOptions.cost,
flags: this.routeOptions.flags,
}, opts);
if (cr.statusCode !== 200) {
throw new Error(`${cr.statusCode} ${cr.statusText}`);
}
} finally {
untap();
}
if (this.refreshInterval !== false) {
this.scheduleRefresh(name, state, this.refreshInterval);
}

const cr = await this.tap((opts) => invokeGeneric(
"rib/register", [TT.ControlParameters, name, ...this.routeOptions], opts));
this.checkSuccess(cr);
}

private scheduleRefresh(name: Name, state: State, after: number): void {
Expand All @@ -168,17 +169,14 @@ class NfdPrefixReg extends ReadvertiseDestination<State> {
if (this.closed) {
return;
}
const [opts, untap] = await this.tap();
try {
const cr = await invoke("rib/unregister", {
name,
origin: this.routeOptions.origin,
}, opts);
if (cr.statusCode !== 200) {
throw new Error(`${cr.statusCode} ${cr.statusText}`);
}
} finally {
untap();
const cr = await this.tap((opts) => invokeGeneric(
"rib/unregister", [TT.ControlParameters, name, this.routeOptions[0]], opts));
this.checkSuccess(cr);
}

private checkSuccess(cr: ControlResponse): void {
if (cr.statusCode !== 200) {
throw new Error(`${cr.statusCode} ${cr.statusText}`);
}
}
}
Expand Down
Loading

0 comments on commit 74a79aa

Please sign in to comment.