From f6b53ec5865720a2937bc238ee75d72588e10cce Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Sat, 8 Apr 2017 18:22:22 +0200 Subject: [PATCH] minimal host interface --- Cargo.toml | 2 +- libp2p-transport/Cargo.toml | 1 - libp2p-transport/src/lib.rs | 74 +++++++++---------------------------- 3 files changed, 18 insertions(+), 59 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index b74045c0..e8ef317d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,4 +4,4 @@ version = "0.1.0" authors = ["Parity Technologies "] [workspace] -members = ["libp2p-transport"] \ No newline at end of file +members = ["libp2p-transport", "libp2p-host"] \ No newline at end of file diff --git a/libp2p-transport/Cargo.toml b/libp2p-transport/Cargo.toml index d922893f..9cdc337f 100644 --- a/libp2p-transport/Cargo.toml +++ b/libp2p-transport/Cargo.toml @@ -2,7 +2,6 @@ name = "libp2p-transport" version = "0.1.0" authors = ["Parity Technologies "] -workspace = ".." [dependencies] multiaddr = "0.2.0" diff --git a/libp2p-transport/src/lib.rs b/libp2p-transport/src/lib.rs index 1a3e78e0..72902b3b 100644 --- a/libp2p-transport/src/lib.rs +++ b/libp2p-transport/src/lib.rs @@ -1,4 +1,4 @@ -//! Rust libp2p implementation +//! Transport and I/O primitives for libp2p. extern crate futures; extern crate tokio_io; @@ -21,10 +21,6 @@ pub type PeerId = String; /// A logical wire between us and a peer. We can read and write through this asynchronously. /// /// You can have multiple `Socket`s between you and any given peer. -// -// What do we call it? Go-libp2p calls them "Streams", but "Streams" in futures-rs specifically -// denote the "writing" half. "Connections" have a different meaning in the libp2p definition. -// `Duplex`? pub trait Socket: AsyncRead + AsyncWrite { /// Get the protocol ID this socket uses. fn protocol_id(&self) -> ProtocolId; @@ -42,61 +38,25 @@ pub trait Conn { fn make_socket(&self, proto: ProtocolId) -> BoxFuture; } -/// Produces a future for each incoming `Socket`. -pub trait Handler { - type Future: IntoFuture; - - /// Handle the incoming socket, producing a future which should resolve - /// when the handler is finished. - fn handle(&self, socket: S) -> Self::Future; - fn boxed(self) -> BoxHandler where - Self: Sized + Send + 'static, - ::Future: Send + 'static - { - BoxHandler(Box::new(move |socket| - self.handle(socket).into_future().boxed() - )) - } -} - -impl Handler for F - where F: Fn(S) -> U, U: IntoFuture -{ - type Future = U; - - fn handle(&self, socket: S) -> U { (self)(socket) } -} - -/// A boxed handler. -pub struct BoxHandler(Box>>); - -impl Handler for BoxHandler { - type Future = BoxFuture<(), ()>; - - fn handle(&self, socket: S) -> Self::Future { - self.0.handle(socket) - } -} - -/// Multiplexes sockets onto handlers by protocol-id. -pub trait Mux: Sync { - /// The socket type this manages. - type Socket: Socket; - - /// Attach an incoming socket. - fn push(&self, socket: Self::Socket); - - /// Set the socket handler for a given protocol id. - fn set_handler(&self, proto: ProtocolId, handler: BoxHandler); - - /// Remove the socket handler for the given protocol id, returning the old handler if it existed. - fn remove_handler(&self, proto: &ProtocolId) -> Option>; -} - pub struct MultiAddr; // stub for multiaddr crate type. /// A transport is a stream producing incoming connections. /// These are transports or wrappers around them. +// +// Listening/Dialing hasn't really been sorted out yet. +// It's easy to do for simple multiaddrs, but for complex ones, +// particularly those with multiple hops, things get much fuzzier. +// +// One example which is difficult to make work is something like +// `ip4/1.2.3.4/tcp/8888/p2p-circuit/p2p/DestPeer` +// +// This address, when used for dialing, says "Connect to the peer DestPeer +// on any available address, through a relay node we will connect to via +// tcp on port 8888 over the ipv4 address 1.2.3.4" +// +// We'll need to require dialers to handle the whole address, +// and give them a closure or similar required to instantiate connections +// to different multi-addresses. pub trait Transport { /// The raw connection. type RawConn: AsyncRead + AsyncWrite; @@ -113,6 +73,6 @@ pub trait Transport { /// Dial to the given multi-addr. /// Returns either a future which may resolve to a connection, - /// or + /// or gives back the multiaddress. fn dial(&mut self, addr: MultiAddr) -> Result; } \ No newline at end of file