xmpp-rs/src/happy_eyeballs.rs

118 lines
3.5 KiB
Rust
Raw Normal View History

2017-07-13 00:56:02 +00:00
use std::str::FromStr;
2017-07-13 20:17:29 +00:00
use std::collections::HashMap;
use std::net::SocketAddr;
2017-07-18 20:54:10 +00:00
use futures::{Future, Poll, Async, Stream};
2017-07-13 00:56:02 +00:00
use tokio_core::reactor::Handle;
use tokio_core::net::{TcpStream, TcpStreamNew};
use domain::resolv::Resolver;
2017-07-18 20:54:10 +00:00
use domain::resolv::lookup::srv::{lookup_srv, LookupSrv, LookupSrvStream};
2017-07-13 00:56:02 +00:00
use domain::bits::DNameBuf;
pub struct Connecter {
handle: Handle,
resolver: Resolver,
lookup: Option<LookupSrv>,
srvs: Option<LookupSrvStream>,
2017-07-13 20:17:29 +00:00
connects: HashMap<SocketAddr, TcpStreamNew>,
2017-07-13 00:56:02 +00:00
}
impl Connecter {
pub fn from_lookup(handle: Handle, domain: &str, srv: &str, fallback_port: u16) -> Result<Connecter, String> {
let domain = try!(
DNameBuf::from_str(domain)
.map_err(|e| format!("{}", e))
);
let srv = try!(
DNameBuf::from_str(srv)
.map_err(|e| format!("{}", e))
);
let resolver = Resolver::new(&handle);
let lookup = lookup_srv(resolver.clone(), srv, domain, fallback_port);
Ok(Connecter {
handle,
resolver,
lookup: Some(lookup),
srvs: None,
2017-07-13 20:17:29 +00:00
connects: HashMap::new(),
2017-07-13 00:56:02 +00:00
})
}
}
impl Future for Connecter {
type Item = TcpStream;
type Error = String;
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
match self.lookup.as_mut().map(|mut lookup| lookup.poll()) {
None => (),
Some(Ok(Async::NotReady)) => (),
Some(Ok(Async::Ready(found_srvs))) => {
self.lookup = None;
match found_srvs {
Some(srvs) =>
self.srvs = Some(srvs.to_stream(self.resolver.clone())),
None =>
return Err("No SRV records".to_owned()),
}
},
Some(Err(e)) =>
return Err(format!("{}", e)),
}
match self.srvs.as_mut().map(|mut srv| srv.poll()) {
None => (),
Some(Ok(Async::NotReady)) => (),
Some(Ok(Async::Ready(None))) =>
self.srvs = None,
Some(Ok(Async::Ready(Some(srv_item)))) => {
2017-07-13 20:17:29 +00:00
let handle = &self.handle;
2017-07-13 00:56:02 +00:00
for addr in srv_item.to_socket_addrs() {
2017-07-13 20:17:29 +00:00
self.connects.entry(addr)
.or_insert_with(|| {
println!("Connect to {}", addr);
TcpStream::connect(&addr, handle)
});
2017-07-13 00:56:02 +00:00
}
},
Some(Err(e)) =>
return Err(format!("{}", e)),
}
let mut connected_stream = None;
self.connects.retain(|_, connect| {
if connected_stream.is_some() {
return false;
}
2017-07-13 00:56:02 +00:00
match connect.poll() {
Ok(Async::NotReady) => true,
Ok(Async::Ready(tcp_stream)) => {
2017-07-13 00:56:02 +00:00
// Success!
connected_stream = Some(tcp_stream);
false
},
Err(e) => {
println!("{}", e);
false
},
2017-07-13 00:56:02 +00:00
}
});
match connected_stream {
Some(tcp_stream) =>
return Ok(Async::Ready(tcp_stream)),
None => (),
2017-07-13 00:56:02 +00:00
}
2017-07-13 20:17:29 +00:00
if self.lookup.is_none() &&
self.srvs.is_none() &&
self.connects.is_empty()
{
return Err("All connection attempts failed".to_owned());
}
2017-07-13 00:56:02 +00:00
Ok(Async::NotReady)
}
}