Switch MemoryTransport to Vec<u8> and fix tests (#1274)

This commit is contained in:
Pierre Krieger
2019-10-21 15:14:31 +00:00
committed by GitHub
parent abe2f2afc1
commit b1f31111e0
7 changed files with 127 additions and 129 deletions

View File

@ -51,32 +51,30 @@ use std::{collections::VecDeque, fmt, pin::Pin};
/// listeners.listen_on("/ip4/0.0.0.0/tcp/0".parse().unwrap()).unwrap();
///
/// // The `listeners` will now generate events when polled.
/// let future = listeners.for_each(move |event| {
/// match event {
/// ListenersEvent::NewAddress { listener_id, listen_addr } => {
/// println!("Listener {:?} is listening at address {}", listener_id, listen_addr);
/// },
/// ListenersEvent::AddressExpired { listener_id, listen_addr } => {
/// println!("Listener {:?} is no longer listening at address {}", listener_id, listen_addr);
/// },
/// ListenersEvent::Closed { listener_id, .. } => {
/// println!("Listener {:?} has been closed", listener_id);
/// },
/// ListenersEvent::Error { listener_id, error } => {
/// println!("Listener {:?} has experienced an error: {}", listener_id, error);
/// },
/// ListenersEvent::Incoming { listener_id, upgrade, local_addr, .. } => {
/// println!("Listener {:?} has a new connection on {}", listener_id, local_addr);
/// // We don't do anything with the newly-opened connection, but in a real-life
/// // program you probably want to use it!
/// drop(upgrade);
/// },
/// };
///
/// Ok(())
/// });
///
/// tokio::run(future.map_err(|_| ()));
/// futures::executor::block_on(async move {
/// while let Some(event) = listeners.next().await {
/// match event {
/// ListenersEvent::NewAddress { listener_id, listen_addr } => {
/// println!("Listener {:?} is listening at address {}", listener_id, listen_addr);
/// },
/// ListenersEvent::AddressExpired { listener_id, listen_addr } => {
/// println!("Listener {:?} is no longer listening at address {}", listener_id, listen_addr);
/// },
/// ListenersEvent::Closed { listener_id, .. } => {
/// println!("Listener {:?} has been closed", listener_id);
/// },
/// ListenersEvent::Error { listener_id, error } => {
/// println!("Listener {:?} has experienced an error: {}", listener_id, error);
/// },
/// ListenersEvent::Incoming { listener_id, upgrade, local_addr, .. } => {
/// println!("Listener {:?} has a new connection on {}", listener_id, local_addr);
/// // We don't do anything with the newly-opened connection, but in a real-life
/// // program you probably want to use it!
/// drop(upgrade);
/// },
/// }
/// }
/// })
/// # }
/// ```
pub struct ListenersStream<TTrans>
@ -358,7 +356,6 @@ mod tests {
use super::*;
use crate::transport::{self, ListenerEvent};
use assert_matches::assert_matches;
use tokio::runtime::current_thread::Runtime;
use std::{io, iter::FromIterator};
use futures::{future::{self}, stream};
use crate::PeerId;

View File

@ -19,7 +19,7 @@
// DEALINGS IN THE SOFTWARE.
use crate::{Transport, transport::{TransportError, ListenerEvent}};
use bytes::{Bytes, IntoBuf};
use bytes::IntoBuf;
use fnv::FnvHashMap;
use futures::{future::{self, Ready}, prelude::*, channel::mpsc, task::Context, task::Poll};
use lazy_static::lazy_static;
@ -29,7 +29,7 @@ use rw_stream_sink::RwStreamSink;
use std::{collections::hash_map::Entry, error, fmt, io, num::NonZeroU64, pin::Pin};
lazy_static! {
static ref HUB: Mutex<FnvHashMap<NonZeroU64, mpsc::Sender<Channel<Bytes>>>> =
static ref HUB: Mutex<FnvHashMap<NonZeroU64, mpsc::Sender<Channel<Vec<u8>>>>> =
Mutex::new(FnvHashMap::default());
}
@ -39,13 +39,13 @@ pub struct MemoryTransport;
/// Connection to a `MemoryTransport` currently being opened.
pub struct DialFuture {
sender: mpsc::Sender<Channel<Bytes>>,
channel_to_send: Option<Channel<Bytes>>,
channel_to_return: Option<Channel<Bytes>>,
sender: mpsc::Sender<Channel<Vec<u8>>>,
channel_to_send: Option<Channel<Vec<u8>>>,
channel_to_return: Option<Channel<Vec<u8>>>,
}
impl Future for DialFuture {
type Output = Result<Channel<Bytes>, MemoryTransportError>;
type Output = Result<Channel<Vec<u8>>, MemoryTransportError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
match self.sender.poll_ready(cx) {
@ -67,7 +67,7 @@ impl Future for DialFuture {
}
impl Transport for MemoryTransport {
type Output = Channel<Bytes>;
type Output = Channel<Vec<u8>>;
type Error = MemoryTransportError;
type Listener = Listener;
type ListenerUpgrade = Ready<Result<Self::Output, Self::Error>>;
@ -168,13 +168,13 @@ pub struct Listener {
/// The address we are listening on.
addr: Multiaddr,
/// Receives incoming connections.
receiver: mpsc::Receiver<Channel<Bytes>>,
receiver: mpsc::Receiver<Channel<Vec<u8>>>,
/// Generate `ListenerEvent::NewAddress` to inform about our listen address.
tell_listen_addr: bool
}
impl Stream for Listener {
type Item = Result<ListenerEvent<Ready<Result<Channel<Bytes>, MemoryTransportError>>>, MemoryTransportError>;
type Item = Result<ListenerEvent<Ready<Result<Channel<Vec<u8>>, MemoryTransportError>>>, MemoryTransportError>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
if self.tell_listen_addr {
@ -230,7 +230,7 @@ pub type Channel<T> = RwStreamSink<Chan<T>>;
/// A channel represents an established, in-memory, logical connection between two endpoints.
///
/// Implements `Sink` and `Stream`.
pub struct Chan<T = Bytes> {
pub struct Chan<T = Vec<u8>> {
incoming: mpsc::Receiver<T>,
outgoing: mpsc::Sender<T>,
}