-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description Adds a `subscribe` method to the `Discovery` trait that returns an `Option<BoxStream>`. The `subscribe` method will send `DiscoveryItems` each time the discovery service discovers a remote node. The magicsock is now subscribed to the discovery service and updates the internal address book each time it receives a `DiscoveryItem`. The source is marked as `Source::Discovery{ service: String }` and the `Instant` that the magicsock received the information. Users can now filter their list of `RemoteInfo`s for sources. ## Breaking Changes - struct `RemoteInfo` now has field `sources`, which is a `Vec` of `(iroh::net::endpoint::Source, Duration)`. The `Source` is how we heard about the remote, and the `Duration` is how long ago we heard about it. The `sources` field is ordered from the first time we heard about the remote node to the most recent time we learned about the remote node, in this session. ## Change checklist - [x] Self-review. - [x] Documentation updates following the [style guide](https://rust-lang.github.io/rfcs/1574-more-api-documentation-conventions.html#appendix-a-full-conventions-text), if relevant. - [x] Tests if relevant. - [x] All breaking changes documented. --------- Co-authored-by: Kasey Huizinga <[email protected]> Co-authored-by: Divma <[email protected]> Co-authored-by: Floris Bruynooghe <[email protected]>
- Loading branch information
1 parent
25c8305
commit 06c0844
Showing
16 changed files
with
553 additions
and
106 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//! A small example showing how to get a list of nodes that were discovered via [`iroh_net::discovery::LocalSwarmDiscovery`]. LocalSwarmDiscovery uses [`swarm-discovery`](https://crates.io/crates/swarm-discovery) to discover other nodes in the local network ala mDNS. | ||
//! | ||
//! This example creates an iroh endpoint, a few additional iroh endpoints to discover, waits a few seconds, and reports all of the iroh NodeIds (also called `[iroh_net::key::PublicKey]`s) it has discovered. | ||
//! | ||
//! This is an async, non-determinate process, so the number of NodeIDs discovered each time may be different. If you have other iroh endpoints or iroh nodes with [`LocalSwarmDiscovery`] enabled, it may discover those nodes as well. | ||
use iroh_net::{ | ||
discovery::local_swarm_discovery::LocalSwarmDiscovery, endpoint::Source, key::SecretKey, | ||
Endpoint, | ||
}; | ||
use std::time::Duration; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<()> { | ||
tracing_subscriber::fmt::init(); | ||
println!("locally discovered nodes example!\n"); | ||
let key = SecretKey::generate(); | ||
let id = key.public(); | ||
println!("creating endpoint {id:?}\n"); | ||
let ep = Endpoint::builder() | ||
.secret_key(key) | ||
.discovery(Box::new(LocalSwarmDiscovery::new(id)?)) | ||
.bind() | ||
.await?; | ||
|
||
let node_count = 5; | ||
println!("creating {node_count} additional endpoints to discover locally:"); | ||
let mut discoverable_eps = Vec::with_capacity(node_count); | ||
for _ in 0..node_count { | ||
let key = SecretKey::generate(); | ||
let id = key.public(); | ||
println!("\t{id:?}"); | ||
let ep = Endpoint::builder() | ||
.secret_key(key) | ||
.discovery(Box::new(LocalSwarmDiscovery::new(id)?)) | ||
.bind() | ||
.await?; | ||
discoverable_eps.push(ep); | ||
} | ||
|
||
let duration = Duration::from_secs(3); | ||
println!("\nwaiting {duration:?} to allow discovery to occur...\n"); | ||
tokio::time::sleep(duration).await; | ||
|
||
// get an iterator of all the remote nodes this endpoint knows about | ||
let remotes = ep.remote_info_iter(); | ||
// filter that list down to the nodes that have a `Source::Discovery` with | ||
// the `service` name [`iroh_net::discovery::local_swarm_discovery::NAME`] | ||
// If you have a long running node and want to only get the nodes that were | ||
// discovered recently, you can also filter on the `Duration` of the source, | ||
// which indicates how long ago we got information from that source. | ||
let locally_discovered: Vec<_> = remotes | ||
.filter(|remote| { | ||
remote.sources().iter().any(|(source, _duration)| { | ||
if let Source::Discovery { name } = source { | ||
name == iroh_net::discovery::local_swarm_discovery::NAME | ||
} else { | ||
false | ||
} | ||
}) | ||
}) | ||
.map(|remote| remote.node_id) | ||
.collect(); | ||
|
||
println!("found:"); | ||
for id in locally_discovered { | ||
println!("\t{id:?}"); | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.