rust-libp2p/example/examples/echo-dialer.rs

70 lines
2.6 KiB
Rust
Raw Normal View History

2017-11-02 11:58:02 +01:00
// Copyright 2017 Parity Technologies (UK) Ltd.
//
// 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
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// 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
// 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;
extern crate tokio_core;
extern crate tokio_io;
use bytes::BytesMut;
use futures::{Stream, Sink, Future};
use swarm::Transport;
use tcp::TcpConfig;
2017-11-02 11:58:02 +01:00
use tokio_core::reactor::Core;
use tokio_io::codec::length_delimited;
fn main() {
let mut core = Core::new().unwrap();
let tcp = TcpConfig::new(core.handle());
2017-11-02 11:58:02 +01:00
let with_secio = tcp
2017-12-04 16:36:58 +01:00
.with_upgrade(swarm::PlainTextConfig)
2017-11-02 11:58:02 +01:00
.or_upgrade({
2017-12-04 15:39:40 +01:00
let private_key = include_bytes!("test-private-key.pk8");
2017-11-02 11:58:02 +01:00
let public_key = include_bytes!("test-public-key.der").to_vec();
2017-12-04 15:50:14 +01:00
secio::SecioConfig {
2017-12-04 15:39:40 +01:00
key: secio::SecioKeyPair::rsa_from_pkcs8(private_key, public_key).unwrap(),
2017-11-02 11:58:02 +01:00
}
});
let with_echo = with_secio.with_simple_protocol_upgrade("/echo/1.0.0", |socket| {
Ok(length_delimited::Framed::<_, BytesMut>::new(socket))
});
2017-11-02 11:58:02 +01:00
let dialer = with_echo.dial(swarm::multiaddr::Multiaddr::new("/ip4/127.0.0.1/tcp/10333").unwrap())
.unwrap_or_else(|_| panic!())
2017-11-02 11:58:02 +01:00
.and_then(|f| {
f.send("hello world".into())
})
.and_then(|f| {
f.into_future()
.map(|(msg, rest)| {
println!("received: {:?}", msg);
rest
})
.map_err(|(err, _)| err)
});
core.run(dialer).unwrap();
}