diff --git a/spring-2025/hcd-201/.~lock.HCD-201:00010 - Understanding and Applying ACM Code of Ethics - Thanawin Pattanaphol.odt# b/spring-2025/hcd-201/.~lock.HCD-201:00010 - Understanding and Applying ACM Code of Ethics - Thanawin Pattanaphol.odt# new file mode 100644 index 0000000..a0df39b --- /dev/null +++ b/spring-2025/hcd-201/.~lock.HCD-201:00010 - Understanding and Applying ACM Code of Ethics - Thanawin Pattanaphol.odt# @@ -0,0 +1 @@ +,winsdominoes,aurora,21.02.2025 23:51,file:///var/home/winsdominoes/.var/app/org.libreoffice.LibreOffice/config/libreoffice/4; \ No newline at end of file diff --git a/spring-2025/hcd-201/HCD-201:00010 - Understanding and Applying ACM Code of Ethics - Thanawin Pattanaphol.odt b/spring-2025/hcd-201/HCD-201:00010 - Understanding and Applying ACM Code of Ethics - Thanawin Pattanaphol.odt new file mode 100644 index 0000000..b2f4c69 Binary files /dev/null and b/spring-2025/hcd-201/HCD-201:00010 - Understanding and Applying ACM Code of Ethics - Thanawin Pattanaphol.odt differ diff --git a/spring-2025/sen-109/00010/rust-chat-server/server/Cargo.toml b/spring-2025/sen-109/00010/rust-chat-server/server/Cargo.toml index 98c09ba..b2907c1 100644 --- a/spring-2025/sen-109/00010/rust-chat-server/server/Cargo.toml +++ b/spring-2025/sen-109/00010/rust-chat-server/server/Cargo.toml @@ -1,19 +1,21 @@ -[package] -name = "server" -version = "0.1.0" -edition = "2021" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -anyhow = "1.0.75" -comms = { path = "../comms", features = ["server"] } -nanoid = "0.4.0" -serde = "1.0" -serde_json = "1.0" -tokio = { version = "1.43.0", features = ["full"] } -tokio-stream = { version = "0.1.17" } - -[dev-dependencies] -comms = { path = "../comms", features = ["client"] } -rand = "0.8.5" +[package] +name = "server" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0.75" +circular-queue = "0.2.6" +comms = { path = "../comms", features = ["server"] } +futures = "0.3.31" +nanoid = "0.4.0" +serde = "1.0" +serde_json = "1.0" +tokio = { version = "1.43.0", features = ["full"] } +tokio-stream = { version = "0.1.17" } + +[dev-dependencies] +comms = { path = "../comms", features = ["client"] } +rand = "0.8.5" diff --git a/spring-2025/sen-109/00010/rust-chat-server/server/src/room_manager/mod.rs b/spring-2025/sen-109/00010/rust-chat-server/server/src/room_manager/mod.rs index c058c79..2680aea 100644 --- a/spring-2025/sen-109/00010/rust-chat-server/server/src/room_manager/mod.rs +++ b/spring-2025/sen-109/00010/rust-chat-server/server/src/room_manager/mod.rs @@ -1,12 +1,16 @@ use std::sync::Arc; +use std::thread; use tokio::sync::Mutex; +use tokio::runtime::Runtime; use self::room::ChatRoom; pub use self::room::{ChatRoomMetadata, SessionAndUserId, UserSessionHandle}; pub use self::room_manager::RoomManager; +use futures::executor; + mod room; #[allow(clippy::module_inception)] mod room_manager; @@ -25,9 +29,10 @@ impl RoomManagerBuilder { /// Add a room to the room manager /// Will panic if a room with the same name already exists + pub fn create_room(mut self, metadata: ChatRoomMetadata) -> Self { let chat_room = Arc::new(Mutex::new(ChatRoom::new(metadata.clone()))); - + if self .chat_rooms .iter() @@ -36,7 +41,9 @@ impl RoomManagerBuilder { panic!("room with the same name already exists"); } - self.chat_rooms.push((metadata, chat_room)); + self.chat_rooms.push((metadata.clone(), chat_room)); + + ChatRoom::listen_messages(&mut ChatRoom::new(metadata)); self } diff --git a/spring-2025/sen-109/00010/rust-chat-server/server/src/room_manager/room/chat_room.rs b/spring-2025/sen-109/00010/rust-chat-server/server/src/room_manager/room/chat_room.rs index b6be756..84762e8 100644 --- a/spring-2025/sen-109/00010/rust-chat-server/server/src/room_manager/room/chat_room.rs +++ b/spring-2025/sen-109/00010/rust-chat-server/server/src/room_manager/room/chat_room.rs @@ -1,6 +1,7 @@ use comms::event::{self, Event}; use serde::{Deserialize, Serialize}; use tokio::sync::broadcast; +use circular_queue::CircularQueue; use super::{ user_registry::UserRegistry, user_session_handle::UserSessionHandle, SessionAndUserId, @@ -13,6 +14,12 @@ pub struct ChatRoomMetadata { pub description: String, } +#[derive(Debug, Clone)] +pub enum MessageBoxItem { + Message { user_id: String, content: String }, + Notification(String), +} + const BROADCAST_CHANNEL_CAPACITY: usize = 100; #[derive(Debug)] @@ -22,6 +29,7 @@ pub struct ChatRoom { metadata: ChatRoomMetadata, broadcast_tx: broadcast::Sender, user_registry: UserRegistry, + messages: CircularQueue } impl ChatRoom { @@ -32,6 +40,7 @@ impl ChatRoom { metadata, broadcast_tx, user_registry: UserRegistry::new(), + messages: CircularQueue::with_capacity(10) } } @@ -39,15 +48,24 @@ impl ChatRoom { self.user_registry.get_unique_user_ids() } - pub async fn store_message(&self) { - let broadcast_tx = self.broadcast_tx.clone(); - let mut broadcast_rx = broadcast_tx.subscribe(); - - let message_data = broadcast_rx.recv().await.unwrap(); - - println!("{:?}", message_data); + pub fn store_message(&mut self, user_id: String, content: String) + { + self.messages.push(MessageBoxItem::Message{user_id, content}); + println!("{:#?}", self.messages); } + pub async fn listen_messages(&mut self) + { + let mut broadcast_rx = self.broadcast_tx.subscribe(); + + while let Ok(event) = broadcast_rx.recv().await + { + if let Event::UserMessage(event) = event + { + self.store_message(event.user_id.clone(), event.content.clone()); + } + } + } /// Add a participant to the room and broadcast that they joined /// diff --git a/spring-2025/sen-109/00010/rust-chat-server/server/src/room_manager/room/user_session_handle.rs b/spring-2025/sen-109/00010/rust-chat-server/server/src/room_manager/room/user_session_handle.rs index ffded59..4ddb0cb 100644 --- a/spring-2025/sen-109/00010/rust-chat-server/server/src/room_manager/room/user_session_handle.rs +++ b/spring-2025/sen-109/00010/rust-chat-server/server/src/room_manager/room/user_session_handle.rs @@ -1,7 +1,6 @@ use anyhow::Context; use comms::event; use tokio::sync::broadcast; -use super::chat_room; #[derive(Debug, Clone)] pub struct SessionAndUserId { @@ -59,8 +58,6 @@ impl UserSessionHandle { }, )) .context("could not write to the broadcast channel")?; - - chat_room::store_message(); Ok(()) }