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, } 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, 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 { 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 { try_rm!(self, self.io.timeout(t)); None } fn remove(&mut self, ctx: &mut Context) { self.itf.release(); self.io.remove(ctx); } }