From 3900ec55d4ec037d10f6a0230a4266367f3eaf3b Mon Sep 17 00:00:00 2001 From: Adam Doyle Date: Tue, 19 Nov 2024 21:38:35 -0500 Subject: [PATCH] let user handler own user lookup map --- server/src/lib.rs | 1 - server/src/main.rs | 4 +--- server/src/user_handler.rs | 17 +++++++++-------- 3 files changed, 10 insertions(+), 12 deletions(-) diff --git a/server/src/lib.rs b/server/src/lib.rs index d71e0e1..4eaa3a1 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -74,7 +74,6 @@ pub struct AppState { pub tx_game_handler: mpsc::Sender, pub tx_incoming_message_handler: mpsc::Sender<(SocketAddr, Message)>, pub tx_user_handler: mpsc::Sender, - pub users_by_id: RwLock>>>, pub white_cards_by_id: HashMap>, } diff --git a/server/src/main.rs b/server/src/main.rs index 6bc58be..8285627 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -79,7 +79,6 @@ async fn main() -> Result<()> { let offline_users = RwLock::new(HashMap::>>::new()); let online_users = RwLock::new(HashMap::>>::new()); let reserved_names = RwLock::new(HashSet::::new()); - let users_by_id = RwLock::new(HashMap::>>::new()); let app_state = Arc::new(AppState { first_names, @@ -94,7 +93,6 @@ async fn main() -> Result<()> { tx_game_handler, tx_incoming_message_handler, tx_user_handler, - users_by_id, white_cards_by_id, }); @@ -113,7 +111,7 @@ async fn main() -> Result<()> { // TODO: Make an outgoing message handler? // Spawn task to handle User things - let user_handler = UserHandler::new(app_state.clone()); + let mut user_handler = UserHandler::new(app_state.clone()); tokio::task::Builder::new() .name("User Handler") .spawn(async move { diff --git a/server/src/user_handler.rs b/server/src/user_handler.rs index 34dabcb..6a4f931 100644 --- a/server/src/user_handler.rs +++ b/server/src/user_handler.rs @@ -14,6 +14,7 @@ use std::sync::{Arc, RwLock}; pub struct UserHandler { /// Pointer to global state state: Arc, + users_by_id: HashMap>>, } pub enum DmUserMethod { @@ -40,11 +41,14 @@ pub enum SendUserMessage { impl UserHandler { /// Returns new UserHandler pub fn new(state: Arc) -> Self { - UserHandler { state } + UserHandler { + state, + users_by_id: HashMap::>>::new(), + } } /// Handles incoming messages - pub async fn handle(&self, message: UserHandlerMessage) { + pub async fn handle(&mut self, message: UserHandlerMessage) { match message { NewUser(user, addr) => { // TODO: make this not async @@ -63,7 +67,7 @@ impl UserHandler { // Determine lookup method match method { Id(id) => { - if let Some(user) = self.state.users_by_id.read().unwrap().get(&id) { + if let Some(user) = self.users_by_id.get(&id) { tx = user.read().unwrap().tx.clone(); } else { tracing::error!("Attempted to send message to invalid user id!"); @@ -113,7 +117,7 @@ impl UserHandler { } /// Create, register, and hydrate new user - async fn set_up_new_user(&self, user: User, addr: SocketAddr) { + async fn set_up_new_user(&mut self, user: User, addr: SocketAddr) { let user_tx = user.tx.clone(); let new_user = Arc::new(RwLock::new(user)); @@ -123,10 +127,7 @@ impl UserHandler { tokio::spawn(async move { tx.send(msg).await }); // Register uuid - self.state - .users_by_id - .write() - .unwrap() + self.users_by_id .insert(new_user.read().unwrap().uuid.clone(), new_user.clone()); // Register online using `addr` as key until something longer lived exists