1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
#![doc = include_str!("../README.md")]
mod guardian;
mod receiver;
mod sender;
mod socket;
mod updater;
use acto::{AcTokio, ActoHandle, ActoRuntime, TokioJoinHandle};
use anyhow::Context;
use hickory_proto::rr::Name;
use serde::{Deserialize, Serialize};
use socket::Sockets;
use std::{
collections::BTreeMap,
fmt::Display,
net::IpAddr,
str::FromStr,
time::{Duration, Instant},
};
use tokio::runtime::Handle;
type Callback = Box<dyn FnMut(&str, &Peer) + Send + 'static>;
/// Builder for a swarm discovery service.
///
/// # Example
///
/// ```rust
/// use if_addrs::get_if_addrs;
/// use swarm_discovery::Discoverer;
/// use tokio::runtime::Builder;
///
/// // create Tokio runtime
/// let rt = Builder::new_multi_thread()
/// .enable_all()
/// .build()
/// .expect("build runtime");
///
/// // make up some peer ID
/// let peer_id = "peer_id42".to_owned();
///
/// // get local addresses and make up some port
/// let addrs = get_if_addrs().unwrap().into_iter().map(|i| i.addr.ip()).collect::<Vec<_>>();
/// let port = 1234;
///
/// // start announcing and discovering
/// let _guard = Discoverer::new("swarm".to_owned(), peer_id)
/// .with_addrs(port, addrs)
/// .with_callback(|peer_id, peer| {
/// println!("discovered {}: {:?}", peer_id, peer);
/// })
/// .spawn(rt.handle())
/// .expect("discoverer spawn");
/// ```
pub struct Discoverer {
name: String,
protocol: Protocol,
peer_id: String,
peers: BTreeMap<String, Peer>,
callback: Callback,
tau: Duration,
phi: f32,
class: IpClass,
}
/// A peer discovered by the swarm discovery service.
///
/// The discovery yields service instances, which are located by a port and a list of IP addresses.
/// Both IPv4 and IPv6 addresses may be present, depending on the configuration via [Discoverer::with_ip_class].
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Peer {
pub port: u16,
pub addrs: Vec<IpAddr>,
pub last_seen: Instant,
}
/// This selects which sockets will be created by the [Discoverer].
///
/// Responses will be sent on that socket which received the query.
/// Queries will prefer v4 when available.
/// Default is [IpClass::V4AndV6].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum IpClass {
V4Only,
V6Only,
#[default]
V4AndV6,
}
impl IpClass {
/// Returns `true` if IPv4 is enabled.
pub fn has_v4(self) -> bool {
matches!(self, Self::V4Only | Self::V4AndV6)
}
/// Returns `true` if IPv6 is enabled.
pub fn has_v6(self) -> bool {
matches!(self, Self::V6Only | Self::V4AndV6)
}
}
/// This selects which protocol suffix to use for the service name.
///
/// Default is [Protocol::Udp].
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum Protocol {
#[default]
Udp,
Tcp,
}
impl Display for Protocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Protocol::Udp => write!(f, "_udp"),
Protocol::Tcp => write!(f, "_tcp"),
}
}
}
impl Discoverer {
/// Creates a new builder for a swarm discovery service.
///
/// The `name` is the name of the mDNS service, meaning that it will be discoverable under the name `_name._udp.local.`.
/// The `peer_id` is the unique identifier of this peer, which will be discoverable under the name `peer_id._name._udp.local.`.
pub fn new(name: String, peer_id: String) -> Self {
Self {
name,
protocol: Protocol::default(),
peer_id,
peers: BTreeMap::new(),
callback: Box::new(|_, _| {}),
tau: Duration::from_secs(10),
phi: 1.0,
class: IpClass::default(),
}
}
/// Set the protocol suffix to use for the service name.
///
/// Note that this does not change the protocol used for discovery, which is always UDP-based mDNS.
/// Default is [Protocol::Udp].
pub fn with_protocol(mut self, protocol: Protocol) -> Self {
self.protocol = protocol;
self
}
/// Register the local peer’s port and IP addresses.
///
/// If this method is not called, the local peer will not advertise itself.
/// It can still discover others.
pub fn with_addrs(mut self, port: u16, mut addrs: Vec<IpAddr>) -> Self {
addrs.sort();
self.peers.insert(
self.peer_id.clone(),
Peer {
port,
addrs,
last_seen: Instant::now(),
},
);
self
}
/// Register a callback to be called when a peer is discovered or its addresses change.
///
/// When a peer is removed, the callback will be called with an empty list of addresses.
/// This happens after not receiving any responses for a time period greater than three
/// times the estimated swarm size divided by the response frequency.
pub fn with_callback(mut self, callback: impl FnMut(&str, &Peer) + Send + 'static) -> Self {
self.callback = Box::new(callback);
self
}
/// Set the discovery time target.
///
/// After roughly this time a new peer should have discovered some parts of the swarm.
/// The default is 10 seconds.
pub fn with_cadence(mut self, tau: Duration) -> Self {
self.tau = tau;
self
}
/// Set the response frequency target.
///
/// While query-response cycles follow the configured cadence (see [Discoverer::with_cadence]),
/// the response rate determines the (soft) maximum of how many responses should be received per second.
///
/// With cadence 10sec, setting this to 1.0Hz means that at most 10 responses will be received per cycle.
/// Setting it to 0.5Hz means that up to roughly 5 responses will be received per cycle.
///
/// The default is 1.0Hz.
pub fn with_response_rate(mut self, phi: f32) -> Self {
self.phi = phi;
self
}
/// Set which IP classes to use.
///
/// The default is to use both IPv4 and IPv6, where IPv4 is preferred for sending queries.
/// Responses will be sent using that class which the query used.
pub fn with_ip_class(mut self, class: IpClass) -> Self {
self.class = class;
self
}
/// Start the discovery service.
///
/// This will spawn asynchronous tasks and return a guard which will stop the discovery when dropped.
/// Changing the configuration is done by stopping the discovery and starting a new one.
pub fn spawn(self, handle: &Handle) -> anyhow::Result<DropGuard> {
let _entered = handle.enter();
let sockets = Sockets::new(self.class)?;
let service_name = Name::from_str(&format!("_{}.{}.local.", self.name, self.protocol))
.context("constructing service name")?;
// need to test this here so it won’t fail in the actor
Name::from_str(&self.peer_id)
.context("constructing name from peer ID")?
.append_domain(&service_name)
.context("appending service name to peer ID")?;
let rt = AcTokio::from_handle("swarm-discovery", handle.clone());
let task = rt
.spawn_actor("guardian", move |ctx| {
guardian::guardian(ctx, self, sockets, service_name)
})
.handle;
Ok(DropGuard {
task: Some(task),
_rt: rt,
})
}
}
/// A guard which will keep the discovery running until it is dropped.
#[must_use = "dropping this value will stop the mDNS discovery"]
pub struct DropGuard {
task: Option<TokioJoinHandle<()>>,
_rt: AcTokio,
}
impl Drop for DropGuard {
fn drop(&mut self) {
self.task.take().unwrap().abort();
}
}