use std::io;
use std::net::SocketAddr;
use std::time::Duration;
use futures::Future;
use tokio_core::net::TcpStream;
use tokio_core::reactor::Core;
use error::*;
use client::{ClientConnection, ClientStreamHandle};
use tcp::TcpClientStream;
pub struct TcpClientConnection {
io_loop: Core,
tcp_client_stream: Box<Future<Item = TcpClientStream<TcpStream>, Error = io::Error>>,
client_stream_handle: Box<ClientStreamHandle>,
}
impl TcpClientConnection {
pub fn new(name_server: SocketAddr) -> ClientResult<Self> {
Self::with_timeout(name_server, Duration::from_secs(5))
}
pub fn with_timeout(name_server: SocketAddr, timeout: Duration) -> ClientResult<Self> {
let io_loop = try!(Core::new());
let (tcp_client_stream, handle) =
TcpClientStream::<TcpStream>::with_timeout(name_server, &io_loop.handle(), timeout);
Ok(TcpClientConnection {
io_loop: io_loop,
tcp_client_stream: tcp_client_stream,
client_stream_handle: handle,
})
}
}
impl ClientConnection for TcpClientConnection {
type MessageStream = TcpClientStream<TcpStream>;
fn unwrap
(self)
-> (Core,
Box<Future<Item = Self::MessageStream, Error = io::Error>>,
Box<ClientStreamHandle>) {
(self.io_loop, self.tcp_client_stream, self.client_stream_handle)
}
}