Skip to content

Commit

Permalink
Merge pull request #5 from rsachdeva/chatty5
Browse files Browse the repository at this point in the history
minor refactoring
  • Loading branch information
rsachdeva authored Jan 24, 2025
2 parents 2de5a0c + ab68172 commit ab0bfba
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 6 deletions.
3 changes: 2 additions & 1 deletion chatty-tcp/src/connect/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::io::Write;
use std::process;
use tokio::io::{stdin, AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio::net::tcp::OwnedWriteHalf;
use tokio::select;
use tokio::signal;
use tracing::debug;

Expand All @@ -21,7 +22,7 @@ pub async fn send_command(writer_half: OwnedWriteHalf, username: String) -> Resu
stdout().flush()?;

loop {
tokio::select! {
select! {
// Handle input from the user
line = reader.next_line() => {
if let Ok(Some(line)) = line {
Expand Down
4 changes: 2 additions & 2 deletions chatty-tcp/src/listen/command.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::listen::response::{
send_from_broadcast_channel_task, send_response, send_to_broadcast_channel,
send_from_broadcast_channel, send_response, send_to_broadcast_channel,
};
use crate::listen::state::RoomState;
use anyhow::Result;
Expand Down Expand Up @@ -45,7 +45,7 @@ pub async fn process_command(

let chat_response = if !user_already_exist {
let rx = room_state.tx.subscribe();
let send_task_handle = tokio::spawn(send_from_broadcast_channel_task(
let send_task_handle = tokio::spawn(send_from_broadcast_channel(
writer.clone(),
rx,
username.clone(),
Expand Down
6 changes: 3 additions & 3 deletions chatty-tcp/src/listen/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub async fn send_to_broadcast_channel(
Ok(())
}

pub async fn send_from_broadcast_channel_task(
pub async fn send_from_broadcast_channel(
writer: Arc<Mutex<OwnedWriteHalf>>,
mut rx: broadcast::Receiver<ChatResponse>,
username: String,
Expand Down Expand Up @@ -99,7 +99,7 @@ mod tests {

let writer = Arc::new(Mutex::new(writer_half));
let _handle = tokio::spawn(async move {
assert_ok!(send_from_broadcast_channel_task(writer, rx, "alice".to_string()).await);
assert_ok!(send_from_broadcast_channel(writer, rx, "alice".to_string()).await);
});

let (mut stream, _) = assert_ok!(listener.accept().await);
Expand Down Expand Up @@ -130,7 +130,7 @@ mod tests {

let writer = Arc::new(Mutex::new(writer_half));
let _handle = tokio::spawn(async move {
assert_ok!(send_from_broadcast_channel_task(writer, rx, "alice".to_string()).await);
assert_ok!(send_from_broadcast_channel(writer, rx, "alice".to_string()).await);
});

let (mut stream, _) = assert_ok!(listener.accept().await);
Expand Down

0 comments on commit ab0bfba

Please sign in to comment.