updated the HCD-201 essay
This commit is contained in:
parent
b1187f118e
commit
8f3cbc65d0
|
@ -0,0 +1 @@
|
|||
,winsdominoes,aurora,21.02.2025 23:51,file:///var/home/winsdominoes/.var/app/org.libreoffice.LibreOffice/config/libreoffice/4;
|
Binary file not shown.
|
@ -7,7 +7,9 @@ edition = "2021"
|
|||
|
||||
[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"
|
||||
|
|
|
@ -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,6 +29,7 @@ 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())));
|
||||
|
||||
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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<Event>,
|
||||
user_registry: UserRegistry,
|
||||
messages: CircularQueue<MessageBoxItem>
|
||||
}
|
||||
|
||||
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
|
||||
///
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
use anyhow::Context;
|
||||
use comms::event;
|
||||
use tokio::sync::broadcast;
|
||||
use super::chat_room;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SessionAndUserId {
|
||||
|
@ -60,8 +59,6 @@ impl UserSessionHandle {
|
|||
))
|
||||
.context("could not write to the broadcast channel")?;
|
||||
|
||||
chat_room::store_message();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue