Skip to content

Commit

Permalink
Refactor RoomError for server
Browse files Browse the repository at this point in the history
  • Loading branch information
rsachdeva committed Dec 1, 2024
1 parent 400050b commit 46e95e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 31 deletions.
26 changes: 7 additions & 19 deletions chatty-tcp/src/listen/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,14 @@ pub enum RoomError {
#[error("IO error is: {0}")]
Io(#[from] std::io::Error),

#[error("IO error is: {0}")]
IoAnyhow(#[from] anyhow::Error),

#[error("Json parse error is: {0}")]
JsonParse(#[from] serde_json::Error),

#[error("Lock error is: {0}")]
Lock(String),
#[error("Broadcast receive error is: {0}")]
BroadcastReceive(String),

#[error("Broadcast error is: {0}")]
Broadcast(String),

#[error("Join error is: {0}")]
JoinError(#[from] tokio::task::JoinError),
#[error("Broadcast send error: {0}")]
BroadcastSend(#[from] tokio::sync::broadcast::error::SendError<ChatResponse>),
}
pub async fn process_command(
writer_half: OwnedWriteHalf,
Expand Down Expand Up @@ -100,7 +94,7 @@ pub async fn process_command(
send_to_broadcast_channel(chat_response, room_state.clone()).await?;
}
ChatCommand::Leave(username) => {
remove_username(username.clone(), room_state.clone()).await?;
remove_username(username.clone(), room_state.clone()).await;
debug!("User {} has left", username);
if let Some(handle) = room_state.task_handles.lock().await.remove(&username) {
info!("Aborting background task for user: {}", username);
Expand All @@ -122,18 +116,13 @@ pub async fn process_command(
Ok(())
}

pub async fn remove_username(
username: String,
room_state: Arc<RoomState>,
) -> Result<(), RoomError> {
pub async fn remove_username(username: String, room_state: Arc<RoomState>) {
let mut users = room_state.user_set.lock().await;
users.remove(&username);
info!("User {} removed from room", username);
// list connected users
let users: Vec<String> = users.iter().cloned().collect();
info!("Users in room after removal: {:?}", users);

Ok(())
}

#[cfg(test)]
Expand All @@ -156,8 +145,7 @@ mod tests {
});

// Execute removal
let result = remove_username("test_user".to_string(), room_state.clone()).await;
assert!(result.is_ok());
remove_username("test_user".to_string(), room_state.clone()).await;

// Verify user was removed
let users = room_state.user_set.lock().await;
Expand Down
19 changes: 7 additions & 12 deletions chatty-tcp/src/listen/response.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::listen::command::RoomError;
use crate::listen::state::RoomState;
use anyhow::{Context, Result};
use anyhow::Result;
use broadcast::error::RecvError;
use chatty_types::response::ChatResponse;
use std::sync::Arc;
Expand All @@ -12,7 +12,7 @@ use tracing::{debug, info};
pub async fn send_to_broadcast_channel(
chat_response: ChatResponse,
room_state: Arc<RoomState>,
) -> Result<()> {
) -> Result<(), RoomError> {
// send the chat_response to the broadcast channel
let _ = room_state.tx.send(chat_response)?;

Expand All @@ -32,7 +32,7 @@ pub async fn send_from_broadcast_channel_task(
recv_chat_response
);
let ChatResponse::Broadcast(recv_memo) = recv_chat_response.clone() else {
return Err(RoomError::Broadcast(
return Err(RoomError::BroadcastReceive(
"Failed to get memo from received chat response".to_string(),
));
};
Expand Down Expand Up @@ -69,17 +69,12 @@ pub async fn send_from_broadcast_channel_task(
pub async fn send_response(
chat_response: ChatResponse,
writer: Arc<Mutex<OwnedWriteHalf>>,
) -> Result<()> {
) -> Result<(), RoomError> {
let serialized = serde_json::to_string(&chat_response)?;
let mut writer = writer.lock().await;
writer
.write_all(serialized.as_bytes())
.await
.context(format!("Failed to send response {:?}", chat_response))?;
writer
.write_all(b"\n")
.await
.context("Failed to send newline for framing")?; // Add newline for framing
writer.write_all(serialized.as_bytes()).await?;

writer.write_all(b"\n").await?;
Ok(())
}

Expand Down

0 comments on commit 46e95e2

Please sign in to comment.