updated the HCD-201 essay

This commit is contained in:
Win 2025-02-21 23:51:48 +07:00
parent b1187f118e
commit 8f3cbc65d0
6 changed files with 56 additions and 31 deletions

View File

@ -0,0 +1 @@
,winsdominoes,aurora,21.02.2025 23:51,file:///var/home/winsdominoes/.var/app/org.libreoffice.LibreOffice/config/libreoffice/4;

View File

@ -7,7 +7,9 @@ edition = "2021"
[dependencies] [dependencies]
anyhow = "1.0.75" anyhow = "1.0.75"
circular-queue = "0.2.6"
comms = { path = "../comms", features = ["server"] } comms = { path = "../comms", features = ["server"] }
futures = "0.3.31"
nanoid = "0.4.0" nanoid = "0.4.0"
serde = "1.0" serde = "1.0"
serde_json = "1.0" serde_json = "1.0"

View File

@ -1,12 +1,16 @@
use std::sync::Arc; use std::sync::Arc;
use std::thread;
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tokio::runtime::Runtime;
use self::room::ChatRoom; use self::room::ChatRoom;
pub use self::room::{ChatRoomMetadata, SessionAndUserId, UserSessionHandle}; pub use self::room::{ChatRoomMetadata, SessionAndUserId, UserSessionHandle};
pub use self::room_manager::RoomManager; pub use self::room_manager::RoomManager;
use futures::executor;
mod room; mod room;
#[allow(clippy::module_inception)] #[allow(clippy::module_inception)]
mod room_manager; mod room_manager;
@ -25,6 +29,7 @@ impl RoomManagerBuilder {
/// Add a room to the room manager /// Add a room to the room manager
/// Will panic if a room with the same name already exists /// Will panic if a room with the same name already exists
pub fn create_room(mut self, metadata: ChatRoomMetadata) -> Self { pub fn create_room(mut self, metadata: ChatRoomMetadata) -> Self {
let chat_room = Arc::new(Mutex::new(ChatRoom::new(metadata.clone()))); let chat_room = Arc::new(Mutex::new(ChatRoom::new(metadata.clone())));
@ -36,7 +41,9 @@ impl RoomManagerBuilder {
panic!("room with the same name already exists"); 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 self
} }

View File

@ -1,6 +1,7 @@
use comms::event::{self, Event}; use comms::event::{self, Event};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tokio::sync::broadcast; use tokio::sync::broadcast;
use circular_queue::CircularQueue;
use super::{ use super::{
user_registry::UserRegistry, user_session_handle::UserSessionHandle, SessionAndUserId, user_registry::UserRegistry, user_session_handle::UserSessionHandle, SessionAndUserId,
@ -13,6 +14,12 @@ pub struct ChatRoomMetadata {
pub description: String, pub description: String,
} }
#[derive(Debug, Clone)]
pub enum MessageBoxItem {
Message { user_id: String, content: String },
Notification(String),
}
const BROADCAST_CHANNEL_CAPACITY: usize = 100; const BROADCAST_CHANNEL_CAPACITY: usize = 100;
#[derive(Debug)] #[derive(Debug)]
@ -22,6 +29,7 @@ pub struct ChatRoom {
metadata: ChatRoomMetadata, metadata: ChatRoomMetadata,
broadcast_tx: broadcast::Sender<Event>, broadcast_tx: broadcast::Sender<Event>,
user_registry: UserRegistry, user_registry: UserRegistry,
messages: CircularQueue<MessageBoxItem>
} }
impl ChatRoom { impl ChatRoom {
@ -32,6 +40,7 @@ impl ChatRoom {
metadata, metadata,
broadcast_tx, broadcast_tx,
user_registry: UserRegistry::new(), user_registry: UserRegistry::new(),
messages: CircularQueue::with_capacity(10)
} }
} }
@ -39,15 +48,24 @@ impl ChatRoom {
self.user_registry.get_unique_user_ids() self.user_registry.get_unique_user_ids()
} }
pub async fn store_message(&self) { pub fn store_message(&mut self, user_id: String, content: String)
let broadcast_tx = self.broadcast_tx.clone(); {
let mut broadcast_rx = broadcast_tx.subscribe(); self.messages.push(MessageBoxItem::Message{user_id, content});
println!("{:#?}", self.messages);
let message_data = broadcast_rx.recv().await.unwrap();
println!("{:?}", message_data);
} }
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 /// Add a participant to the room and broadcast that they joined
/// ///

View File

@ -1,7 +1,6 @@
use anyhow::Context; use anyhow::Context;
use comms::event; use comms::event;
use tokio::sync::broadcast; use tokio::sync::broadcast;
use super::chat_room;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SessionAndUserId { pub struct SessionAndUserId {
@ -60,8 +59,6 @@ impl UserSessionHandle {
)) ))
.context("could not write to the broadcast channel")?; .context("could not write to the broadcast channel")?;
chat_room::store_message();
Ok(()) Ok(())
} }
} }