diff options
Diffstat (limited to 'mumd/src/command.rs')
| -rw-r--r-- | mumd/src/command.rs | 42 |
1 files changed, 41 insertions, 1 deletions
diff --git a/mumd/src/command.rs b/mumd/src/command.rs index 5d6cca4..b4bd1b7 100644 --- a/mumd/src/command.rs +++ b/mumd/src/command.rs @@ -1,4 +1,12 @@ -enum Command { +use crate::state::{Channel, Server, State, StatePhase}; + +use log::*; +use std::collections::HashMap; +use std::sync::{Arc, Mutex}; +use tokio::sync::mpsc; + +#[derive(Clone, Debug)] +pub enum Command { ChannelJoin { channel_id: u32, }, @@ -12,3 +20,35 @@ enum Command { ServerDisconnect, Status, } + +#[derive(Debug)] +pub enum CommandResponse { + ChannelList { + channels: HashMap<u32, Channel>, + }, + Status { + username: Option<String>, + server_state: Server, + }, +} + +pub async fn handle( + state: Arc<Mutex<State>>, + mut command_receiver: mpsc::UnboundedReceiver<Command>, + command_response_sender: mpsc::UnboundedSender<Result<Option<CommandResponse>, ()>>, +) { + //TODO err if not connected + while let Some(command) = command_receiver.recv().await { + debug!("Parsing command {:?}", command); + let mut state = state.lock().unwrap(); + let (wait_for_connected, command_response) = state.handle_command(command).await; + if wait_for_connected { + let mut watcher = state.phase_receiver(); + drop(state); + while !matches!(watcher.recv().await.unwrap(), StatePhase::Connected) {} + } + command_response_sender.send(command_response).unwrap(); + } + + debug!("Finished handling commands"); +} |
