added old projects

This commit is contained in:
Austin Bennett
2026-02-03 08:18:39 -06:00
parent 43acf989bf
commit 2451448b8a
623 changed files with 28117 additions and 0 deletions
+46
View File
@@ -0,0 +1,46 @@
use std::net::TcpListener;
use std::net::TcpStream;
use std::io::prelude::*;
use std::fs;
use mtr_server_app::ThreadPool;
fn main() {
let listener = TcpListener::bind("127.0.0.1:7878").unwrap();
let pool = ThreadPool::new(4);
for stream in listener.incoming() {
let stream = stream.unwrap();
pool.execute(|| {
handle_connection(stream);
});
}
}
fn handle_connection(mut stream: TcpStream) {
let mut buffer = [0; 1024];
stream.read(&mut buffer).unwrap();
let request = std::str::from_utf8(&buffer).unwrap_or_default();
let request_line = request.lines().next().unwrap_or_default();
let request_parts: Vec<&str> = request_line.split_whitespace().collect();
if request_parts.len() < 2 {
return;
}
let requested_path = request_parts[1];
let mut path = format!("public{}", requested_path);
let status_line = if fs::metadata(&path).is_ok() {
"HTTP/1.1 200 OK"
} else {
path = "./public/404.html".to_string();
"HTTP/1.1 404 NOT FOUND"
};
let contents = fs::read_to_string(&path).unwrap_or_else(|_| "Error loading file".to_string());
let response = format!("{}\r\nContent-Length: {}\r\n\r\n{}", status_line, contents.len(), contents);
stream.write(response.as_bytes()).unwrap();
stream.flush().unwrap();
}
+98
View File
@@ -0,0 +1,98 @@
use std::thread;
use std::sync::mpsc;
use std::sync::Arc;
use std::sync::Mutex;
enum Message {
NewJob(Job),
Terminate
}
pub struct ThreadPool {
workers: Vec<Worker>,
sender: mpsc::Sender<Message>
}
type Job = Box<dyn FnOnce() + Send + 'static>;
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<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) {
println!("Sending terminate message to all workers.");
for _ in &self.workers {
self.sender.send(Message::Terminate).unwrap();
}
for worker in &mut self.workers {
println!("Shutting down Worker {}", worker.id);
// worker.thread.join().unwrap();
if let Some(thread) = worker.thread.take() {
thread.join().unwrap();
}
}
}
}
struct Worker {
id: usize,
thread: Option<thread::JoinHandle<()>>
}
impl Worker {
fn new(id: usize, receiver: Arc<Mutex<mpsc::Receiver<Message>>>) -> Worker {
let thread = thread::spawn(move || loop {
let message = receiver.lock().unwrap().recv().unwrap();
// println!("Worker {} got a job. Executing...", id);
match message {
Message::NewJob(job) => {
println!("Worker {} got a job. Executing...", id);
job();
}
Message::Terminate => {
println!("Worker {} was told to terminate.", id);
break;
}
}
});
Worker {
id,
thread: Some(thread),
}
}
}