use futures::prelude::*;
use crate::muxing::{Shutdown, StreamMuxer};
use std::io::Error as IoError;
#[derive(Debug)]
pub struct DummySubstream {}
#[derive(Debug)]
pub struct DummyOutboundSubstream {}
#[derive(Debug, PartialEq, Clone)]
pub enum DummyConnectionState {
Pending, Closed, Opened, }
#[derive(Debug, PartialEq, Clone)]
struct DummyConnection {
state: DummyConnectionState,
}
#[derive(Debug, PartialEq, Clone)]
pub struct DummyMuxer{
in_connection: DummyConnection,
out_connection: DummyConnection,
}
impl DummyMuxer {
pub fn new() -> Self {
DummyMuxer {
in_connection: DummyConnection {
state: DummyConnectionState::Pending,
},
out_connection: DummyConnection {
state: DummyConnectionState::Closed,
},
}
}
pub fn set_inbound_connection_state(&mut self, state: DummyConnectionState) {
self.in_connection.state = state
}
pub fn set_outbound_connection_state(&mut self, state: DummyConnectionState) {
self.out_connection.state = state
}
}
impl StreamMuxer for DummyMuxer {
type Substream = DummySubstream;
type OutboundSubstream = DummyOutboundSubstream;
fn poll_inbound(&self) -> Poll<Option<Self::Substream>, IoError> {
match self.in_connection.state {
DummyConnectionState::Pending => Ok(Async::NotReady),
DummyConnectionState::Closed => Ok(Async::Ready(None)),
DummyConnectionState::Opened => Ok(Async::Ready(Some(Self::Substream {}))),
}
}
fn open_outbound(&self) -> Self::OutboundSubstream {
Self::OutboundSubstream {}
}
fn poll_outbound(
&self,
_substream: &mut Self::OutboundSubstream,
) -> Poll<Option<Self::Substream>, IoError> {
match self.out_connection.state {
DummyConnectionState::Pending => Ok(Async::NotReady),
DummyConnectionState::Closed => Ok(Async::Ready(None)),
DummyConnectionState::Opened => Ok(Async::Ready(Some(Self::Substream {}))),
}
}
fn destroy_outbound(&self, _: Self::OutboundSubstream) {}
fn read_substream(&self, _: &mut Self::Substream, _buf: &mut [u8]) -> Poll<usize, IoError> {
unreachable!()
}
fn write_substream(&self, _: &mut Self::Substream, _buf: &[u8]) -> Poll<usize, IoError> {
unreachable!()
}
fn flush_substream(&self, _: &mut Self::Substream) -> Poll<(), IoError> {
unreachable!()
}
fn shutdown_substream(&self, _: &mut Self::Substream, _: Shutdown) -> Poll<(), IoError> {
unreachable!()
}
fn destroy_substream(&self, _: Self::Substream) {}
fn shutdown(&self, _: Shutdown) -> Poll<(), IoError> {
Ok(Async::Ready(()))
}
fn flush_all(&self) -> Poll<(), IoError> {
Ok(Async::Ready(()))
}
}