summaryrefslogtreecommitdiff
path: root/src/listener.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/listener.rs')
-rw-r--r--src/listener.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/listener.rs b/src/listener.rs
new file mode 100644
index 0000000..e6683f3
--- /dev/null
+++ b/src/listener.rs
@@ -0,0 +1,49 @@
+use mio::{Ready,Token,Event};
+use mio::tcp::TcpListener;
+use std::net::SocketAddr;
+use std::io::Result;
+use config::Config;
+use eventloop::{Machine,Context,EventLoop};
+
+
+struct Listener {
+ sock: TcpListener,
+ io: Token
+}
+
+
+impl Listener {
+ fn new(addr: &SocketAddr) -> Result<Listener> {
+ trace!("Binding {}", addr);
+ let sock = try!(TcpListener::bind(addr));
+ Ok(Listener {
+ sock: sock,
+ 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.sock.local_addr().unwrap());
+ }
+
+ fn handle(&mut self, _: &mut Context, _: Event) {
+ let(_, addr) = self.sock.accept().unwrap(); // TODO: ERROR HANDLING!
+ debug!("New connection from {}", 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(())
+}