aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: a382f153e9fc62b9d58c35d94228743e0a99a5b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![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::RedisCommands;
use resp_parser::{parse, RespType};

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) {
            Ok(0) => return, // connection closed
            Ok(n) => n,
            Err(_) => return, // error occurred
        };

        let parsed_resp = parse(&buffer).unwrap();
        let response = RedisCommands::from(parsed_resp.0).execute(cache.clone());

        // Hardcode PONG response for now
        stream.write(&response).unwrap();

        // Echo the message back
        // if let Err(_) = stream.write_all(&buffer[..bytes_read]) {
        //     return; // writing failed
        // }
    }
}

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, cache_clone);
                });
            }
            Err(e) => {
                eprintln!("Connection failed: {}", e);
            }
        }
    }
    Ok(())
}