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
52
53
54
|
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,
},
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: 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");
}
|