pub struct Swarm<TBehaviour>where
TBehaviour: NetworkBehaviour,{ /* private fields */ }
Expand description
Contains the state of the network, plus the way it should behave.
Note: Needs to be polled via <Swarm as Stream>
in order to make
progress.
Implementations§
source§impl<TBehaviour> Swarm<TBehaviour>where
TBehaviour: NetworkBehaviour,
impl<TBehaviour> Swarm<TBehaviour>where TBehaviour: NetworkBehaviour,
sourcepub fn new(
transport: Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
local_peer_id: PeerId
) -> Swarm<TBehaviour>
👎Deprecated since 0.41.0: This constructor is considered ambiguous regarding the executor. Use one of the new, executor-specific constructors or Swarm::with_threadpool_executor
for the same behaviour.
pub fn new( transport: Boxed<(PeerId, StreamMuxerBox)>, behaviour: TBehaviour, local_peer_id: PeerId ) -> Swarm<TBehaviour>
Swarm::with_threadpool_executor
for the same behaviour.Builds a new Swarm
.
sourcepub fn with_executor(
transport: Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
local_peer_id: PeerId,
executor: impl Executor + Send + 'static
) -> Swarm<TBehaviour>
pub fn with_executor( transport: Boxed<(PeerId, StreamMuxerBox)>, behaviour: TBehaviour, local_peer_id: PeerId, executor: impl Executor + Send + 'static ) -> Swarm<TBehaviour>
Builds a new Swarm
with a provided executor.
sourcepub fn with_tokio_executor(
transport: Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
local_peer_id: PeerId
) -> Swarm<TBehaviour>
pub fn with_tokio_executor( transport: Boxed<(PeerId, StreamMuxerBox)>, behaviour: TBehaviour, local_peer_id: PeerId ) -> Swarm<TBehaviour>
Builds a new Swarm
with a tokio executor.
sourcepub fn with_threadpool_executor(
transport: Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
local_peer_id: PeerId
) -> Swarm<TBehaviour>
pub fn with_threadpool_executor( transport: Boxed<(PeerId, StreamMuxerBox)>, behaviour: TBehaviour, local_peer_id: PeerId ) -> Swarm<TBehaviour>
Builds a new Swarm
with a threadpool executor.
sourcepub fn without_executor(
transport: Boxed<(PeerId, StreamMuxerBox)>,
behaviour: TBehaviour,
local_peer_id: PeerId
) -> Swarm<TBehaviour>
pub fn without_executor( transport: Boxed<(PeerId, StreamMuxerBox)>, behaviour: TBehaviour, local_peer_id: PeerId ) -> Swarm<TBehaviour>
Builds a new Swarm
without an executor, instead using the current task.
⚠️ Performance warning
All connections will be polled on the current task, thus quite bad performance
characteristics should be expected. Whenever possible use an executor and
Swarm::with_executor
.
sourcepub fn network_info(&self) -> NetworkInfo
pub fn network_info(&self) -> NetworkInfo
Returns information about the connections underlying the Swarm
.
sourcepub fn listen_on(
&mut self,
addr: Multiaddr
) -> Result<ListenerId, TransportError<Error>>
pub fn listen_on( &mut self, addr: Multiaddr ) -> Result<ListenerId, TransportError<Error>>
Starts listening on the given address. Returns an error if the address is not supported.
Listeners report their new listening addresses as SwarmEvent::NewListenAddr
.
Depending on the underlying transport, one listener may have multiple listening addresses.
sourcepub fn remove_listener(&mut self, listener_id: ListenerId) -> bool
pub fn remove_listener(&mut self, listener_id: ListenerId) -> bool
Remove some listener.
Returns true
if there was a listener with this ID, false
otherwise.
sourcepub fn dial(&mut self, opts: impl Into<DialOpts>) -> Result<(), DialError>
pub fn dial(&mut self, opts: impl Into<DialOpts>) -> Result<(), DialError>
Dial a known or unknown peer.
See also DialOpts
.
let mut swarm = Swarm::new(
DummyTransport::new().boxed(),
dummy::Behaviour,
PeerId::random(),
);
// Dial a known peer.
swarm.dial(PeerId::random());
// Dial an unknown peer.
swarm.dial("/ip6/::1/tcp/12345".parse::<Multiaddr>().unwrap());
sourcepub fn listeners(&self) -> impl Iterator<Item = &Multiaddr>
pub fn listeners(&self) -> impl Iterator<Item = &Multiaddr>
Returns an iterator that produces the list of addresses we’re listening on.
sourcepub fn local_peer_id(&self) -> &PeerId
pub fn local_peer_id(&self) -> &PeerId
Returns the peer ID of the swarm passed as parameter.
sourcepub fn external_addresses(&self) -> impl Iterator<Item = &AddressRecord>
pub fn external_addresses(&self) -> impl Iterator<Item = &AddressRecord>
Returns an iterator for AddressRecord
s of external addresses
of the local node, in decreasing order of their current
score.
sourcepub fn add_external_address(
&mut self,
a: Multiaddr,
s: AddressScore
) -> AddAddressResult
pub fn add_external_address( &mut self, a: Multiaddr, s: AddressScore ) -> AddAddressResult
Adds an external address record for the local node.
An external address is an address of the local node known to
be (likely) reachable for other nodes, possibly taking into
account NAT. The external addresses of the local node may be
shared with other nodes by the NetworkBehaviour
.
The associated score determines both the position of the address
in the list of external addresses (which can determine the
order in which addresses are used to connect to) as well as
how long the address is retained in the list, depending on
how frequently it is reported by the NetworkBehaviour
via
NetworkBehaviourAction::ReportObservedAddr
or explicitly
through this method.
sourcepub fn remove_external_address(&mut self, addr: &Multiaddr) -> bool
pub fn remove_external_address(&mut self, addr: &Multiaddr) -> bool
Removes an external address of the local node, regardless of
its current score. See Swarm::add_external_address
for details.
Returns true
if the address existed and was removed, false
otherwise.
sourcepub fn ban_peer_id(&mut self, peer_id: PeerId)
pub fn ban_peer_id(&mut self, peer_id: PeerId)
Bans a peer by its peer ID.
Any incoming connection and any dialing attempt will immediately be rejected. This function has no effect if the peer is already banned.
sourcepub fn unban_peer_id(&mut self, peer_id: PeerId)
pub fn unban_peer_id(&mut self, peer_id: PeerId)
Unbans a peer.
sourcepub fn disconnect_peer_id(&mut self, peer_id: PeerId) -> Result<(), ()>
pub fn disconnect_peer_id(&mut self, peer_id: PeerId) -> Result<(), ()>
Disconnects a peer by its peer ID, closing all connections to said peer.
Returns Ok(())
if there was one or more established connections to the peer.
Note: Closing a connection via Swarm::disconnect_peer_id
does
not inform the corresponding ConnectionHandler
.
Closing a connection via a ConnectionHandler
can be done either in a
collaborative manner across ConnectionHandler
s
with ConnectionHandler::connection_keep_alive
or directly with
ConnectionHandlerEvent::Close
.
sourcepub fn is_connected(&self, peer_id: &PeerId) -> bool
pub fn is_connected(&self, peer_id: &PeerId) -> bool
Checks whether there is an established connection to a peer.
sourcepub fn connected_peers(&self) -> impl Iterator<Item = &PeerId>
pub fn connected_peers(&self) -> impl Iterator<Item = &PeerId>
Returns the currently connected peers.
sourcepub fn behaviour(&self) -> &TBehaviour
pub fn behaviour(&self) -> &TBehaviour
Returns a reference to the provided NetworkBehaviour
.
sourcepub fn behaviour_mut(&mut self) -> &mut TBehaviour
pub fn behaviour_mut(&mut self) -> &mut TBehaviour
Returns a mutable reference to the provided NetworkBehaviour
.
Trait Implementations§
source§impl<TBehaviour> FusedStream for Swarm<TBehaviour>where
TBehaviour: NetworkBehaviour,
impl<TBehaviour> FusedStream for Swarm<TBehaviour>where TBehaviour: NetworkBehaviour,
The stream of swarm events never terminates, so we can implement fused for it.
source§fn is_terminated(&self) -> bool
fn is_terminated(&self) -> bool
true
if the stream should no longer be polled.source§impl<TBehaviour> Stream for Swarm<TBehaviour>where
TBehaviour: NetworkBehaviour,
impl<TBehaviour> Stream for Swarm<TBehaviour>where TBehaviour: NetworkBehaviour,
Stream of events returned by Swarm
.
Includes events from the NetworkBehaviour
as well as events about
connection and listener status. See SwarmEvent
for details.
Note: This stream is infinite and it is guaranteed that
Stream::poll_next
will never return Poll::Ready(None)
.
§type Item = SwarmEvent<<TBehaviour as NetworkBehaviour>::OutEvent, <<<TBehaviour as NetworkBehaviour>::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::Error>
type Item = SwarmEvent<<TBehaviour as NetworkBehaviour>::OutEvent, <<<TBehaviour as NetworkBehaviour>::ConnectionHandler as IntoConnectionHandler>::Handler as ConnectionHandler>::Error>
source§fn poll_next(
self: Pin<&mut Swarm<TBehaviour>>,
cx: &mut Context<'_>
) -> Poll<Option<<Swarm<TBehaviour> as Stream>::Item>>
fn poll_next( self: Pin<&mut Swarm<TBehaviour>>, cx: &mut Context<'_> ) -> Poll<Option<<Swarm<TBehaviour> as Stream>::Item>>
None
if the stream is exhausted. Read moreimpl<TBehaviour> Unpin for Swarm<TBehaviour>where TBehaviour: NetworkBehaviour,
Auto Trait Implementations§
impl<TBehaviour> !RefUnwindSafe for Swarm<TBehaviour>
impl<TBehaviour> Send for Swarm<TBehaviour>where TBehaviour: Send,
impl<TBehaviour> !Sync for Swarm<TBehaviour>
impl<TBehaviour> !UnwindSafe for Swarm<TBehaviour>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
source§impl<T> StreamExt for Twhere
T: Stream + ?Sized,
impl<T> StreamExt for Twhere T: Stream + ?Sized,
source§fn next(&mut self) -> Next<'_, Self> ⓘwhere
Self: Unpin,
fn next(&mut self) -> Next<'_, Self> ⓘwhere Self: Unpin,
source§fn into_future(self) -> StreamFuture<Self> ⓘwhere
Self: Sized + Unpin,
fn into_future(self) -> StreamFuture<Self> ⓘwhere Self: Sized + Unpin,
source§fn map<T, F>(self, f: F) -> Map<Self, F>where
F: FnMut(Self::Item) -> T,
Self: Sized,
fn map<T, F>(self, f: F) -> Map<Self, F>where F: FnMut(Self::Item) -> T, Self: Sized,
source§fn enumerate(self) -> Enumerate<Self>where
Self: Sized,
fn enumerate(self) -> Enumerate<Self>where Self: Sized,
source§fn filter<Fut, F>(self, f: F) -> Filter<Self, Fut, F>where
F: FnMut(&Self::Item) -> Fut,
Fut: Future<Output = bool>,
Self: Sized,
fn filter<Fut, F>(self, f: F) -> Filter<Self, Fut, F>where F: FnMut(&Self::Item) -> Fut, Fut: Future<Output = bool>, Self: Sized,
source§fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>where
F: FnMut(Self::Item) -> Fut,
Fut: Future<Output = Option<T>>,
Self: Sized,
fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>where F: FnMut(Self::Item) -> Fut, Fut: Future<Output = Option<T>>, Self: Sized,
source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>where
F: FnMut(Self::Item) -> Fut,
Fut: Future,
Self: Sized,
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>where F: FnMut(Self::Item) -> Fut, Fut: Future, Self: Sized,
source§fn collect<C>(self) -> Collect<Self, C> ⓘwhere
C: Default + Extend<Self::Item>,
Self: Sized,
fn collect<C>(self) -> Collect<Self, C> ⓘwhere C: Default + Extend<Self::Item>, Self: Sized,
source§fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB> ⓘwhere
FromA: Default + Extend<A>,
FromB: Default + Extend<B>,
Self: Sized + Stream<Item = (A, B)>,
fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB> ⓘwhere FromA: Default + Extend<A>, FromB: Default + Extend<B>, Self: Sized + Stream<Item = (A, B)>,
source§fn concat(self) -> Concat<Self> ⓘwhere
Self: Sized,
Self::Item: Extend<<Self::Item as IntoIterator>::Item> + IntoIterator + Default,
fn concat(self) -> Concat<Self> ⓘwhere Self: Sized, Self::Item: Extend<<Self::Item as IntoIterator>::Item> + IntoIterator + Default,
source§fn count(self) -> Count<Self>where
Self: Sized,
fn count(self) -> Count<Self>where Self: Sized,
source§fn fold<T, Fut, F>(self, init: T, f: F) -> Fold<Self, Fut, T, F> ⓘwhere
F: FnMut(T, Self::Item) -> Fut,
Fut: Future<Output = T>,
Self: Sized,
fn fold<T, Fut, F>(self, init: T, f: F) -> Fold<Self, Fut, T, F> ⓘwhere F: FnMut(T, Self::Item) -> Fut, Fut: Future<Output = T>, Self: Sized,
source§fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F>where
F: FnMut(Self::Item) -> Fut,
Fut: Future<Output = bool>,
Self: Sized,
fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F>where F: FnMut(Self::Item) -> Fut, Fut: Future<Output = bool>, Self: Sized,
true
if any element in stream satisfied a predicate. Read moresource§fn all<Fut, F>(self, f: F) -> All<Self, Fut, F>where
F: FnMut(Self::Item) -> Fut,
Fut: Future<Output = bool>,
Self: Sized,
fn all<Fut, F>(self, f: F) -> All<Self, Fut, F>where F: FnMut(Self::Item) -> Fut, Fut: Future<Output = bool>, Self: Sized,
true
if all element in stream satisfied a predicate. Read moresource§fn flatten(self) -> Flatten<Self>where
Self::Item: Stream,
Self: Sized,
fn flatten(self) -> Flatten<Self>where Self::Item: Stream, Self: Sized,
source§fn flatten_unordered(
self,
limit: impl Into<Option<usize>>
) -> FlattenUnordered<Self>where
Self::Item: Stream + Unpin,
Self: Sized,
fn flatten_unordered( self, limit: impl Into<Option<usize>> ) -> FlattenUnordered<Self>where Self::Item: Stream + Unpin, Self: Sized,
source§fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>where
F: FnMut(Self::Item) -> U,
U: Stream,
Self: Sized,
fn flat_map<U, F>(self, f: F) -> FlatMap<Self, U, F>where F: FnMut(Self::Item) -> U, U: Stream, Self: Sized,
source§fn flat_map_unordered<U, F>(
self,
limit: impl Into<Option<usize>>,
f: F
) -> FlatMapUnordered<Self, U, F>where
U: Stream + Unpin,
F: FnMut(Self::Item) -> U,
Self: Sized,
fn flat_map_unordered<U, F>( self, limit: impl Into<Option<usize>>, f: F ) -> FlatMapUnordered<Self, U, F>where U: Stream + Unpin, F: FnMut(Self::Item) -> U, Self: Sized,
StreamExt::map
but flattens nested Stream
s
and polls them concurrently, yielding items in any order, as they made
available. Read moresource§fn scan<S, B, Fut, F>(self, initial_state: S, f: F) -> Scan<Self, S, Fut, F>where
F: FnMut(&mut S, Self::Item) -> Fut,
Fut: Future<Output = Option<B>>,
Self: Sized,
fn scan<S, B, Fut, F>(self, initial_state: S, f: F) -> Scan<Self, S, Fut, F>where F: FnMut(&mut S, Self::Item) -> Fut, Fut: Future<Output = Option<B>>, Self: Sized,
StreamExt::fold
that holds internal state
and produces a new stream. Read moresource§fn skip_while<Fut, F>(self, f: F) -> SkipWhile<Self, Fut, F>where
F: FnMut(&Self::Item) -> Fut,
Fut: Future<Output = bool>,
Self: Sized,
fn skip_while<Fut, F>(self, f: F) -> SkipWhile<Self, Fut, F>where F: FnMut(&Self::Item) -> Fut, Fut: Future<Output = bool>, Self: Sized,
true
. Read moresource§fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>where
F: FnMut(&Self::Item) -> Fut,
Fut: Future<Output = bool>,
Self: Sized,
fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>where F: FnMut(&Self::Item) -> Fut, Fut: Future<Output = bool>, Self: Sized,
true
. Read moresource§fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>where
Fut: Future,
Self: Sized,
fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>where Fut: Future, Self: Sized,
source§fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F> ⓘwhere
F: FnMut(Self::Item) -> Fut,
Fut: Future<Output = ()>,
Self: Sized,
fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F> ⓘwhere F: FnMut(Self::Item) -> Fut, Fut: Future<Output = ()>, Self: Sized,
source§fn for_each_concurrent<Fut, F>(
self,
limit: impl Into<Option<usize>>,
f: F
) -> ForEachConcurrent<Self, Fut, F> ⓘwhere
F: FnMut(Self::Item) -> Fut,
Fut: Future<Output = ()>,
Self: Sized,
fn for_each_concurrent<Fut, F>( self, limit: impl Into<Option<usize>>, f: F ) -> ForEachConcurrent<Self, Fut, F> ⓘwhere F: FnMut(Self::Item) -> Fut, Fut: Future<Output = ()>, Self: Sized,
source§fn take(self, n: usize) -> Take<Self>where
Self: Sized,
fn take(self, n: usize) -> Take<Self>where Self: Sized,
n
items of the underlying stream. Read moresource§fn skip(self, n: usize) -> Skip<Self>where
Self: Sized,
fn skip(self, n: usize) -> Skip<Self>where Self: Sized,
n
items of the underlying stream. Read moresource§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where Self: Sized + UnwindSafe,
source§fn boxed<'a>(
self
) -> Pin<Box<dyn Stream<Item = Self::Item> + Send + 'a, Global>>where
Self: Sized + Send + 'a,
fn boxed<'a>( self ) -> Pin<Box<dyn Stream<Item = Self::Item> + Send + 'a, Global>>where Self: Sized + Send + 'a,
source§fn boxed_local<'a>(self) -> Pin<Box<dyn Stream<Item = Self::Item> + 'a, Global>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Stream<Item = Self::Item> + 'a, Global>>where Self: Sized + 'a,
source§fn buffered(self, n: usize) -> Buffered<Self>where
Self::Item: Future,
Self: Sized,
fn buffered(self, n: usize) -> Buffered<Self>where Self::Item: Future, Self: Sized,
source§fn buffer_unordered(self, n: usize) -> BufferUnordered<Self>where
Self::Item: Future,
Self: Sized,
fn buffer_unordered(self, n: usize) -> BufferUnordered<Self>where Self::Item: Future, Self: Sized,
source§fn zip<St>(self, other: St) -> Zip<Self, St>where
St: Stream,
Self: Sized,
fn zip<St>(self, other: St) -> Zip<Self, St>where St: Stream, Self: Sized,
source§fn chain<St>(self, other: St) -> Chain<Self, St>where
St: Stream<Item = Self::Item>,
Self: Sized,
fn chain<St>(self, other: St) -> Chain<Self, St>where St: Stream<Item = Self::Item>, Self: Sized,
source§fn peekable(self) -> Peekable<Self>where
Self: Sized,
fn peekable(self) -> Peekable<Self>where Self: Sized,
peek
method. Read moresource§fn chunks(self, capacity: usize) -> Chunks<Self>where
Self: Sized,
fn chunks(self, capacity: usize) -> Chunks<Self>where Self: Sized,
source§fn ready_chunks(self, capacity: usize) -> ReadyChunks<Self>where
Self: Sized,
fn ready_chunks(self, capacity: usize) -> ReadyChunks<Self>where Self: Sized,
source§fn forward<S>(self, sink: S) -> Forward<Self, S> ⓘwhere
S: Sink<Self::Ok, Error = Self::Error>,
Self: TryStream + Sized,
fn forward<S>(self, sink: S) -> Forward<Self, S> ⓘwhere S: Sink<Self::Ok, Error = Self::Error>, Self: TryStream + Sized,
source§fn split<Item>(self) -> (SplitSink<Self, Item>, SplitStream<Self>)where
Self: Sink<Item> + Sized,
fn split<Item>(self) -> (SplitSink<Self, Item>, SplitStream<Self>)where Self: Sink<Item> + Sized,
source§fn inspect<F>(self, f: F) -> Inspect<Self, F>where
F: FnMut(&Self::Item),
Self: Sized,
fn inspect<F>(self, f: F) -> Inspect<Self, F>where F: FnMut(&Self::Item), Self: Sized,
source§fn right_stream<B>(self) -> Either<B, Self> ⓘwhere
B: Stream<Item = Self::Item>,
Self: Sized,
fn right_stream<B>(self) -> Either<B, Self> ⓘwhere B: Stream<Item = Self::Item>, Self: Sized,
source§fn poll_next_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>where
Self: Unpin,
fn poll_next_unpin(&mut self, cx: &mut Context<'_>) -> Poll<Option<Self::Item>>where Self: Unpin,
Stream::poll_next
on Unpin
stream types.