Skip to content

Commit e4f0df3

Browse files
committed
Simplify the examples using the builder, while still publicly using the router API
1 parent 6147f39 commit e4f0df3

File tree

3 files changed

+14
-56
lines changed

3 files changed

+14
-56
lines changed

examples/custom-protocol.rs

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,7 @@ use iroh::{
5151
router::ProtocolHandler,
5252
};
5353
use iroh_base::hash::Hash;
54-
use iroh_blobs::{
55-
downloader::Downloader, net_protocol::Blobs, rpc::client::blobs::MemClient,
56-
util::local_pool::LocalPool,
57-
};
54+
use iroh_blobs::{net_protocol::Blobs, rpc::client::blobs::MemClient, util::local_pool::LocalPool};
5855
use tracing_subscriber::{prelude::*, EnvFilter};
5956

6057
#[derive(Debug, Parser)]
@@ -93,21 +90,9 @@ async fn main() -> Result<()> {
9390
// Build a in-memory node. For production code, you'd want a persistent node instead usually.
9491
let mut builder = iroh::node::Node::memory().build().await?;
9592
let local_pool = LocalPool::default();
96-
let store = iroh_blobs::store::mem::Store::new();
97-
let downloader = Downloader::new(
98-
store.clone(),
99-
builder.endpoint().clone(),
100-
local_pool.handle().clone(),
101-
);
102-
let blobs = Arc::new(Blobs::new(
103-
store,
104-
local_pool.handle().clone(),
105-
Default::default(),
106-
downloader,
107-
builder.endpoint().clone(),
108-
));
109-
let blobs_client = blobs.clone().client();
110-
builder = builder.accept(iroh_blobs::protocol::ALPN.to_vec(), blobs);
93+
let blobs = Blobs::memory().build(&local_pool.handle(), builder.endpoint());
94+
builder = builder.accept(iroh_blobs::protocol::ALPN.to_vec(), blobs.clone());
95+
let blobs_client = blobs.client();
11196

11297
// Build our custom protocol handler. The `builder` exposes access to various subsystems in the
11398
// iroh node. In our case, we need a blobs client and the endpoint.

examples/hello-world-fetch.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@
33
//!
44
//! This is using an in memory database and a random node id.
55
//! Run the `provide` example, which will give you instructions on how to run this example.
6-
use std::{env, str::FromStr, sync::Arc};
6+
use std::{env, str::FromStr};
77

88
use anyhow::{bail, ensure, Context, Result};
99
use iroh::base::ticket::BlobTicket;
10-
use iroh_blobs::{
11-
downloader::Downloader, net_protocol::Blobs, util::local_pool::LocalPool, BlobFormat,
12-
};
10+
use iroh_blobs::{net_protocol::Blobs, util::local_pool::LocalPool, BlobFormat};
1311
use tracing_subscriber::{prelude::*, EnvFilter};
1412

1513
// set the RUST_LOG env var to one of {debug,info,warn} to see logging info
@@ -39,22 +37,10 @@ async fn main() -> Result<()> {
3937
// create a new node
4038
let mut builder = iroh::node::Node::memory().build().await?;
4139
let local_pool = LocalPool::default();
42-
let store = iroh_blobs::store::mem::Store::new();
43-
let downloader = Downloader::new(
44-
store.clone(),
45-
builder.endpoint().clone(),
46-
local_pool.handle().clone(),
47-
);
48-
let blobs = Arc::new(Blobs::new(
49-
store,
50-
local_pool.handle().clone(),
51-
Default::default(),
52-
downloader,
53-
builder.endpoint().clone(),
54-
));
55-
let blobs_client = blobs.clone().client();
56-
builder = builder.accept(iroh_blobs::protocol::ALPN.to_vec(), blobs);
40+
let blobs = Blobs::memory().build(&local_pool.handle(), builder.endpoint());
41+
builder = builder.accept(iroh_blobs::protocol::ALPN.to_vec(), blobs.clone());
5742
let node = builder.spawn().await?;
43+
let blobs_client = blobs.client();
5844

5945
println!("fetching hash: {}", ticket.hash());
6046
println!("node id: {}", node.node_id());

examples/local-swarm-discovery.rs

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
//! Wait for output that looks like the following:
66
//! $ cargo run --example local_swarm_discovery --features="discovery-local-network" -- connect [NODE_ID] [HASH] -o [FILE_PATH]
77
//! Run that command on another machine in the same local network, replacing [FILE_PATH] to the path on which you want to save the transferred content.
8-
use std::{path::PathBuf, sync::Arc};
8+
use std::path::PathBuf;
99

1010
use anyhow::ensure;
1111
use clap::{Parser, Subcommand};
@@ -15,8 +15,7 @@ use iroh::{
1515
node::DiscoveryConfig,
1616
};
1717
use iroh_blobs::{
18-
downloader::Downloader, net_protocol::Blobs, rpc::client::blobs::WrapOption,
19-
util::local_pool::LocalPool,
18+
net_protocol::Blobs, rpc::client::blobs::WrapOption, util::local_pool::LocalPool,
2019
};
2120
use tracing_subscriber::{prelude::*, EnvFilter};
2221

@@ -76,22 +75,10 @@ async fn main() -> anyhow::Result<()> {
7675
.build()
7776
.await?;
7877
let local_pool = LocalPool::default();
79-
let store = iroh_blobs::store::mem::Store::new();
80-
let downloader = Downloader::new(
81-
store.clone(),
82-
builder.endpoint().clone(),
83-
local_pool.handle().clone(),
84-
);
85-
let blobs = Arc::new(Blobs::new(
86-
store,
87-
local_pool.handle().clone(),
88-
Default::default(),
89-
downloader,
90-
builder.endpoint().clone(),
91-
));
92-
let blobs_client = blobs.clone().client();
93-
builder = builder.accept(iroh_blobs::protocol::ALPN.to_vec(), blobs);
78+
let blobs = Blobs::memory().build(&local_pool.handle(), builder.endpoint());
79+
builder = builder.accept(iroh_blobs::ALPN.to_vec(), blobs.clone());
9480
let node = builder.spawn().await?;
81+
let blobs_client = blobs.client();
9582

9683
match &cli.command {
9784
Commands::Accept { path } => {

0 commit comments

Comments
 (0)