2017-11-02 11:58:02 +01:00
|
|
|
// Copyright 2017 Parity Technologies (UK) Ltd.
|
2017-12-14 17:37:32 +01:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the "Software"),
|
|
|
|
// to deal in the Software without restriction, including without limitation
|
|
|
|
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
// and/or sell copies of the Software, and to permit persons to whom the
|
2017-11-02 11:58:02 +01:00
|
|
|
// Software is furnished to do so, subject to the following conditions:
|
|
|
|
//
|
2017-12-14 17:37:32 +01:00
|
|
|
// The above copyright notice and this permission notice shall be included in
|
2017-11-02 11:58:02 +01:00
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
2017-12-14 17:37:32 +01:00
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
2017-11-02 11:58:02 +01:00
|
|
|
// DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
extern crate bytes;
|
|
|
|
extern crate futures;
|
|
|
|
extern crate libp2p_secio as secio;
|
|
|
|
extern crate libp2p_swarm as swarm;
|
|
|
|
extern crate libp2p_tcp_transport as tcp;
|
2017-12-14 17:37:32 +01:00
|
|
|
extern crate multiplex;
|
2017-11-02 11:58:02 +01:00
|
|
|
extern crate tokio_core;
|
|
|
|
extern crate tokio_io;
|
|
|
|
|
2017-12-01 14:30:08 +01:00
|
|
|
use bytes::BytesMut;
|
2017-12-14 17:37:32 +01:00
|
|
|
use futures::{Future, Sink, Stream};
|
2017-12-18 11:30:24 +01:00
|
|
|
use std::env;
|
2017-12-18 12:29:21 +01:00
|
|
|
use swarm::{UpgradeExt, SimpleProtocol, Transport, MuxedTransport};
|
2017-12-04 16:05:37 +01:00
|
|
|
use tcp::TcpConfig;
|
2017-11-02 11:58:02 +01:00
|
|
|
use tokio_core::reactor::Core;
|
|
|
|
use tokio_io::codec::length_delimited;
|
|
|
|
|
|
|
|
fn main() {
|
2017-12-18 11:30:24 +01:00
|
|
|
// Determine which address to dial.
|
|
|
|
let target_addr = env::args().nth(1).unwrap_or("/ip4/127.0.0.1/tcp/10333".to_owned());
|
|
|
|
|
2017-12-07 18:06:38 +01:00
|
|
|
// We start by building the tokio engine that will run all the sockets.
|
2017-11-02 11:58:02 +01:00
|
|
|
let mut core = Core::new().unwrap();
|
2017-12-07 18:06:38 +01:00
|
|
|
|
|
|
|
// Now let's build the transport stack.
|
|
|
|
// We start by creating a `TcpConfig` that indicates that we want TCP/IP.
|
|
|
|
let transport = TcpConfig::new(core.handle())
|
|
|
|
|
|
|
|
// On top of TCP/IP, we will use either the plaintext protocol or the secio protocol,
|
|
|
|
// depending on which one the remote supports.
|
2017-12-12 12:48:28 +01:00
|
|
|
.with_upgrade({
|
|
|
|
let plain_text = swarm::PlainTextConfig;
|
|
|
|
|
|
|
|
let secio = {
|
|
|
|
let private_key = include_bytes!("test-private-key.pk8");
|
|
|
|
let public_key = include_bytes!("test-public-key.der").to_vec();
|
|
|
|
secio::SecioConfig {
|
|
|
|
key: secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
plain_text.or_upgrade(secio)
|
2017-12-07 18:06:38 +01:00
|
|
|
})
|
|
|
|
|
2017-12-18 11:30:24 +01:00
|
|
|
// On top of plaintext or secio, we will use the multiplex protocol.
|
|
|
|
.with_upgrade(multiplex::MultiplexConfig)
|
|
|
|
// The object returned by the call to `with_upgrade(MultiplexConfig)` can't be used as a
|
|
|
|
// `Transport` because the output of the upgrade is not a stream but a controller for
|
|
|
|
// muxing. We have to explicitly call `into_connection_reuse()` in order to turn this into
|
|
|
|
// a `Transport`.
|
2017-12-18 12:29:21 +01:00
|
|
|
.into_connection_reuse();
|
2017-12-12 10:42:34 +01:00
|
|
|
|
2017-12-18 12:29:21 +01:00
|
|
|
let transport_with_echo = transport
|
|
|
|
.clone()
|
2017-12-07 18:06:38 +01:00
|
|
|
// On top of plaintext or secio, we use the "echo" protocol, which is a custom protocol
|
|
|
|
// just for this example.
|
|
|
|
// For this purpose, we create a `SimpleProtocol` struct.
|
|
|
|
.with_upgrade(SimpleProtocol::new("/echo/1.0.0", |socket| {
|
|
|
|
// This closure is called whenever a stream using the "echo" protocol has been
|
|
|
|
// successfully negotiated. The parameter is the raw socket (implements the AsyncRead
|
|
|
|
// and AsyncWrite traits), and the closure must return an implementation of
|
|
|
|
// `IntoFuture` that can yield any type of object.
|
|
|
|
Ok(length_delimited::Framed::<_, BytesMut>::new(socket))
|
|
|
|
}));
|
2017-11-02 11:58:02 +01:00
|
|
|
|
2017-12-07 18:06:38 +01:00
|
|
|
// We now have a `transport` variable that can be used either to dial nodes or listen to
|
|
|
|
// incoming connections, and that will automatically apply all the selected protocols on top
|
|
|
|
// of any opened stream.
|
2017-11-02 11:58:02 +01:00
|
|
|
|
2017-12-18 11:30:24 +01:00
|
|
|
// We use it to dial the address.
|
2017-12-18 12:29:21 +01:00
|
|
|
let dialer = transport_with_echo
|
|
|
|
.dial(swarm::Multiaddr::new(&target_addr).expect("invalid multiaddr"))
|
2017-12-18 15:55:49 +01:00
|
|
|
// If the multiaddr protocol exists but is not supported, then we get an error containing
|
|
|
|
// the transport and the original multiaddress. Therefore we cannot directly use `unwrap()`
|
|
|
|
// or `expect()`, but have to add a `map_err()` beforehand.
|
|
|
|
.map_err(|(_, addr)| addr).expect("unsupported multiaddr")
|
|
|
|
|
2017-12-18 12:29:21 +01:00
|
|
|
.and_then(|echo| {
|
2017-12-07 18:06:38 +01:00
|
|
|
// `echo` is what the closure used when initializing "echo" returns.
|
|
|
|
// Consequently, please note that the `send` method is available only because the type
|
|
|
|
// `length_delimited::Framed` has a `send` method.
|
2017-12-13 12:08:40 +01:00
|
|
|
println!("Sending \"hello world\" to listener");
|
2017-12-18 12:29:21 +01:00
|
|
|
echo.send("hello world".into())
|
2017-11-02 11:58:02 +01:00
|
|
|
})
|
2017-12-07 18:06:38 +01:00
|
|
|
.and_then(|echo| {
|
|
|
|
// The message has been successfully sent. Now wait for an answer.
|
2017-12-18 12:29:21 +01:00
|
|
|
echo.into_future()
|
2017-11-02 11:58:02 +01:00
|
|
|
.map(|(msg, rest)| {
|
2017-12-13 12:08:40 +01:00
|
|
|
println!("Received message from listener: {:?}", msg);
|
2017-11-02 11:58:02 +01:00
|
|
|
rest
|
|
|
|
})
|
|
|
|
.map_err(|(err, _)| err)
|
|
|
|
});
|
|
|
|
|
2017-12-07 18:06:38 +01:00
|
|
|
// `dialer` is a future that contains all the behaviour that we want, but nothing has actually
|
|
|
|
// started yet. Because we created the `TcpConfig` with tokio, we need to run the future
|
|
|
|
// through the tokio core.
|
2017-12-18 12:29:21 +01:00
|
|
|
core.run(dialer.map(|_| ()).select(transport.incoming().for_each(|_| Ok(()))))
|
|
|
|
.unwrap_or_else(|_| panic!());
|
2017-11-02 11:58:02 +01:00
|
|
|
}
|