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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//! Handlers of connections and sockets.
//!
//! The [`Fragment`]s provided by this crate are not directly useful on their own. Unlike for
//! example logging, a listening TCP socket doesn't *do* anything on its own.
//!
//! This module provides [`Transformation`]s that bind the sockets with some useful action
//! (different handlers represent different abstractions from the actions). In general, these
//! actions should return a [`Future`] and that one can in turn be installed.
//!
//! See the [crate example](../index.html#examples) for the usage.
//!
//! [`Fragment`]: spirit::Fragment
//! [`Transformation`]: spirit::fragment::Transformation
//! [`Future`]: futures::Future
use std::fmt::Debug;
use std::io::Error as IoError;

use failure::{Error, Fail};
use futures::{try_ready, Async, Future, IntoFuture, Poll, Stream};
use log::{trace, warn, Level};
use spirit::fragment::Transformation;
use spirit::utils::{self, ErrorLogFormat};

use crate::installer::FutureInstaller;
use crate::net::IntoIncoming;

/// A [`Transformation`] to handle the whole socket.
///
/// This is a newtype that turns a closure taking a socket and the [`Fragment`] describing it and
/// returns a future of something that can be done with it.
///
/// This is appropriate for UDP-like sockets and if you want to accept the connections on a
/// TCP-like socket manually.
///
/// If you want to handle separate connections from a TCP listening socket but don't want to care
/// about accepting them (and want to leave some other bookkeeping, like the limits of connections,
/// to this crate), then you want the [`HandleListener`] instead.
///
/// [`Fragment`]: spirit::Fragment
#[derive(Clone, Debug)]
pub struct HandleSocket<F>(pub F);

impl<Socket, InputInstaller, SubFragment, F, R> Transformation<Socket, InputInstaller, SubFragment>
    for HandleSocket<F>
where
    F: FnMut(Socket, &SubFragment) -> Result<R, Error>,
    R: 'static,
    SubFragment: Debug,
    R: IntoFuture<Item = (), Error = ()>,
{
    type OutputResource = R;
    type OutputInstaller = FutureInstaller<R>;
    fn installer(&mut self, _: InputInstaller, name: &str) -> Self::OutputInstaller {
        trace!("Creating future installer for {}", name);
        FutureInstaller::default()
    }
    fn transform(&mut self, sock: Socket, cfg: &SubFragment, name: &str) -> Result<R, Error> {
        trace!("Creating a future out of socket {} on {:?}", name, cfg);
        (self.0)(sock, cfg)
    }
}

#[doc(hidden)]
pub trait ConnectionHandler<Conn, Ctx> {
    type Output;
    fn execute(&self, conn: Conn, ctx: &mut Ctx) -> Self::Output;
}

impl<F, Conn, Ctx, R> ConnectionHandler<Conn, Ctx> for F
where
    F: Fn(Conn, &mut Ctx) -> R,
{
    type Output = R;
    fn execute(&self, conn: Conn, ctx: &mut Ctx) -> R {
        self(conn, ctx)
    }
}

#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct ConfigAdaptor<F>(F);

impl<F, Conn, Cfg, R> ConnectionHandler<Conn, Cfg> for ConfigAdaptor<F>
where
    F: Fn(Conn, &Cfg) -> R,
{
    type Output = R;
    fn execute(&self, conn: Conn, cfg: &mut Cfg) -> R {
        (self.0)(conn, cfg)
    }
}

#[doc(hidden)]
pub struct Acceptor<Incoming, Ctx, Handler> {
    name: &'static str,
    incoming: Incoming,
    ctx: Ctx,
    handler: Handler,
}

impl<Incoming, Ctx, Handler> Future for Acceptor<Incoming, Ctx, Handler>
where
    Incoming: Stream<Error = IoError>,
    Handler: ConnectionHandler<Incoming::Item, Ctx>,
    Handler::Output: IntoFuture<Item = ()>,
    <<Handler as ConnectionHandler<Incoming::Item, Ctx>>::Output as IntoFuture>::Future:
        Send + 'static,
    <<Handler as ConnectionHandler<Incoming::Item, Ctx>>::Output as IntoFuture>::Error: Into<Error>,
{
    type Item = ();
    type Error = ();
    fn poll(&mut self) -> Poll<(), ()> {
        loop {
            let incoming = self.incoming.poll().map_err(|e| {
                let e = e.context("Listening socket terminated unexpectedly").into();
                utils::log_error(Level::Error, module_path!(), &e, ErrorLogFormat::Multiline);
            });
            let connection = try_ready!(incoming);
            if let Some(conn) = connection {
                let name = self.name;
                let future = self
                    .handler
                    .execute(conn, &mut self.ctx)
                    .into_future()
                    .map_err(move |e| {
                        let e = e
                            .into()
                            .context(format!("Failed to handle connection on {}", name));
                        utils::log_error(
                            Level::Error,
                            module_path!(),
                            &e.into(),
                            ErrorLogFormat::Multiline,
                        );
                    });
                tokio::spawn(future);
            } else {
                warn!("The listening socket on {} terminated", self.name);
                return Ok(Async::Ready(()));
            }
        }
    }
}

