Reformatted conn and transport.
This commit is contained in:
parent
2035739aa5
commit
54326e0047
2 changed files with 31 additions and 23 deletions
|
@ -6,7 +6,7 @@ use error;
|
||||||
use client::data::Config;
|
use client::data::Config;
|
||||||
use client::transport::{IrcTransport, LogView, Logged};
|
use client::transport::{IrcTransport, LogView, Logged};
|
||||||
use proto::{IrcCodec, Message};
|
use proto::{IrcCodec, Message};
|
||||||
use encoding::{EncoderTrap};
|
use encoding::EncoderTrap;
|
||||||
use encoding::label::encoding_from_whatwg_label;
|
use encoding::label::encoding_from_whatwg_label;
|
||||||
use futures::{Async, Poll, Future, Sink, StartSend, Stream};
|
use futures::{Async, Poll, Future, Sink, StartSend, Stream};
|
||||||
use native_tls::{Certificate, TlsConnector};
|
use native_tls::{Certificate, TlsConnector};
|
||||||
|
@ -81,12 +81,14 @@ impl<'a> Future for ConnectionFuture<'a> {
|
||||||
let encoding = enc?;
|
let encoding = enc?;
|
||||||
let init_str = config.mock_initial_value();
|
let init_str = config.mock_initial_value();
|
||||||
let initial: error::Result<_> = {
|
let initial: error::Result<_> = {
|
||||||
encoding.encode(&init_str, EncoderTrap::Replace).map_err(|data| {
|
encoding.encode(&init_str, EncoderTrap::Replace).map_err(
|
||||||
|
|data| {
|
||||||
io::Error::new(
|
io::Error::new(
|
||||||
io::ErrorKind::InvalidInput,
|
io::ErrorKind::InvalidInput,
|
||||||
&format!("Failed to encode {} as {}.", data, encoding.name())[..],
|
&format!("Failed to encode {} as {}.", data, encoding.name())[..],
|
||||||
).into()
|
).into()
|
||||||
})
|
},
|
||||||
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let framed = MockStream::new(&initial?).framed(IrcCodec::new(config.encoding())?);
|
let framed = MockStream::new(&initial?).framed(IrcCodec::new(config.encoding())?);
|
||||||
|
@ -140,7 +142,7 @@ impl Connection {
|
||||||
pub fn log_view(&self) -> Option<LogView> {
|
pub fn log_view(&self) -> Option<LogView> {
|
||||||
match self {
|
match self {
|
||||||
&Connection::Mock(ref inner) => Some(inner.view()),
|
&Connection::Mock(ref inner) => Some(inner.view()),
|
||||||
_ => None
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -93,27 +93,33 @@ pub struct LogView {
|
||||||
impl LogView {
|
impl LogView {
|
||||||
/// Gets a read guard for all the messages sent on the transport.
|
/// Gets a read guard for all the messages sent on the transport.
|
||||||
pub fn sent(&self) -> error::Result<RwLockReadGuard<Vec<Message>>> {
|
pub fn sent(&self) -> error::Result<RwLockReadGuard<Vec<Message>>> {
|
||||||
self.sent.read().map_err(|_|
|
self.sent.read().map_err(
|
||||||
error::ErrorKind::PoisonedLog.into()
|
|_| error::ErrorKind::PoisonedLog.into(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Gets a read guard for all the messages received on the transport.
|
/// Gets a read guard for all the messages received on the transport.
|
||||||
pub fn received(&self) -> error::Result<RwLockReadGuard<Vec<Message>>> {
|
pub fn received(&self) -> error::Result<RwLockReadGuard<Vec<Message>>> {
|
||||||
self.received.read().map_err(|_|
|
self.received.read().map_err(
|
||||||
error::ErrorKind::PoisonedLog.into()
|
|_| error::ErrorKind::PoisonedLog.into(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A logged version of the `IrcTransport` that records all sent and received messages.
|
/// A logged version of the `IrcTransport` that records all sent and received messages.
|
||||||
/// Note: this will introduce some performance overhead by cloning all messages.
|
/// Note: this will introduce some performance overhead by cloning all messages.
|
||||||
pub struct Logged<T> where T: AsyncRead + AsyncWrite {
|
pub struct Logged<T>
|
||||||
|
where
|
||||||
|
T: AsyncRead + AsyncWrite,
|
||||||
|
{
|
||||||
inner: IrcTransport<T>,
|
inner: IrcTransport<T>,
|
||||||
view: LogView,
|
view: LogView,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Logged<T> where T: AsyncRead + AsyncWrite {
|
impl<T> Logged<T>
|
||||||
|
where
|
||||||
|
T: AsyncRead + AsyncWrite,
|
||||||
|
{
|
||||||
/// Wraps the given `IrcTransport` in logging.
|
/// Wraps the given `IrcTransport` in logging.
|
||||||
pub fn wrap(inner: IrcTransport<T>) -> Logged<T> {
|
pub fn wrap(inner: IrcTransport<T>) -> Logged<T> {
|
||||||
Logged {
|
Logged {
|
||||||
|
@ -121,7 +127,7 @@ impl<T> Logged<T> where T: AsyncRead + AsyncWrite {
|
||||||
view: LogView {
|
view: LogView {
|
||||||
sent: Arc::new(RwLock::new(vec![])),
|
sent: Arc::new(RwLock::new(vec![])),
|
||||||
received: Arc::new(RwLock::new(vec![])),
|
received: Arc::new(RwLock::new(vec![])),
|
||||||
}
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +138,7 @@ impl<T> Logged<T> where T: AsyncRead + AsyncWrite {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Stream for Logged<T>
|
impl<T> Stream for Logged<T>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
{
|
{
|
||||||
type Item = Message;
|
type Item = Message;
|
||||||
|
@ -141,19 +147,19 @@ impl<T> Stream for Logged<T>
|
||||||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
||||||
match try_ready!(self.inner.poll()) {
|
match try_ready!(self.inner.poll()) {
|
||||||
Some(msg) => {
|
Some(msg) => {
|
||||||
let recv: error::Result<_> = self.view.received.write().map_err(|_|
|
let recv: error::Result<_> = self.view.received.write().map_err(|_| {
|
||||||
error::ErrorKind::PoisonedLog.into()
|
error::ErrorKind::PoisonedLog.into()
|
||||||
);
|
});
|
||||||
recv?.push(msg.clone());
|
recv?.push(msg.clone());
|
||||||
Ok(Async::Ready(Some(msg)))
|
Ok(Async::Ready(Some(msg)))
|
||||||
},
|
}
|
||||||
None => Ok(Async::Ready(None))
|
None => Ok(Async::Ready(None)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Sink for Logged<T>
|
impl<T> Sink for Logged<T>
|
||||||
where
|
where
|
||||||
T: AsyncRead + AsyncWrite,
|
T: AsyncRead + AsyncWrite,
|
||||||
{
|
{
|
||||||
type SinkItem = Message;
|
type SinkItem = Message;
|
||||||
|
@ -161,9 +167,9 @@ impl<T> Sink for Logged<T>
|
||||||
|
|
||||||
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
|
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
|
||||||
let res = self.inner.start_send(item.clone())?;
|
let res = self.inner.start_send(item.clone())?;
|
||||||
let sent: error::Result<_> = self.view.sent.write().map_err(|_|
|
let sent: error::Result<_> = self.view.sent.write().map_err(|_| {
|
||||||
error::ErrorKind::PoisonedLog.into()
|
error::ErrorKind::PoisonedLog.into()
|
||||||
);
|
});
|
||||||
sent?.push(item);
|
sent?.push(item);
|
||||||
Ok(res)
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue