From: Martin Pitt Date: Fri, 16 Sep 2022 12:11:19 +0000 (+0200) Subject: tokio-tutorial-mini-redis: Rename to server binary X-Git-Url: https://piware.de/gitweb/?a=commitdiff_plain;h=8130bd0b1f91135585cf0507e1b5b651cf05400a;p=learn-rust.git tokio-tutorial-mini-redis: Rename to server binary Invoke with cargo run --bin server --- diff --git a/tokio-tutorial-mini-redis/src/bin/server.rs b/tokio-tutorial-mini-redis/src/bin/server.rs new file mode 100644 index 0000000..2488f03 --- /dev/null +++ b/tokio-tutorial-mini-redis/src/bin/server.rs @@ -0,0 +1,47 @@ +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; + +use bytes::Bytes; +use mini_redis::{Connection, Frame}; +use mini_redis::Command::{self, Get, Set}; +use tokio::net::{TcpListener, TcpStream}; + +type Db = Arc>>; + +#[tokio::main] +async fn main() { + let listener = TcpListener::bind("127.0.0.1:6379").await.unwrap(); + let db: Db = Arc::new(Mutex::new(HashMap::new())); + + loop { + // The second item contains the IP and port of the new connection + let (socket, _) = listener.accept().await.unwrap(); + let db_i = db.clone(); + tokio::spawn(async move { process(socket, db_i).await }); + } +} + +async fn process(socket: TcpStream, db: Db) { + let mut connection = Connection::new(socket); + + while let Some(frame) = connection.read_frame().await.unwrap() { + let response = match Command::from_frame(frame).unwrap() { + Set(cmd) => { + // The value is stored as `Vec` + db.lock().unwrap().insert(cmd.key().to_string(), cmd.value().clone()); + Frame::Simple("OK".to_string()) + } + Get(cmd) => { + if let Some(value) = db.lock().unwrap().get(cmd.key()) { + Frame::Bulk(value.clone()) + } else { + Frame::Null + } + } + cmd => panic!("unimplemented {:?}", cmd), + }; + + // Write the response to the client + connection.write_frame(&response).await.unwrap(); + } +} diff --git a/tokio-tutorial-mini-redis/src/main.rs b/tokio-tutorial-mini-redis/src/main.rs deleted file mode 100644 index 2488f03..0000000 --- a/tokio-tutorial-mini-redis/src/main.rs +++ /dev/null @@ -1,47 +0,0 @@ -use std::collections::HashMap; -use std::sync::{Arc, Mutex}; - -use bytes::Bytes; -use mini_redis::{Connection, Frame}; -use mini_redis::Command::{self, Get, Set}; -use tokio::net::{TcpListener, TcpStream}; - -type Db = Arc>>; - -#[tokio::main] -async fn main() { - let listener = TcpListener::bind("127.0.0.1:6379").await.unwrap(); - let db: Db = Arc::new(Mutex::new(HashMap::new())); - - loop { - // The second item contains the IP and port of the new connection - let (socket, _) = listener.accept().await.unwrap(); - let db_i = db.clone(); - tokio::spawn(async move { process(socket, db_i).await }); - } -} - -async fn process(socket: TcpStream, db: Db) { - let mut connection = Connection::new(socket); - - while let Some(frame) = connection.read_frame().await.unwrap() { - let response = match Command::from_frame(frame).unwrap() { - Set(cmd) => { - // The value is stored as `Vec` - db.lock().unwrap().insert(cmd.key().to_string(), cmd.value().clone()); - Frame::Simple("OK".to_string()) - } - Get(cmd) => { - if let Some(value) = db.lock().unwrap().get(cmd.key()) { - Frame::Bulk(value.clone()) - } else { - Frame::Null - } - } - cmd => panic!("unimplemented {:?}", cmd), - }; - - // Write the response to the client - connection.write_frame(&response).await.unwrap(); - } -}