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,
config: Config,
) -> Swarm<TBehaviour>
pub fn new( transport: Boxed<(PeerId, StreamMuxerBox)>, behaviour: TBehaviour, local_peer_id: PeerId, config: Config, ) -> Swarm<TBehaviour>
Creates a new Swarm
from the given Transport
, NetworkBehaviour
, PeerId
and
Config
.
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 = SwarmBuilder::with_tokio_executor(
DummyTransport::new().boxed(),
dummy::Behaviour,
PeerId::random(),
).build();
// 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 = &Multiaddr>
pub fn external_addresses(&self) -> impl Iterator<Item = &Multiaddr>
List all confirmed external address for the local node.
sourcepub fn add_external_address(&mut self, a: Multiaddr)
pub fn add_external_address(&mut self, a: Multiaddr)
Add a confirmed external address for the local node.
This function should only be called with addresses that are guaranteed to be reachable.
The address is broadcast to all NetworkBehaviour
s via FromSwarm::ExternalAddrConfirmed
.
sourcepub fn remove_external_address(&mut self, addr: &Multiaddr)
pub fn remove_external_address(&mut self, addr: &Multiaddr)
Remove an external address for the local node.
The address is broadcast to all NetworkBehaviour
s via FromSwarm::ExternalAddrExpired
.
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 close_connection(&mut self, connection_id: ConnectionId) -> bool
pub fn close_connection(&mut self, connection_id: ConnectionId) -> bool
Attempt to gracefully close a connection.
Closing a connection is asynchronous but this function will return immediately.
A SwarmEvent::ConnectionClosed
event will be emitted once the connection is actually closed.
§Returns
true
if the connection was established and is now being closed.false
if the connection was not found or is no longer established.
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
futures::Stream::poll_next
will never return Poll::Ready(None)
.
§type Item = SwarmEvent<<TBehaviour as NetworkBehaviour>::ToSwarm, <<TBehaviour as NetworkBehaviour>::ConnectionHandler as ConnectionHandler>::Error>
type Item = SwarmEvent<<TBehaviour as NetworkBehaviour>::ToSwarm, <<TBehaviour as NetworkBehaviour>::ConnectionHandler 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> !Freeze for Swarm<TBehaviour>
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> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> StreamExt for T
impl<T> StreamExt for T
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> ⓘ
fn into_future(self) -> StreamFuture<Self> ⓘ
source§fn map<T, F>(self, f: F) -> Map<Self, F>
fn map<T, F>(self, f: F) -> Map<Self, F>
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>
fn filter<Fut, F>(self, f: F) -> Filter<Self, Fut, F>
source§fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>
fn filter_map<Fut, T, F>(self, f: F) -> FilterMap<Self, Fut, F>
source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
source§fn collect<C>(self) -> Collect<Self, C> ⓘ
fn collect<C>(self) -> Collect<Self, C> ⓘ
source§fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB> ⓘ
fn unzip<A, B, FromA, FromB>(self) -> Unzip<Self, FromA, FromB> ⓘ
source§fn concat(self) -> Concat<Self> ⓘ
fn concat(self) -> Concat<Self> ⓘ
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> ⓘ
fn fold<T, Fut, F>(self, init: T, f: F) -> Fold<Self, Fut, T, F> ⓘ
source§fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F> ⓘ
fn any<Fut, F>(self, f: F) -> Any<Self, Fut, F> ⓘ
true
if any element in stream satisfied a predicate. Read moresource§fn all<Fut, F>(self, f: F) -> All<Self, Fut, F> ⓘ
fn all<Fut, F>(self, f: F) -> All<Self, Fut, F> ⓘ
true
if all element in stream satisfied a predicate. Read moresource§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
source§fn flatten_unordered(
self,
limit: impl Into<Option<usize>>,
) -> FlattenUnorderedWithFlowController<Self, ()>
fn flatten_unordered( self, limit: impl Into<Option<usize>>, ) -> FlattenUnorderedWithFlowController<Self, ()>
source§fn flat_map_unordered<U, F>(
self,
limit: impl Into<Option<usize>>,
f: F,
) -> FlatMapUnordered<Self, U, F>
fn flat_map_unordered<U, F>( self, limit: impl Into<Option<usize>>, f: F, ) -> FlatMapUnordered<Self, U, F>
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>
fn scan<S, B, Fut, F>(self, initial_state: S, f: F) -> Scan<Self, S, Fut, F>
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>
fn skip_while<Fut, F>(self, f: F) -> SkipWhile<Self, Fut, F>
true
. Read moresource§fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>
fn take_while<Fut, F>(self, f: F) -> TakeWhile<Self, Fut, F>
true
. Read moresource§fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>
fn take_until<Fut>(self, fut: Fut) -> TakeUntil<Self, Fut>
source§fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F> ⓘ
fn for_each<Fut, F>(self, f: F) -> ForEach<Self, Fut, F> ⓘ
source§fn for_each_concurrent<Fut, F>(
self,
limit: impl Into<Option<usize>>,
f: F,
) -> ForEachConcurrent<Self, Fut, F> ⓘ
fn for_each_concurrent<Fut, F>( self, limit: impl Into<Option<usize>>, f: F, ) -> ForEachConcurrent<Self, Fut, F> ⓘ
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>>
fn boxed<'a>(self) -> Pin<Box<dyn Stream<Item = Self::Item> + Send + 'a>>
source§fn boxed_local<'a>(self) -> Pin<Box<dyn Stream<Item = Self::Item> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Stream<Item = Self::Item> + 'a>>where
Self: Sized + 'a,
source§fn buffered(self, n: usize) -> Buffered<Self>
fn buffered(self, n: usize) -> Buffered<Self>
source§fn buffer_unordered(self, n: usize) -> BufferUnordered<Self>
fn buffer_unordered(self, n: usize) -> BufferUnordered<Self>
source§fn zip<St>(self, other: St) -> Zip<Self, St>
fn zip<St>(self, other: St) -> Zip<Self, St>
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> ⓘ
fn forward<S>(self, sink: S) -> Forward<Self, S> ⓘ
source§fn split<Item>(self) -> (SplitSink<Self, Item>, SplitStream<Self>)
fn split<Item>(self) -> (SplitSink<Self, Item>, SplitStream<Self>)
source§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
source§fn left_stream<B>(self) -> Either<Self, B> ⓘ
fn left_stream<B>(self) -> Either<Self, B> ⓘ
source§fn right_stream<B>(self) -> Either<B, Self> ⓘ
fn right_stream<B>(self) -> Either<B, Self> ⓘ
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.