Skip to content

Commit

Permalink
add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiyaa committed Oct 25, 2024
1 parent f437d19 commit d09ee23
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 18 deletions.
83 changes: 83 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 @@ -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" ] }
Expand Down
12 changes: 7 additions & 5 deletions src/bin/server/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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;
Expand All @@ -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));
});

Expand All @@ -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,
Expand All @@ -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(())
Expand Down
30 changes: 17 additions & 13 deletions src/bin/server/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub enum AppEvent {

pub type AppEventReceiver = mpsc::Receiver<AppEvent>;

/// Send client requests for the server to process
pub type ClientRequestSender = mpsc::Sender<(String, ClientRequest)>;
// pub type ClientRequestReceiver = mpsc::Receiver<(String, ClientRequest)>;

Expand All @@ -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,

Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/bin/server/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions src/bin/server/server_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
Expand Down

0 comments on commit d09ee23

Please sign in to comment.