Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some documentation to listeners stream #547

Merged
merged 4 commits into from
Oct 15, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions core/src/nodes/listeners.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,58 @@ 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` as many times as you want to start the actual act of
/// listening.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would I want to call listen_on multiple times? I.e. is there a use case where this is necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, to listen on multiple different addresses.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, then I'd suggest: "Then, call ListenersStream::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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to call drop here? I.e. should we tell users to explicitly drop connections they don't want to handle?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary, it's to make it consistent with the comment just above.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For n00b users it kind of looks like I have to call drop to dispose of it. Maybe just remove it to avoid misunderstandings?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, this is more about Rust than about libp2p, and Rust beginners are not supposed to use the ListenersStream directly.

/// },
/// };
///
/// Ok(())
/// });
///
/// tokio::run(future.map_err(|_| ()));
/// # }
/// ```
pub struct ListenersStream<TTrans>
where
TTrans: Transport,
Expand Down