Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/chat-fww. Chat app project for frameworks and workflows and w...?
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168use std::error::Error;use std::sync::Arc;
use crate::actor::websocket::SocketId;use tokio::sync::mpsc::{self};
use super::{delivery_service, websocket, ChatMessage};
/// A user actor represents a user that can send and receive messages/// It also keeps track of all sockets that are connected to it as a user can have multiple devices connected a once/// and the messages are sent to all of them.struct User { delivery_service: delivery_service::Handle, name: Arc<str>, receiver: mpsc::Receiver<Message>, sockets: Vec<websocket::Handle>,}
enum Message { AddSocket(websocket::Handle), ProcessSocketMessage(SocketId, Arc<ChatMessage>), ReceiveMessage(Arc<ChatMessage>), RemoveSocket(SocketId), AddContact(Arc<str>), RemoveContact(Arc<str>),}
async fn run_actor(mut actor: User) { while let Some(message) = actor.receiver.recv().await { match message { Message::AddSocket(socket) => { actor.sockets.push(socket); } Message::ProcessSocketMessage(source, message) => { // Synchronize message to all other connected sockets for this user for socket in &actor.sockets { if socket.id == source { continue; }
tracing::debug!("Syncing message"); let result = socket.synchronize_message(message.clone()).await; if let Err(error) = result { tracing::error!("Error sending message to socket: {}", error); } }
// Send message to the user it is intended for through delivery service let result = actor.delivery_service.send_message(message).await; let Err(error) = result else { continue; }; tracing::error!("Error sending message to delivery service: {:?}", error); } Message::ReceiveMessage(message) => { // Creating a reference that is easier to clone for each loop iteration //TODO this can easily be parallelized as it is fire and forget for socket in &actor.sockets { let result = socket.send_message(message.clone()).await; if let Err(error) = result { tracing::error!("Error sending message to socket: {}", error); } } } Message::RemoveSocket(handle_id) => { // Not using retain as it would need to go through all elements, and we can be fairly // sure that the socket is only once in the list. Meaning after it was found the // iterations would be useless. let position = actor .sockets .iter() .position(|handle| handle.id == handle_id); let Some(position) = position else { // This is weird tracing::warn!("Socket not found for deletion"); continue; };
let _ = actor.sockets.remove(position);
// If the socket is the last one, we can remove the user from the delivery service if actor.sockets.is_empty() { tracing::debug!("All sockets closed, removing user from delivery service"); let result = actor.delivery_service.remove_user(actor.name.clone()).await; // Shut down if let Err(error) = result { tracing::error!("Error removing user from delivery service: {:?}", error); } // Shut down anyway? break; } } Message::AddContact(user_name) => { for socket in &actor.sockets { let result = socket.add_contact(user_name.clone()).await; if let Err(error) = result { tracing::error!("Error adding user to socket: {}", error); } } } Message::RemoveContact(user_name) => { for socket in &actor.sockets { let result = socket.remove_contact(user_name.clone()).await; if let Err(error) = result { tracing::error!("Error removing user from socket: {}", error); } } } } }}
#[derive(Clone)]pub(crate) struct Handle { sender: mpsc::Sender<Message>,}
impl Handle { pub(crate) fn new(name: Arc<str>, delivery_service: delivery_service::Handle) -> Self { let (sender, receiver) = mpsc::channel(8);
let actor = User { delivery_service, name, receiver, // We know from context that a user will be only created if there is at least one socket sockets: Vec::with_capacity(1), };
tokio::spawn(run_actor(actor));
Self { sender } }
pub(crate) async fn add_socket(&self, socket: websocket::Handle) -> Result<(), impl Error> { self.sender.send(Message::AddSocket(socket)).await }
pub(super) async fn process_socket_message( &self, source: SocketId, message: Arc<ChatMessage>, ) -> Result<(), impl Error + Send + Sync> { self.sender .send(Message::ProcessSocketMessage(source, message)) .await }
pub(super) async fn receive_message( &self, message: Arc<ChatMessage>, ) -> Result<(), impl Error> { self.sender.send(Message::ReceiveMessage(message)).await }
pub(super) async fn remove_socket(&self, socket_id: SocketId) -> Result<(), impl Error> { self.sender.send(Message::RemoveSocket(socket_id)).await }
pub(super) async fn add_contact(&self, user_name: Arc<str>) -> Result<(), impl Error> { self.sender.send(Message::AddContact(user_name)).await }
pub(super) async fn remove_contact(&self, user_name: Arc<str>) -> Result<(), impl Error> { self.sender.send(Message::RemoveContact(user_name)).await }}