Change Server to contain a Vec of (ip, port) pairs to allow repeat listening
Expose Server::many for creating a Server that will listen on many (ip, port) pairs. Handler still receives a simple Iterator of (Request, Response) pairs. This is a breaking change since it changes the representation of Listener, but Handler and Server::http are unchanged in their API. Fixes #7
This commit is contained in:
		| @@ -13,6 +13,12 @@ git = "https://github.com/seanmonstar/mime.rs" | |||||||
| [dependencies.unsafe-any] | [dependencies.unsafe-any] | ||||||
| git = "https://github.com/reem/rust-unsafe-any" | git = "https://github.com/reem/rust-unsafe-any" | ||||||
|  |  | ||||||
|  | [dependencies.intertwine] | ||||||
|  | git = "https://github.com/reem/rust-intertwine" | ||||||
|  |  | ||||||
|  | [dependencies.move-acceptor] | ||||||
|  | git = "https://github.com/reem/rust-move-acceptor" | ||||||
|  |  | ||||||
| [dev-dependencies.curl] | [dev-dependencies.curl] | ||||||
| git = "https://github.com/carllerche/curl-rust" | git = "https://github.com/carllerche/curl-rust" | ||||||
|  |  | ||||||
|   | |||||||
| @@ -9,6 +9,8 @@ extern crate url; | |||||||
| #[phase(plugin,link)] extern crate log; | #[phase(plugin,link)] extern crate log; | ||||||
| #[cfg(test)] extern crate test; | #[cfg(test)] extern crate test; | ||||||
| extern crate "unsafe-any" as uany; | extern crate "unsafe-any" as uany; | ||||||
|  | extern crate "move-acceptor" as macceptor; | ||||||
|  | extern crate intertwine; | ||||||
|  |  | ||||||
| pub use std::io::net::ip::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr, Port}; | pub use std::io::net::ip::{SocketAddr, IpAddr, Ipv4Addr, Ipv6Addr, Port}; | ||||||
| pub use mimewrapper::mime; | pub use mimewrapper::mime; | ||||||
|   | |||||||
| @@ -1,11 +1,16 @@ | |||||||
| //! HTTP Server | //! HTTP Server | ||||||
| use std::io::{Acceptor, Listener, IoResult, EndOfFile, IncomingConnections}; | use std::io::{Listener, IoResult, EndOfFile}; | ||||||
| use std::io::net::ip::{IpAddr, Port, SocketAddr}; | use std::io::net::ip::{IpAddr, Port, SocketAddr}; | ||||||
|  |  | ||||||
|  | use intertwine::{Intertwine, Intertwined}; | ||||||
|  | use macceptor::MoveAcceptor; | ||||||
|  |  | ||||||
| pub use self::request::Request; | pub use self::request::Request; | ||||||
| pub use self::response::{Response, Fresh, Streaming}; | pub use self::response::{Response, Fresh, Streaming}; | ||||||
|  |  | ||||||
| use net::{NetworkListener, NetworkAcceptor, NetworkStream, HttpAcceptor, HttpListener}; | use net::{NetworkListener, NetworkAcceptor, NetworkStream, HttpAcceptor, HttpListener, HttpStream}; | ||||||
|  |  | ||||||
|  | use {HttpResult}; | ||||||
|  |  | ||||||
| pub mod request; | pub mod request; | ||||||
| pub mod response; | pub mod response; | ||||||
| @@ -15,55 +20,71 @@ pub mod response; | |||||||
| /// Once listening, it will create a `Request`/`Response` pair for each | /// Once listening, it will create a `Request`/`Response` pair for each | ||||||
| /// incoming connection, and hand them to the provided handler. | /// incoming connection, and hand them to the provided handler. | ||||||
| pub struct Server<L = HttpListener> { | pub struct Server<L = HttpListener> { | ||||||
|     ip: IpAddr, |     pairs: Vec<(IpAddr, Port)> | ||||||
|     port: Port |  | ||||||
| } | } | ||||||
|  |  | ||||||
|  | macro_rules! try_option( | ||||||
|  |     ($e:expr) => {{ | ||||||
|  |         match $e { | ||||||
|  |             Some(v) => v, | ||||||
|  |             None => return None | ||||||
|  |         } | ||||||
|  |     }} | ||||||
|  | ) | ||||||
|  |  | ||||||
| impl Server<HttpListener> { | impl Server<HttpListener> { | ||||||
|     /// Creates a new server that will handle `HttpStream`s. |     /// Creates a new server that will handle `HttpStream`s. | ||||||
|     pub fn http(ip: IpAddr, port: Port) -> Server { |     pub fn http(ip: IpAddr, port: Port) -> Server { | ||||||
|         Server { |         Server { pairs: vec![(ip, port)] } | ||||||
|             ip: ip, |     } | ||||||
|             port: port |  | ||||||
|         } |     /// Creates a server that can listen to many (ip, port) pairs. | ||||||
|  |     pub fn many(pairs: Vec<(IpAddr, Port)>) -> Server { | ||||||
|  |         Server { pairs: pairs } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| impl<L: NetworkListener<S, A>, S: NetworkStream, A: NetworkAcceptor<S>> Server<L> { | impl<L: NetworkListener<S, A>, S: NetworkStream, A: NetworkAcceptor<S>> Server<L> { | ||||||
|     /// Creates a server that can listen for and handle `NetworkStreams`. |  | ||||||
|     pub fn new(ip: IpAddr, port: Port) -> Server<L> { |  | ||||||
|         Server { |  | ||||||
|             ip: ip, |  | ||||||
|             port: port |  | ||||||
|         } |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /// Binds to a socket, and starts handling connections. |     /// Binds to a socket, and starts handling connections. | ||||||
|     pub fn listen<H: Handler<A, S>>(self, handler: H) -> IoResult<Listening<A>> { |     pub fn listen<S, A, H, L>(self, handler: H) -> HttpResult<Listening<A>> | ||||||
|         let mut listener: L = try!(NetworkListener::bind(self.ip.to_string().as_slice(), self.port)); |     where S: NetworkStream, | ||||||
|         let socket = try!(listener.socket_name()); |           A: NetworkAcceptor<S>, | ||||||
|         let acceptor = try!(listener.listen()); |           H: Handler<A, S>, | ||||||
|         let mut worker = acceptor.clone(); |           L: NetworkListener<S, A> { | ||||||
|  |         let mut acceptors = Vec::new(); | ||||||
|  |         let mut sockets = Vec::new(); | ||||||
|  |         for (ip, port) in self.pairs.move_iter() { | ||||||
|  |             let mut listener: L = try_io!(NetworkListener::<S, A>::bind(ip.to_string().as_slice(), port)); | ||||||
|  |  | ||||||
|  |             sockets.push(try_io!(listener.socket_name())); | ||||||
|  |  | ||||||
|  |             let acceptor = try_io!(listener.listen()); | ||||||
|  |             acceptors.push(acceptor.clone()); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         let connections = acceptors.clone() | ||||||
|  |             .move_iter() | ||||||
|  |             .map(|acceptor| acceptor.move_incoming()) | ||||||
|  |             .intertwine(); | ||||||
|  |  | ||||||
|         spawn(proc() { |         spawn(proc() { | ||||||
|             handler.handle(Incoming { from: worker.incoming() }); |             handler.handle(Incoming { from: connections }); | ||||||
|         }); |         }); | ||||||
|  |  | ||||||
|         Ok(Listening { |         Ok(Listening { | ||||||
|             acceptor: acceptor, |             acceptors: acceptors, | ||||||
|             socket_addr: socket, |             sockets: sockets, | ||||||
|         }) |         }) | ||||||
|     } |     } | ||||||
|  |  | ||||||
| } | } | ||||||
|  |  | ||||||
| /// An iterator over incoming connections, represented as pairs of | /// An iterator over incoming connections, represented as pairs of | ||||||
| /// hyper Requests and Responses. | /// hyper Requests and Responses. | ||||||
| pub struct Incoming<'a, A: 'a = HttpAcceptor> { | pub struct Incoming<S: Send = HttpStream> { | ||||||
|     from: IncomingConnections<'a, A> |     from: Intertwined<IoResult<S>> | ||||||
| } | } | ||||||
|  |  | ||||||
| impl<'a, S: NetworkStream, A: NetworkAcceptor<S>> Iterator<(Request, Response<Fresh>)> for Incoming<'a, A> { | impl<S: NetworkStream + 'static> Iterator<(Request, Response<Fresh>)> for Incoming<S> { | ||||||
|     fn next(&mut self) -> Option<(Request, Response<Fresh>)> { |     fn next(&mut self) -> Option<(Request, Response<Fresh>)> { | ||||||
|         for conn in self.from { |         for conn in self.from { | ||||||
|             match conn { |             match conn { | ||||||
| @@ -93,17 +114,23 @@ impl<'a, S: NetworkStream, A: NetworkAcceptor<S>> Iterator<(Request, Response<Fr | |||||||
| } | } | ||||||
|  |  | ||||||
| /// A listening server, which can later be closed. | /// A listening server, which can later be closed. | ||||||
| pub struct Listening<A> { | pub struct Listening<A = HttpAcceptor> { | ||||||
|     acceptor: A, |     acceptors: Vec<A>, | ||||||
|     /// The socket address that the server is bound to. |     /// The socket addresses that the server is bound to. | ||||||
|     pub socket_addr: SocketAddr, |     pub sockets: Vec<SocketAddr>, | ||||||
| } | } | ||||||
|  |  | ||||||
| impl<A: NetworkAcceptor<S>, S: NetworkStream> Listening<A> { | impl<A: NetworkAcceptor<S>, S: NetworkStream> Listening<A> { | ||||||
|     /// Stop the server from listening to it's socket address. |     /// Stop the server from listening to all of its socket addresses. | ||||||
|     pub fn close(mut self) -> IoResult<()> { |     /// | ||||||
|  |     /// If closing any of the servers acceptors fails, this function returns Err | ||||||
|  |     /// and does not close the rest of the acceptors. | ||||||
|  |     pub fn close(&mut self) -> HttpResult<()> { | ||||||
|         debug!("closing server"); |         debug!("closing server"); | ||||||
|         self.acceptor.close() |         for acceptor in self.acceptors.mut_iter() { | ||||||
|  |             try_io!(acceptor.close()); | ||||||
|  |         } | ||||||
|  |         Ok(()) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -112,11 +139,11 @@ pub trait Handler<A: NetworkAcceptor<S>, S: NetworkStream>: Send { | |||||||
|     /// Receives a `Request`/`Response` pair, and should perform some action on them. |     /// Receives a `Request`/`Response` pair, and should perform some action on them. | ||||||
|     /// |     /// | ||||||
|     /// This could reading from the request, and writing to the response. |     /// This could reading from the request, and writing to the response. | ||||||
|     fn handle(self, Incoming<A>); |     fn handle(self, Incoming<S>); | ||||||
| } | } | ||||||
|  |  | ||||||
| impl<A: NetworkAcceptor<S>, S: NetworkStream> Handler<A, S> for fn(Incoming<A>) { | impl<A: NetworkAcceptor<S>, S: NetworkStream> Handler<A, S> for fn(Incoming<S>) { | ||||||
|     fn handle(self, incoming: Incoming<A>) { |     fn handle(self, incoming: Incoming<S>) { | ||||||
|         (self)(incoming) |         (self)(incoming) | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user