From 5e62ee15a9ca15df3205a3050ad3bf4392a311a7 Mon Sep 17 00:00:00 2001 From: Knut Magnus Aasrud Date: Tue, 2 May 2023 18:53:12 +0200 Subject: [PATCH] feat: writing from client to daemon --- src/bin/toed.rs | 35 ++++++++++++++++++++++++++++------- src/bin/toenail.rs | 19 +++++++++++++++++-- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/src/bin/toed.rs b/src/bin/toed.rs index e567847..60183ec 100644 --- a/src/bin/toed.rs +++ b/src/bin/toed.rs @@ -1,19 +1,23 @@ +use std::collections::HashMap; use std::env; use std::error::Error; use std::io::{BufRead, BufReader, Write}; use std::net::{TcpListener, TcpStream}; use std::process; +use std::sync::{Arc, Mutex}; use std::thread; fn main() -> Result<(), Box> { let listener = TcpListener::bind("127.0.0.1:1702")?; + let db = Arc::new(Mutex::new(HashMap::new())); for stream in listener.incoming() { match stream { Ok(stream) => { println!("New connection: {}", stream.peer_addr().unwrap()); + let db = Arc::clone(&db); thread::spawn(move || { - handle_client(stream).unwrap(); + handle_client(stream, db).unwrap(); }); } Err(e) => { @@ -25,7 +29,10 @@ fn main() -> Result<(), Box> { Ok(()) } -fn handle_client(mut stream: TcpStream) -> Result<(), Box> { +fn handle_client( + mut stream: TcpStream, + db: Arc>>, +) -> Result<(), Box> { let buf_reader = BufReader::new(&mut stream); let request: Vec = buf_reader .lines() @@ -33,13 +40,27 @@ fn handle_client(mut stream: TcpStream) -> Result<(), Box> { .take_while(|line| !line.is_empty()) .collect(); - if let Some(request) = request.get(0) { - match request.as_str() { - "user" => { - writeln!(stream, "Daemon host user: {}", get_user().unwrap())?; + if let Some(command) = request.get(0) { + match command.as_str() { + "add" => { + let user = request.get(1).unwrap(); + let email = request.get(2).unwrap(); + let mut db = db.lock().unwrap(); + db.insert(user.to_string(), email.to_string()); + println!("Add user: {} {}", user, email); + } + "email" => { + let db = db.lock().unwrap(); + let user = request.get(1).unwrap(); + if let Some(email) = db.get(user) { + writeln!(stream, "User: {}", user)?; + writeln!(stream, "Email: {}", email)?; + } else { + writeln!(stream, "User not found: {}", user)?; + } stream.flush()?; } - _ => writeln!(stream, "Unknown request: {request}").unwrap(), + _ => writeln!(stream, "Unknown command: {command}").unwrap(), } } diff --git a/src/bin/toenail.rs b/src/bin/toenail.rs index 05f0caa..918d487 100644 --- a/src/bin/toenail.rs +++ b/src/bin/toenail.rs @@ -2,11 +2,26 @@ use std::io::{prelude::*, BufRead, BufReader}; use std::net::TcpStream; fn main() { - let request = std::env::args().nth(1).unwrap_or("user".to_string()); + let args = std::env::args().collect::>(); + let command = &args[1]; match TcpStream::connect("127.0.0.1:1702") { Ok(mut stream) => { - write!(stream, "{request}\n\n").unwrap(); + match command.as_str() { + "add" => { + let user = args[2].as_str(); + let email = args[3].as_str(); + write!(stream, "add\n{}\n{}\n\n", user, email).unwrap(); + } + "email" => { + let user = args[2].as_str(); + write!(stream, "email\n{}\n\n", user).unwrap(); + } + _ => { + eprintln!("Unknown command: {}", command); + std::process::exit(1); + } + } stream.flush().unwrap(); let buf_reader = BufReader::new(&mut stream); -- 2.45.2