Skip to content

Commit

Permalink
nfdmgmt: ControlParameters.toString()
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Dec 24, 2023
1 parent 96ef55c commit 2300576
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 78 deletions.
38 changes: 7 additions & 31 deletions integ/nfdmgmt-interop/facemgmt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,35 @@ import { ControlCommand, ControlParameters } from "@ndn/nfdmgmt";

await openUplinks();

function facePropertiesToString(p: ControlParameters): string {
return ([
["faceId", undefined],
["uri", undefined],
["localUri", undefined],
["facePersistency", ControlParameters.FacePersistency],
["baseCongestionMarkingInterval", undefined],
["defaultCongestionThreshold", undefined],
["mtu", undefined],
["flags", undefined],
] satisfies ReadonlyArray<[keyof ControlParameters, Record<number, string> | undefined]>).map(([key, Enum]) => {
const value = p[key];
let s = `${key}=${value}`;
if (Enum && typeof value === "number") {
if (Enum[value]) {
s += `(${Enum[value]})`;
} else {
s += "(invalid)";
}
}
return s;
}).join(" ");
}

const res0 = await ControlCommand.call("faces/create", {
uri: "udp4://127.0.0.1:7001",
facePersistency: 0,
facePersistency: ControlParameters.FacePersistency.OnDemand,
});
assert.equal(res0.statusCode, 200);
assert(res0.body !== undefined);
const body0 = ControlParameters.decodeFromResponseBody(res0);
console.log(facePropertiesToString(body0));
assert.equal(body0.facePersistency, 0);
console.log(body0.toString());
assert.equal(body0.facePersistency, ControlParameters.FacePersistency.OnDemand);
const faceId = body0.faceId!;

const res1 = await ControlCommand.call("faces/update", {
faceId,
facePersistency: 2,
facePersistency: ControlParameters.FacePersistency.Permanent,
});
assert.equal(res1.statusCode, 200);
assert(res1.body !== undefined);
const body1 = ControlParameters.decodeFromResponseBody(res1);
console.log(facePropertiesToString(body1));
console.log(body1.toString());
assert.equal(body1.faceId, faceId);
assert.equal(body1.facePersistency, 2);
assert.equal(body1.facePersistency, ControlParameters.FacePersistency.Permanent);

const res2 = await ControlCommand.call("faces/destroy", {
faceId,
});
assert.equal(res2.statusCode, 200);
assert(res2.body !== undefined);
const body2 = ControlParameters.decodeFromResponseBody(res2);
console.log(facePropertiesToString(body2));
console.log(body2.toString());
assert.equal(body2.faceId, faceId);

closeUplinks();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"@types/wtfnode": "^0.7.3",
"@typescript/lib-dom": "npm:@types/[email protected]",
"@vitest/coverage-v8": "^1.1.0",
"@yoursunny/xo-config": "0.56.0",
"@yoursunny/xo-config": "0.56.1",
"codedown": "^2.2.0",
"tslib": "^2.6.2",
"typedoc": "^0.25.4",
Expand Down
104 changes: 60 additions & 44 deletions packages/nfdmgmt/src/control-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,6 @@ import { Name, TT } from "@ndn/packet";
import { Decoder, type Encodable, type Encoder, EvDecoder, NNI } from "@ndn/tlv";
import { toUtf8 } from "@ndn/util";

const TtControlParameters = 0x68;

const fieldEncoders: Array<[tt: number, key: keyof ControlParameters.Fields, encodeValue: (v: any) => Encodable]> = [];

const EVD = new EvDecoder<ControlParameters.Fields>("ControlParameters", TtControlParameters)
.setIsCritical(() => false);

function defField<K extends keyof ControlParameters.Fields>(tt: number, key: K,
encodeValue: (v: NonNullable<ControlParameters.Fields[K]>) => Encodable,
decode: (tlv: Decoder.Tlv) => ControlParameters.Fields[K],
): void {
fieldEncoders.push([tt, key, encodeValue]);
EVD.add(tt, (t, tlv) => {
t[key] = decode(tlv);
});
}

function decodeNNI({ nni }: Decoder.Tlv) {
return nni;
}

function decodeString({ text }: Decoder.Tlv) {
return text;
}

defField(TT.Name, "name", (name) => name.value, ({ decoder }) => decoder.decode(Name));
defField(0x69, "faceId", NNI, decodeNNI);
defField(0x72, "uri", toUtf8, decodeString);
defField(0x81, "localUri", toUtf8, decodeString);
defField(0x6F, "origin", NNI, decodeNNI);
defField(0x6A, "cost", NNI, decodeNNI);
defField(0x83, "capacity", NNI, decodeNNI);
defField(0x84, "count", NNI, decodeNNI);
defField(0x87, "baseCongestionMarkingInterval", NNI, decodeNNI);
defField(0x88, "defaultCongestionThreshold", NNI, decodeNNI);
defField(0x89, "mtu", NNI, decodeNNI);
defField(0x6C, "flags", NNI, decodeNNI);
defField(0x70, "mask", NNI, decodeNNI);
defField(0x6B, "strategy", (name) => name, ({ vd }) => vd.decode(Name));
defField(0x6D, "expirationPeriod", NNI, decodeNNI);
defField(0x85, "facePersistency", NNI, decodeNNI);

/** NFD Management ControlParameters struct. */
export class ControlParameters {
public static decodeFrom(decoder: Decoder): ControlParameters {
Expand All @@ -65,12 +23,19 @@ export class ControlParameters {
public encodeTo(encoder: Encoder) {
encoder.prependTlv(
TtControlParameters,
...fieldEncoders.map(([tt, key, encodeValue]): Encodable => {
...fieldDefs.map(([tt, key, encodeValue]): Encodable => {
const v = this[key];
return v !== undefined && [tt, encodeValue(v)];
}),
);
}

public toString(): string {
return fieldDefs.flatMap(([, key]) => {
const v = this[key];
return v === undefined ? [] : `${key}=${v}`;
}).join(", ");
}
}
export interface ControlParameters extends ControlParameters.Fields {}

Expand Down Expand Up @@ -101,8 +66,59 @@ export namespace ControlParameters {
}

export const FaceFlags = {
LocalFieldsEnabled: Math.trunc(1),
LocalFieldsEnabled: 1 << 0,
LpReliabilityEnabled: 1 << 1,
CongestionMarkingEnabled: 1 << 2,
};

export const RouteFlags = {
ChildInherit: 1 << 0,
Capture: 1 << 1,
};
}

const TtControlParameters = 0x68;

const fieldDefs: Array<[
tt: number,
key: keyof ControlParameters.Fields,
encodeValue: (v: any) => Encodable,
]> = [];

const EVD = new EvDecoder<ControlParameters.Fields>("ControlParameters", TtControlParameters)
.setIsCritical(() => false);

function defField<K extends keyof ControlParameters.Fields>(tt: number, key: K,
encodeValue: (v: NonNullable<ControlParameters.Fields[K]>) => Encodable,
decode: (tlv: Decoder.Tlv) => ControlParameters.Fields[K],
): void {
fieldDefs.push([tt, key, encodeValue]);
EVD.add(tt, (t, tlv) => {
t[key] = decode(tlv);
});
}

function decodeNNI({ nni }: Decoder.Tlv) {
return nni;
}

function decodeString({ text }: Decoder.Tlv) {
return text;
}

defField(TT.Name, "name", (name) => name.value, ({ decoder }) => decoder.decode(Name));
defField(0x69, "faceId", NNI, decodeNNI);
defField(0x72, "uri", toUtf8, decodeString);
defField(0x81, "localUri", toUtf8, decodeString);
defField(0x6F, "origin", NNI, decodeNNI);
defField(0x6A, "cost", NNI, decodeNNI);
defField(0x83, "capacity", NNI, decodeNNI);
defField(0x84, "count", NNI, decodeNNI);
defField(0x87, "baseCongestionMarkingInterval", NNI, decodeNNI);
defField(0x88, "defaultCongestionThreshold", NNI, decodeNNI);
defField(0x89, "mtu", NNI, decodeNNI);
defField(0x6C, "flags", NNI, decodeNNI);
defField(0x70, "mask", NNI, decodeNNI);
defField(0x6B, "strategy", (name) => name, ({ vd }) => vd.decode(Name));
defField(0x6D, "expirationPeriod", NNI, decodeNNI);
defField(0x85, "facePersistency", NNI, decodeNNI);
4 changes: 2 additions & 2 deletions packages/nfdmgmt/src/prefix-reg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import map from "obliterator/map.js";
import type { Except } from "type-fest";

import { ControlCommand } from "./control-command";
import type { ControlParameters } from "./control-parameters";
import { ControlParameters } from "./control-parameters";

type CommandOptions = Except<ControlCommand.Options, "endpoint">;
type RouteOptions = Pick<ControlParameters.Fields, "origin" | "cost" | "flags">;
Expand Down Expand Up @@ -51,7 +51,7 @@ class NfdPrefixReg extends ReadvertiseDestination<State> {
this.routeOptions = {
origin: 65,
cost: 0,
flags: 0x02, // CAPTURE
flags: ControlParameters.RouteFlags.Capture,
...opts,
};

Expand Down

0 comments on commit 2300576

Please sign in to comment.