Skip to content

Commit 11280b5

Browse files
author
“ramfox”
committed
rename LocalSwarmDiscovery to MdnsDiscovery
1 parent 40a196f commit 11280b5

File tree

4 files changed

+42
-46
lines changed

4 files changed

+42
-46
lines changed

iroh/examples/locally-discovered-nodes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
//! A small example showing how to get a list of nodes that were discovered via [`iroh::discovery::LocalSwarmDiscovery`]. LocalSwarmDiscovery uses [`swarm-discovery`](https://crates.io/crates/swarm-discovery) to discover other nodes in the local network ala mDNS.
1+
//! A small example showing how to get a list of nodes that were discovered via [`iroh::discovery::MdnsDiscovery`]. MdnsDiscovery uses [`swarm-discovery`](https://crates.io/crates/swarm-discovery), an opinionated implementation of mDNS to discover other nodes in the local network.
22
//!
33
//! 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::key::PublicKey]`s) it has discovered.
44
//!
5-
//! 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.
5+
//! 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 [`MdnsDiscovery`] enabled, it may discover those nodes as well.
66
use std::time::Duration;
77

88
use anyhow::Result;

iroh/src/discovery.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
//! - The [`PkarrResolver`] which can perform lookups from designated [pkarr relay servers]
3535
//! using HTTP.
3636
//!
37-
//! - [`LocalSwarmDiscovery`]: local_swarm_discovery::LocalSwarmDiscovery which is an mDNS
38-
//! implementation.
37+
//! - [`MdnsDiscovery`]: mdns::MdnsDiscovery which uses the crate `swarm-discovery`, an
38+
//! opinionated mDNS implementation, to discover nodes on the local network.
3939
//!
4040
//! - The [`DhtDiscovery`] also uses the [`pkarr`] system but can also publish and lookup
4141
//! records to/from the Mainline DHT.
@@ -69,14 +69,14 @@
6969
//! # }
7070
//! ```
7171
//!
72-
//! To also enable [`LocalSwarmDiscovery`] it can be added as another service in the
72+
//! To also enable [`MdnsDiscovery`] it can be added as another service in the
7373
//! [`ConcurrentDiscovery`]:
7474
//!
7575
//! ```no_run
7676
//! # #[cfg(feature = "discovery-local-network")]
7777
//! # {
7878
//! # use iroh::discovery::dns::DnsDiscovery;
79-
//! # use iroh::discovery::local_swarm_discovery::LocalSwarmDiscovery;
79+
//! # use iroh::discovery::mdns::MdnsDiscovery;
8080
//! # use iroh::discovery::pkarr::PkarrPublisher;
8181
//! # use iroh::discovery::ConcurrentDiscovery;
8282
//! # use iroh::SecretKey;
@@ -86,7 +86,7 @@
8686
//! let discovery = ConcurrentDiscovery::from_services(vec![
8787
//! Box::new(PkarrPublisher::n0_dns(secret_key.clone())),
8888
//! Box::new(DnsDiscovery::n0_dns()),
89-
//! Box::new(LocalSwarmDiscovery::new(secret_key.public())?),
89+
//! Box::new(MdnsDiscovery::new(secret_key.public())?),
9090
//! ]);
9191
//! # Ok(())
9292
//! # }
@@ -102,7 +102,7 @@
102102
//! [`PkarrPublisher`]: pkarr::PkarrPublisher
103103
//! [`DhtDiscovery`]: pkarr::dht::DhtDiscovery
104104
//! [pkarr relay servers]: https://pkarr.org/#servers
105-
//! [`LocalSwarmDiscovery`]: local_swarm_discovery::LocalSwarmDiscovery
105+
//! [`MdnsDiscovery`]: mdns::MdnsDiscovery
106106
//! [`StaticProvider`]: static_provider::StaticProvider
107107
108108
use std::sync::Arc;
@@ -126,7 +126,7 @@ use crate::Endpoint;
126126
pub mod dns;
127127

128128
#[cfg(feature = "discovery-local-network")]
129-
pub mod local_swarm_discovery;
129+
pub mod mdns;
130130
pub mod pkarr;
131131
pub mod static_provider;
132132

