summaryrefslogtreecommitdiff
path: root/src/listener.rs
blob: 293099ee0f5c86800dc55c289cb44f396ccca2dc (plain)
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
use mio::{Ready,Token,Event};
use mio::tcp::TcpListener;
use std::net::SocketAddr;
use std::io::{Result,ErrorKind};
use config::Config;
use eventloop::{Machine,Context,EventLoop};

use itf_http;


struct Listener {
    sock: TcpListener,
    addr: SocketAddr,
    io: Token
}


impl Listener {
    fn new(addr: &SocketAddr) -> Result<Listener> {
        trace!("Binding {}", addr);
        let sock = try!(TcpListener::bind(addr));
        Ok(Listener {
            sock: sock,
            addr: *addr,
            io: Token(0)
        })
    }
}


impl Machine for Listener {
    fn init(&mut self, ctx: &mut Context) {
        self.io = ctx.assign();
        ctx.register(&self.sock, self.io, Ready::readable());
        info!("Listening on {}", self.addr);
    }

    fn handle(&mut self, ctx: &mut Context, _: Event) {
        // TODO: max_connections or something
        match self.sock.accept() {
            Err(err) => {
                match err.kind() {
                    ErrorKind::WouldBlock |
                    ErrorKind::Interrupted |
                    ErrorKind::TimedOut => { },
                    _ => {
                        error!("Error accepting on {}: {}", self.addr, err);
                        // If the error is persistent, we may be getting into an infinite loop.
                        // TODO: Have a back-off timer here (especially for EMFILE).
                    }
                }
            },
            Ok((sock, addr)) => {
                debug!("{}: New connection", addr);
                ctx.spawn(Box::new(itf_http::ItfHttp::new(sock, addr)));
            }
        }
    }
}


pub fn setup(ev: &mut EventLoop, cfg: &Config) -> Result<()> {
    for itf in &cfg.itf {
        for addr in &itf.addr {
            let lst = try!(Listener::new(&addr));
            ev.spawn(Box::new(lst));
        }
    }
    Ok(())
}