mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-07-03 11:41:34 +00:00
Add helpers for easier Transports creation (#777)
* Add helpers for easier Transports creation * Fix doctests
This commit is contained in:
@ -56,10 +56,8 @@ extern crate tokio;
|
|||||||
|
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
NetworkBehaviour, Transport,
|
NetworkBehaviour,
|
||||||
core::upgrade::{self, OutboundUpgradeExt},
|
|
||||||
secio,
|
secio,
|
||||||
mplex,
|
|
||||||
tokio_codec::{FramedRead, LinesCodec}
|
tokio_codec::{FramedRead, LinesCodec}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -71,14 +69,8 @@ fn main() {
|
|||||||
let local_pub_key = local_key.to_public_key();
|
let local_pub_key = local_key.to_public_key();
|
||||||
println!("Local peer id: {:?}", local_pub_key.clone().into_peer_id());
|
println!("Local peer id: {:?}", local_pub_key.clone().into_peer_id());
|
||||||
|
|
||||||
// Set up a an encrypted DNS-enabled TCP Transport over the Mplex protocol
|
// Set up a an encrypted DNS-enabled TCP Transport over the Mplex and Yamux protocols
|
||||||
let transport = libp2p::CommonTransport::new()
|
let transport = libp2p::build_development_transport(local_key);
|
||||||
.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())
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create a Floodsub topic
|
// Create a Floodsub topic
|
||||||
let floodsub_topic = libp2p::floodsub::TopicBuilder::new("chat").build();
|
let floodsub_topic = libp2p::floodsub::TopicBuilder::new("chat").build();
|
||||||
|
@ -30,11 +30,8 @@ extern crate tokio;
|
|||||||
|
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use libp2p::{
|
use libp2p::{
|
||||||
Transport,
|
|
||||||
core::PublicKey,
|
core::PublicKey,
|
||||||
core::upgrade::{self, OutboundUpgradeExt},
|
|
||||||
secio,
|
secio,
|
||||||
mplex,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@ -43,13 +40,7 @@ fn main() {
|
|||||||
let local_pub_key = local_key.to_public_key();
|
let local_pub_key = local_key.to_public_key();
|
||||||
|
|
||||||
// Set up a an encrypted DNS-enabled TCP Transport over the Mplex protocol
|
// Set up a an encrypted DNS-enabled TCP Transport over the Mplex protocol
|
||||||
let transport = libp2p::CommonTransport::new()
|
let transport = libp2p::build_development_transport(local_key);
|
||||||
.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())
|
|
||||||
});
|
|
||||||
|
|
||||||
// Create the topology of the network with the IPFS bootstrap nodes.
|
// Create the topology of the network with the IPFS bootstrap nodes.
|
||||||
let mut topology = libp2p::core::topology::MemoryTopology::empty(local_pub_key.clone());
|
let mut topology = libp2p::core::topology::MemoryTopology::empty(local_pub_key.clone());
|
||||||
|
56
src/lib.rs
56
src/lib.rs
@ -53,14 +53,14 @@
|
|||||||
//! // unless it is run through a tokio runtime.
|
//! // unless it is run through a tokio runtime.
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! The easiest way to create a transport is to use the `CommonTransport` struct. This struct
|
//! The easiest way to create a transport is to use the `build_development_transport` function.
|
||||||
//! provides support for the most common protocols.
|
//! This function provides support for the most common protocols.
|
||||||
//!
|
//!
|
||||||
//! Example:
|
//! Example:
|
||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use libp2p::CommonTransport;
|
//! let key = libp2p::secio::SecioKeyPair::ed25519_generated().unwrap();
|
||||||
//! let _transport = CommonTransport::new();
|
//! let _transport = libp2p::build_development_transport(key);
|
||||||
//! // _transport.dial(...);
|
//! // _transport.dial(...);
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
@ -172,22 +172,62 @@ pub use self::multiaddr::Multiaddr;
|
|||||||
pub use self::simple::SimpleProtocol;
|
pub use self::simple::SimpleProtocol;
|
||||||
pub use self::transport_ext::TransportExt;
|
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.
|
/// 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
|
/// The list currently is TCP/IP, DNS, and WebSockets. However this list could change in the
|
||||||
/// future to get new transports.
|
/// future to get new transports.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct CommonTransport {
|
struct CommonTransport {
|
||||||
// The actual implementation of everything.
|
// The actual implementation of everything.
|
||||||
inner: CommonTransportInner
|
inner: CommonTransportInner
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(all(not(target_os = "emscripten"), feature = "libp2p-websocket"))]
|
#[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")))]
|
#[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")]
|
#[cfg(target_os = "emscripten")]
|
||||||
pub type InnerImplementation = websocket::BrowserWsConfig;
|
type InnerImplementation = websocket::BrowserWsConfig;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
struct CommonTransportInner {
|
struct CommonTransportInner {
|
||||||
|
Reference in New Issue
Block a user