From 2fbb522c06db003c9931980f2790cd5c83b5ab35 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 17 Apr 2022 18:50:10 +0800 Subject: [PATCH] modify the web server to use mpsc, multiple workers This is basically the Rust example for the web server, with some tweaks. Add a command to make the device buzz when a particular URL is hit, and to wait before responding when another one is hit. --- services/shellchat/src/cmds.rs | 2 + services/shellchat/src/cmds/net_cmd.rs | 256 +++++++++++++++++-------- 2 files changed, 182 insertions(+), 76 deletions(-) diff --git a/services/shellchat/src/cmds.rs b/services/shellchat/src/cmds.rs index 1f4428d7f..9acb233ab 100644 --- a/services/shellchat/src/cmds.rs +++ b/services/shellchat/src/cmds.rs @@ -47,6 +47,7 @@ pub struct CommonEnv { trng: Trng, netmgr: net::NetManager, xns: xous_names::XousNames, + boot_instant: std::time::Instant, } impl CommonEnv { pub fn register_handler(&mut self, verb: String::<256>) -> u32 { @@ -163,6 +164,7 @@ impl CmdEnv { trng: Trng::new(&xns).unwrap(), xns: xous_names::XousNames::new().unwrap(), netmgr: net::NetManager::new(), + boot_instant: std::time::Instant::now(), }; //let fcc = Fcc::new(&mut common); #[cfg(feature="benchmarks")] diff --git a/services/shellchat/src/cmds/net_cmd.rs b/services/shellchat/src/cmds/net_cmd.rs index aff9a9472..634ece84a 100644 --- a/services/shellchat/src/cmds/net_cmd.rs +++ b/services/shellchat/src/cmds/net_cmd.rs @@ -9,6 +9,9 @@ use num_traits::*; use std::net::{IpAddr, TcpStream}; use std::io::Write; use std::io::Read; +use std::sync::{Arc, Mutex}; +use std::thread; +use std::sync::mpsc; use dns::Dns; // necessary to work around https://github.com/rust-lang/rust/issues/94182 pub struct NetCmd { @@ -107,86 +110,33 @@ impl<'a> ShellCmdApi<'a> for NetCmd { } } "server" => { - // PLEASE NOTE: - // Trying to make a TCP server of some kind? Don't be shy to open an issue at - // https://github.com/betrusted-io/xous-core/issues. The TCP stack is very thinly - // tested. Also, this is not a "real" web server, obviously -- so it's going to have quirks, - // such as Firefox reporting that connections have been reset because this doesn't implement - // a complete HTTP life cycle. - let _ = std::thread::spawn({ - let listener = std::net::TcpListener::bind( - "0.0.0.0:80" - ).unwrap(); - let ticktimer = ticktimer_server::Ticktimer::new().unwrap(); - let callback_conn = self.callback_conn.clone(); + // this is adapted from https://doc.rust-lang.org/book/ch20-03-graceful-shutdown-and-cleanup.html + thread::spawn({ + let boot_instant = env.boot_instant.clone(); move || { - loop { - match listener.accept() { - Ok((mut stream, addr)) => { - let elapsed_time = ticktimer.elapsed_ms(); - let test_string = std::format!("Hello from Precursor!\n\rI have been up for {}:{:02}:{:02}.\n\r", - (elapsed_time / 3_600_000), - (elapsed_time / 60_000) % 60, - (elapsed_time / 1000) % 60, - ); - let mut request = [0u8; 1024]; - // this is probably not the "right way" to handle this -- but the "keep-alive" from the browser makes us hang on the read - // which prevents us from answering requests from other browsers (because this is a single-threaded implementation of a server) - // stream.set_read_timeout(Some(std::time::Duration::from_millis(2_000))).unwrap(); - match stream.read(&mut request) { - Ok(len) => { - let r = std::string::String::from_utf8_lossy(&request[..len]); - log::info!("Request received from {:?}: {}", stream.peer_addr().unwrap(), r); - // this works because the recipient will take only one type of memory message and it's a 512-byte length string. - xous_ipc::String::<512>::from_str(&r).send(callback_conn).unwrap(); - let mut tokens = r.split(' '); - let mut valid = true; - if let Some(verb) = tokens.next() { - if verb != "GET" { - log::info!("Expected GET, got {}", verb); - valid = false; - } - } - if let Some(path) = tokens.next() { - if path != "/" { - log::info!("We only know /, got {}", path); - valid = false; - } - } - if valid { - // now send a page back... - let page = format!("HTTP/1.1 200 OK\r\nContent-Type: text/plain; charset=utf-8\n\rContent-Length: {}\n\rConnection: close\n\r\n\r{}\n\r", - test_string.len(), - test_string); - log::info!("Responding with: {}", page); - write!(stream, "{}", page).unwrap(); - stream.flush().unwrap(); - log::info!("Sent a response {:?}", addr); - xous_ipc::String::<512>::from_str(format!("Sent 200 to {:?}", addr)).send(callback_conn).unwrap(); - } else { - let errstring = "Sorry, I have only one trick, and it's pretty dumb."; - let notfound = format!("HTTP/1.1 404 Not Found\n\rConnection: close\n\rContent-Type: text/plain; charset=utf-8\n\rContent-Length: {}\n\r\n\r{}\n\r", - errstring.len(), - errstring - ); - log::info!("Responding with: {}", notfound); - write!(stream, "{}", notfound).unwrap(); - stream.flush().unwrap(); - xous_ipc::String::<512>::from_str(format!("Sent 404 to {:?}", addr)).send(callback_conn).unwrap(); - } - } - Err(e) => { - log::info!("Stream was opened, but no request received: {:?}", e); - xous_ipc::String::<512>::from_str(format!("Socket opened but no request data received: {:?}", e)).send(callback_conn).unwrap(); - } - } - // stream should close automatically as `stream` goes out of scope here and Drop is called. - } + let listener = std::net::TcpListener::bind("0.0.0.0:80").unwrap(); + // limit to 2 because we're a bit shy on space in shellchat right now; there is a 32-thread limit per process, and shellchat has the kitchen sink. + let pool = ThreadPool::new(4); + + for stream in listener.incoming() { + let stream = match stream { + Ok(s) => s, Err(e) => { - xous_ipc::String::<512>::from_str(format!("Got error on TCP accept: {:?}", e)).send(callback_conn).unwrap(); + log::warn!("Listener returned error: {:?}", e); + continue; } - } + }; + + pool.execute({ + let bi = boot_instant.clone(); + move || { + handle_connection(stream, bi); + log::info!("connection closed"); + } + }); } + + log::info!("demo server shutting down."); } }); write!(ret, "TCP listener started on port 80").unwrap(); @@ -396,3 +346,157 @@ impl<'a> ShellCmdApi<'a> for NetCmd { Ok(Some(ret)) } } + +enum Responses { + Uptime, + NotFound, + Buzz, +} + +fn handle_connection(mut stream: TcpStream, boot_instant: std::time::Instant) { + // the result is implementation dependent, on Xous hardware, this is effectively the same as ticktimer.elapsed_ms() + let elapsed_time = std::time::Instant::now().duration_since(boot_instant); + let uptime = std::format!("Hello from Precursor!\n\rI have been up for {}:{:02}:{:02}.\n\r", + (elapsed_time.as_millis() / 3_600_000), + (elapsed_time.as_millis() / 60_000) % 60, + (elapsed_time.as_millis() / 1000) % 60, + ); + + let mut buffer = [0; 1024]; + 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 buzz = b"GET /buzz HTTP/1.1\r\n"; + + let (status_line, response_index) = if buffer.starts_with(get) { + ("HTTP/1.1 200 OK", Responses::Uptime) + } else if buffer.starts_with(sleep) { + thread::sleep(std::time::Duration::from_secs(5)); + ("HTTP/1.1 200 OK", Responses::Uptime) + } else if buffer.starts_with(buzz) { + ("HTTP/1.1 200 OK", Responses::Buzz) + } else { + ("HTTP/1.1 404 NOT FOUND", Responses::NotFound) + }; + + let contents = match response_index { + Responses::Uptime => { + uptime.as_str() + }, + Responses::Buzz => { + let xns = xous_names::XousNames::new().unwrap(); + llio::Llio::new(&xns).vibe(llio::VibePattern::Double).ok(); + "The motor on the Precursor goes bzz bzz" + } + Responses::NotFound => { + "Ceci n'est pas une page vide" + }, + }; + + let response = format!( + "{}\r\nContent-Length: {}\r\n\r\n{}", + status_line, + contents.len(), + contents + ); + + stream.write(response.as_bytes()).unwrap(); + stream.flush().unwrap(); +} + +pub struct ThreadPool { + workers: Vec, + sender: mpsc::Sender, +} + +type Job = Box; + +enum Message { + NewJob(Job), + Terminate, +} + +impl ThreadPool { + /// Create a new ThreadPool. + /// + /// The size is the number of threads in the pool. + /// + /// # Panics + /// + /// The `new` function will panic if the size is zero. + pub fn new(size: usize) -> ThreadPool { + assert!(size > 0); + + 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))); + } + + ThreadPool { workers, sender } + } + + pub fn execute(&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) { + log::info!("Sending terminate message to all workers."); + + for _ in &self.workers { + self.sender.send(Message::Terminate).unwrap(); + } + + log::info!("Shutting down all workers."); + + for worker in &mut self.workers { + log::info!("Shutting down worker {}", worker.id); + + if let Some(thread) = worker.thread.take() { + thread.join().unwrap(); + } + } + } +} + +struct Worker { + id: usize, + thread: Option>, +} + +impl Worker { + fn new(id: usize, receiver: Arc>>) -> Worker { + let thread = thread::spawn(move || loop { + let message = receiver.lock().unwrap().recv().unwrap(); + + match message { + Message::NewJob(job) => { + log::info!("Worker {} got a job; executing.", id); + + job(); + } + Message::Terminate => { + log::info!("Worker {} was told to terminate.", id); + + break; + } + } + }); + + Worker { + id, + thread: Some(thread), + } + } +} \ No newline at end of file