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::transport::{IrcTransport, LogView, Logged};
|
||||
use proto::{IrcCodec, Message};
|
||||
use encoding::{EncoderTrap};
|
||||
use encoding::EncoderTrap;
|
||||
use encoding::label::encoding_from_whatwg_label;
|
||||
use futures::{Async, Poll, Future, Sink, StartSend, Stream};
|
||||
use native_tls::{Certificate, TlsConnector};
|
||||
|
@ -81,12 +81,14 @@ impl<'a> Future for ConnectionFuture<'a> {
|
|||
let encoding = enc?;
|
||||
let init_str = config.mock_initial_value();
|
||||
let initial: error::Result<_> = {
|
||||
encoding.encode(&init_str, EncoderTrap::Replace).map_err(|data| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
&format!("Failed to encode {} as {}.", data, encoding.name())[..],
|
||||
).into()
|
||||
})
|
||||
encoding.encode(&init_str, EncoderTrap::Replace).map_err(
|
||||
|data| {
|
||||
io::Error::new(
|
||||
io::ErrorKind::InvalidInput,
|
||||
&format!("Failed to encode {} as {}.", data, encoding.name())[..],
|
||||
).into()
|
||||
},
|
||||
)
|
||||
};
|
||||
|
||||
let framed = MockStream::new(&initial?).framed(IrcCodec::new(config.encoding())?);
|
||||
|
@ -140,7 +142,7 @@ impl Connection {
|
|||
pub fn log_view(&self) -> Option<LogView> {
|
||||
match self {
|
||||
&Connection::Mock(ref inner) => Some(inner.view()),
|
||||
_ => None
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -93,27 +93,33 @@ pub struct LogView {
|
|||
impl LogView {
|
||||
/// Gets a read guard for all the messages sent on the transport.
|
||||
pub fn sent(&self) -> error::Result<RwLockReadGuard<Vec<Message>>> {
|
||||
self.sent.read().map_err(|_|
|
||||
error::ErrorKind::PoisonedLog.into()
|
||||
self.sent.read().map_err(
|
||||
|_| error::ErrorKind::PoisonedLog.into(),
|
||||
)
|
||||
}
|
||||
|
||||
/// Gets a read guard for all the messages received on the transport.
|
||||
pub fn received(&self) -> error::Result<RwLockReadGuard<Vec<Message>>> {
|
||||
self.received.read().map_err(|_|
|
||||
error::ErrorKind::PoisonedLog.into()
|
||||
self.received.read().map_err(
|
||||
|_| error::ErrorKind::PoisonedLog.into(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/// A logged version of the `IrcTransport` that records all sent and received 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>,
|
||||
view: LogView,
|
||||
}
|
||||
|
||||
impl<T> Logged<T> where T: AsyncRead + AsyncWrite {
|
||||
impl<T> Logged<T>
|
||||
where
|
||||
T: AsyncRead + AsyncWrite,
|
||||
{
|
||||
/// Wraps the given `IrcTransport` in logging.
|
||||
pub fn wrap(inner: IrcTransport<T>) -> Logged<T> {
|
||||
Logged {
|
||||
|
@ -121,7 +127,7 @@ impl<T> Logged<T> where T: AsyncRead + AsyncWrite {
|
|||
view: LogView {
|
||||
sent: 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>
|
||||
where
|
||||
where
|
||||
T: AsyncRead + AsyncWrite,
|
||||
{
|
||||
type Item = Message;
|
||||
|
@ -141,19 +147,19 @@ impl<T> Stream for Logged<T>
|
|||
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
||||
match try_ready!(self.inner.poll()) {
|
||||
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()
|
||||
);
|
||||
});
|
||||
recv?.push(msg.clone());
|
||||
Ok(Async::Ready(Some(msg)))
|
||||
},
|
||||
None => Ok(Async::Ready(None))
|
||||
}
|
||||
None => Ok(Async::Ready(None)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Sink for Logged<T>
|
||||
where
|
||||
where
|
||||
T: AsyncRead + AsyncWrite,
|
||||
{
|
||||
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> {
|
||||
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()
|
||||
);
|
||||
});
|
||||
sent?.push(item);
|
||||
Ok(res)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue