Skip to content

Commit

Permalink
Add helpers for easier Transports creation (libp2p#777)
Browse files Browse the repository at this point in the history
* Add helpers for easier Transports creation

* Fix doctests
  • Loading branch information
tomaka authored and jamesray1 committed Dec 18, 2018
1 parent 40a503f commit 16d22b3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
14 changes: 3 additions & 11 deletions examples/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,8 @@ extern crate tokio;

use futures::prelude::*;
use libp2p::{
NetworkBehaviour, Transport,
core::upgrade::{self, OutboundUpgradeExt},
NetworkBehaviour,
secio,
mplex,
tokio_codec::{FramedRead, LinesCodec}
};

Expand All @@ -71,14 +69,8 @@ fn main() {
let local_pub_key = local_key.to_public_key();
println!("Local peer id: {:?}", local_pub_key.clone().into_peer_id());

// Set up a an encrypted DNS-enabled TCP Transport over the Mplex protocol
let transport = libp2p::CommonTransport::new()
.with_upgrade(secio::SecioConfig::new(local_key))
.and_then(move |out, _| {
let peer_id = out.remote_key.into_peer_id();
let upgrade = mplex::MplexConfig::new().map_outbound(move |muxer| (peer_id, muxer) );
upgrade::apply_outbound(out.stream, upgrade).map_err(|e| e.into_io_error())
});
// Set up a an encrypted DNS-enabled TCP Transport over the Mplex and Yamux protocols
let transport = libp2p::build_development_transport(local_key);

// Create a Floodsub topic
let floodsub_topic = libp2p::floodsub::TopicBuilder::new("chat").build();
Expand Down
11 changes: 1 addition & 10 deletions examples/ipfs-kad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,8 @@ extern crate tokio;

use futures::prelude::*;
use libp2p::{
Transport,
core::PublicKey,
core::upgrade::{self, OutboundUpgradeExt},
secio,
mplex,
};

fn main() {
Expand All @@ -43,13 +40,7 @@ fn main() {
let local_pub_key = local_key.to_public_key();

// Set up a an encrypted DNS-enabled TCP Transport over the Mplex protocol
let transport = libp2p::CommonTransport::new()
.with_upgrade(secio::SecioConfig::new(local_key))
.and_then(move |out, _| {
let peer_id = out.remote_key.into_peer_id();
let upgrade = mplex::MplexConfig::new().map_outbound(move |muxer| (peer_id, muxer) );
upgrade::apply_outbound(out.stream, upgrade).map_err(|e| e.into_io_error())
});
let transport = libp2p::build_development_transport(local_key);

// Create the topology of the network with the IPFS bootstrap nodes.
let mut topology = libp2p::core::topology::MemoryTopology::empty(local_pub_key.clone());
Expand Down
56 changes: 48 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@
//! // unless it is run through a tokio runtime.
//! ```
//!
//! The easiest way to create a transport is to use the `CommonTransport` struct. This struct
//! provides support for the most common protocols.
//! The easiest way to create a transport is to use the `build_development_transport` function.
//! This function provides support for the most common protocols.
//!
//! Example:
//!
//! ```rust
//! use libp2p::CommonTransport;
//! let _transport = CommonTransport::new();
//! let key = libp2p::secio::SecioKeyPair::ed25519_generated().unwrap();
//! let _transport = libp2p::build_development_transport(key);
//! // _transport.dial(...);
//! ```
//!
Expand Down Expand Up @@ -172,22 +172,62 @@ pub use self::multiaddr::Multiaddr;
pub use self::simple::SimpleProtocol;
pub use self::transport_ext::TransportExt;

use futures::prelude::*;
use std::time::Duration;

/// Builds a `Transport` that supports the most commonly-used protocols that libp2p supports.
///
/// > **Note**: This `Transport` is not suitable for production usage, as its implementation
/// > reserves the right to support additional protocols or remove deprecated protocols.
#[inline]
pub fn build_development_transport(local_private_key: secio::SecioKeyPair)
-> impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send> + Send + Sync), Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone
{
build_tcp_ws_secio_mplex_yamux(local_private_key)
}

/// Builds an implementation of `Transport` that is suitable for usage with the `Swarm`.
///
/// The implementation supports TCP/IP, WebSockets over TCP/IP, secio as the encryption layer,
/// and mplex or yamux as the multiplexing layer.
///
/// > **Note**: If you ever need to express the type of this `Transport`.
pub fn build_tcp_ws_secio_mplex_yamux(local_private_key: secio::SecioKeyPair)
-> impl Transport<Output = (PeerId, impl core::muxing::StreamMuxer<OutboundSubstream = impl Send, Substream = impl Send> + Send + Sync), Listener = impl Send, Dial = impl Send, ListenerUpgrade = impl Send> + Clone
{
CommonTransport::new()
.with_upgrade(secio::SecioConfig::new(local_private_key))
.and_then(move |out, endpoint| {
let peer_id = out.remote_key.into_peer_id();
let peer_id2 = peer_id.clone();
let upgrade = core::upgrade::SelectUpgrade::new(yamux::Config::default(), mplex::MplexConfig::new())
// TODO: use a single `.map` instead of two maps
.map_inbound(move |muxer| (peer_id, muxer))
.map_outbound(move |muxer| (peer_id2, muxer));

core::upgrade::apply(out.stream, upgrade, endpoint)
.map(|(id, muxer)| (id, core::muxing::StreamMuxerBox::new(muxer)))
.map_err(|e| e.into_io_error())
})
.with_timeout(Duration::from_secs(20))
}

/// Implementation of `Transport` that supports the most common protocols.
///
/// The list currently is TCP/IP, DNS, and WebSockets. However this list could change in the
/// future to get new transports.
#[derive(Debug, Clone)]
pub struct CommonTransport {
struct CommonTransport {
// The actual implementation of everything.
inner: CommonTransportInner
}

#[cfg(all(not(target_os = "emscripten"), feature = "libp2p-websocket"))]
pub type InnerImplementation = core::transport::OrTransport<dns::DnsConfig<tcp::TcpConfig>, websocket::WsConfig<dns::DnsConfig<tcp::TcpConfig>>>;
type InnerImplementation = core::transport::OrTransport<dns::DnsConfig<tcp::TcpConfig>, websocket::WsConfig<dns::DnsConfig<tcp::TcpConfig>>>;
#[cfg(all(not(target_os = "emscripten"), not(feature = "libp2p-websocket")))]
pub type InnerImplementation = dns::DnsConfig<tcp::TcpConfig>;
type InnerImplementation = dns::DnsConfig<tcp::TcpConfig>;
#[cfg(target_os = "emscripten")]
pub type InnerImplementation = websocket::BrowserWsConfig;
type InnerImplementation = websocket::BrowserWsConfig;

#[derive(Debug, Clone)]
struct CommonTransportInner {
Expand Down

0 comments on commit 16d22b3

Please sign in to comment.