-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
nfdmgmt: ControlCommand.call => invoke
- Loading branch information
Showing
9 changed files
with
155 additions
and
116 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Component, digestSigning, Interest, Name, SignedInterestPolicy, type Signer, TT } from "@ndn/packet"; | ||
import { Decoder, type Encodable, Encoder } from "@ndn/tlv"; | ||
|
||
import { ControlResponse } from "./control-response"; | ||
import { CommonOptions } from "./options"; | ||
|
||
const defaultSIP = new SignedInterestPolicy(SignedInterestPolicy.Nonce(), SignedInterestPolicy.Time()); | ||
|
||
export interface ControlCommandOptions extends CommonOptions { | ||
/** | ||
* Command Interest signer. | ||
* Default is digest signing. | ||
*/ | ||
signer?: Signer; | ||
|
||
/** | ||
* Signed Interest policy for the command Interest. | ||
* Default is including SigNonce and SigTime in the signed Interest. | ||
*/ | ||
signedInterestPolicy?: SignedInterestPolicy; | ||
} | ||
|
||
/** | ||
* Invoke generic ControlCommand and wait for response. | ||
* @param command command name components. | ||
* @param params command parameters. | ||
* @param opts target prefix and other options. | ||
* @returns command response. | ||
*/ | ||
export async function invokeGeneric(command: string, params: Encodable, opts: ControlCommandOptions = {}): Promise<ControlResponse> { | ||
const { endpoint, prefix, verifier } = CommonOptions.applyDefaults(opts); | ||
const { | ||
signer = digestSigning, | ||
signedInterestPolicy = defaultSIP, | ||
} = opts; | ||
|
||
const interest = new Interest(new Name([ | ||
...prefix.comps, | ||
...command.split("/"), | ||
new Component(TT.GenericNameComponent, Encoder.encode(params)), | ||
])); | ||
await signedInterestPolicy.makeSigner(signer).sign(interest); | ||
|
||
const data = await endpoint.consume(interest, { | ||
describe: `ControlCommand(${command})`, | ||
verifier, | ||
}); | ||
return Decoder.decode(data.content, ControlResponse); | ||
} |
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
export * from "./control-command"; | ||
export * from "./control-parameters"; | ||
export * from "./control-command-generic"; | ||
export * from "./control-command-nfd"; | ||
export * from "./control-response"; | ||
export { localhopPrefix, localhostPrefix, getPrefix } from "./options"; | ||
export * from "./prefix-reg"; | ||
export * from "./sign-interest-02"; |
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,41 @@ | ||
import { Endpoint } from "@ndn/endpoint"; | ||
import { Name, noopSigning, type Verifier } from "@ndn/packet"; | ||
|
||
export const localhostPrefix = new Name("/localhost/nfd"); | ||
export const localhopPrefix = new Name("/localhop/nfd"); | ||
|
||
/** | ||
* Determine the NFD management prefix. | ||
* @param isLocal whether the client is connected to a NFD local face. | ||
* @returns NFD management prefix. | ||
*/ | ||
export function getPrefix(isLocal = false) { | ||
return isLocal ? localhostPrefix : localhopPrefix; | ||
} | ||
|
||
export interface CommonOptions { | ||
/** Endpoint for communication. */ | ||
endpoint?: Endpoint; | ||
|
||
/** | ||
* NFD management prefix. | ||
* @default getPrefix() | ||
*/ | ||
prefix?: Name; | ||
|
||
/** | ||
* Data verifier. | ||
* Default is no verification. | ||
*/ | ||
verifier?: Verifier; | ||
} | ||
|
||
export namespace CommonOptions { | ||
export function applyDefaults({ | ||
endpoint = new Endpoint(), | ||
prefix = localhostPrefix, | ||
verifier = noopSigning, | ||
}: CommonOptions): Required<CommonOptions> { | ||
return { endpoint, prefix, verifier }; | ||
} | ||
} |
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