From d09ee2398cb59af276f8f76257c5cfffc743e927 Mon Sep 17 00:00:00 2001 From: kamiyaa Date: Fri, 25 Oct 2024 12:43:02 -0400 Subject: [PATCH] add comments --- Cargo.lock | 83 +++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/bin/server/client.rs | 12 ++--- src/bin/server/events.rs | 30 +++++++------ src/bin/server/server.rs | 1 + src/bin/server/server_util.rs | 3 ++ 6 files changed, 112 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d14f4c6..d4c38d7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,21 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" + [[package]] name = "ahash" version = "0.8.11" @@ -156,6 +171,21 @@ version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c4b4d0bd25bd0b74681c0ad21497610ce1b7c91b1022cd21c80c6fbdd9476b0" +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if 1.0.0", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets 0.52.6", +] + [[package]] name = "base64" version = "0.13.1" @@ -700,6 +730,7 @@ dependencies = [ "strfmt", "symphonia", "termion 1.5.6", + "tokio", "toml", "tracing", "tracing-subscriber", @@ -786,6 +817,12 @@ dependencies = [ "wasi 0.11.0+wasi-snapshot-preview1", ] +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + [[package]] name = "glob" version = "0.3.1" @@ -1147,6 +1184,15 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + [[package]] name = "ndk" version = "0.8.0" @@ -1287,6 +1333,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8f8bdf33df195859076e54ab11ee78a1b208382d3a26ec40d142ffc1ecc49ef" +[[package]] +name = "object" +version = "0.36.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" +dependencies = [ + "memchr", +] + [[package]] name = "oboe" version = "0.6.1" @@ -1621,6 +1676,12 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + [[package]] name = "rustc-hash" version = "1.1.0" @@ -2182,6 +2243,28 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "tokio" +version = "1.41.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "145f3413504347a2be84393cc8a7d2fb4d863b375909ea59f2158261aa258bbb" +dependencies = [ + "backtrace", + "pin-project-lite", + "tokio-macros", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.77", +] + [[package]] name = "toml" version = "0.8.19" diff --git a/Cargo.toml b/Cargo.toml index 8afb84e..9b9043c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -44,6 +44,7 @@ skim = "^0" strfmt = "^0" symphonia = { version = "^0", features = ["all"] } termion = "^1" +tokio = { version = "^1", features = [ "macros", "rt", "rt-multi-thread" ] } toml = "^0" tracing = "^0" tracing-subscriber = { version = "^0", features = [ "std", "env-filter" ] } diff --git a/src/bin/server/client.rs b/src/bin/server/client.rs index 01f3481..9955e4c 100644 --- a/src/bin/server/client.rs +++ b/src/bin/server/client.rs @@ -24,7 +24,7 @@ pub fn handle_client( ) -> DiziResult { let (event_tx, event_rx) = mpsc::channel(); - // listen for server events + // listen for events broadcasted by the server let event_tx_clone = event_tx.clone(); let _ = thread::spawn(move || { while let Ok(server_event) = server_event_rx.recv() { @@ -39,11 +39,12 @@ pub fn handle_client( let uuid_string = uuid.to_string(); - // listen for client requests + // listen for requests sent by client let event_tx_clone = event_tx; - let stream_clone = stream.try_clone().unwrap(); + let stream_clone = stream.try_clone().expect("Failed to clone UnixStream"); let _ = thread::spawn(move || { let cursor = BufReader::new(stream_clone); + // keep listening for client requests for line in cursor.lines().flatten() { if event_tx_clone.send(ClientMessage::Client(line)).is_err() { return; @@ -53,7 +54,8 @@ pub fn handle_client( let response = ClientRequest::ClientLeave { uuid: uuid.to_string(), }; - let json = serde_json::to_string(&response).unwrap(); + let json = serde_json::to_string(&response). + expect("Failed to serialize ClientRequest"); let _ = event_tx_clone.send(ClientMessage::Client(json)); }); @@ -74,6 +76,7 @@ pub fn handle_client( Ok(()) } +/// Forwards client requests to the server via `ClientRequestSender` pub fn forward_client_request( client_request_tx: &ClientRequestSender, uuid: &str, @@ -86,7 +89,6 @@ pub fn forward_client_request( pub fn process_server_event(stream: &mut UnixStream, event: &ServerBroadcastEvent) -> DiziResult { let json = serde_json::to_string(&event)?; - stream.write_all(json.as_bytes())?; utils::flush(stream)?; Ok(()) diff --git a/src/bin/server/events.rs b/src/bin/server/events.rs index f2c1dd3..80d1516 100644 --- a/src/bin/server/events.rs +++ b/src/bin/server/events.rs @@ -27,6 +27,7 @@ pub enum AppEvent { pub type AppEventReceiver = mpsc::Receiver; +/// Send client requests for the server to process pub type ClientRequestSender = mpsc::Sender<(String, ClientRequest)>; // pub type ClientRequestReceiver = mpsc::Receiver<(String, ClientRequest)>; @@ -45,7 +46,6 @@ pub struct Events { pub client_request_tx: ClientRequestSender, // use if you want to send server events pub server_event_tx: ServerEventSender, - // main listening loop pub app_event_rx: AppEventReceiver, @@ -64,20 +64,24 @@ impl Events { let (app_event_tx, app_event_rx) = mpsc::channel(); // listen to client requests - let app_event_tx2 = app_event_tx.clone(); - let _ = thread::spawn(move || loop { - if let Ok((uuid, request)) = client_request_rx.recv() { - let _ = app_event_tx2.send(AppEvent::Client { uuid, request }); - } - }); + { + let event_tx = app_event_tx.clone(); + let _ = thread::spawn(move || loop { + if let Ok((uuid, request)) = client_request_rx.recv() { + let _ = event_tx.send(AppEvent::Client { uuid, request }); + } + }); + } // listen to server requests - let app_event_tx2 = app_event_tx.clone(); - let _ = thread::spawn(move || loop { - if let Ok(msg) = server_event_rx.recv() { - let _ = app_event_tx2.send(AppEvent::Server(msg)); - } - }); + { + let event_tx = app_event_tx.clone(); + let _ = thread::spawn(move || loop { + if let Ok(msg) = server_event_rx.recv() { + let _ = event_tx.send(AppEvent::Server(msg)); + } + }); + } Events { client_request_tx, diff --git a/src/bin/server/server.rs b/src/bin/server/server.rs index 8911ee6..3c07477 100644 --- a/src/bin/server/server.rs +++ b/src/bin/server/server.rs @@ -85,6 +85,7 @@ pub fn serve(config: AppConfig) -> DiziResult { } tracing::debug!("Playlist saved!"); + // broadcast to all clients that the server has exited context .events .broadcast_event(ServerBroadcastEvent::ServerQuit); diff --git a/src/bin/server/server_util.rs b/src/bin/server/server_util.rs index cf51da9..73de861 100644 --- a/src/bin/server/server_util.rs +++ b/src/bin/server/server_util.rs @@ -23,8 +23,11 @@ pub fn process_server_event(context: &mut AppContext, event: ServerEvent) -> Diz let client_tx2 = context.events.client_request_sender().clone(); let (server_tx, server_rx) = mpsc::channel(); + // assign a uuid for client let client_uuid = Uuid::new_v4(); let uuid_string = client_uuid.to_string(); + + // thread to listen to client requests thread::spawn(move || { client::handle_client(client_uuid, stream, client_tx2, server_rx) });