Skip to content

Commit

Permalink
Change Command implementation from trait to enum
Browse files Browse the repository at this point in the history
  • Loading branch information
RayanRal committed Apr 22, 2024
1 parent c63ccb0 commit d0cc82d
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 34 deletions.
57 changes: 44 additions & 13 deletions src/server/commands.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
pub trait Command {
// fn execute(&self);
pub enum CommandEnum {
PutCommand(Put),
GetCommand(Get),
ExistsCommand(Exists),
ExitCommand(Exit),
}

// Define concrete command types
pub struct Put {
key: String,
value: String,
pub key: String,
pub value: String,
}

pub struct Get {
key: String,
pub key: String,
}

pub struct Exists {
key: String,
pub key: String,
}

pub struct Exit {}

impl Command for Put {}

impl Command for Get {}

impl Command for Exists {}

impl Command for Exit {}
// impl Command for PutCommand {}
//
// impl Command for GetCommand {}
//
// impl Command for ExistsCommand {}
//
// impl Command for ExitCommand {}


pub trait CommandResponse {
Expand Down Expand Up @@ -81,3 +84,31 @@ impl CommandResponse for CommandNotFoundResponse {
return String::from("Command not found");
}
}

pub fn deserialize_command(input: String) -> CommandEnum {
let parts: Vec<&str> = input.split_whitespace().collect();
let command = parts.get(0);

return match command {
Some(&"set") => {
let key = String::from(parts[1]);
let value = String::from(parts[2]);
CommandEnum::PutCommand(Put { key, value })
}
Some(&"get") => {
let key = String::from(parts[1]);
CommandEnum::GetCommand(Get { key })
}
Some(&"exists") => {
let key = String::from(parts[1]);
CommandEnum::ExistsCommand(Exists { key })
}
Some(&"exit") => {
CommandEnum::ExitCommand(Exit {})
}
_ => {
// TODO: proper handling
panic!("Command {command:#?} not found.");
}
};
}
24 changes: 7 additions & 17 deletions src/server/control_plane.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,31 @@
use log::warn;
use crate::server::cache::Cache;
use crate::server::commands;
use crate::server::commands::CommandEnum;

pub fn process_command(input: &String, cache: &mut Cache) -> Box<dyn commands::CommandResponse> {
let parts: Vec<&str> = input.split_whitespace().collect();
let command = parts.get(0);

pub fn process_command(command: CommandEnum, cache: &mut Cache) -> Box<dyn commands::CommandResponse> {
return match command {
Some(&"set") => {
let key = String::from(parts[1]);
let value = String::from(parts[2]);
CommandEnum::PutCommand(commands::Put { key, value }) => {
cache.put(&key, &value);
let response = commands::PutResponse {};
Box::new(response) // "Called set with: {key} -> {value}");
Box::new(response)
}
Some(&"get") => {
let key = String::from(parts[1]);
CommandEnum::GetCommand(commands::Get { key }) => {
let value = cache.get(&key).map(|s| s.clone());
let response = commands::GetResponse {
key,
value,
};
Box::new(response)
}
Some(&"exists") => {
let key = String::from(parts[1]);
CommandEnum::ExistsCommand(commands::Exists { key }) => {
let exists = cache.exists(&key);
let response = commands::ExistsResponse { exists };
Box::new(response)
}
Some(&"exit") => {
CommandEnum::ExitCommand(commands::Exit {}) => {
warn!("Received EXIT command. Wrapping up.");
panic!("Received EXIT command");
}
_ => {
warn!("Command {command:#?} not found.");
Box::new(commands::CommandNotFoundResponse {})
}
};
}
5 changes: 3 additions & 2 deletions src/server/listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::{Arc, Mutex};
use log::info;
use rayon::ThreadPoolBuilder;
use crate::server::cache::Cache;
use crate::server::control_plane;
use crate::server::{commands, control_plane};

pub fn start_listener(cache: Cache) {
let port = 7878;// &args[2];
Expand All @@ -28,9 +28,10 @@ fn handle_connection(stream: TcpStream, cache: Arc<Mutex<Cache>>) {
let mut s = String::new();
reader.read_line(&mut s).unwrap();
info!("Received command: {s}");
let command = commands::deserialize_command(s);

let mut cache = cache.lock().unwrap();
let response = control_plane::process_command(&s, &mut cache);
let response = control_plane::process_command(command, &mut cache);
let mut response_str = response.serialize();
response_str.push_str("\n");

Expand Down
5 changes: 3 additions & 2 deletions src/server/test_mode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::io;
use log::info;
use crate::server::cache::Cache;
use crate::server::control_plane;
use crate::server::{commands, control_plane};


pub fn run_test_mode(mut cache: Cache) {
Expand All @@ -11,6 +11,7 @@ pub fn run_test_mode(mut cache: Cache) {
io::stdin()
.read_line(&mut input)
.expect("Failed to read line");
control_plane::process_command(&input, &mut cache);
let command = commands::deserialize_command(input);
control_plane::process_command(command, &mut cache);
}
}

0 comments on commit d0cc82d

Please sign in to comment.