Skip to content

Commit 2d367f9

Browse files
dignifiedquireramfox“ramfox”
authored
feat(iroh): introduce endpoint presets (#3523)
## Description Closes #3470 ## Breaking Changes - removed - `iroh::endpoint::Builder::n0_discovery` - `iroh::endpoint::Builder::discovery_dht` - `iroh::endpoint::Builder::discovery_local_network` - `iroh::endpoint::Builder::discovery` - `iroh::discovery::DiscoveryContext` - `iroh::endpoint::Builder::default` - added - `iroh::Endpoint::bind` - this allows to immediately create an `Endpoint` with the defaults and binds it - `iroh::endpoint::Endpoint::empty_builder` - `iroh::endpoint::presets` - new module, defining a trait on how to preconfigure and `Endpoint` - `iroh::endpoint::Builder::new` - creates a new builder with the `N0` preset - `iroh::endpoint::Builder::preset` - `iroh::endpoint::Builder::empty` - creates a new builder, completely empty (no discovery, no relay map) - changed - `iroh::'Endpoint::add_discovery` is now called just `discovery` - `iroh::discovery::IntoDiscovery` now takes an `Endpoint` instead of a `DiscoveryContext` --------- Co-authored-by: ramfox <[email protected]> Co-authored-by: “ramfox” <“[email protected]”>
1 parent b7baaeb commit 2d367f9

File tree

19 files changed

+346
-434
lines changed

19 files changed

+346
-434
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Install it using `cargo add iroh`, then on the connecting side:
6060
```rs
6161
const ALPN: &[u8] = b"iroh-example/echo/0";
6262

63-
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
63+
let endpoint = Endpoint::bind().await?;
6464

6565
// Open a connection to the accepting endpoint
6666
let conn = endpoint.connect(addr, ALPN).await?;
@@ -85,7 +85,7 @@ endpoint.close().await;
8585

8686
And on the accepting side:
8787
```rs
88-
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
88+
let endpoint = Endpoint::bind().await?;
8989

9090
let router = Router::builder(endpoint)
9191
.accept(ALPN.to_vec(), Arc::new(Echo))

iroh/examples/echo-no-router.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async fn main() -> Result<()> {
3333
}
3434

3535
async fn connect_side(addr: EndpointAddr) -> Result<()> {
36-
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
36+
let endpoint = Endpoint::bind().await?;
3737

3838
// Open a connection to the accepting endpoint
3939
let conn = endpoint.connect(addr, ALPN).await?;
@@ -68,7 +68,6 @@ async fn connect_side(addr: EndpointAddr) -> Result<()> {
6868

6969
async fn start_accept_side() -> Result<Endpoint> {
7070
let endpoint = Endpoint::builder()
71-
.discovery_n0()
7271
// The accept side needs to opt-in to the protocols it accepts,
7372
// as any connection attempts that can't be found with a matching ALPN
7473
// will be rejected.

iroh/examples/echo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ async fn main() -> Result<()> {
3535
}
3636

3737
async fn connect_side(addr: EndpointAddr) -> Result<()> {
38-
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
38+
let endpoint = Endpoint::bind().await?;
3939

4040
// Open a connection to the accepting endpoint
4141
let conn = endpoint.connect(addr, ALPN).await?;
@@ -68,7 +68,7 @@ async fn connect_side(addr: EndpointAddr) -> Result<()> {
6868
}
6969

7070
async fn start_accept_side() -> Result<Router> {
71-
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
71+
let endpoint = Endpoint::bind().await?;
7272

7373
// Build our protocol handler and add our protocol, identified by its ALPN, and spawn the endpoint.
7474
let router = Router::builder(endpoint).accept(ALPN, Echo).spawn();

iroh/examples/locally-discovered-nodes.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ async fn main() -> Result<()> {
1919
tracing_subscriber::fmt::init();
2020
println!("Discovering Local Endpoints Example!");
2121

22-
let ep = Endpoint::builder().bind().await?;
22+
let ep = Endpoint::bind().await?;
2323
let endpoint_id = ep.id();
2424

2525
let mdns = MdnsDiscovery::builder().build(endpoint_id)?;
@@ -68,7 +68,8 @@ async fn main() -> Result<()> {
6868
for _ in 0..endpoint_count {
6969
let ud = user_data.clone();
7070
set.spawn(async move {
71-
let ep = Endpoint::builder().discovery_local_network().bind().await?;
71+
let ep = Endpoint::bind().await?;
72+
ep.discovery().add(MdnsDiscovery::builder().build(ep.id())?);
7273
ep.set_user_data_for_discovery(Some(ud));
7374
tokio::time::sleep(Duration::from_secs(3)).await;
7475
ep.close().await;

iroh/examples/screening-connection.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ async fn main() -> Result<()> {
4646
}
4747

4848
async fn connect_side(addr: &EndpointAddr) -> Result<()> {
49-
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
49+
let endpoint = Endpoint::bind().await?;
5050

5151
// Open a connection to the accepting endpoint
5252
let conn = endpoint.connect(addr.clone(), ALPN).await?;
@@ -79,7 +79,7 @@ async fn connect_side(addr: &EndpointAddr) -> Result<()> {
7979
}
8080

8181
async fn start_accept_side() -> Result<Router> {
82-
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
82+
let endpoint = Endpoint::bind().await?;
8383

8484
let echo = ScreenedEcho {
8585
conn_attempt_count: Arc::new(AtomicU64::new(0)),

iroh/examples/search.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ async fn main() -> Result<()> {
7575
let args = Cli::parse();
7676

7777
// Build an endpoint
78-
let endpoint = Endpoint::builder().discovery_n0().bind().await?;
78+
let endpoint = Endpoint::bind().await?;
7979

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

iroh/examples/transfer.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -220,27 +220,14 @@ impl EndpointArgs {
220220
let url = self
221221
.pkarr_relay_url
222222
.unwrap_or_else(|| self.env.pkarr_relay_url());
223-
builder = builder.add_discovery(PkarrPublisher::builder(url));
223+
builder = builder.discovery(PkarrPublisher::builder(url));
224224
}
225225

226226
if !self.no_dns_resolve {
227227
let domain = self
228228
.dns_origin_domain
229229
.unwrap_or_else(|| self.env.dns_origin_domain());
230-
builder = builder.add_discovery(DnsDiscovery::builder(domain));
231-
}
232-
233-
if self.mdns {
234-
#[cfg(feature = "discovery-local-network")]
235-
{
236-
builder = builder.discovery_local_network();
237-
}
238-
#[cfg(not(feature = "discovery-local-network"))]
239-
{
240-
snafu::whatever!(
241-
"Must have the `test-utils` feature enabled when using the `--relay-only` flag"
242-
);
243-
}
230+
builder = builder.discovery(DnsDiscovery::builder(domain));
244231
}
245232

246233
if self.relay_only {
@@ -270,6 +257,23 @@ impl EndpointArgs {
270257

271258
let endpoint = builder.alpns(vec![TRANSFER_ALPN.to_vec()]).bind().await?;
272259

260+
if self.mdns {
261+
#[cfg(feature = "discovery-local-network")]
262+
{
263+
use iroh::discovery::mdns::MdnsDiscovery;
264+
265+
endpoint
266+
.discovery()
267+
.add(MdnsDiscovery::builder().build(endpoint.id())?);
268+
}
269+
#[cfg(not(feature = "discovery-local-network"))]
270+
{
271+
snafu::whatever!(
272+
"Must have the `test-utils` feature enabled when using the `--relay-only` flag"
273+
);
274+
}
275+
}
276+
273277
let endpoint_id = endpoint.id();
274278
println!("Our endpoint id:\n\t{endpoint_id}");
275279

0 commit comments

Comments
 (0)