~kmaasrud/toe

5e62ee15a9ca15df3205a3050ad3bf4392a311a7 — Knut Magnus Aasrud 9 months ago 3340218
feat: writing from client to daemon
2 files changed, 45 insertions(+), 9 deletions(-)

M src/bin/toed.rs
M src/bin/toenail.rs
M src/bin/toed.rs => src/bin/toed.rs +28 -7
@@ 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<dyn Error>> {
    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<dyn Error>> {
    Ok(())
}

fn handle_client(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
fn handle_client(
    mut stream: TcpStream,
    db: Arc<Mutex<HashMap<String, String>>>,
) -> Result<(), Box<dyn Error>> {
    let buf_reader = BufReader::new(&mut stream);
    let request: Vec<String> = buf_reader
        .lines()


@@ 33,13 40,27 @@ fn handle_client(mut stream: TcpStream) -> Result<(), Box<dyn Error>> {
        .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(),
        }
    }


M src/bin/toenail.rs => src/bin/toenail.rs +17 -2
@@ 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::<Vec<String>>();
    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);