diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/src/main.rs b/src/main.rs index 7ddd478..a382f15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,17 +1,21 @@ #![allow(unused_imports)] use std::{ + collections::HashMap, io::{Read, Write}, net::{TcpListener, TcpStream}, + sync::{Arc, Mutex}, thread, }; mod resp_commands; mod resp_parser; -use resp_commands::RespCommands; -use resp_parser::parse; +use resp_commands::RedisCommands; +use resp_parser::{parse, RespType}; -fn handle_client(mut stream: TcpStream) { +pub type SharedCache = Arc<Mutex<HashMap<String, String>>>; + +fn handle_client(mut stream: TcpStream, cache: SharedCache) { let mut buffer = [0; 512]; loop { let bytes_read = match stream.read(&mut buffer) { @@ -21,7 +25,7 @@ fn handle_client(mut stream: TcpStream) { }; let parsed_resp = parse(&buffer).unwrap(); - let response = RespCommands::from(parsed_resp.0).execute(); + let response = RedisCommands::from(parsed_resp.0).execute(cache.clone()); // Hardcode PONG response for now stream.write(&response).unwrap(); @@ -35,12 +39,14 @@ fn handle_client(mut stream: TcpStream) { fn main() -> std::io::Result<()> { let listener = TcpListener::bind("127.0.0.1:6379").unwrap(); + let cache: SharedCache = Arc::new(Mutex::new(HashMap::new())); for stream in listener.incoming() { match stream { Ok(stream) => { + let cache_clone = cache.clone(); thread::spawn(|| { - handle_client(stream); + handle_client(stream, cache_clone); }); } Err(e) => { |
