Skip to content

Commit

Permalink
fix: disable async_dht when not using the default features
Browse files Browse the repository at this point in the history
see: cargo run --no-default-features --example get_immutable 4238af8aff56cf6e0007d9d2003bf23d33eea7c3
  • Loading branch information
Nuhvi committed Feb 6, 2024
1 parent 1f8f71f commit 4573369
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ futures = "0.3.29"
[features]
async = ["flume/async"]
default = ["async"]

[[example]]
name = "async_get_immutable"
path = "examples/async/get_immutable.rs"
required-features = ["async"]
6 changes: 6 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,9 @@ cargo run --example put_mutable <64 bytes hex secret_key> <string>
```sh
cargo run --example get_mutable <40 bytes hex target from put_mutable>
`````

## Async Get Mutable

```sh
cargo run --example async_get_mutable <40 bytes hex target from put_mutable>
`````
67 changes: 67 additions & 0 deletions examples/async/get_immutable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use std::{str::FromStr, time::Instant};

use mainline::{Dht, Id};

use clap::Parser;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
/// Immutable data sha1 hash to lookup.
target: String,
}

async fn async_main() {
let cli = Cli::parse();

let target_parse_result: Result<Id, _> = Id::from_str(cli.target.as_str());

match target_parse_result {
Ok(infohash) => {
let dht = Dht::default().as_async();

let start = Instant::now();

println!("\nLooking up immutable data: {} ...\n", cli.target);

let response = &mut dht.get_immutable(infohash).await;

if let Some(res) = response.next_async().await {
println!(
"Got result in {:?} seconds\n",
start.elapsed().as_secs_f32()
);

// No need to stream responses, just print the first result, since
// all immutable data items are guaranteed to be the same.

match String::from_utf8(res.value.to_vec()) {
Ok(string) => {
println!("Got immutable data: {:?} | from: {:?}", string, res.from);
}
Err(_) => {
println!("Got immutable data: {:?} | from: {:?}", res.value, res.from);
}
};
}

println!(
"\nVisited {:?} nodes, found {:?} closest nodes",
response.visited,
&response.closest_nodes.len()
);

println!(
"\nQuery exhausted in {:?} seconds",
start.elapsed().as_secs_f32(),
);
}
Err(err) => {
println!("Error: {}", err)
}
};
}

fn main() {
futures::executor::block_on(async_main());
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! Rust implementation of read-only BitTorrent Mainline DHT client.
// Public modules
#[cfg(feature = "async")]
pub mod async_dht;
pub mod common;
pub mod dht;
Expand Down

0 comments on commit 4573369

Please sign in to comment.