Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/mealt. Mind melt OpenMLS, Tauri, SolidJS, axum experimentation
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148use std::sync::Arc;
use axum::{ body::Bytes, extract::ws::{Message, WebSocket},};use openmls::framing::MlsMessageIn;use tokio::sync::{ mpsc::{self, error::SendError}, Mutex,};
struct UserActor { id: String, receiver: mpsc::Receiver<UserActorMessage>, websocket: WebSocket, other_actors: Arc<Mutex<Vec<UserActorHandle>>>,}enum UserActorMessage { /// Instruct the actor to send a message to the user represented by the actor SendMessage(Vec<u8>),}
enum Instruction { Continue, Stop,}
impl UserActor { fn new( id: String, websocket: WebSocket, receiver: mpsc::Receiver<UserActorMessage>, other_actors: Arc<Mutex<Vec<UserActorHandle>>>, ) -> Self { UserActor { id, receiver, websocket, other_actors, } }
async fn handle_websocket_message(&mut self, message: Message) -> Instruction { if let Message::Close(close) = message { tracing::debug!("Received close message: {:?}", close); return Instruction::Stop; }
let Message::Binary(binary) = message else { tracing::warn!("Received unexpected message type"); return Instruction::Continue; };
let mut others = self.other_actors.lock().await; let mut dead_actors = Vec::new(); for (index, other) in others.iter().enumerate() { if other.id == self.id { continue; } //TODO use shared reference instead to avoid cloning of possibly large messages let result = other.send_message(binary.clone()).await; // Erros when channel is closed if let Err(error) = result { dead_actors.push(index); tracing::error!("Error sending actor message: {:?}", error); } }
for index in dead_actors.into_iter() { tracing::debug!("Cleaning up deceased actor remains at index: {:?}", index); others.remove(index); }
Instruction::Continue }
async fn handle_message(&mut self, message: UserActorMessage) -> Instruction { match message { UserActorMessage::SendMessage(message) => { tracing::debug!("Sending message: {:?}", message); let result = self.websocket.send(Message::Binary(message)).await; if let Err(error) = result { tracing::error!("Error sending message: {:?}", error); return Instruction::Stop; }
Instruction::Continue } } }}
async fn run_my_actor(mut actor: UserActor) { tracing::debug!("Actor started");
loop { tokio::select! { Some(message) = actor.receiver.recv() => { let result = actor.handle_message(message).await; if let Instruction::Stop = result { break; } }, // Stop actor on error Some(Ok(message)) = actor.websocket.recv() => { let result = actor.handle_websocket_message(message).await; if let Instruction::Stop = result { break; } }, else => break, } }
tracing::debug!("Actor stopped");}
pub(crate) struct UserActorHandle { /// A simple unique identifier for the actor to distinguish it from others /// The actor and actor handle share the same id id: String, sender: mpsc::Sender<UserActorMessage>,}
impl UserActorHandle { pub(crate) fn new( id: String, websocket: WebSocket, other_actors: Arc<Mutex<Vec<UserActorHandle>>>, ) -> Self { let (sender, receiver) = mpsc::channel(8); let actor = UserActor::new(id.clone(), websocket, receiver, other_actors); tokio::spawn(run_my_actor(actor)); Self { id, sender } }
/// Errors when actor stopped receiving messages meaning the channel is closed and the actor is deceased pub(crate) async fn send_message( &self, message: Vec<u8>, ) -> Result<(), impl std::error::Error> { self.sender .send(UserActorMessage::SendMessage(message)) .await }}