1
- //! A static discovery implementation that allows adding info for nodes manually.
1
+ //! A static node discovery to manually add node addressing information.
2
+ //!
3
+ //! Often an application might get node addressing information out-of-band in an
4
+ //! application-specific way. [`NodeTicket`]'s are one common way used to achieve this.
5
+ //! This "static" addressing information is often only usable for a limited time so needs to
6
+ //! be able to be removed again once know it is no longer useful.
7
+ //!
8
+ //! This is where the [`StaticProvider`] is useful: it allows applications to add and
9
+ //! retract node addressing information that is otherwise out-of-band to iroh.
10
+ //!
11
+ //! [`NodeTicket`]: https://docs.rs/iroh-base/latest/iroh_base/ticket/struct.NodeTicket
12
+
2
13
use std:: {
3
14
collections:: { btree_map:: Entry , BTreeMap , BTreeSet } ,
4
15
net:: SocketAddr ,
@@ -11,7 +22,48 @@ use iroh_base::{NodeAddr, NodeId, RelayUrl};
11
22
12
23
use super :: { Discovery , DiscoveryItem } ;
13
24
14
- /// A static discovery implementation that allows providing info for nodes manually.
25
+ /// A static node discovery to manually add node addressing information.
26
+ ///
27
+ /// Often an application might get node addressing information out-of-band in an
28
+ /// application-specific way. [`NodeTicket`]'s are one common way used to achieve this.
29
+ /// This "static" addressing information is often only usable for a limited time so needs to
30
+ /// be able to be removed again once know it is no longer useful.
31
+ ///
32
+ /// This is where the [`StaticProvider`] is useful: it allows applications to add and
33
+ /// retract node addressing information that is otherwise out-of-band to iroh.
34
+ ///
35
+ /// # Examples
36
+ ///
37
+ /// ```rust
38
+ /// use iroh::{discovery::static_provider::StaticProvider, Endpoint, NodeAddr};
39
+ /// use iroh_base::SecretKey;
40
+ ///
41
+ /// # #[tokio::main]
42
+ /// # async fn main() -> anyhow::Result<()> {
43
+ /// // Create the discovery service and endpoint.
44
+ /// let discovery = StaticProvider::new();
45
+ ///
46
+ /// let _ep = Endpoint::builder()
47
+ /// .add_discovery({
48
+ /// let discovery = discovery.clone();
49
+ /// move |_| Some(discovery)
50
+ /// })
51
+ /// .bind()
52
+ /// .await?;
53
+ ///
54
+ /// /// Sometime later add a RelayUrl for a fake NodeId.
55
+ /// let key = SecretKey::from_bytes(&[0u8; 32]); // Do not use fake secret keys!
56
+ /// discovery.add_node_addr(NodeAddr {
57
+ /// node_id: key.public(),
58
+ /// relay_url: Some("https://example.com".parse()?),
59
+ /// direct_addresses: Default::default(),
60
+ /// });
61
+ ///
62
+ /// # Ok(())
63
+ /// # }
64
+ /// ```
65
+ ///
66
+ /// [`NodeTicket`]: https://docs.rs/iroh-base/latest/iroh_base/ticket/struct.NodeTicket
15
67
#[ derive( Debug , Default , Clone ) ]
16
68
#[ repr( transparent) ]
17
69
pub struct StaticProvider {
@@ -27,16 +79,22 @@ struct NodeInfo {
27
79
28
80
impl StaticProvider {
29
81
/// The provenance string for this discovery implementation.
82
+ ///
83
+ /// This is mostly used for debugging information and allows understanding the origin of
84
+ /// addressing information used by an iroh [`Endpoint`].
85
+ ///
86
+ /// [`Endpoint`]: crate::Endpoint
30
87
pub const PROVENANCE : & ' static str = "static_discovery" ;
31
88
32
- /// Create a new static discovery instance.
89
+ /// Creates a new static discovery instance.
33
90
pub fn new ( ) -> Self {
34
91
Self :: default ( )
35
92
}
36
93
37
- /// Creates a static discovery instance from something that can be converted into node addresses.
94
+ /// Creates a static discovery instance from node addresses.
95
+ ///
96
+ /// # Examples
38
97
///
39
- /// Example:
40
98
/// ```rust
41
99
/// use std::{net::SocketAddr, str::FromStr};
42
100
///
@@ -68,7 +126,7 @@ impl StaticProvider {
68
126
res
69
127
}
70
128
71
- /// Add node info for the given node id .
129
+ /// Sets node addressing information for the given node ID .
72
130
///
73
131
/// This will completely overwrite any existing info for the node.
74
132
pub fn set_node_addr ( & self , info : impl Into < NodeAddr > ) -> Option < NodeAddr > {
@@ -90,9 +148,11 @@ impl StaticProvider {
90
148
} )
91
149
}
92
150
93
- /// Add node info for the given node id, combining it with any existing info .
151
+ /// Augments node addressing information for the given node ID .
94
152
///
95
- /// This will add any new direct addresses and overwrite the relay url.
153
+ /// The provided addressing information is combined with the existing info in the static
154
+ /// provider. Any new direct addresses are added to those already present while the
155
+ /// relay URL is overwritten.
96
156
pub fn add_node_addr ( & self , info : impl Into < NodeAddr > ) {
97
157
let info: NodeAddr = info. into ( ) ;
98
158
let last_updated = SystemTime :: now ( ) ;
@@ -114,7 +174,7 @@ impl StaticProvider {
114
174
}
115
175
}
116
176
117
- /// Get node info for the given node id .
177
+ /// Returns node addressing information for the given node ID .
118
178
pub fn get_node_addr ( & self , node_id : NodeId ) -> Option < NodeAddr > {
119
179
let guard = self . nodes . read ( ) . expect ( "poisoned" ) ;
120
180
let info = guard. get ( & node_id) ?;
@@ -125,7 +185,9 @@ impl StaticProvider {
125
185
} )
126
186
}
127
187
128
- /// Remove node info for the given node id.
188
+ /// Removes all node addressing information for the given node ID.
189
+ ///
190
+ /// Any removed information is returned.
129
191
pub fn remove_node_addr ( & self , node_id : NodeId ) -> Option < NodeAddr > {
130
192
let mut guard = self . nodes . write ( ) . expect ( "poisoned" ) ;
131
193
let info = guard. remove ( & node_id) ?;
0 commit comments