Skip to content

Commit

Permalink
Add some documentation to listeners stream (#547)
Browse files Browse the repository at this point in the history
* Add some documentation to listeners stream

* Fix concern
  • Loading branch information
tomaka authored and twittner committed Oct 15, 2018
1 parent 7d1a023 commit e179951
Showing 1 changed file with 50 additions and 2 deletions.
52 changes: 50 additions & 2 deletions core/src/nodes/listeners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,57 @@ use std::fmt;
use void::Void;
use {Multiaddr, Transport};

/// Implementation of `Stream` that handles listeners.
/// Implementation of `futures::Stream` that allows listening on multiaddresses.
///
/// The stream cannot produce errors.
/// To start using a `ListenersStream`, create one with `new` by passing an implementation of
/// `Transport`. This `Transport` will be used to start listening, therefore you want to pass
/// a `Transport` that supports the protocols you wish you listen on.
///
/// Then, call `ListenerStream::listen_on` for all addresses you want to start listening on.
///
/// The `ListenersStream` never ends and never produces errors. If a listener errors or closes,
/// an event is generated on the stream and the listener is then dropped, but the `ListenersStream`
/// itself continues.
///
/// # Example
///
/// ```no_run
/// # extern crate futures;
/// # extern crate libp2p_core;
/// # extern crate libp2p_tcp_transport;
/// # extern crate tokio;
/// # fn main() {
/// use futures::prelude::*;
/// use libp2p_core::nodes::listeners::{ListenersEvent, ListenersStream};
///
/// let mut listeners = ListenersStream::new(libp2p_tcp_transport::TcpConfig::new());
///
/// // Ask the `listeners` to start listening on the given multiaddress.
/// listeners.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();
///
/// // You can retreive the list of active listeners with `listeners()`.
/// println!("Listening on: {:?}", listeners.listeners().collect::<Vec<_>>());
///
/// // The `listeners` will now generate events when polled.
/// let future = listeners.for_each(move |event| {
/// match event {
/// ListenersEvent::Closed { listen_addr, listener, result } => {
/// println!("Listener {} has been closed: {:?}", listen_addr, result);
/// },
/// ListenersEvent::Incoming { upgrade, listen_addr } => {
/// println!("A connection has arrived on {}", listen_addr);
/// // We don't do anything with the newly-opened connection, but in a real-life
/// // program you probably want to use it!
/// drop(upgrade);
/// },
/// };
///
/// Ok(())
/// });
///
/// tokio::run(future.map_err(|_| ()));
/// # }
/// ```
pub struct ListenersStream<TTrans>
where
TTrans: Transport,
Expand Down

0 comments on commit e179951

Please sign in to comment.