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

72 lines
2.9 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 futures::future::{Future, IntoFuture, loop_fn, Loop};
2017-11-02 11:58:02 +01:00
use futures::{Stream, Sink};
2017-12-07 12:56:44 +01:00
use swarm::{Transport, SimpleProtocol};
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
}
});
2017-12-07 12:56:44 +01:00
let with_echo = with_secio.with_upgrade(SimpleProtocol::new("/echo/1.0.0", |socket| {
Ok(length_delimited::Framed::new(socket))
}));
2017-11-02 11:58:02 +01:00
let future = with_echo.listen_on(swarm::multiaddr::Multiaddr::new("/ip4/0.0.0.0/tcp/10333").unwrap())
.unwrap_or_else(|_| panic!()).0
2017-11-28 12:20:28 +01:00
.for_each(|(socket, _)| {
2017-11-02 11:58:02 +01:00
loop_fn(socket, |socket| {
socket.into_future()
.map_err(|(err, _)| err)
.and_then(|(msg, rest)| {
if let Some(msg) = msg {
Box::new(rest.send(msg).map(|m| Loop::Continue(m))) as Box<Future<Item = _, Error = _>>
} else {
Box::new(Ok(Loop::Break(())).into_future()) as Box<Future<Item = _, Error = _>>
}
})
})
});
core.run(future).unwrap();
}