aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/command.rs
blob: 63148709ba4937f1f5ab045f350a49797c5fd7a6 (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
use crate::state::{State, StatePhase};

use ipc_channel::ipc::IpcSender;
use log::*;
use mumlib::command::{Command, CommandResponse};
use std::sync::{Arc, Mutex};
use tokio::sync::mpsc;

pub async fn handle(
    state: Arc<Mutex<State>>,
    mut command_receiver: mpsc::UnboundedReceiver<(Command, IpcSender<mumlib::error::Result<Option<CommandResponse>>>)>,
) {
    debug!("Begin listening for commands");
    loop {
        debug!("Enter loop");
        let command = command_receiver.recv().await.unwrap();
        debug!("Received command {:?}", command.0);
        let mut state = state.lock().unwrap();
        debug!("Got mutex lock");
        let (wait_for_connected, command_response) = state.handle_command(command.0).await;
        if wait_for_connected {
            let mut watcher = state.phase_receiver();
            drop(state);
            debug!("Waiting to be connected");
            while !matches!(watcher.recv().await.unwrap(), StatePhase::Connected) {}
        }
        debug!("Sending response");
        command.1.send(command_response).unwrap();
        debug!("Sent response");
    }
    //TODO err if not connected
    //while let Some(command) = command_receiver.recv().await {
    //    debug!("Parsing command {:?}", command);
    //}

    //debug!("Finished handling commands");
}