summaryrefslogtreecommitdiff
path: root/hello_server
diff options
context:
space:
mode:
authorAdam Carpenter <53hornet@gmail.com>2019-03-27 15:32:37 -0400
committerAdam Carpenter <53hornet@gmail.com>2019-03-27 15:32:37 -0400
commit67cdcc2e12118becb823e20a40cc2687f2b8425a (patch)
treeed92c3234b89079e6d4cf36f5e80c5ffa79def48 /hello_server
parente25482fca375d318a39c3b54db396b0db6e0b263 (diff)
downloadlearning-rust-67cdcc2e12118becb823e20a40cc2687f2b8425a.tar.xz
learning-rust-67cdcc2e12118becb823e20a40cc2687f2b8425a.zip
Started Rust in Action MEAP.
Diffstat (limited to 'hello_server')
-rw-r--r--hello_server/404.html19
-rw-r--r--hello_server/Cargo.lock4
-rw-r--r--hello_server/Cargo.toml7
-rw-r--r--hello_server/hello.html18
-rw-r--r--hello_server/src/bin/main.rs46
-rw-r--r--hello_server/src/lib.rs117
6 files changed, 0 insertions, 211 deletions
diff --git a/hello_server/404.html b/hello_server/404.html
deleted file mode 100644
index b106938..0000000
--- a/hello_server/404.html
+++ /dev/null
@@ -1,19 +0,0 @@
-
-<!doctype html>
-<html lang="en">
- <head>
- <meta charset="utf-8">
- <title>
- Hello!
- </title>
- </head>
- <body>
- <h1>
- Oops!
- </h1>
- <p>
- Sorry, Idk what you're asking for.
- </p>
- </body>
-</html>
-
diff --git a/hello_server/Cargo.lock b/hello_server/Cargo.lock
deleted file mode 100644
index 738010c..0000000
--- a/hello_server/Cargo.lock
+++ /dev/null
@@ -1,4 +0,0 @@
-[[package]]
-name = "hello_server"
-version = "0.1.0"
-
diff --git a/hello_server/Cargo.toml b/hello_server/Cargo.toml
deleted file mode 100644
index 39057cc..0000000
--- a/hello_server/Cargo.toml
+++ /dev/null
@@ -1,7 +0,0 @@
-[package]
-name = "hello_server"
-version = "0.1.0"
-authors = ["Adam Carpenter <53hornet@gmail.com>"]
-edition = "2018"
-
-[dependencies]
diff --git a/hello_server/hello.html b/hello_server/hello.html
deleted file mode 100644
index 869c8f9..0000000
--- a/hello_server/hello.html
+++ /dev/null
@@ -1,18 +0,0 @@
-<!doctype html>
-<html lang="en">
- <head>
- <meta charset="utf-8">
- <title>
- Hello!
- </title>
- </head>
- <body>
- <h1>
- Hello!
- </h1>
- <p>
- Hi from Rust
- </p>
- </body>
-</html>
-
diff --git a/hello_server/src/bin/main.rs b/hello_server/src/bin/main.rs
deleted file mode 100644
index bcf1e46..0000000
--- a/hello_server/src/bin/main.rs
+++ /dev/null
@@ -1,46 +0,0 @@
-use hello_server::ThreadPool;
-use std::thread;
-use std::time::Duration;
-use std::io::prelude::*;
-use std::fs;
-use std::net::TcpStream;
-use std::net::TcpListener;
-
-fn main() {
- let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
- let pool = ThreadPool::new(4).unwrap();
-
- for stream in listener.incoming().take(2) {
- let stream = stream.unwrap();
-
- pool.execute(|| {
- handle_connection(stream);
- });
- }
-
- println!("shutting down...");
-
-}
-
-fn handle_connection(mut stream: TcpStream) {
- let mut buffer = [0; 512];
- stream.read(&mut buffer).unwrap();
- let get = b"GET / HTTP/1.1\r\n";
- let sleep = b"GET /sleep HTTP/1.1\r\n";
-
- let (status_line, filename) = if buffer.starts_with(get) {
- ("HTTP/1.1 200 OK\r\n\r\n", "hello.html")
- }
- else if buffer.starts_with(sleep) {
- thread::sleep(Duration::from_secs(5));
- ("HTTP/1.1 200 OK\r\n\r\n", "hello.html")
- }
- else {
- ("HTTP/1.1 404 NOT FOUND\r\n\r\n", "404.html")
- };
-
- let contents = fs::read_to_string(filename).unwrap();
- let response = format!("{}{}", status_line, contents);
- stream.write(response.as_bytes()).unwrap();
- stream.flush().unwrap();
-}
diff --git a/hello_server/src/lib.rs b/hello_server/src/lib.rs
deleted file mode 100644
index cd1f616..0000000
--- a/hello_server/src/lib.rs
+++ /dev/null
@@ -1,117 +0,0 @@
-use std::sync::Arc;
-use std::sync::Mutex;
-use std::sync::mpsc;
-use std::thread;
-
-enum Message {
- NewJob(Job),
- Terminate,
-}
-
-trait FnBox {
- fn call_box(self: Box<Self>);
-}
-
-impl <F: FnOnce()> FnBox for F {
- fn call_box(self: Box<F>) {
- (*self)()
- }
-}
-
-type Job = Box<FnBox + Send + 'static>;
-
-pub struct ThreadPool {
- workers: Vec<Worker>,
- sender: mpsc::Sender<Message>,
-}
-
-impl ThreadPool {
- pub fn new(size: usize) -> Result<ThreadPool, &'static str> {
- if size <= 0 {
- return Err("failed to create pool");
- }
-
- let (sender, receiver) = mpsc::channel();
-
- let receiver = Arc::new(Mutex::new(receiver));
-
- let mut workers = Vec::with_capacity(size);
-
- for id in 0..size {
- workers.push(Worker::new(id, Arc::clone(&receiver))?);
- }
-
- Ok(ThreadPool {
- workers,
- sender,
- })
- }
-
- pub fn spawn<F, T>(f: F) -> thread::JoinHandle<T>
- where
- F: FnOnce() -> T + Send + 'static,
- T: Send + 'static
- {
- thread::spawn(f)
- }
-
- pub fn execute<F>(&self, f: F)
- where
- F: FnOnce() + Send + 'static
- {
- let job = Box::new(f);
- self.sender.send(Message::NewJob(job)).unwrap();
- }
-
-}
-
-impl Drop for ThreadPool {
- fn drop(&mut self) {
- for _ in &mut self.workers {
- self.sender.send(Message::Terminate).unwrap();
- }
-
- println!("shutting down all workers...");
-
- for worker in &mut self.workers {
- println!("shutting down worker {}", worker.id);
-
- if let Some(thread) = worker.thread.take() {
- thread.join().unwrap();
- }
- }
- }
-}
-
-struct Worker {
- id: usize,
- thread: Option<thread::JoinHandle<()>>,
-}
-
-impl Worker {
- pub fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>)
- -> Result<Worker, &'static str>
- {
- let thread = thread::spawn(move || {
- loop {
- let message = receiver.lock().unwrap().recv().unwrap();
-
- match message {
- Message::NewJob(job) => {
- println!("worker {} got job, executing...", id);
- job.call_box();
- },
- Message::Terminate => {
- println!("worker {} was told to terminate...", id);
- break;
- },
- }
- }
- });
-
- Ok(Worker {
- id,
- thread: Some(thread),
- })
- }
-}