iroh/src/discovery/local_swarm_discovery.rs renamed to iroh/src/discovery/mdns.rs

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This allows you to use an mdns-like swarm discovery service to find address information about nodes that are on your local network, no relay or outside internet needed.
44
//! See the [`swarm-discovery`](https://crates.io/crates/swarm-discovery) crate for more details.
55
//!
6-
//! When [`LocalSwarmDiscovery`] is enabled, it's possible to get a list of the locally discovered nodes by filtering a list of `RemoteInfo`s.
6+
//! When [`MdnsDiscovery`] is enabled, it's possible to get a list of the locally discovered nodes by filtering a list of `RemoteInfo`s.
77
//!
88
//! ```
99
//! use std::time::Duration;
@@ -20,7 +20,7 @@
2020
//! .filter(|remote| {
2121
//! remote.sources().iter().any(|(source, duration)| {
2222
//! if let Source::Discovery { name } = source {
23-
//! name == iroh::discovery::local_swarm_discovery::NAME && *duration <= recent
23+
//! name == iroh::discovery::mdns::NAME && *duration <= recent
2424
//! } else {
2525
//! false
2626
//! }
@@ -72,7 +72,7 @@ const DISCOVERY_DURATION: Duration = Duration::from_secs(10);
7272

7373
/// Discovery using `swarm-discovery`, a variation on mdns
7474
#[derive(Debug)]
75-
pub struct LocalSwarmDiscovery {
75+
pub struct MdnsDiscovery {
7676
#[allow(dead_code)]
7777
handle: AbortOnDropHandle<()>,
7878
sender: mpsc::Sender<Message>,
@@ -128,8 +128,8 @@ impl Subscribers {
128128
}
129129
}
130130

131-
impl LocalSwarmDiscovery {
132-
/// Create a new [`LocalSwarmDiscovery`] Service.
131+
impl MdnsDiscovery {
132+
/// Create a new [`MdnsDiscovery`] Service.
133133
///
134134
/// This starts a [`Discoverer`] that broadcasts your addresses and receives addresses from other nodes in your local network.
135135
///
@@ -139,16 +139,12 @@ impl LocalSwarmDiscovery {
139139
/// # Panics
140140
/// This relies on [`tokio::runtime::Handle::current`] and will panic if called outside of the context of a tokio runtime.
141141
pub fn new(node_id: NodeId) -> Result<Self> {
142-
debug!("Creating new LocalSwarmDiscovery service");
142+
debug!("Creating new MdnsDiscovery service");
143143
let (send, mut recv) = mpsc::channel(64);
144144
let task_sender = send.clone();
145145
let rt = tokio::runtime::Handle::current();
146-
let discovery = LocalSwarmDiscovery::spawn_discoverer(
147-
node_id,
148-
task_sender.clone(),
149-
BTreeSet::new(),
150-
&rt,
151-
)?;
146+
let discovery =
147+
MdnsDiscovery::spawn_discoverer(node_id, task_sender.clone(), BTreeSet::new(), &rt)?;
152148

153149
let local_addrs: Watchable<Option<NodeData>> = Watchable::default();
154150
let mut addrs_change = local_addrs.watch();
@@ -162,16 +158,16 @@ impl LocalSwarmDiscovery {
162158
> = HashMap::default();
163159
let mut timeouts = JoinSet::new();
164160
loop {
165-
trace!(?node_addrs, "LocalSwarmDiscovery Service loop tick");
161+
trace!(?node_addrs, "MdnsDiscovery Service loop tick");
166162
let msg = tokio::select! {
167163
msg = recv.recv() => {
168164
msg
169165
}
170166
Ok(Some(data)) = addrs_change.updated() => {
171-
tracing::trace!(?data, "LocalSwarmDiscovery address changed");
167+
tracing::trace!(?data, "MdnsDiscovery address changed");
172168
discovery.remove_all();
173169
let addrs =
174-
LocalSwarmDiscovery::socketaddrs_to_addrs(data.direct_addresses());
170+
MdnsDiscovery::socketaddrs_to_addrs(data.direct_addresses());
175171
for addr in addrs {
176172
discovery.add(addr.0, addr.1)
177173
}
@@ -185,8 +181,8 @@ impl LocalSwarmDiscovery {
185181
};
186182
let msg = match msg {
187183
None => {
188-
error!("LocalSwarmDiscovery channel closed");
189-
error!("closing LocalSwarmDiscovery");
184+
error!("MdnsDiscovery channel closed");
185+
error!("closing MdnsDiscovery");
190186
timeouts.abort_all();
191187
return;
192188
}
@@ -197,7 +193,7 @@ impl LocalSwarmDiscovery {
197193
trace!(
198194
?discovered_node_id,
199195
?peer_info,
200-
"LocalSwarmDiscovery Message::Discovery"
196+
"MdnsDiscovery Message::Discovery"
201197
);
202198
let discovered_node_id = match PublicKey::from_str(&discovered_node_id) {
203199
Ok(node_id) => node_id,
@@ -217,7 +213,7 @@ impl LocalSwarmDiscovery {
217213
if peer_info.is_expiry() {
218214
trace!(
219215
?discovered_node_id,
220-
"removing node from LocalSwarmDiscovery address book"
216+
"removing node from MdnsDiscovery address book"
221217
);
222218
node_addrs.remove(&discovered_node_id);
223219
continue;
@@ -234,7 +230,7 @@ impl LocalSwarmDiscovery {
234230
debug!(
235231
?discovered_node_id,
236232
?peer_info,
237-
"adding node to LocalSwarmDiscovery address book"
233+
"adding node to MdnsDiscovery address book"
238234
);
239235

240236
let mut resolved = false;
@@ -258,7 +254,7 @@ impl LocalSwarmDiscovery {
258254
Message::Resolve(node_id, sender) => {
259255
let id = last_id + 1;
260256
last_id = id;
261-
trace!(?node_id, "LocalSwarmDiscovery Message::SendAddrs");
257+
trace!(?node_id, "MdnsDiscovery Message::SendAddrs");
262258
if let Some(peer_info) = node_addrs.get(&node_id) {
263259
let item = peer_to_discovery_item(peer_info, &node_id);
264260
debug!(?item, "sending DiscoveryItem");
@@ -282,7 +278,7 @@ impl LocalSwarmDiscovery {
282278
});
283279
}
284280
Message::Timeout(node_id, id) => {
285-
trace!(?node_id, "LocalSwarmDiscovery Message::Timeout");
281+
trace!(?node_id, "MdnsDiscovery Message::Timeout");
286282
if let Some(senders_for_node_id) = senders.get_mut(&node_id) {
287283
senders_for_node_id.remove(&id);
288284
if senders_for_node_id.is_empty() {
@@ -291,7 +287,7 @@ impl LocalSwarmDiscovery {
291287
}
292288
}
293289
Message::Subscribe(subscriber) => {
294-
trace!("LocalSwarmDiscovery Message::Subscribe");
290+
trace!("MdnsDiscovery Message::Subscribe");
295291
subscribers.push(subscriber);
296292
}
297293
}
@@ -316,7 +312,7 @@ impl LocalSwarmDiscovery {
316312
trace!(
317313
node_id,
318314
?peer,
319-
"Received peer information from LocalSwarmDiscovery"
315+
"Received peer information from MdnsDiscovery"
320316
);
321317

322318
let sender = sender.clone();
@@ -326,7 +322,7 @@ impl LocalSwarmDiscovery {
326322
sender.send(Message::Discovery(node_id, peer)).await.ok();
327323
});
328324
};
329-
let addrs = LocalSwarmDiscovery::socketaddrs_to_addrs(&socketaddrs);
325+
let addrs = MdnsDiscovery::socketaddrs_to_addrs(&socketaddrs);
330326
let node_id_str = data_encoding::BASE32_NOPAD
331327
.encode(node_id.as_bytes())
332328
.to_ascii_lowercase();
@@ -376,7 +372,7 @@ fn peer_to_discovery_item(peer: &Peer, node_id: &NodeId) -> DiscoveryItem {
376372
DiscoveryItem::new(node_info, NAME, None)
377373
}
378374

379-
impl Discovery for LocalSwarmDiscovery {
375+
impl Discovery for MdnsDiscovery {
380376
fn resolve(&self, _ep: Endpoint, node_id: NodeId) -> Option<BoxStream<Result<DiscoveryItem>>> {
381377
use futures_util::FutureExt;
382378

@@ -425,7 +421,7 @@ mod tests {
425421

426422
#[tokio::test]
427423
#[traced_test]
428-
async fn local_swarm_discovery_publish_resolve() -> TestResult {
424+
async fn mdns_publish_resolve() -> TestResult {
429425
let (_, discovery_a) = make_discoverer()?;
430426
let (node_id_b, discovery_b) = make_discoverer()?;
431427

@@ -459,7 +455,7 @@ mod tests {
459455

460456
#[tokio::test]
461457
#[traced_test]
462-
async fn local_swarm_discovery_subscribe() -> TestResult {
458+
async fn mdns_subscribe() -> TestResult {
463459
let num_nodes = 5;
464460
let mut node_ids = BTreeSet::new();
465461
let mut discoverers = vec![];
@@ -499,9 +495,9 @@ mod tests {
499495
Ok(())
500496
}
501497

502-
fn make_discoverer() -> Result<(PublicKey, LocalSwarmDiscovery)> {
498+
fn make_discoverer() -> Result<(PublicKey, MdnsDiscovery)> {
503499
let node_id = SecretKey::generate(rand::thread_rng()).public();
504-
Ok((node_id, LocalSwarmDiscovery::new(node_id)?))
500+
Ok((node_id, MdnsDiscovery::new(node_id)?))
505501
}
506502
}
507503
}

iroh/src/endpoint.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,14 @@ impl Builder {
385385
#[cfg(feature = "discovery-local-network")]
386386
/// Configures the endpoint to also use local network discovery.
387387
///
388-
/// This is equivalent to adding a [`crate::discovery::local_swarm_discovery::LocalSwarmDiscovery`]
389-
/// with default settings. Note that LocalSwarmDiscovery has various more advanced
388+
/// This is equivalent to adding a [`crate::discovery::mdns::MdnsDiscovery`]
389+
/// with default settings. Note that MdnsDiscovery has various more advanced
390390
/// configuration options. If you need any of those, you should manually
391-
/// create a LocalSwarmDiscovery and add it with [`Builder::add_discovery`].
391+
/// create a MdnsDiscovery and add it with [`Builder::add_discovery`].
392392
pub fn discovery_local_network(mut self) -> Self {
393-
use crate::discovery::local_swarm_discovery::LocalSwarmDiscovery;
393+
use crate::discovery::mdns::MdnsDiscovery;
394394
self.discovery.push(Box::new(|secret_key| {
395-
LocalSwarmDiscovery::new(secret_key.public())
395+
MdnsDiscovery::new(secret_key.public())
396396
.map(|x| Box::new(x) as _)
397397
.ok()
398398
}));
@@ -1011,7 +1011,7 @@ impl Endpoint {
10111011
/// through [`Discovery::resolve`], which is invoked automatically when calling
10121012
/// [`Endpoint::connect`] for a [`NodeId`] unknown to the endpoint. It also includes
10131013
/// nodes that the endpoint discovers passively from discovery services that implement
1014-
/// [`Discovery::subscribe`], which e.g. [`LocalSwarmDiscovery`] does.
1014+
/// [`Discovery::subscribe`], which e.g. [`MdnsDiscovery`] does.
10151015
///
10161016
/// The stream does not yield information about nodes that are added manually to the endpoint's
10171017
/// addressbook by calling [`Endpoint::add_node_addr`] or by supplying a full [`NodeAddr`] to
@@ -1026,7 +1026,7 @@ impl Endpoint {
10261026
/// See also [`Endpoint::remote_info_iter`], which returns an iterator over all remotes
10271027
/// the endpoint knows about at a specific point in time.
10281028
///
1029-
/// [`LocalSwarmDiscovery`]: crate::discovery::local_swarm_discovery::LocalSwarmDiscovery
1029+
/// [`MdnsDiscovery`]: crate::discovery::mdns::MdnsDiscovery
10301030
/// [`StaticProvider`]: crate::discovery::static_provider::StaticProvider
10311031
pub fn discovery_stream(&self) -> impl Stream<Item = Result<DiscoveryItem, Lagged>> {
10321032
self.msock.discovery_subscribers().subscribe()

0 commit comments

Comments
 (0)