1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//! A number of combinators returned by methods on traits
use futures::{Async, Future, Stream};
use futures::future::{FutureResult, err};
use {Name, Address, IpList, Error};
use {Resolve, Subscribe, HostResolve, HostSubscribe};

/// A stream returned from subscription on FrozenResolver
///
/// This stream basically yields a first value of a future and never returns
/// ready again, effectively making the stream unlimited (the end of name
/// stream shuts down the consumer by a convention)
#[derive(Debug)]
pub struct StreamOnce<F> {
    future: Option<F>,
}

/// A subscriber that resolves once and never updates the result
///
/// You can create it with `Resolve::frozen_subscriber`
#[derive(Debug)]
pub struct FrozenSubscriber<R> {
    pub(crate) resolver: R,
}

/// A resolver that implements implements Resolve+HostResolve but returns
/// `NameNotFound` on `resolve`
///
/// This is needed to add resolver that can only resolve hostnames to
/// the router.
///
/// You can create it with `HostResolve::null_service_resolver`
#[derive(Debug)]
pub struct NullResolver<R> {
    pub(crate) resolver: R,
}

/// A resolver that implements implements Resolve+HostResolve but returns
/// `NameNotFound` on `resolve_host`
///
/// This is needed to add resolver that can only resolve services to
/// the router.
///
/// You can create it with `Resolve::null_host_resolver`
#[derive(Debug)]
pub struct NullHostResolver<R> {
    pub(crate) resolver: R,
}

impl<F: Future> Stream for StreamOnce<F> {
    type Item = F::Item;
    type Error = F::Error;
    fn poll(&mut self) -> Result<Async<Option<F::Item>>, F::Error> {
        let result = match self.future.as_mut() {
            Some(f) => {
                match f.poll()? {
                    Async::Ready(v) => v,
                    Async::NotReady => return Ok(Async::NotReady),
                }
            }
            None => return Ok(Async::NotReady),
        };
        self.future = None;
        return Ok(Async::Ready(Some(result)));
    }
}

impl<R: Resolve> Resolve for NullHostResolver<R> {
    type Future = R::Future;
    fn resolve(&self, name: &Name) -> Self::Future {
        self.resolver.resolve(name)
    }
}

impl<R> Resolve for NullResolver<R> {
    type Future = FutureResult<Address, Error>;
    fn resolve(&self, _name: &Name) -> Self::Future {
        err(Error::NameNotFound)
    }
}

impl<R: Resolve> Resolve for FrozenSubscriber<R> {
    type Future = R::Future;
    fn resolve(&self, name: &Name) -> Self::Future {
        self.resolver.resolve(name)
    }
}

impl<R: Resolve> Subscribe for FrozenSubscriber<R> {
    type Stream = StreamOnce<R::Future>;
    type Error = <R::Future as Future>::Error;
    fn subscribe(&self, name: &Name) -> Self::Stream {
        StreamOnce { future: Some(self.resolve(name)) }
    }
}

impl<R: HostResolve> HostResolve for NullResolver<R> {
    type HostFuture = R::HostFuture;
    fn resolve_host(&self, name: &Name) -> Self::HostFuture {
        self.resolver.resolve_host(name)
    }
}

impl<R> HostResolve for NullHostResolver<R> {
    type HostFuture = FutureResult<IpList, Error>;
    fn resolve_host(&self, _name: &Name) -> Self::HostFuture {
        err(Error::NameNotFound)
    }
}

impl<R: HostResolve> HostResolve for FrozenSubscriber<R> {
    type HostFuture = R::HostFuture;
    fn resolve_host(&self, name: &Name) -> Self::HostFuture {
        self.resolver.resolve_host(name)
    }
}

impl<R: HostResolve> HostSubscribe for FrozenSubscriber<R> {
    type HostStream = StreamOnce<R::HostFuture>;
    type HostError = <R::HostFuture as Future>::Error;
    fn subscribe_host(&self, name: &Name) -> Self::HostStream {
        StreamOnce { future: Some(self.resolve_host(name)) }
    }
}