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.
|
@ -1,19 +1,21 @@
|
||||||
[package]
|
[package]
|
||||||
name = "server"
|
name = "server"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow = "1.0.75"
|
anyhow = "1.0.75"
|
||||||
comms = { path = "../comms", features = ["server"] }
|
circular-queue = "0.2.6"
|
||||||
nanoid = "0.4.0"
|
comms = { path = "../comms", features = ["server"] }
|
||||||
serde = "1.0"
|
futures = "0.3.31"
|
||||||
serde_json = "1.0"
|
nanoid = "0.4.0"
|
||||||
tokio = { version = "1.43.0", features = ["full"] }
|
serde = "1.0"
|
||||||
tokio-stream = { version = "0.1.17" }
|
serde_json = "1.0"
|
||||||
|
tokio = { version = "1.43.0", features = ["full"] }
|
||||||
[dev-dependencies]
|
tokio-stream = { version = "0.1.17" }
|
||||||
comms = { path = "../comms", features = ["client"] }
|
|
||||||
rand = "0.8.5"
|
[dev-dependencies]
|
||||||
|
comms = { path = "../comms", features = ["client"] }
|
||||||
|
rand = "0.8.5"
|
||||||
|
|
|
@ -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,9 +29,10 @@ 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())));
|
||||||
|
|
||||||
if self
|
if self
|
||||||
.chat_rooms
|
.chat_rooms
|
||||||
.iter()
|
.iter()
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -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
|
||||||
///
|
///
|
||||||
|
|
|
@ -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 {
|
||||||
|
@ -59,8 +58,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(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue