Skip to content

Commit

Permalink
l3face: Transport.Attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
yoursunny committed Nov 13, 2019
1 parent 77aeba9 commit 2ebc04a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 30 deletions.
16 changes: 12 additions & 4 deletions packages/fw/src/face.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,15 @@ export class FaceImpl extends (EventEmitter as new() => Emitter) {
public running = true;
public readonly routes = new Set<string>(); // used by FIB
public advertise?: Advertise;
public get isLocal() { return this.inner.isLocal ?? false; }
public readonly txQueue = new Fifo<Face.Txable>();
public txQueueLength = 0;

public get attributes(): Face.Attributes {
return {
...(this.inner.attributes ?? {}),
};
}

constructor(public readonly fw: ForwarderImpl,
public readonly inner: Face.Base) {
super();
Expand Down Expand Up @@ -158,12 +163,16 @@ export namespace FaceImpl {

/** A socket or network interface associated with forwarding plane. */
export interface Face extends Pick<FaceImpl,
"fw"|"advertise"|"close"|"isLocal"|"toString"|"addRoute"|"removeRoute"|
"fw"|"advertise"|"attributes"|"close"|"toString"|"addRoute"|"removeRoute"|
Exclude<keyof Emitter, "emit">> {
readonly running: boolean;
}

export namespace Face {
export interface Attributes extends Record<string, any> {
local?: boolean;
}

/** Item that can be received on face. */
export type Rxable = Interest|InterestRequest|Data|CancelInterest;
/** Item that can be transmitted on face, when extendedTx is enabled. */
Expand Down Expand Up @@ -196,8 +205,7 @@ export namespace Face {

/** Underlying face. */
export type Base = RxTx & {
/** Indicate whether face connects to a local process. */
readonly isLocal?: boolean;
readonly attributes?: Attributes;

/** Return short string to identify this face. */
toString?: () => string;
Expand Down
2 changes: 1 addition & 1 deletion packages/l3face/src/datagram-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class DatagramTransport extends Transport {
public readonly rx: Transport.Rx;
public readonly tx: Transport.Tx;

constructor(conn: NodeJS.ReadWriteStream, attrs: Transport.Attributes = {}) {
constructor(conn: NodeJS.ReadWriteStream, attrs: Record<string, any> = {}) {
super(attrs);
this.rx = rxFromPacketStream(conn);
this.tx = txToStream(conn);
Expand Down
8 changes: 6 additions & 2 deletions packages/l3face/src/l3face.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ type Emitter = StrictEventEmitter<EventEmitter, Events>;

/** Network layer face for sending and receiving L3 packets. */
export class L3Face extends (EventEmitter as new() => Emitter) {
public readonly attributes: L3Face.Attributes;
public readonly lp = new LpService();
public readonly rx: AsyncIterable<Packet>;

constructor(public readonly transport: Transport) {
constructor(public readonly transport: Transport, attributes: L3Face.Attributes = {}) {
super();
this.attributes = { ...transport.attributes, ...attributes };
this.rx = pipeline(
() => transport.rx,
this.lp.rx,
Expand All @@ -43,7 +45,7 @@ export class L3Face extends (EventEmitter as new() => Emitter) {
}

public toString() {
return this.transport.toString();
return this.attributes.describe as string ?? `L3Face(${this.transport})`;
}

private encode = async (packet: Packet): Promise<Uint8Array|undefined> => {
Expand Down Expand Up @@ -74,6 +76,8 @@ export class L3Face extends (EventEmitter as new() => Emitter) {
}

export namespace L3Face {
export type Attributes = Transport.Attributes;

export class RxError extends Error {
constructor(inner: Error, public packet: Uint8Array) {
super(`${inner.message} ${toHex(packet)}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/l3face/src/stream-transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export class StreamTransport extends Transport {
public readonly rx: Transport.Rx;
public readonly tx: Transport.Tx;

constructor(conn: NodeJS.ReadWriteStream, attrs: Transport.Attributes = {}) {
constructor(conn: NodeJS.ReadWriteStream, attrs: Record<string, any> = {}) {
super(attrs);
this.rx = rxFromContinuousStream(conn);
this.tx = txToStream(conn);
Expand Down
18 changes: 6 additions & 12 deletions packages/l3face/src/transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,18 @@ export abstract class Transport {
public abstract readonly rx: Transport.Rx;
public abstract readonly tx: Transport.Tx;

public readonly isLocal: boolean;
private describe: string;

protected constructor({
isLocal: isLocal = false,
describe,
}: Transport.Attributes = {}) {
this.isLocal = isLocal;
this.describe = describe || this.constructor.name;
protected constructor(public readonly attributes: Transport.Attributes) {
}

public toString() { return this.describe; }
public toString() {
return this.attributes.describe ?? this.constructor.name;
}
}

export namespace Transport {
export interface Attributes {
isLocal?: boolean;
export interface Attributes extends Record<string, any> {
describe?: string;
local?: boolean;
}

/** RX iterable for incoming packets. */
Expand Down
27 changes: 20 additions & 7 deletions packages/l3face/tests/l3face.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,27 @@ import { consume } from "streaming-iterables";
import { DatagramTransport, L3Face } from "..";
import { makeDuplex } from "../test-fixture/pair";

test("name unspecified", () => {
const face = new L3Face(new DatagramTransport(makeDuplex(undefined, undefined)));
expect(face.toString()).toBe("DatagramTransport");
});
describe("name", () => {
test("unspecified", () => {
const transport = new DatagramTransport(makeDuplex(undefined, undefined));
const face = new L3Face(transport);
expect(transport.toString()).toBe("DatagramTransport");
expect(face.toString()).toBe("L3Face(DatagramTransport)");
});

test("name specified", () => {
const face = new L3Face(new DatagramTransport(makeDuplex(undefined, undefined), { describe: "face-name" }));
expect(face.toString()).toBe("face-name");
test("specified at transport", () => {
const transport = new DatagramTransport(makeDuplex(undefined, undefined), { describe: "tname" });
const face = new L3Face(transport);
expect(transport.toString()).toBe("tname");
expect(face.toString()).toBe("tname");
});

test("specified at face", () => {
const transport = new DatagramTransport(makeDuplex(undefined, undefined), { describe: "tname" });
const face = new L3Face(transport, { describe: "fname" });
expect(transport.toString()).toBe("tname");
expect(face.toString()).toBe("fname");
});
});

test("RX error on unknown TLV-TYPE", async () => {
Expand Down
4 changes: 4 additions & 0 deletions packages/nfdmgmt/src/control-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export namespace ControlCommand {
export const localhostPrefix = new Name("/localhost/nfd");
export const localhopPrefix = new Name("/localhop/nfd");

export function getPrefix(isLocal?: boolean) {
return (isLocal ?? false) ? localhostPrefix : localhopPrefix;
}

/** Invoke a command and wait for response. */
export async function call<C extends keyof Commands>(
command: C, params: Commands[C], opt: Options = {}): Promise<ControlResponse> {
Expand Down
3 changes: 1 addition & 2 deletions packages/nfdmgmt/src/prefix-reg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class NfdAdvertise extends Advertise {
...opts,
fw: face.fw,
};
this.opts.commandPrefix = this.opts.commandPrefix ??
(face.isLocal ? ControlCommand.localhostPrefix : ControlCommand.localhopPrefix);
this.opts.commandPrefix = this.opts.commandPrefix ?? ControlCommand.getPrefix(face.attributes.local);
face.addRoute(this.opts.commandPrefix);
}

Expand Down
3 changes: 2 additions & 1 deletion packages/nfdmgmt/tests/prefix-reg.t.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ test.each(TABLE)("reg %#", async ({ faceIsLocal, commandPrefix, expectedPrefix }
},
});
if (typeof faceIsLocal !== "undefined") {
jest.spyOn(face, "isLocal", "get").mockReturnValue(faceIsLocal);
const faceAttrs = { ...face.attributes, local: faceIsLocal };
jest.spyOn(face, "attributes", "get").mockReturnValue(faceAttrs);
}
enableNfdPrefixReg(face, { commandPrefix });

Expand Down

0 comments on commit 2ebc04a

Please sign in to comment.