summaryrefslogtreecommitdiff
path: root/src/itf_http.rs
blob: 0d261a2640e0b0c1e77775672ab718269cec184d (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
use mio::Event;
use mio::tcp::TcpStream;
use std::net::SocketAddr;
use std::sync::Arc;

use iostream::IoStream;
use listener::Interface;
use eventloop::{Machine,Context,TToken,Action};


pub struct ItfHttp {
    io: IoStream,
    addr: SocketAddr,
    itf: Arc<Interface>,
}


macro_rules! try_rm {
    ( $s: expr, $e: expr ) => {{
        match $e {
            Err(e) => {
                debug!("{}: {}", $s.addr, e);
                return Some(Action::Remove);
            },
            Ok(v) => v
        }
    }};
}


impl ItfHttp {
    pub fn new(ctx: &mut Context, itf: Arc<Interface>, sock: TcpStream, addr: SocketAddr) -> ItfHttp {
        let io = IoStream::new(ctx, sock, itf.cfg.io_timeout, itf.cfg.io_timeout, 4096);
        ItfHttp {
            io: io,
            addr: addr,
            itf: itf,
        }
    }
}


impl Machine for ItfHttp {
    fn handle(&mut self, ctx: &mut Context, ev: Event) -> Option<Action> {
        try_rm!(self, self.io.handle(ctx, ev));

        // This is where we parse the stuff, generate a request object, throw the request object
        // through some handlers to get a response and send it back. But for now let's just act
        // like we wanted to be an echo server all along.
        let _ = self.io.rbuf.write_to(&mut self.io.wbuf);
        self.io.set_ioreg(ctx);

        None
    }

    fn timeout(&mut self, _: &mut Context, t: TToken) -> Option<Action> {
        try_rm!(self, self.io.timeout(t));
        None
    }

    fn remove(&mut self, ctx: &mut Context) {
        self.itf.release();
        self.io.remove(ctx);
    }
}