aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/command.rs
blob: 8a5c7152b1861a9b976e77d61c919227505677b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use crate::state::{Channel, Server, State, StatePhase};

use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;
use log::*;

#[derive(Debug)]
pub enum Command {
    ChannelJoin {
        channel_id: u32,
    },
    ChannelList,
    ServerConnect {
        host: String,
        port: u16,
        username: String,
        accept_invalid_cert: bool, //TODO ask when connecting
    },
    ServerDisconnect,
    Status,
}

#[derive(Debug)]
pub enum CommandResponse {
    ChannelList {
        channels: HashMap<u32, Channel>,
    },
    Status {
        username: 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, _) = 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) {}
        }
    }
}