Skip to content

Commit

Permalink
Remove the use of endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
zjkmxy committed Mar 14, 2024
1 parent 6b45515 commit 330c34c
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 120 deletions.
30 changes: 23 additions & 7 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
{
"tasks": {
},
"unstable": ["byonm", "net", "fs"],
"tasks": {},
"unstable": [
"byonm",
"net",
"fs"
],
"compilerOptions": {
"types": [
"@types/wicg-file-system-access"
Expand All @@ -13,12 +16,25 @@
"indentWidth": 2,
"useTabs": false,
"semiColons": true,
"include": ["src/**", "build.ts"]
"include": [
"src/**",
"build.ts"
]
},
"lint": {
"include": ["src/**", "build.ts"]
"include": [
"src/**",
"build.ts"
],
"rules": {
"include": [
"deprecated"
]
}
},
"test": {
"exclude": ["dist/"]
"exclude": [
"dist/"
]
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ucla-irl/ndnts-aux",
"version": "1.1.5",
"version": "2.0.0",
"description": "NDNts Auxiliary Package for Web and Deno",
"scripts": {
"test": "deno test --no-check",
Expand Down
10 changes: 5 additions & 5 deletions src/namespace/base-node.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Endpoint } from '@ndn/endpoint';
import type { Forwarder } from '@ndn/fw';
import type { Data, Interest, Verifier } from '@ndn/packet';
import * as namePattern from './name-pattern.ts';
import * as schemaTree from './schema-tree.ts';
import { EventChain } from '../utils/event-chain.ts';
import { NamespaceHandler } from './nt-schema.ts';

export interface BaseNodeEvents {
attach(path: namePattern.Pattern, endpoint: Endpoint): Promise<void>;
detach(endpoint: Endpoint): Promise<void>;
attach(path: namePattern.Pattern, fw: Forwarder): Promise<void>;
detach(fw: Forwarder): Promise<void>;
}

export class BaseNode {
Expand Down Expand Up @@ -58,11 +58,11 @@ export class BaseNode {
public async processAttach(path: namePattern.Pattern, handler: NamespaceHandler) {
// All children's attach events are called
this.handler = handler;
await this.onAttach.emit(path, handler.endpoint!);
await this.onAttach.emit(path, handler.fw!);
}

public async processDetach() {
await this.onDetach.emit(this.handler!.endpoint!);
await this.onDetach.emit(this.handler!.fw!);
this.handler = undefined;
// Then call children's detach
}
Expand Down
6 changes: 4 additions & 2 deletions src/namespace/expressing-point.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { RetxPolicy } from '@ndn/endpoint';
import { consume, RetxPolicy } from '@ndn/endpoint';
import { Data, Interest, Signer, type Verifier } from '@ndn/packet';
import * as schemaTree from './schema-tree.ts';
import { BaseNode, BaseNodeEvents } from './base-node.ts';
Expand Down Expand Up @@ -198,7 +198,9 @@ export class ExpressingPoint extends BaseNode {
throw new Error(`Interest surpressed: ${interestName.toString()} @${this.describe}`);
}

const data = await this.handler!.endpoint!.consume(interest, {
// TODO: Handle data directly instead of relying on consume. Because of the segmented object.
const data = await consume(interest, {
fw: this.handler!.fw!,
// deno-lint-ignore no-explicit-any
signal: opts.abortSignal as any,
retx: this.config.retx,
Expand Down
26 changes: 11 additions & 15 deletions src/namespace/nt-schema.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { assert } from '../dep.ts';
import { AsyncDisposableStack, name, Responder } from '../utils/mod.ts';
import { Endpoint } from '@ndn/endpoint';
import { consume } from '@ndn/endpoint';
import { Data, digestSigning, SigType } from '@ndn/packet';
import { Decoder, Encoder } from '@ndn/tlv';
import { Bridge } from '@ndn/l3face';
Expand All @@ -15,8 +15,6 @@ export const b = ([value]: TemplateStringsArray) => new TextEncoder().encode(val
Deno.test('NtSchema.1 Basic Interest and Data', async () => {
using bridge = Bridge.create({});
const { fwA, fwB } = bridge;
const epA = new Endpoint({ fw: fwA });
const epB = new Endpoint({ fw: fwB });
await using closers = new AsyncDisposableStack();
const appPrefix = name`/prefix`;

Expand Down Expand Up @@ -44,13 +42,13 @@ Deno.test('NtSchema.1 Basic Interest and Data', async () => {
await digestSigning.sign(data3);
return data3;
});
await schema.attach(appPrefix, epA);
await schema.attach(appPrefix, fwA);
closers.defer(async () => await schema.detach());

// Responder side
const storage = new InMemoryStorage();
closers.use(storage);
const responder = new Responder(appPrefix, epB, storage);
const responder = new Responder(appPrefix, fwB, storage);
closers.use(responder);
const data1 = new Data(
name`${appPrefix}/records/8=rec1`,
Expand Down Expand Up @@ -85,8 +83,9 @@ Deno.test('NtSchema.1 Basic Interest and Data', async () => {
assert.assertEquals(recved2.content, b`World.`);

// Test NTSchema's producing data (on request, without storage)
const recved3 = await epB.consume(name`${appPrefix}/records/8=rec3`, {
const recved3 = await consume(name`${appPrefix}/records/8=rec3`, {
verifier: digestSigning,
fw: fwB,
});
assert.assertExists(recved3);
assert.assert(recved3.name.equals(name`${appPrefix}/records/8=rec3`));
Expand All @@ -97,8 +96,6 @@ Deno.test('NtSchema.1 Basic Interest and Data', async () => {
Deno.test('NtSchema.2 Data Storage', async () => {
using bridge = Bridge.create({});
const { fwA, fwB } = bridge;
const epA = new Endpoint({ fw: fwA });
const epB = new Endpoint({ fw: fwB });
await using closers = new AsyncDisposableStack();
const appPrefix = name`/prefix`;

Expand Down Expand Up @@ -127,13 +124,13 @@ Deno.test('NtSchema.2 Data Storage', async () => {
return undefined;
}
});
await schema.attach(appPrefix, epA);
await schema.attach(appPrefix, fwA);
closers.defer(async () => await schema.detach());

// Responder side
const storageB = new InMemoryStorage();
closers.use(storageB);
const responder = new Responder(appPrefix, epB, storageB);
const responder = new Responder(appPrefix, fwB, storageB);
closers.use(responder);
const data1 = new Data(
name`${appPrefix}/records/8=rec1`,
Expand All @@ -149,8 +146,9 @@ Deno.test('NtSchema.2 Data Storage', async () => {
'provide',
b`World.`,
);
const received = await epB.consume(name`${appPrefix}/records/8=rec2`, {
const received = await consume(name`${appPrefix}/records/8=rec2`, {
verifier: digestSigning,
fw: fwB,
});
assert.assertExists(received);
assert.assert(received.name.equals(name`${appPrefix}/records/8=rec2`));
Expand Down Expand Up @@ -178,8 +176,6 @@ Deno.test('NtSchema.2 Data Storage', async () => {
Deno.test('NtSchema.3 Verification', async () => {
using bridge = Bridge.create({});
const { fwA, fwB } = bridge;
const epA = new Endpoint({ fw: fwA });
const epB = new Endpoint({ fw: fwB });
await using closers = new AsyncDisposableStack();
const appPrefix = name`/prefix`;
const [prvKey, pubKey] = await generateSigningKey(/*identity*/ appPrefix, Ed25519);
Expand Down Expand Up @@ -216,13 +212,13 @@ Deno.test('NtSchema.3 Verification', async () => {
return prevResult;
}
});
await schema.attach(appPrefix, epA);
await schema.attach(appPrefix, fwA);
closers.defer(async () => await schema.detach());

// Responder side
const storage = new InMemoryStorage();
closers.use(storage);
const responder = new Responder(appPrefix, epB, storage);
const responder = new Responder(appPrefix, fwB, storage);
closers.use(responder);
const data1 = new Data(
name`${appPrefix}/records/8=rec1`,
Expand Down
26 changes: 17 additions & 9 deletions src/namespace/nt-schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Endpoint, Producer } from '@ndn/endpoint';
import { produce, Producer } from '@ndn/endpoint';
import type { Forwarder } from '@ndn/fw';
import { Data, Interest, Name, type Verifier } from '@ndn/packet';
import * as namePattern from './name-pattern.ts';
import * as schemaTree from './schema-tree.ts';
Expand All @@ -14,20 +15,20 @@ export enum VerifyResult {
}

export interface NamespaceHandler {
get endpoint(): Endpoint | undefined;
get fw(): Forwarder | undefined;
get attachedPrefix(): Name | undefined;
getVerifier(deadline: number | undefined, verificationContext?: Record<string, unknown>): Verifier;
storeData(data: Data): Promise<void>;
}

export class NtSchema implements NamespaceHandler, AsyncDisposable {
public readonly tree = schemaTree.create<BaseNode>();
protected _endpoint: Endpoint | undefined;
protected _fw: Forwarder | undefined;
protected _attachedPrefix: Name | undefined;
protected _producer: Producer | undefined;

get endpoint() {
return this._endpoint;
get fw() {
return this._fw;
}

get attachedPrefix() {
Expand Down Expand Up @@ -71,17 +72,24 @@ export class NtSchema implements NamespaceHandler, AsyncDisposable {
return undefined;
}

public async attach(prefix: Name, endpoint: Endpoint) {
public async attach(prefix: Name, fw: Forwarder) {
if (this._fw !== undefined) {
if (this._fw !== fw) {
throw new Error('You cannot attach a running NTSchema to another forwarder');
}
return;
}
this._attachedPrefix = prefix;
this._endpoint = endpoint;
this._fw = fw;
await schemaTree.traverse(this.tree, {
post: async (node, path) => await node.resource?.processAttach(path, this),
});

this._producer = endpoint.produce(prefix, this.onInterest.bind(this), {
this._producer = produce(prefix, this.onInterest.bind(this), {
describe: `NtSchema[${prefix.toString()}]`,
routeCapture: false,
announcement: prefix,
fw: fw,
});
}

Expand All @@ -90,7 +98,7 @@ export class NtSchema implements NamespaceHandler, AsyncDisposable {
await schemaTree.traverse(this.tree, {
pre: async (node) => await node.resource?.processDetach(),
});
this._endpoint = undefined;
this._fw = undefined;
this._attachedPrefix = undefined;
}

Expand Down
7 changes: 2 additions & 5 deletions src/namespace/segmented-object/fetcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assert } from '../../dep.ts';
import { AsyncDisposableStack, name, Responder } from '../../utils/mod.ts';
import { Endpoint } from '@ndn/endpoint';
import { Data, digestSigning } from '@ndn/packet';
import { Encoder } from '@ndn/tlv';
import { Bridge } from '@ndn/l3face';
Expand All @@ -14,8 +13,6 @@ export const b = ([value]: TemplateStringsArray) => new TextEncoder().encode(val
Deno.test('Fetcher.1 Basic fetching', async () => {
using bridge = Bridge.create({});
const { fwA, fwB } = bridge;
const epA = new Endpoint({ fw: fwA });
const epB = new Endpoint({ fw: fwB });
await using closers = new AsyncDisposableStack();
const appPrefix = name`/prefix`;

Expand All @@ -30,13 +27,13 @@ Deno.test('Fetcher.1 Basic fetching', async () => {
return VerifyResult.Fail;
}
});
await schema.attach(appPrefix, epA);
await schema.attach(appPrefix, fwA);
closers.defer(async () => await schema.detach());

// Responder side
const storage = new InMemoryStorage();
closers.use(storage);
const responder = new Responder(appPrefix, epB, storage);
const responder = new Responder(appPrefix, fwB, storage);
closers.use(responder);
const payloads = [b`SEG1 `, b`SEG2 `, b`SEG3;`];
for (const [i, p] of payloads.entries()) {
Expand Down
10 changes: 3 additions & 7 deletions src/namespace/segmented-object/segmented-object.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assert } from '../../dep.ts';
import { AsyncDisposableStack, name, Responder } from '../../utils/mod.ts';
import { Endpoint } from '@ndn/endpoint';
import { Data, digestSigning } from '@ndn/packet';
import { Decoder, Encoder } from '@ndn/tlv';
import { Bridge } from '@ndn/l3face';
Expand All @@ -16,8 +15,6 @@ export const b = ([value]: TemplateStringsArray) => new TextEncoder().encode(val
Deno.test('SegmentedObject.1 Basic fetching', async () => {
using bridge = Bridge.create({});
const { fwA, fwB } = bridge;
const epA = new Endpoint({ fw: fwA });
const epB = new Endpoint({ fw: fwB });
await using closers = new AsyncDisposableStack();
const appPrefix = name`/prefix`;

Expand All @@ -36,13 +33,13 @@ Deno.test('SegmentedObject.1 Basic fetching', async () => {
leafNode,
lifetimeAfterRto: 100,
});
await schema.attach(appPrefix, epA);
await schema.attach(appPrefix, fwA);
closers.defer(async () => await schema.detach());

// Responder side
const storage = new InMemoryStorage();
closers.use(storage);
const responder = new Responder(appPrefix, epB, storage);
const responder = new Responder(appPrefix, fwB, storage);
closers.use(responder);
const payloads = [b`SEG1 `, b`SEG2 `, b`SEG3;`];
for (const [i, p] of payloads.entries()) {
Expand Down Expand Up @@ -70,7 +67,6 @@ Deno.test('SegmentedObject.1 Basic fetching', async () => {
Deno.test('SegmentedObject.2 Basic provide', async () => {
using bridge = Bridge.create({});
const { fwA, fwB } = bridge;
const epA = new Endpoint({ fw: fwA });
await using closers = new AsyncDisposableStack();
const appPrefix = name`/prefix`;

Expand Down Expand Up @@ -102,7 +98,7 @@ Deno.test('SegmentedObject.2 Basic provide', async () => {
leafNode,
lifetimeAfterRto: 100,
});
await schema.attach(appPrefix, epA);
await schema.attach(appPrefix, fwA);
closers.defer(async () => await schema.detach());

// Provide object
Expand Down
2 changes: 1 addition & 1 deletion src/namespace/sync/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class SvsInstNode extends BaseNode {
const describe = this.describe ? `${this.describe}(${matched.name.toString()})` : undefined;

const ret = await SvSync.create({
endpoint: this.handler!.endpoint!,
fw: this.handler!.fw!,
syncPrefix: matched.name,
signer: signer,
verifier: verifier,
Expand Down
Loading

0 comments on commit 330c34c

Please sign in to comment.