Skip to content

Commit

Permalink
some general improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
PumPum7 committed Oct 26, 2024
1 parent 760aa81 commit 3d1b399
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 16 deletions.
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ env_logger = "0.11.5"
rustyline = "14.0.0"
rustyline-derive = "0.10.0"
crc32fast = "1.4.2"
threadpool = "1.8.1"

[[bin]]
name = "client"
Expand Down
6 changes: 2 additions & 4 deletions src/bin/client.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use rustyline::completion::Completer;
use rustyline::error::ReadlineError;
use rustyline::highlight::Highlighter;
use rustyline::highlight::MatchingBracketHighlighter;
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
use rustyline::hint::Hinter;
use rustyline::validate::MatchingBracketValidator;
use rustyline::validate::Validator;
use rustyline::validate::{Validator, MatchingBracketValidator};
use rustyline::{CompletionType, Config, Editor};
use std::io::{BufRead, BufReader, Write};
use std::net::TcpStream;
Expand Down
34 changes: 22 additions & 12 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ mod tests;
use std::io::{BufRead, BufReader, Write};
use std::net::{TcpListener, TcpStream};
use std::sync::{Arc, Mutex};
use std::time::Duration;
use log::{info, warn};
use threadpool::ThreadPool;

use crate::Database;
use database::Value;
Expand All @@ -21,27 +24,29 @@ impl Server {
}

pub fn run(&self) -> std::io::Result<()> {
let pool = ThreadPool::new(4);
let listener = TcpListener::bind(format!("127.0.0.1:{}", self.port))?;
println!("Server listening on port {}", self.port);

for stream in listener.incoming() {
match stream {
Ok(stream) => {
let db = Arc::clone(&self.db);
std::thread::spawn(move || {
if let Err(e) = handle_client(stream, db) {
eprintln!("Error handling client: {}", e);
}
});
}
Err(e) => eprintln!("Error accepting connection: {}", e),
let db = Arc::clone(&self.db);
if let Ok(stream) = stream {
pool.execute(move || {
if let Err(e) = handle_client(stream, db) {
eprintln!("Error handling client: {}", e);
}
});
} else {
eprintln!("Error accepting client connection");
}
}
Ok(())
}
}

fn handle_client(mut stream: TcpStream, db: Arc<Mutex<Database>>) -> std::io::Result<()> {
stream.set_read_timeout(Some(Duration::from_secs(30)))?;
stream.set_write_timeout(Some(Duration::from_secs(30)))?;

let mut reader = BufReader::new(stream.try_clone()?);
let mut line = String::new();

Expand Down Expand Up @@ -87,6 +92,8 @@ fn handle_command(
return Ok("OK\n".to_string());
}

info!("Received command: {}", cmd);

let mut db = db.lock().unwrap();
match parts[0].to_uppercase().as_str() {
"GET" => {
Expand Down Expand Up @@ -135,6 +142,9 @@ fn handle_command(
"EXIT" => {
std::process::exit(0);
}
_ => Ok("ERROR: Unknown command\n".to_string()),
_ => {
warn!("Unknown command: {}", parts[0]);
Ok("ERROR: Unknown command\n".to_string())
}
}
}

0 comments on commit 3d1b399

Please sign in to comment.