|
2 | 2 |
|
3 | 3 | use std::{collections::BTreeMap, fmt, sync::Arc}; |
4 | 4 |
|
5 | | -use anyhow::{ensure, Result}; |
6 | 5 | use iroh_base::RelayUrl; |
7 | 6 | use serde::{Deserialize, Serialize}; |
8 | 7 |
|
@@ -52,50 +51,53 @@ impl RelayMap { |
52 | 51 | pub fn is_empty(&self) -> bool { |
53 | 52 | self.nodes.is_empty() |
54 | 53 | } |
| 54 | +} |
55 | 55 |
|
56 | | - /// Creates a new [`RelayMap`] with a single relay server configured. |
57 | | - /// |
58 | | - /// Allows to set a custom STUN port and different IP addresses for IPv4 and IPv6. |
59 | | - /// If IP addresses are provided, no DNS lookup will be performed. |
60 | | - /// |
61 | | - /// Sets the port to the default [`DEFAULT_RELAY_QUIC_PORT`]. |
62 | | - pub fn default_from_node(url: RelayUrl, stun_port: u16) -> Self { |
63 | | - let mut nodes = BTreeMap::new(); |
64 | | - nodes.insert( |
65 | | - url.clone(), |
66 | | - RelayNode { |
67 | | - url, |
68 | | - stun_only: false, |
69 | | - stun_port, |
70 | | - quic: Some(RelayQuicConfig::default()), |
71 | | - } |
72 | | - .into(), |
73 | | - ); |
74 | | - |
75 | | - RelayMap { |
76 | | - nodes: Arc::new(nodes), |
| 56 | +impl FromIterator<RelayNode> for RelayMap { |
| 57 | + fn from_iter<T: IntoIterator<Item = RelayNode>>(iter: T) -> Self { |
| 58 | + Self { |
| 59 | + nodes: Arc::new( |
| 60 | + iter.into_iter() |
| 61 | + .map(|node| (node.url.clone(), Arc::new(node))) |
| 62 | + .collect(), |
| 63 | + ), |
77 | 64 | } |
78 | 65 | } |
| 66 | +} |
79 | 67 |
|
80 | | - /// Returns a [`RelayMap`] from a [`RelayUrl`]. |
| 68 | +impl From<RelayUrl> for RelayMap { |
| 69 | + /// Creates a [`RelayMap`] from a [`RelayUrl`]. |
81 | 70 | /// |
82 | | - /// This will use the default STUN port, the default QUIC port |
83 | | - /// (as defined by the `iroh-relay` crate) and IP addresses |
84 | | - /// resolved from the URL's host name via DNS. |
85 | | - /// relay nodes are specified at <../../docs/relay_nodes.md> |
86 | | - pub fn from_url(url: RelayUrl) -> Self { |
87 | | - Self::default_from_node(url, DEFAULT_STUN_PORT) |
| 71 | + /// The [`RelayNode`]s in the [`RelayMap`] will have the default STUN and QUIC address |
| 72 | + /// discovery ports. |
| 73 | + fn from(value: RelayUrl) -> Self { |
| 74 | + Self { |
| 75 | + nodes: Arc::new([(value.clone(), Arc::new(value.into()))].into()), |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +impl From<RelayNode> for RelayMap { |
| 81 | + fn from(value: RelayNode) -> Self { |
| 82 | + Self { |
| 83 | + nodes: Arc::new([(value.url.clone(), Arc::new(value))].into()), |
| 84 | + } |
88 | 85 | } |
| 86 | +} |
89 | 87 |
|
90 | | - /// Constructs the [`RelayMap`] from an iterator of [`RelayNode`]s. |
91 | | - pub fn from_nodes<I: Into<Arc<RelayNode>>>(value: impl IntoIterator<Item = I>) -> Result<Self> { |
92 | | - let mut map = BTreeMap::new(); |
93 | | - for node in value.into_iter() { |
94 | | - let node = node.into(); |
95 | | - ensure!(!map.contains_key(&node.url), "Duplicate node url"); |
96 | | - map.insert(node.url.clone(), node); |
| 88 | +impl FromIterator<RelayUrl> for RelayMap { |
| 89 | + /// Creates a [`RelayMap`] from an iterator of [`RelayUrl`]. |
| 90 | + /// |
| 91 | + /// The [`RelayNode`]s in the [`RelayMap`] will have the default STUN and QUIC address |
| 92 | + /// discovery ports. |
| 93 | + fn from_iter<T: IntoIterator<Item = RelayUrl>>(iter: T) -> Self { |
| 94 | + Self { |
| 95 | + nodes: Arc::new( |
| 96 | + iter.into_iter() |
| 97 | + .map(|url| (url.clone(), Arc::new(url.into()))) |
| 98 | + .collect(), |
| 99 | + ), |
97 | 100 | } |
98 | | - Ok(RelayMap { nodes: map.into() }) |
99 | 101 | } |
100 | 102 | } |
101 | 103 |
|
@@ -131,6 +133,17 @@ pub struct RelayNode { |
131 | 133 | pub quic: Option<RelayQuicConfig>, |
132 | 134 | } |
133 | 135 |
|
| 136 | +impl From<RelayUrl> for RelayNode { |
| 137 | + fn from(value: RelayUrl) -> Self { |
| 138 | + Self { |
| 139 | + url: value, |
| 140 | + stun_only: false, |
| 141 | + stun_port: DEFAULT_STUN_PORT, |
| 142 | + quic: quic_config(), |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
134 | 147 | fn quic_config() -> Option<RelayQuicConfig> { |
135 | 148 | Some(RelayQuicConfig::default()) |
136 | 149 | } |
|
0 commit comments