Abstract out NetworkStream

This introduces a new Trait, NetworkStream, which abstracts over
the functionality provided by TcpStream so that it can be easily
mocked and extended in testing and hyper can be used for
other connection sources.
This commit is contained in:
Sean McArthur
2014-09-07 14:18:51 -07:00
committed by Jonathan Reem
parent a8d7b681da
commit 0285fc2acc
9 changed files with 240 additions and 111 deletions

View File

@@ -1,4 +1,4 @@
#![feature(macro_rules)]
#![feature(macro_rules, default_type_params)]
extern crate hyper;
extern crate debug;
@@ -10,15 +10,16 @@ use std::sync::Arc;
use hyper::{Get, Post};
use hyper::server::{Server, Handler, Incoming, Request, Response, Fresh};
use hyper::header::common::ContentLength;
use hyper::net::{HttpStream, HttpAcceptor};
trait ConcurrentHandler: Send + Sync {
fn handle(&self, req: Request, res: Response<Fresh>);
fn handle(&self, req: Request, res: Response<Fresh, HttpStream>);
}
struct Concurrent<H: ConcurrentHandler> { handler: Arc<H> }
impl<H: ConcurrentHandler> Handler for Concurrent<H> {
fn handle(self, mut incoming: Incoming) {
impl<H: ConcurrentHandler> Handler<HttpAcceptor, HttpStream> for Concurrent<H> {
fn handle(self, mut incoming: Incoming<HttpAcceptor>) {
for (mut req, mut res) in incoming {
let clone = self.handler.clone();
spawn(proc() { clone.handle(req, res) })
@@ -38,7 +39,7 @@ macro_rules! try_abort(
struct Echo;
impl ConcurrentHandler for Echo {
fn handle(&self, mut req: Request, mut res: Response<Fresh>) {
fn handle(&self, mut req: Request, mut res: Response<Fresh, HttpStream>) {
match req.uri {
hyper::uri::AbsolutePath(ref path) => match (&req.method, path.as_slice()) {
(&Get, "/") | (&Get, "/echo") => {