2018-01-03 14:19:24 +01:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2018-08-08 12:00:38 +02:00
|
|
|
use futures::stream::StreamFuture;
|
|
|
|
use futures::sync::oneshot;
|
|
|
|
use futures::{Async, Future, IntoFuture, Poll, Stream};
|
|
|
|
use futures::task;
|
|
|
|
use parking_lot::Mutex;
|
2018-05-14 15:55:16 +02:00
|
|
|
use std::fmt;
|
2018-07-14 13:46:11 +02:00
|
|
|
use std::io::{Error as IoError, ErrorKind as IoErrorKind};
|
2018-08-08 12:00:38 +02:00
|
|
|
use std::sync::Arc;
|
2018-05-14 15:55:16 +02:00
|
|
|
use {Multiaddr, MuxedTransport, Transport};
|
2018-01-03 14:19:24 +01:00
|
|
|
|
|
|
|
/// Creates a swarm.
|
|
|
|
///
|
|
|
|
/// Requires an upgraded transport, and a function or closure that will turn the upgrade into a
|
|
|
|
/// `Future` that produces a `()`.
|
|
|
|
///
|
|
|
|
/// Produces a `SwarmController` and an implementation of `Future`. The controller can be used to
|
|
|
|
/// control, and the `Future` must be driven to completion in order for things to work.
|
2018-05-14 15:55:16 +02:00
|
|
|
pub fn swarm<T, H, F>(
|
2018-03-07 16:20:55 +01:00
|
|
|
transport: T,
|
|
|
|
handler: H,
|
2018-08-08 12:00:38 +02:00
|
|
|
) -> (SwarmController<T>, SwarmFuture<T, H>)
|
2018-03-07 16:20:55 +01:00
|
|
|
where
|
|
|
|
T: MuxedTransport + Clone + 'static, // TODO: 'static :-/
|
2018-06-19 14:38:55 +02:00
|
|
|
H: FnMut(T::Output, Box<Future<Item = Multiaddr, Error = IoError>>) -> F,
|
2018-03-07 16:20:55 +01:00
|
|
|
F: IntoFuture<Item = (), Error = IoError>,
|
2018-01-03 14:19:24 +01:00
|
|
|
{
|
2018-08-08 12:00:38 +02:00
|
|
|
let shared = Arc::new(Mutex::new(Shared {
|
|
|
|
next_incoming: transport.clone().next_incoming(),
|
|
|
|
listeners: Vec::new(),
|
|
|
|
listeners_upgrade: Vec::new(),
|
|
|
|
dialers: Vec::new(),
|
|
|
|
to_process: Vec::new(),
|
|
|
|
task_to_notify: None,
|
|
|
|
}));
|
2018-01-03 15:46:45 +01:00
|
|
|
|
2018-01-03 14:19:24 +01:00
|
|
|
let future = SwarmFuture {
|
2018-05-14 15:55:16 +02:00
|
|
|
transport: transport.clone(),
|
2018-08-08 12:00:38 +02:00
|
|
|
shared: shared.clone(),
|
2018-01-03 14:19:24 +01:00
|
|
|
handler: handler,
|
|
|
|
};
|
|
|
|
|
|
|
|
let controller = SwarmController {
|
2018-08-08 12:00:38 +02:00
|
|
|
transport,
|
|
|
|
shared,
|
2018-01-03 14:19:24 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
(controller, future)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Allows control of what the swarm is doing.
|
2018-05-14 15:55:16 +02:00
|
|
|
pub struct SwarmController<T>
|
2018-03-07 16:20:55 +01:00
|
|
|
where
|
2018-05-14 15:55:16 +02:00
|
|
|
T: MuxedTransport + 'static, // TODO: 'static :-/
|
2018-01-03 14:19:24 +01:00
|
|
|
{
|
2018-08-08 12:00:38 +02:00
|
|
|
/// Shared between the swarm infrastructure.
|
|
|
|
shared: Arc<Mutex<Shared<T>>>,
|
|
|
|
|
|
|
|
/// Transport used to dial or listen.
|
2018-01-03 15:46:45 +01:00
|
|
|
transport: T,
|
2018-01-03 14:19:24 +01:00
|
|
|
}
|
|
|
|
|
2018-05-14 15:55:16 +02:00
|
|
|
impl<T> fmt::Debug for SwarmController<T>
|
2018-03-15 15:18:21 +01:00
|
|
|
where
|
|
|
|
T: fmt::Debug + MuxedTransport + 'static, // TODO: 'static :-/
|
|
|
|
{
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt.debug_tuple("SwarmController")
|
2018-05-14 15:55:16 +02:00
|
|
|
.field(&self.transport)
|
2018-03-15 15:18:21 +01:00
|
|
|
.finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 15:55:16 +02:00
|
|
|
impl<T> Clone for SwarmController<T>
|
2018-03-15 11:58:11 +01:00
|
|
|
where
|
|
|
|
T: MuxedTransport + Clone + 'static, // TODO: 'static :-/
|
|
|
|
{
|
2018-05-14 15:55:16 +02:00
|
|
|
fn clone(&self) -> SwarmController<T> {
|
2018-03-15 11:58:11 +01:00
|
|
|
SwarmController {
|
|
|
|
transport: self.transport.clone(),
|
2018-08-08 12:00:38 +02:00
|
|
|
shared: self.shared.clone(),
|
2018-03-15 11:58:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-14 15:55:16 +02:00
|
|
|
impl<T> SwarmController<T>
|
2018-03-07 16:20:55 +01:00
|
|
|
where
|
|
|
|
T: MuxedTransport + Clone + 'static, // TODO: 'static :-/
|
2018-01-03 14:19:24 +01:00
|
|
|
{
|
2018-01-03 15:46:45 +01:00
|
|
|
/// Asks the swarm to dial the node with the given multiaddress. The connection is then
|
|
|
|
/// upgraded using the `upgrade`, and the output is sent to the handler that was passed when
|
|
|
|
/// calling `swarm`.
|
2018-07-14 13:46:11 +02:00
|
|
|
///
|
|
|
|
/// Returns a future that is signalled once the closure in the `swarm` has returned its future.
|
|
|
|
/// Therefore if the closure in the swarm has some side effect (eg. write something in a
|
|
|
|
/// variable), this side effect will be observable when this future succeeds.
|
2018-08-08 12:00:38 +02:00
|
|
|
#[inline]
|
2018-07-14 13:46:11 +02:00
|
|
|
pub fn dial<Du>(&self, multiaddr: Multiaddr, transport: Du)
|
|
|
|
-> Result<impl Future<Item = (), Error = IoError>, Multiaddr>
|
2018-03-07 16:20:55 +01:00
|
|
|
where
|
2018-05-14 15:55:16 +02:00
|
|
|
Du: Transport + 'static, // TODO: 'static :-/
|
|
|
|
Du::Output: Into<T::Output>,
|
2018-08-08 12:00:38 +02:00
|
|
|
{
|
|
|
|
self.dial_then(multiaddr, transport, |v| v)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Internal version of `dial` that allows adding a closure that is called after either the
|
|
|
|
/// dialing fails or the handler has been called with the resulting future.
|
|
|
|
///
|
|
|
|
/// The returned future is filled with the output of `then`.
|
|
|
|
pub(crate) fn dial_then<Du, F>(&self, multiaddr: Multiaddr, transport: Du, then: F)
|
|
|
|
-> Result<impl Future<Item = (), Error = IoError>, Multiaddr>
|
|
|
|
where
|
|
|
|
Du: Transport + 'static, // TODO: 'static :-/
|
|
|
|
Du::Output: Into<T::Output>,
|
|
|
|
F: FnOnce(Result<(), IoError>) -> Result<(), IoError> + 'static,
|
2018-01-03 15:46:45 +01:00
|
|
|
{
|
2018-05-17 15:14:13 +02:00
|
|
|
trace!("Swarm dialing {}", multiaddr);
|
2018-03-16 16:39:02 +01:00
|
|
|
|
2018-05-14 15:55:16 +02:00
|
|
|
match transport.dial(multiaddr.clone()) {
|
2018-01-03 14:19:24 +01:00
|
|
|
Ok(dial) => {
|
2018-07-14 13:46:11 +02:00
|
|
|
let (tx, rx) = oneshot::channel();
|
2018-08-08 12:00:38 +02:00
|
|
|
let mut then = Some(move |val| {
|
|
|
|
let _ = tx.send(then(val));
|
|
|
|
});
|
|
|
|
// Unfortunately the `Box<FnOnce(_)>` type is still unusable in Rust right now,
|
|
|
|
// so we use a `Box<FnMut(_)>` instead and panic if it is called multiple times.
|
|
|
|
let mut then = Box::new(move |val: Result<(), IoError>| {
|
|
|
|
let then = then.take().expect("The Boxed FnMut should only be called once");
|
|
|
|
then(val);
|
|
|
|
}) as Box<FnMut(_)>;
|
|
|
|
|
2018-07-14 13:46:11 +02:00
|
|
|
let dial = dial.then(|result| {
|
|
|
|
match result {
|
|
|
|
Ok((output, client_addr)) => {
|
|
|
|
let client_addr = Box::new(client_addr) as Box<Future<Item = _, Error = _>>;
|
2018-08-08 12:00:38 +02:00
|
|
|
Ok((output.into(), then, client_addr))
|
2018-07-14 13:46:11 +02:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
debug!("Error in dialer upgrade: {:?}", err);
|
2018-08-08 12:00:38 +02:00
|
|
|
then(Err(err));
|
2018-07-14 13:46:11 +02:00
|
|
|
Err(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-08-08 12:00:38 +02:00
|
|
|
|
|
|
|
let mut shared = self.shared.lock();
|
|
|
|
shared.dialers.push((multiaddr, Box::new(dial) as Box<_>));
|
|
|
|
if let Some(task) = shared.task_to_notify.take() {
|
|
|
|
task.notify();
|
|
|
|
}
|
|
|
|
|
2018-07-14 13:46:11 +02:00
|
|
|
Ok(rx.then(|result| {
|
|
|
|
match result {
|
|
|
|
Ok(Ok(())) => Ok(()),
|
|
|
|
Ok(Err(err)) => Err(err),
|
|
|
|
Err(_) => Err(IoError::new(IoErrorKind::ConnectionAborted,
|
|
|
|
"dial cancelled the swarm future has been destroyed")),
|
|
|
|
}
|
|
|
|
}))
|
2018-03-07 16:20:55 +01:00
|
|
|
}
|
|
|
|
Err((_, multiaddr)) => Err(multiaddr),
|
2018-01-03 14:19:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 12:00:38 +02:00
|
|
|
/// Interrupts all dialing attempts to a specific multiaddress.
|
|
|
|
///
|
|
|
|
/// Has no effect if the dialing attempt has already succeeded, in which case it will be
|
|
|
|
/// dispatched to the handler.
|
|
|
|
pub fn interrupt_dial(&self, multiaddr: &Multiaddr) {
|
|
|
|
let mut shared = self.shared.lock();
|
|
|
|
shared.dialers.retain(|dialer| {
|
|
|
|
&dialer.0 != multiaddr
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-03 15:46:45 +01:00
|
|
|
/// Adds a multiaddr to listen on. All the incoming connections will use the `upgrade` that
|
|
|
|
/// was passed to `swarm`.
|
2018-08-08 12:00:38 +02:00
|
|
|
// TODO: add a way to cancel a listener
|
2018-01-03 14:19:24 +01:00
|
|
|
pub fn listen_on(&self, multiaddr: Multiaddr) -> Result<Multiaddr, Multiaddr> {
|
2018-05-14 15:55:16 +02:00
|
|
|
match self.transport.clone().listen_on(multiaddr) {
|
2018-01-03 14:19:24 +01:00
|
|
|
Ok((listener, new_addr)) => {
|
2018-05-17 15:14:13 +02:00
|
|
|
trace!("Swarm listening on {}", new_addr);
|
2018-08-08 12:00:38 +02:00
|
|
|
let mut shared = self.shared.lock();
|
|
|
|
let listener = Box::new(
|
|
|
|
listener.map(|f| {
|
|
|
|
let f = f.map(|(out, maf)| {
|
|
|
|
(out, Box::new(maf) as Box<Future<Item = Multiaddr, Error = IoError>>)
|
|
|
|
});
|
|
|
|
|
|
|
|
Box::new(f) as Box<Future<Item = _, Error = _>>
|
|
|
|
}),
|
|
|
|
) as Box<Stream<Item = _, Error = _>>;
|
|
|
|
shared.listeners.push(listener.into_future());
|
|
|
|
if let Some(task) = shared.task_to_notify.take() {
|
|
|
|
task.notify();
|
|
|
|
}
|
2018-01-03 14:19:24 +01:00
|
|
|
Ok(new_addr)
|
2018-03-07 16:20:55 +01:00
|
|
|
}
|
|
|
|
Err((_, multiaddr)) => Err(multiaddr),
|
2018-01-03 14:19:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Future that must be driven to completion in order for the swarm to work.
|
2018-09-04 18:30:57 +08:00
|
|
|
#[must_use = "futures do nothing unless polled"]
|
2018-08-08 12:00:38 +02:00
|
|
|
pub struct SwarmFuture<T, H>
|
2018-03-07 16:20:55 +01:00
|
|
|
where
|
2018-05-14 15:55:16 +02:00
|
|
|
T: MuxedTransport + 'static, // TODO: 'static :-/
|
2018-01-03 14:19:24 +01:00
|
|
|
{
|
2018-08-08 12:00:38 +02:00
|
|
|
/// Shared between the swarm infrastructure.
|
|
|
|
shared: Arc<Mutex<Shared<T>>>,
|
|
|
|
|
|
|
|
/// The transport used to dial.
|
2018-05-14 15:55:16 +02:00
|
|
|
transport: T,
|
2018-08-08 12:00:38 +02:00
|
|
|
|
|
|
|
/// Swarm handler.
|
2018-01-03 14:19:24 +01:00
|
|
|
handler: H,
|
|
|
|
}
|
|
|
|
|
2018-08-08 12:00:38 +02:00
|
|
|
impl<T, H, If, F> Future for SwarmFuture<T, H>
|
2018-03-07 16:20:55 +01:00
|
|
|
where
|
|
|
|
T: MuxedTransport + Clone + 'static, // TODO: 'static :-/,
|
2018-06-19 14:38:55 +02:00
|
|
|
H: FnMut(T::Output, Box<Future<Item = Multiaddr, Error = IoError>>) -> If,
|
2018-03-07 16:20:55 +01:00
|
|
|
If: IntoFuture<Future = F, Item = (), Error = IoError>,
|
2018-08-08 12:00:38 +02:00
|
|
|
F: Future<Item = (), Error = IoError> + 'static, // TODO: 'static :-/
|
2018-01-03 14:19:24 +01:00
|
|
|
{
|
|
|
|
type Item = ();
|
|
|
|
type Error = IoError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
2018-08-08 12:00:38 +02:00
|
|
|
let mut shared = self.shared.lock();
|
2018-01-03 14:19:24 +01:00
|
|
|
let handler = &mut self.handler;
|
|
|
|
|
2018-06-25 14:29:15 +02:00
|
|
|
loop {
|
2018-08-08 12:00:38 +02:00
|
|
|
match shared.next_incoming.poll() {
|
2018-06-25 14:29:15 +02:00
|
|
|
Ok(Async::Ready(connec)) => {
|
|
|
|
debug!("Swarm received new multiplexed incoming connection");
|
2018-08-08 12:00:38 +02:00
|
|
|
shared.next_incoming = self.transport.clone().next_incoming();
|
2018-06-25 14:29:15 +02:00
|
|
|
let connec = connec.map(|(out, maf)| {
|
|
|
|
(out, Box::new(maf) as Box<Future<Item = Multiaddr, Error = IoError>>)
|
|
|
|
});
|
2018-08-08 12:00:38 +02:00
|
|
|
shared.listeners_upgrade.push(Box::new(connec) as Box<_>);
|
2018-06-25 14:29:15 +02:00
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => break,
|
|
|
|
Err(err) => {
|
2018-08-08 12:00:38 +02:00
|
|
|
// TODO: should that stop everything?
|
2018-06-25 14:29:15 +02:00
|
|
|
debug!("Error in multiplexed incoming connection: {:?}", err);
|
2018-08-08 12:00:38 +02:00
|
|
|
shared.next_incoming = self.transport.clone().next_incoming();
|
2018-06-25 14:29:15 +02:00
|
|
|
break;
|
|
|
|
}
|
2018-03-07 16:20:55 +01:00
|
|
|
}
|
2018-06-22 11:02:47 +02:00
|
|
|
}
|
2018-01-03 14:19:24 +01:00
|
|
|
|
2018-08-08 12:00:38 +02:00
|
|
|
// We remove each element from `shared.listeners` one by one and add them back only
|
|
|
|
// if relevant.
|
|
|
|
for n in (0 .. shared.listeners.len()).rev() {
|
|
|
|
let mut listener = shared.listeners.swap_remove(n);
|
|
|
|
loop {
|
|
|
|
match listener.poll() {
|
|
|
|
Ok(Async::Ready((Some(upgrade), remaining))) => {
|
|
|
|
trace!("Swarm received new connection on listener socket");
|
|
|
|
shared.listeners_upgrade.push(upgrade);
|
|
|
|
listener = remaining.into_future();
|
|
|
|
}
|
|
|
|
Ok(Async::Ready((None, _))) => {
|
|
|
|
debug!("Listener closed gracefully");
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
Err((err, _)) => {
|
|
|
|
debug!("Error in listener: {:?}", err);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
shared.listeners.push(listener);
|
|
|
|
break;
|
|
|
|
}
|
2018-05-21 16:27:03 +02:00
|
|
|
}
|
2018-04-16 18:55:16 +09:00
|
|
|
}
|
2018-01-03 14:19:24 +01:00
|
|
|
}
|
|
|
|
|
2018-08-08 12:00:38 +02:00
|
|
|
// We remove each element from `shared.listeners_upgrade` one by one and add them back
|
|
|
|
// only if relevant.
|
|
|
|
for n in (0 .. shared.listeners_upgrade.len()).rev() {
|
|
|
|
let mut listener_upgrade = shared.listeners_upgrade.swap_remove(n);
|
|
|
|
match listener_upgrade.poll() {
|
|
|
|
Ok(Async::Ready((output, client_addr))) => {
|
2018-06-22 11:02:47 +02:00
|
|
|
debug!("Successfully upgraded incoming connection");
|
2018-08-08 12:00:38 +02:00
|
|
|
// TODO: unlock mutex before calling handler, in order to avoid deadlocks if
|
|
|
|
// the user does something stupid
|
|
|
|
shared.to_process.push(Box::new(handler(output, client_addr).into_future()));
|
2018-06-22 11:02:47 +02:00
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
debug!("Error in listener upgrade: {:?}", err);
|
|
|
|
}
|
2018-08-08 12:00:38 +02:00
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
shared.listeners_upgrade.push(listener_upgrade);
|
|
|
|
},
|
2018-04-16 18:55:16 +09:00
|
|
|
}
|
2018-01-03 14:19:24 +01:00
|
|
|
}
|
|
|
|
|
2018-08-08 12:00:38 +02:00
|
|
|
// We remove each element from `shared.dialers` one by one and add them back only
|
|
|
|
// if relevant.
|
|
|
|
for n in (0 .. shared.dialers.len()).rev() {
|
|
|
|
let (client_addr, mut dialer) = shared.dialers.swap_remove(n);
|
|
|
|
match dialer.poll() {
|
|
|
|
Ok(Async::Ready((output, mut notifier, addr))) => {
|
2018-06-22 11:02:47 +02:00
|
|
|
trace!("Successfully upgraded dialed connection");
|
2018-08-08 12:00:38 +02:00
|
|
|
// TODO: unlock mutex before calling handler, in order to avoid deadlocks if
|
|
|
|
// the user does something stupid
|
|
|
|
shared.to_process.push(Box::new(handler(output, addr).into_future()));
|
|
|
|
notifier(Ok(()));
|
2018-06-22 11:02:47 +02:00
|
|
|
}
|
2018-08-08 12:00:38 +02:00
|
|
|
Err(()) => {},
|
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
shared.dialers.push((client_addr, dialer));
|
|
|
|
},
|
2018-01-03 14:19:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 12:00:38 +02:00
|
|
|
// We remove each element from `shared.to_process` one by one and add them back only
|
|
|
|
// if relevant.
|
|
|
|
for n in (0 .. shared.to_process.len()).rev() {
|
|
|
|
let mut to_process = shared.to_process.swap_remove(n);
|
|
|
|
match to_process.poll() {
|
|
|
|
Ok(Async::Ready(())) => {
|
2018-06-22 11:02:47 +02:00
|
|
|
trace!("Future returned by swarm handler driven to completion");
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
debug!("Error in processing: {:?}", err);
|
|
|
|
}
|
2018-08-08 12:00:38 +02:00
|
|
|
Ok(Async::NotReady) => {
|
|
|
|
shared.to_process.push(to_process);
|
|
|
|
}
|
2018-01-03 14:19:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: we never return `Ok(Ready)` because there's no way to know whether
|
2018-08-08 12:00:38 +02:00
|
|
|
// `next_incoming()` can produce anything more in the future ; also we would need to
|
|
|
|
// know when the controller has been dropped
|
|
|
|
shared.task_to_notify = Some(task::current());
|
2018-01-03 14:19:24 +01:00
|
|
|
Ok(Async::NotReady)
|
|
|
|
}
|
|
|
|
}
|
2018-07-14 13:31:22 +02:00
|
|
|
|
2018-08-08 12:00:38 +02:00
|
|
|
// TODO: stronger typing
|
|
|
|
struct Shared<T> where T: MuxedTransport + 'static {
|
|
|
|
/// Next incoming substream on the transport.
|
|
|
|
next_incoming: T::Incoming,
|
|
|
|
|
|
|
|
/// All the active listeners.
|
|
|
|
listeners: Vec<
|
|
|
|
StreamFuture<
|
|
|
|
Box<
|
|
|
|
Stream<
|
|
|
|
Item = Box<Future<Item = (T::Output, Box<Future<Item = Multiaddr, Error = IoError>>), Error = IoError>>,
|
|
|
|
Error = IoError,
|
|
|
|
>,
|
|
|
|
>,
|
|
|
|
>,
|
|
|
|
>,
|
|
|
|
|
|
|
|
/// Futures that upgrade an incoming listening connection to a full connection.
|
|
|
|
listeners_upgrade:
|
|
|
|
Vec<Box<Future<Item = (T::Output, Box<Future<Item = Multiaddr, Error = IoError>>), Error = IoError>>>,
|
|
|
|
|
|
|
|
/// Futures that dial a remote address.
|
|
|
|
///
|
|
|
|
/// Contains the address we dial, so that we can cancel it if necessary.
|
|
|
|
dialers: Vec<(Multiaddr, Box<Future<Item = (T::Output, Box<FnMut(Result<(), IoError>)>, Box<Future<Item = Multiaddr, Error = IoError>>), Error = ()>>)>,
|
|
|
|
|
|
|
|
/// List of futures produced by the swarm closure. Must be processed to the end.
|
|
|
|
to_process: Vec<Box<Future<Item = (), Error = IoError>>>,
|
|
|
|
|
|
|
|
/// The task to notify whenever we add a new element in one of the lists.
|
|
|
|
/// Necessary so that the task wakes up and the element gets polled.
|
|
|
|
task_to_notify: Option<task::Task>,
|
|
|
|
}
|
|
|
|
|
2018-07-14 13:31:22 +02:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-08-08 12:00:38 +02:00
|
|
|
use futures::{Future, future};
|
|
|
|
use rand;
|
|
|
|
use transport::{self, DeniedTransport, Transport};
|
|
|
|
use std::io::Error as IoError;
|
|
|
|
use std::sync::{atomic, Arc};
|
2018-07-14 13:31:22 +02:00
|
|
|
use swarm;
|
2018-08-08 12:00:38 +02:00
|
|
|
use tokio::runtime::current_thread;
|
2018-07-14 13:31:22 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn transport_error_propagation_listen() {
|
|
|
|
let (swarm_ctrl, _swarm_future) = swarm(DeniedTransport, |_, _| future::empty());
|
|
|
|
assert!(swarm_ctrl.listen_on("/ip4/127.0.0.1/tcp/10000".parse().unwrap()).is_err());
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn transport_error_propagation_dial() {
|
|
|
|
let (swarm_ctrl, _swarm_future) = swarm(DeniedTransport, |_, _| future::empty());
|
|
|
|
let addr = "/ip4/127.0.0.1/tcp/10000".parse().unwrap();
|
|
|
|
assert!(swarm_ctrl.dial(addr, DeniedTransport).is_err());
|
|
|
|
}
|
2018-08-08 12:00:38 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn basic_dial() {
|
|
|
|
let (tx, rx) = transport::connector();
|
|
|
|
|
|
|
|
let reached_tx = Arc::new(atomic::AtomicBool::new(false));
|
|
|
|
let reached_tx2 = reached_tx.clone();
|
|
|
|
|
|
|
|
let reached_rx = Arc::new(atomic::AtomicBool::new(false));
|
|
|
|
let reached_rx2 = reached_rx.clone();
|
|
|
|
|
|
|
|
let (swarm_ctrl1, swarm_future1) = swarm(rx.with_dummy_muxing(), |_, _| {
|
|
|
|
reached_rx2.store(true, atomic::Ordering::SeqCst);
|
|
|
|
future::empty()
|
|
|
|
});
|
|
|
|
swarm_ctrl1.listen_on("/memory".parse().unwrap()).unwrap();
|
|
|
|
|
|
|
|
let (swarm_ctrl2, swarm_future2) = swarm(tx.clone().with_dummy_muxing(), |_, _| {
|
|
|
|
reached_tx2.store(true, atomic::Ordering::SeqCst);
|
|
|
|
future::empty()
|
|
|
|
});
|
|
|
|
|
|
|
|
let dial_success = swarm_ctrl2.dial("/memory".parse().unwrap(), tx).unwrap();
|
|
|
|
let future = swarm_future2
|
|
|
|
.select(swarm_future1).map(|_| ()).map_err(|(err, _)| err)
|
|
|
|
.select(dial_success).map(|_| ()).map_err(|(err, _)| err);
|
|
|
|
|
|
|
|
current_thread::Runtime::new().unwrap().block_on(future).unwrap();
|
|
|
|
assert!(reached_tx.load(atomic::Ordering::SeqCst));
|
|
|
|
assert!(reached_rx.load(atomic::Ordering::SeqCst));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dial_multiple_times() {
|
|
|
|
let (tx, rx) = transport::connector();
|
|
|
|
let reached = Arc::new(atomic::AtomicUsize::new(0));
|
|
|
|
let reached2 = reached.clone();
|
|
|
|
let (swarm_ctrl, swarm_future) = swarm(rx.with_dummy_muxing(), |_, _| {
|
|
|
|
reached2.fetch_add(1, atomic::Ordering::SeqCst);
|
|
|
|
future::empty()
|
|
|
|
});
|
|
|
|
swarm_ctrl.listen_on("/memory".parse().unwrap()).unwrap();
|
|
|
|
let num_dials = 20000 + rand::random::<usize>() % 20000;
|
|
|
|
let mut dials = Vec::new();
|
|
|
|
for _ in 0 .. num_dials {
|
|
|
|
let f = swarm_ctrl.dial("/memory".parse().unwrap(), tx.clone()).unwrap();
|
|
|
|
dials.push(f);
|
|
|
|
}
|
|
|
|
let future = future::join_all(dials)
|
|
|
|
.map(|_| ())
|
|
|
|
.select(swarm_future)
|
|
|
|
.map_err(|(err, _)| err);
|
|
|
|
current_thread::Runtime::new().unwrap().block_on(future).unwrap();
|
|
|
|
assert_eq!(reached.load(atomic::Ordering::SeqCst), num_dials);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn future_isnt_dropped() {
|
|
|
|
// Tests that the future in the closure isn't being dropped.
|
|
|
|
let (tx, rx) = transport::connector();
|
|
|
|
let (swarm_ctrl, swarm_future) = swarm(rx.with_dummy_muxing(), |_, _| {
|
|
|
|
future::empty()
|
|
|
|
.then(|_: Result<(), ()>| -> Result<(), IoError> { panic!() }) // <-- the test
|
|
|
|
});
|
|
|
|
swarm_ctrl.listen_on("/memory".parse().unwrap()).unwrap();
|
|
|
|
let dial_success = swarm_ctrl.dial("/memory".parse().unwrap(), tx).unwrap();
|
|
|
|
let future = dial_success.select(swarm_future)
|
|
|
|
.map_err(|(err, _)| err);
|
|
|
|
current_thread::Runtime::new().unwrap().block_on(future).unwrap();
|
|
|
|
}
|
2018-07-14 13:31:22 +02:00
|
|
|
}
|