/// A handler of incoming connections with per-listener initialization.
///
/// This is a more complex version of the [`HandleListener`]. This one contains two closures. The
/// first one (`I`) is called for each *listening* socket and produces an arbitrary context. The
/// second one (`F`) is called for each connection accepted on that listening socket. The context
/// is provided to the closure.
///
/// This approach allows certain initialization to happen for each separate listening socket.
#[derive(Clone, Debug)]
pub struct HandleListenerInit<I, F>(pub I, pub F);

impl<Listener, InputInstaller, SubFragment, I, Ctx, F, Fut>
    Transformation<Listener, InputInstaller, SubFragment> for HandleListenerInit<I, F>
where
    Listener: IntoIncoming,
    I: FnMut(&mut Listener, &SubFragment) -> Result<Ctx, Error>,
    F: Fn(Listener::Connection, &mut Ctx) -> Fut + Clone + 'static,
    Fut: IntoFuture<Item = ()>,
    Fut::Error: Into<Error>,
    SubFragment: Debug,
    Ctx: 'static,
{
    type OutputResource = Acceptor<Listener::Incoming, Ctx, F>;
    type OutputInstaller = FutureInstaller<Self::OutputResource>;
    fn installer(&mut self, _: InputInstaller, name: &str) -> Self::OutputInstaller {
        trace!("Creating future installer for listener {}", name);
        FutureInstaller::default()
    }
    fn transform(
        &mut self,
        mut listener: Listener,
        cfg: &SubFragment,
        name: &'static str,
    ) -> Result<Self::OutputResource, Error> {
        trace!("Creating acceptor for {} on {:?}", name, cfg);
        let ctx = (self.0)(&mut listener, cfg)?;
        let incoming = listener.into_incoming();
        Ok(Acceptor {
            incoming,
            name,
            ctx,
            handler: self.1.clone(),
        })
    }
}

/// A handler newtype to handle each separate connection.
///
/// If this handler is used (wrapping a closure taking the connection and the [`Fragment`] that
/// created the relevant listening socket it comes from), the closure is called for already
/// accepted connection. It shall produce a future and that future is spawned onto a runtime.
///
/// Most of the time this is what you want if you just want to serve some requests over TCP or
/// something similar that has accepting semantics (one socket that listens and produces more
/// sockets).
///
/// For slightly more complex situation, you might want to also consider the
/// [`HandleListenerInit`].
///
/// [`Fragment`]: spirit::Fragment
#[derive(Clone, Debug)]
pub struct HandleListener<F>(pub F);

impl<Listener, InputInstaller, SubFragment, F, Fut>
    Transformation<Listener, InputInstaller, SubFragment> for HandleListener<F>
where
    Listener: IntoIncoming,
    F: Fn(Listener::Connection, &SubFragment) -> Fut + Clone + 'static,
    Fut: IntoFuture<Item = ()>,
    Fut::Error: Into<Error>,
    SubFragment: Clone + Debug + 'static,
{
    type OutputResource = Acceptor<Listener::Incoming, SubFragment, ConfigAdaptor<F>>;
    type OutputInstaller = FutureInstaller<Self::OutputResource>;
    fn installer(&mut self, _: InputInstaller, name: &str) -> Self::OutputInstaller {
        trace!("Creating future installer for listener {}", name);
        FutureInstaller::default()
    }
    fn transform(
        &mut self,
        listener: Listener,
        cfg: &SubFragment,
        name: &'static str,
    ) -> Result<Self::OutputResource, Error> {
        trace!("Creating acceptor for {} on {:?}", name, cfg);
        let cfg = cfg.clone();
        let incoming = listener.into_incoming();
        Ok(Acceptor {
            incoming,
            name,
            ctx: cfg,
            handler: ConfigAdaptor(self.0.clone()),
        })
    }
}