#ndn #data #data-encoding #networking #named

ndn-app

Named Data Networking - main protocol parsing and encoding

1 unstable release

0.4.0 Sep 7, 2024

#2 in #ndn

MIT license

56KB
1.5K SLoC

NDN App

Example Usage

use ndn_app::{
    app::{App, AppHandler},
    verifier::ForbidUnsigned,
};
use ndn_protocol::{Data, DigestSha256, Interest};

async fn test_route(handler: AppHandler, interest: Interest<()>, context: ()) -> Option<Data<()>> {
    // AppHandler allows expressing interests, and shutting down the system gracefully
    // Possible future features include adding routes at runtime, changing the signer, etc.
    //
    // interest is the Interest that matched this route. The generic argument is the type of the
    // application parameters, which can be any type that implements TlvDecode
    //
    // The context is provided to App::new and cloned for every route handler.
    // You may place global state in there, for example using Arc<RwLock<_>>
    //
    // If the return value is None, a NACK is sent.
    // If the return value is Some, the contained Data object is sent as a response. It is the
    // responsibility of the route handler to set the name correctly.
    // The generic argument is the type of the content, which may be any type implementing
    // TlvEncode
    Some(Data::new(interest.name().clone(), ()))
}

async fn on_start(handler: AppHandler, context: ()) {
    // Executed after routes are registered and everything is up and running
}

// tokio is required
#[tokio::main]
async fn main() {
    // The verifier can be as simple or complicated as necessary, being composable with .or and .and methods.
    let verifier = ForbidUnsigned;

    App::new(DigestSha256::new(), ())
        .on_start(on_start)
        .route("test/route", verifier, test_route)
        .start()
        .await
        .unwrap();
}

Dependencies

~16–26MB
~390K SLoC