mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-25 15:51:34 +00:00
Swarm rework (#182)
* Rename Transport::RawConn to Output * Remove AsyncRead + AsyncWrite bound on Transport::Output * UpgradedNode now always implements Transport * Add and tweak modifiers for Transport and ConnectionUpgrade * Secio upgrade now returns the pubkey in its output * Add upgrade::apply * Add Transport::and_then * Rework the swarm * Rustfmt * Fix concerns
This commit is contained in:
@ -29,11 +29,11 @@ extern crate libp2p_websocket as websocket;
|
||||
extern crate tokio_core;
|
||||
extern crate tokio_io;
|
||||
|
||||
use futures::{Future, Sink, Stream};
|
||||
use futures::sync::oneshot;
|
||||
use futures::{Future, Sink, Stream};
|
||||
use std::env;
|
||||
use swarm::Transport;
|
||||
use swarm::upgrade::{self, DeniedConnectionUpgrade, SimpleProtocol, UpgradeExt};
|
||||
use swarm::upgrade::{self, DeniedConnectionUpgrade, SimpleProtocol};
|
||||
use tcp::TcpConfig;
|
||||
use tokio_core::reactor::Core;
|
||||
use tokio_io::AsyncRead;
|
||||
@ -73,7 +73,7 @@ fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
plain_text.or_upgrade(secio)
|
||||
upgrade::or(plain_text, upgrade::map(secio, |(socket, _)| socket))
|
||||
})
|
||||
|
||||
// On top of plaintext or secio, we will use the multiplex protocol.
|
||||
@ -89,8 +89,7 @@ fn main() {
|
||||
// by the listening part. We don't want to accept anything, so we pass a dummy object that
|
||||
// represents a connection that is always denied.
|
||||
let (swarm_controller, swarm_future) = swarm::swarm(
|
||||
transport,
|
||||
DeniedConnectionUpgrade,
|
||||
transport.clone().with_upgrade(DeniedConnectionUpgrade),
|
||||
|_socket, _client_addr| -> Result<(), _> {
|
||||
unreachable!("All incoming connections should have been denied")
|
||||
},
|
||||
@ -108,7 +107,7 @@ fn main() {
|
||||
// We now use the controller to dial to the address.
|
||||
let (finished_tx, finished_rx) = oneshot::channel();
|
||||
swarm_controller
|
||||
.dial_custom_handler(target_addr.parse().expect("invalid multiaddr"), proto, |echo, _| {
|
||||
.dial_custom_handler(target_addr.parse().expect("invalid multiaddr"), transport.with_upgrade(proto), |echo, _| {
|
||||
// `echo` is what the closure used when initializing `proto` returns.
|
||||
// Consequently, please note that the `send` method is available only because the type
|
||||
// `length_delimited::Framed` has a `send` method.
|
||||
|
@ -33,7 +33,7 @@ use futures::future::{loop_fn, Future, IntoFuture, Loop};
|
||||
use futures::{Sink, Stream};
|
||||
use std::env;
|
||||
use swarm::Transport;
|
||||
use swarm::upgrade::{self, SimpleProtocol, UpgradeExt};
|
||||
use swarm::upgrade::{self, SimpleProtocol};
|
||||
use tcp::TcpConfig;
|
||||
use tokio_core::reactor::Core;
|
||||
use tokio_io::AsyncRead;
|
||||
@ -72,7 +72,7 @@ fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
plain_text.or_upgrade(secio)
|
||||
upgrade::or(plain_text, upgrade::map(secio, |(socket, _)| socket))
|
||||
})
|
||||
|
||||
// On top of plaintext or secio, we will use the multiplex protocol.
|
||||
@ -99,36 +99,39 @@ fn main() {
|
||||
|
||||
// Let's put this `transport` into a *swarm*. The swarm will handle all the incoming and
|
||||
// outgoing connections for us.
|
||||
let (swarm_controller, swarm_future) = swarm::swarm(transport, proto, |socket, client_addr| {
|
||||
println!("Successfully negotiated protocol with {}", client_addr);
|
||||
let (swarm_controller, swarm_future) = swarm::swarm(
|
||||
transport.clone().with_upgrade(proto),
|
||||
|socket, client_addr| {
|
||||
println!("Successfully negotiated protocol with {}", client_addr);
|
||||
|
||||
// The type of `socket` is exactly what the closure of `SimpleProtocol` returns.
|
||||
// The type of `socket` is exactly what the closure of `SimpleProtocol` returns.
|
||||
|
||||
// We loop forever in order to handle all the messages sent by the client.
|
||||
loop_fn(socket, move |socket| {
|
||||
let client_addr = client_addr.clone();
|
||||
socket
|
||||
.into_future()
|
||||
.map_err(|(e, _)| e)
|
||||
.and_then(move |(msg, rest)| {
|
||||
if let Some(msg) = msg {
|
||||
// One message has been received. We send it back to the client.
|
||||
println!(
|
||||
"Received a message from {}: {:?}\n => Sending back \
|
||||
identical message to remote",
|
||||
client_addr, msg
|
||||
);
|
||||
Box::new(rest.send(msg.freeze()).map(|m| Loop::Continue(m)))
|
||||
as Box<Future<Item = _, Error = _>>
|
||||
} else {
|
||||
// End of stream. Connection closed. Breaking the loop.
|
||||
println!("Received EOF from {}\n => Dropping connection", client_addr);
|
||||
Box::new(Ok(Loop::Break(())).into_future())
|
||||
as Box<Future<Item = _, Error = _>>
|
||||
}
|
||||
})
|
||||
})
|
||||
});
|
||||
// We loop forever in order to handle all the messages sent by the client.
|
||||
loop_fn(socket, move |socket| {
|
||||
let client_addr = client_addr.clone();
|
||||
socket
|
||||
.into_future()
|
||||
.map_err(|(e, _)| e)
|
||||
.and_then(move |(msg, rest)| {
|
||||
if let Some(msg) = msg {
|
||||
// One message has been received. We send it back to the client.
|
||||
println!(
|
||||
"Received a message from {}: {:?}\n => Sending back \
|
||||
identical message to remote",
|
||||
client_addr, msg
|
||||
);
|
||||
Box::new(rest.send(msg.freeze()).map(|m| Loop::Continue(m)))
|
||||
as Box<Future<Item = _, Error = _>>
|
||||
} else {
|
||||
// End of stream. Connection closed. Breaking the loop.
|
||||
println!("Received EOF from {}\n => Dropping connection", client_addr);
|
||||
Box::new(Ok(Loop::Break(())).into_future())
|
||||
as Box<Future<Item = _, Error = _>>
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
);
|
||||
|
||||
// We now use the controller to listen on the address.
|
||||
let address = swarm_controller
|
||||
|
@ -33,12 +33,12 @@ extern crate tokio_core;
|
||||
extern crate tokio_io;
|
||||
extern crate tokio_stdin;
|
||||
|
||||
use futures::future::Future;
|
||||
use futures::Stream;
|
||||
use futures::future::Future;
|
||||
use peerstore::PeerId;
|
||||
use std::{env, mem};
|
||||
use swarm::upgrade;
|
||||
use swarm::{Multiaddr, Transport};
|
||||
use swarm::upgrade::{self, UpgradeExt};
|
||||
use tcp::TcpConfig;
|
||||
use tokio_core::reactor::Core;
|
||||
use websocket::WsConfig;
|
||||
@ -75,7 +75,7 @@ fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
plain_text.or_upgrade(secio)
|
||||
upgrade::or(plain_text, upgrade::map(secio, |(socket, _)| socket))
|
||||
})
|
||||
|
||||
// On top of plaintext or secio, we will use the multiplex protocol.
|
||||
@ -102,8 +102,7 @@ fn main() {
|
||||
// Let's put this `transport` into a *swarm*. The swarm will handle all the incoming and
|
||||
// outgoing connections for us.
|
||||
let (swarm_controller, swarm_future) = swarm::swarm(
|
||||
transport,
|
||||
floodsub_upgrade.clone(),
|
||||
transport.clone().with_upgrade(floodsub_upgrade.clone()),
|
||||
|socket, client_addr| {
|
||||
println!("Successfully negotiated protocol with {}", client_addr);
|
||||
socket
|
||||
@ -142,7 +141,10 @@ fn main() {
|
||||
let target: Multiaddr = msg[6..].parse().unwrap();
|
||||
println!("*Dialing {}*", target);
|
||||
swarm_controller
|
||||
.dial_to_handler(target, floodsub_upgrade.clone())
|
||||
.dial_to_handler(
|
||||
target,
|
||||
transport.clone().with_upgrade(floodsub_upgrade.clone()),
|
||||
)
|
||||
.unwrap();
|
||||
} else {
|
||||
floodsub_ctl.publish(&topic, msg.into_bytes());
|
||||
|
@ -40,7 +40,7 @@ use std::env;
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
use swarm::Transport;
|
||||
use swarm::upgrade::{self, UpgradeExt};
|
||||
use swarm::upgrade;
|
||||
use tcp::TcpConfig;
|
||||
use tokio_core::reactor::Core;
|
||||
|
||||
@ -80,7 +80,7 @@ fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
plain_text.or_upgrade(secio)
|
||||
upgrade::or(plain_text, upgrade::map(secio, |(socket, _)| socket))
|
||||
})
|
||||
|
||||
// On top of plaintext or secio, we will use the multiplex protocol.
|
||||
@ -116,9 +116,13 @@ fn main() {
|
||||
|
||||
// Let's put this `transport` into a *swarm*. The swarm will handle all the incoming and
|
||||
// outgoing connections for us.
|
||||
let (swarm_controller, swarm_future) = swarm::swarm(transport, proto, |upgrade, _| upgrade);
|
||||
let (swarm_controller, swarm_future) = swarm::swarm(
|
||||
transport.clone().with_upgrade(proto.clone()),
|
||||
|upgrade, _| upgrade,
|
||||
);
|
||||
|
||||
let (kad_controller, _kad_init) = kad_ctl_proto.start(swarm_controller.clone());
|
||||
let (kad_controller, _kad_init) =
|
||||
kad_ctl_proto.start(swarm_controller.clone(), transport.with_upgrade(proto));
|
||||
|
||||
for listen_addr in listen_addrs {
|
||||
let addr = swarm_controller
|
||||
|
@ -33,7 +33,7 @@ use futures::Future;
|
||||
use futures::sync::oneshot;
|
||||
use std::env;
|
||||
use swarm::Transport;
|
||||
use swarm::upgrade::{self, DeniedConnectionUpgrade, UpgradeExt};
|
||||
use swarm::upgrade::{self, DeniedConnectionUpgrade};
|
||||
use tcp::TcpConfig;
|
||||
use tokio_core::reactor::Core;
|
||||
|
||||
@ -65,7 +65,7 @@ fn main() {
|
||||
}
|
||||
};
|
||||
|
||||
plain_text.or_upgrade(secio)
|
||||
upgrade::or(plain_text, upgrade::map(secio, |(socket, _)| socket))
|
||||
})
|
||||
|
||||
// On top of plaintext or secio, we will use the multiplex protocol.
|
||||
@ -81,8 +81,7 @@ fn main() {
|
||||
// by the listening part. We don't want to accept anything, so we pass a dummy object that
|
||||
// represents a connection that is always denied.
|
||||
let (swarm_controller, swarm_future) = swarm::swarm(
|
||||
transport,
|
||||
DeniedConnectionUpgrade,
|
||||
transport.clone().with_upgrade(DeniedConnectionUpgrade),
|
||||
|_socket, _client_addr| -> Result<(), _> {
|
||||
unreachable!("All incoming connections should have been denied")
|
||||
},
|
||||
@ -91,7 +90,8 @@ fn main() {
|
||||
// We now use the controller to dial to the address.
|
||||
let (tx, rx) = oneshot::channel();
|
||||
swarm_controller
|
||||
.dial_custom_handler(target_addr.parse().expect("invalid multiaddr"), ping::Ping,
|
||||
.dial_custom_handler(target_addr.parse().expect("invalid multiaddr"),
|
||||
transport.with_upgrade(ping::Ping),
|
||||
|(mut pinger, future), _| {
|
||||
let ping = pinger.ping().map_err(|_| unreachable!()).inspect(|_| {
|
||||
println!("Received pong from the remote");
|
||||
|
Reference in New Issue
Block a user