pub struct Consumer { /* private fields */ }Expand description
High-level NDN consumer — fetches Data by name.
Implementations§
Source§impl Consumer
impl Consumer
Sourcepub async fn connect(socket: impl AsRef<Path>) -> Result<Self, AppError>
pub async fn connect(socket: impl AsRef<Path>) -> Result<Self, AppError>
Connect to an external router via its face socket.
Sourcepub fn from_handle(handle: InProcHandle) -> Self
pub fn from_handle(handle: InProcHandle) -> Self
Create from an in-process InProcHandle (embedded engine).
Sourcepub async fn fetch(&mut self, name: impl Into<Name>) -> Result<Data, AppError>
pub async fn fetch(&mut self, name: impl Into<Name>) -> Result<Data, AppError>
Express an Interest by name and return the decoded Data.
Uses DEFAULT_INTEREST_LIFETIME for the wire Interest and
DEFAULT_TIMEOUT for the local wait. To set hop limit,
application parameters, or forwarding hints, use
fetch_with.
Sourcepub async fn fetch_with(
&mut self,
builder: InterestBuilder,
) -> Result<Data, AppError>
pub async fn fetch_with( &mut self, builder: InterestBuilder, ) -> Result<Data, AppError>
Express an Interest built with InterestBuilder and return the decoded Data.
The local wait timeout is derived from the builder’s Interest lifetime (+ 500 ms forwarding buffer). This is the right method when you need hop limit, forwarding hints, or application parameters:
use ndn_packet::encode::InterestBuilder;
// Hop limit: limit forwarding to 4 hops.
let data = consumer.fetch_with(
InterestBuilder::new("/ndn/remote/data").hop_limit(4)
).await?;
// Forwarding hint: reach a producer via a delegation prefix.
let data = consumer.fetch_with(
InterestBuilder::new("/alice/files/photo.jpg")
.forwarding_hint(vec!["/campus/ndn-hub".parse()?])
).await?;
// Application parameters: parameterised fetch (e.g. RPC / query).
let data = consumer.fetch_with(
InterestBuilder::new("/service/query")
.app_parameters(b"filter=recent&limit=10")
).await?;Sourcepub async fn fetch_wire(
&mut self,
wire: Bytes,
timeout: Duration,
) -> Result<Data, AppError>
pub async fn fetch_wire( &mut self, wire: Bytes, timeout: Duration, ) -> Result<Data, AppError>
Express a pre-encoded Interest and return the decoded Data.
timeout is the local wait duration — set this to at least the
Interest lifetime encoded in wire to avoid timing out before the
forwarder does.
Returns AppError::Nacked if the forwarder responds with a Nack
(e.g. no route to the name prefix).
Sourcepub async fn fetch_verified(
&mut self,
name: impl Into<Name>,
validator: &Validator,
) -> Result<SafeData, AppError>
pub async fn fetch_verified( &mut self, name: impl Into<Name>, validator: &Validator, ) -> Result<SafeData, AppError>
Fetch and verify against a Validator. Returns SafeData on success.
Sourcepub async fn get(&mut self, name: impl Into<Name>) -> Result<Bytes, AppError>
pub async fn get(&mut self, name: impl Into<Name>) -> Result<Bytes, AppError>
Convenience: fetch content as raw bytes.
Sourcepub async fn fetch_all(&mut self, names: &[Name]) -> Vec<Result<Data, AppError>>
pub async fn fetch_all(&mut self, names: &[Name]) -> Vec<Result<Data, AppError>>
Fetch multiple names sequentially and collect results.
Each Interest is expressed in order; the result vector preserves the input order regardless of which fetches succeed or fail.
§Note
Fetches are sequential because a single NdnConnection cannot
correlate concurrent Interests to their responses without PIT tokens.
For true concurrent fetch, create multiple Consumer instances and
use tokio::join!.
Sourcepub async fn fetch_with_retry(
&mut self,
name: impl Into<Name>,
max_attempts: u32,
base_delay: Duration,
) -> Result<Data, AppError>
pub async fn fetch_with_retry( &mut self, name: impl Into<Name>, max_attempts: u32, base_delay: Duration, ) -> Result<Data, AppError>
Fetch with exponential-backoff retry.
On timeout or connection error, waits base_delay, then 2×base_delay,
etc., up to max_attempts total tries (including the first). Returns the
last error if all attempts are exhausted.
Sourcepub async fn fetch_segmented(
&mut self,
prefix: impl Into<Name>,
) -> Result<Bytes, AppError>
pub async fn fetch_segmented( &mut self, prefix: impl Into<Name>, ) -> Result<Bytes, AppError>
Fetch a segmented object produced with [Producer::publish_large].
Fetches /prefix/0, reads FinalBlockId to determine the total segment
count, then fetches all remaining segments in order. Segments are
reassembled into a single contiguous buffer.
Segment names are generic NameComponents with ASCII-decimal indices
(e.g. /prefix/0, /prefix/1, …), matching the convention used by
[Producer::publish_large].
Sourcepub async fn get_verified(
&mut self,
name: impl Into<Name>,
validator: &Validator,
) -> Result<SafeData, AppError>
pub async fn get_verified( &mut self, name: impl Into<Name>, validator: &Validator, ) -> Result<SafeData, AppError>
Fetch and verify against a Validator. Returns SafeData on success.
This is a convenience wrapper around fetch +
Validator::validate_chain.