use std::net::SocketAddr;
use std::io;
use futures::Future;
use openssl::x509::X509 as OpensslX509;
use tokio_core::reactor::Core;
use error::*;
use client::{ClientConnection, ClientStreamHandle};
use tls::{TlsClientStream, TlsClientStreamBuilder};
pub struct TlsClientConnection {
io_loop: Core,
tls_client_stream: Box<Future<Item = TlsClientStream, Error = io::Error>>,
client_stream_handle: Box<ClientStreamHandle>,
}
impl TlsClientConnection {
pub fn builder() -> TlsClientConnectionBuilder {
TlsClientConnectionBuilder(TlsClientStream::builder())
}
}
impl ClientConnection for TlsClientConnection {
type MessageStream = TlsClientStream;
fn unwrap
(self)
-> (Core,
Box<Future<Item = Self::MessageStream, Error = io::Error>>,
Box<ClientStreamHandle>) {
(self.io_loop, self.tls_client_stream, self.client_stream_handle)
}
}
pub struct TlsClientConnectionBuilder(TlsClientStreamBuilder);
impl TlsClientConnectionBuilder {
pub fn add_ca(&mut self, ca: OpensslX509) {
self.0.add_ca(ca);
}
#[cfg(feature = "mtls")]
pub fn identity(&mut self, pkcs12: Pkcs12) {
self.0.identity(pkcs12);
}
pub fn build(self,
name_server: SocketAddr,
subject_name: String)
-> ClientResult<TlsClientConnection> {
let io_loop = try!(Core::new());
let (tls_client_stream, handle) = self.0.build(name_server, subject_name, &io_loop.handle());
Ok(TlsClientConnection {
io_loop: io_loop,
tls_client_stream: tls_client_stream,
client_stream_handle: handle,
})
}
}