Struct swarm_discovery::Discoverer

source ·
pub struct Discoverer { /* private fields */ }
Expand description

Builder for a swarm discovery service.

§Example

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");

Implementations§

source§

impl Discoverer

source

pub fn new(name: String, peer_id: String) -> Self

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..

source

pub fn new_interactive(name: String, peer_id: String) -> Self

Creates a new builder with default cadence and response rate for human interactive applications.

This sets τ=0.7sec and φ=2.5, see Discoverer::new for the name and peer_id arguments.

source

pub fn with_protocol(self, protocol: Protocol) -> Self

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.

source

pub fn with_addrs( self, port: u16, addrs: impl IntoIterator<Item = IpAddr>, ) -> Self

Register the local peer’s port and IP addresses, may be called multiple times with additive effect.

If this method is not called, the local peer will not advertise itself. It can still discover others.

source

pub fn with_callback( self, callback: impl FnMut(&str, &Peer) + Send + 'static, ) -> 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.

source

pub fn with_cadence(self, tau: Duration) -> Self

Set the discovery time target.

After roughly this time a new peer should have discovered some parts of the swarm. The worst-case latency is 1.2•τ.

Note that the product τ•φ must be greater than 1 for the rate limiting to work correctly. For human interactive applications it is recommended to set τ=0.7s and φ=2.5 (see Discoverer::new_interactive).

The default is 10 seconds.

source

pub fn with_response_rate(self, phi: f32) -> Self

Set the response frequency target in Hz.

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.

Note that the product τ•φ must be greater than 1 for the rate limiting to work correctly. For human interactive applications it is recommended to set τ=0.7s and φ=2.5 (see Discoverer::new_interactive).

The default is 1.0Hz.

source

pub fn with_ip_class(self, class: IpClass) -> 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.

source

pub fn spawn(self, handle: &Handle) -> Result<DropGuard>

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.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more