-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
345 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Name } from "@ndn/name"; | ||
import { Decoder, Encoder } from "@ndn/tlv"; | ||
|
||
import { Data } from "../src"; | ||
|
||
/** Obtain Data full name without being cached on Data packet. */ | ||
export async function getDataFullName(data: Data): Promise<Name> { | ||
const copy = new Decoder(Encoder.encode(data)).decode(Data); | ||
return await copy.computeFullName(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "@ndn/pit", | ||
"version": "0.0.0", | ||
"description": "NDNts: Pending Interest Table", | ||
"keywords": [ | ||
"NDN", | ||
"Named Data Networking" | ||
], | ||
"author": "Junxiao Shi <npm@mail1.yoursunny.com>", | ||
"license": "ISC", | ||
"main": "src/index.ts", | ||
"files": [ | ||
"lib" | ||
], | ||
"sideEffects": [], | ||
"homepage": "https://yoursunny.com/p/NDNts/", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/yoursunny/NDNts.git", | ||
"directory": "packages/pit" | ||
}, | ||
"publishConfig": { | ||
"main": "lib/index.js", | ||
"types": "lib/index.d.ts" | ||
}, | ||
"dependencies": { | ||
"@ndn/l3pkt": "workspace:*", | ||
"@ndn/name": "workspace:*", | ||
"strict-event-emitter-types": "^2.0.0" | ||
}, | ||
"devDependencies": { | ||
"delay": "^4.3.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from "./interface"; | ||
export * from "./linear"; | ||
export * from "./pending-interest"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Data, Interest } from "@ndn/l3pkt"; | ||
import { PendingInterest } from "./pending-interest"; | ||
|
||
/** Pending Interest Table. */ | ||
export interface Pit { | ||
readonly length: number; | ||
|
||
/** | ||
* Add an Interest. | ||
* Return PendingInterest if accepted, or undefined if loop detected. | ||
*/ | ||
addInterest(interest: Interest, from: PropertyKey): PendingInterest|undefined; | ||
|
||
/** Asynchronously process incoming Data. */ | ||
processData(data: Data, from: PropertyKey): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Data } from "@ndn/l3pkt"; | ||
|
||
export namespace PitImpl { | ||
export const SATISFY = Symbol("PitImpl.SATISFY"); | ||
export const REMOVE = Symbol("PitImpl.REMOVE"); | ||
|
||
export interface PendingInterest { | ||
/** Satisfy Interest with Data. */ | ||
[SATISFY](data: Data); | ||
} | ||
|
||
export interface Table { | ||
/** Remove from table. */ | ||
[REMOVE](pi: PendingInterest); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { canSatisfySync, Data, Interest } from "@ndn/l3pkt"; | ||
|
||
import { Pit } from "./interface"; | ||
import { PitImpl } from "./internal"; | ||
import { PendingInterest } from "./pending-interest"; | ||
|
||
/** | ||
* PIT implemented with an array. | ||
* | ||
* This implementation does not support Interest aggregation or loop prevention. | ||
* Each Interest is appended to an array. | ||
* Each Data is matched against all pending Interests. | ||
*/ | ||
export class LinearPit implements Pit { | ||
public get length() { return this.table.length; } | ||
|
||
private table: PendingInterest[] = []; | ||
|
||
public addInterest(interest: Interest): PendingInterest { | ||
const pi = new PendingInterest(this, interest); | ||
this.table.push(pi); | ||
return pi; | ||
} | ||
|
||
public processData(data: Data): void { | ||
let needDigest = false; | ||
this.table = this.table.filter((pi) => { | ||
const satisfied = canSatisfySync(pi.interest, data); | ||
if (satisfied === true) { | ||
pi[PitImpl.SATISFY](data); | ||
return false; | ||
} | ||
needDigest = needDigest || satisfied !== false; | ||
return true; | ||
}); | ||
if (needDigest) { | ||
data.computeImplicitDigest() | ||
.then(() => this.processData(data)); | ||
} | ||
} | ||
|
||
public [PitImpl.REMOVE](pi: PitImpl.PendingInterest) { | ||
const i = this.table.findIndex((item) => item === pi); | ||
if (i >= 0) { | ||
this.table.splice(i, 1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { Data, Interest } from "@ndn/l3pkt"; | ||
import { EventEmitter } from "events"; | ||
import { StrictEventEmitter } from "strict-event-emitter-types"; | ||
|
||
import { PitImpl } from "./internal"; | ||
|
||
interface Events { | ||
/** Emitted when Interest has been satisfied. */ | ||
data: Data; | ||
/** Emitted when Interest times out. */ | ||
timeout: void; | ||
} | ||
|
||
type Emitter = StrictEventEmitter<EventEmitter, Events>; | ||
|
||
const TIMEOUT = "4a96aedc-be5d-4eab-8a2b-775f13a7a982"; | ||
|
||
export class PendingInterest extends (EventEmitter as new() => Emitter) { | ||
private timers: Record<string, number> = {}; | ||
|
||
constructor(private readonly table: PitImpl.Table, public readonly interest: Interest) { | ||
super(); | ||
this.setTimer(TIMEOUT, interest.lifetime, () => { | ||
this.clearTimers(); | ||
this.emit("timeout"); | ||
this.table[PitImpl.REMOVE](this); | ||
}); | ||
} | ||
|
||
/** | ||
* Schedule a timer associated with this pending Interest. | ||
* This cancels previous timer with same id. | ||
* All timers will be canceled when the pending Interest is satisfied/expired/canceled. | ||
*/ | ||
public setTimer(id: string, timeout: number, f: () => any) { | ||
this.clearTimer(id); | ||
this.timers[id] = setTimeout(f, timeout) as any; | ||
} | ||
|
||
/** Cancel a timer. */ | ||
public clearTimer(id: string) { | ||
clearTimeout(this.timers[id]); | ||
delete this.timers[id]; | ||
} | ||
|
||
public [PitImpl.SATISFY](data: Data) { | ||
this.clearTimers(); | ||
this.emit("data", data); | ||
} | ||
|
||
/** Indicate the requester no longer wants the Data. */ | ||
public cancel() { | ||
this.clearTimers(); | ||
this.table[PitImpl.REMOVE](this); | ||
} | ||
|
||
private clearTimers() { | ||
Object.values(this.timers).forEach(clearTimeout); | ||
this.timers = {}; | ||
} | ||
} |
Oops, something went wrong.