Skip to content

Commit

Permalink
wip resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmonstar committed Oct 17, 2018
1 parent c5e8077 commit 1762bc7
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 65 deletions.
142 changes: 126 additions & 16 deletions src/client/connect/dns.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,111 @@
use std::io;
use std::{fmt, io, vec};
use std::net::{
Ipv4Addr, Ipv6Addr,
IpAddr, Ipv4Addr, Ipv6Addr,
SocketAddr, ToSocketAddrs,
SocketAddrV4, SocketAddrV6,
};
use std::vec;
use std::sync::Arc;

use ::futures::{Async, Future, Poll};
use futures::{Async, Future, Poll};
use futures::future::{Executor, ExecuteError};
use futures::sync::oneshot;

pub struct Work {
use self::sealed::GaiTask;

pub trait Resolve {
type Addrs: Iterator<Item=IpAddr>;
type Future: Future<Item=Self::Addrs, Error=io::Error>;
fn resolve(&self, host: &str) -> Self::Future;
}

pub struct DefaultResolver {
inner: GaiResolver,
}

pub struct DefaultAddrs;
pub struct DefaultFuture;

pub struct GaiResolver {
executor: GaiExecutor,
}

pub struct GaiAddrs;
pub struct GaiFuture;

impl Resolve for DefaultResolver {
type Addrs = DefaultAddrs;
type Future = DefaultFuture;

fn resolve(&self, host: &str) -> Self::Future {
DefaultFuture
}
}

impl Future for DefaultFuture {
type Item = DefaultAddrs;
type Error = io::Error;

fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
unimplemented!("DefaultFuture::poll");
}
}

impl Iterator for DefaultAddrs {
type Item = IpAddr;

fn next(&mut self) -> Option<Self::Item> {
unimplemented!("DefaultAddrs::next");
}
}

impl Resolve for GaiResolver {
type Addrs = GaiAddrs;
type Future = GaiFuture;

fn resolve(&self, host: &str) -> Self::Future {
GaiFuture
}
}

impl Future for GaiFuture {
type Item = GaiAddrs;
type Error = io::Error;

fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
unimplemented!("GaiFuture::poll");
}
}

impl Iterator for GaiAddrs {
type Item = IpAddr;

fn next(&mut self) -> Option<Self::Item> {
unimplemented!("GaiAddrs::next");
}
}

#[derive(Clone)]
struct GaiExecutor(Arc<Executor<GaiTask> + Send + Sync>);

impl Executor<oneshot::Execute<GaiBlocking>> for GaiExecutor {
fn execute(&self, future: oneshot::Execute<GaiBlocking>) -> Result<(), ExecuteError<oneshot::Execute<GaiBlocking>>> {
self.0.execute(GaiTask { work: future })
.map_err(|err| ExecuteError::new(err.kind(), err.into_future().work))
}
}

pub(super) struct GaiBlocking {
host: String,
port: u16
}

impl Work {
pub fn new(host: String, port: u16) -> Work {
Work { host: host, port: port }
impl GaiBlocking {
pub(super) fn new(host: String, port: u16) -> GaiBlocking {
GaiBlocking { host: host, port: port }
}
}

impl Future for Work {
impl Future for GaiBlocking {
type Item = IpAddrs;
type Error = io::Error;

Expand All @@ -30,28 +116,28 @@ impl Future for Work {
}
}

pub struct IpAddrs {
pub(super) struct IpAddrs {
iter: vec::IntoIter<SocketAddr>,
}

impl IpAddrs {
pub fn new(addrs: Vec<SocketAddr>) -> Self {
pub(super) fn new(addrs: Vec<SocketAddr>) -> Self {
IpAddrs { iter: addrs.into_iter() }
}

pub fn try_parse(host: &str, port: u16) -> Option<IpAddrs> {
pub(super) fn try_parse(host: &str) -> Option<IpAddrs> {
if let Ok(addr) = host.parse::<Ipv4Addr>() {
let addr = SocketAddrV4::new(addr, port);
let addr = SocketAddrV4::new(addr, 0);
return Some(IpAddrs { iter: vec![SocketAddr::V4(addr)].into_iter() })
}
if let Ok(addr) = host.parse::<Ipv6Addr>() {
let addr = SocketAddrV6::new(addr, port, 0, 0);
let addr = SocketAddrV6::new(addr, 0, 0, 0);
return Some(IpAddrs { iter: vec![SocketAddr::V6(addr)].into_iter() })
}
None
}

pub fn split_by_preference(self) -> (IpAddrs, IpAddrs) {
pub(super) fn split_by_preference(self) -> (IpAddrs, IpAddrs) {
let preferring_v6 = self.iter
.as_slice()
.first()
Expand All @@ -64,7 +150,7 @@ impl IpAddrs {
(IpAddrs::new(preferred), IpAddrs::new(fallback))
}

pub fn is_empty(&self) -> bool {
pub(super) fn is_empty(&self) -> bool {
self.iter.as_slice().is_empty()
}
}
Expand All @@ -77,6 +163,30 @@ impl Iterator for IpAddrs {
}
}

// Make this Future unnameable outside of this crate.
mod sealed {
use super::*;
// Blocking task to be executed on a thread pool.
pub struct GaiTask {
pub(super) work: oneshot::Execute<GaiBlocking>
}

impl fmt::Debug for GaiTask {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.pad("GaiTask")
}
}

impl Future for GaiTask {
type Item = ();
type Error = ();

fn poll(&mut self) -> Poll<(), ()> {
self.work.poll()
}
}
}

#[cfg(test)]
mod tests {
use std::net::{Ipv4Addr, Ipv6Addr};
Expand Down
Loading

0 comments on commit 1762bc7

Please sign in to comment.