aboutsummaryrefslogtreecommitdiffstats
path: root/mumd/src/main.rs
blob: e7b4bec42cfdb8d5d4e8f997202daef712c94b45 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
mod audio;
mod client;
mod command;
mod network;
mod notify;
mod state;

use ipc_channel::ipc::{self, IpcOneShotServer, IpcSender};
use log::*;
use mumlib::command::{Command, CommandResponse};
use mumlib::setup_logger;
use std::fs;
use tokio::{join, sync::mpsc};
use tokio::task::spawn_blocking;

#[tokio::main]
async fn main() {
    if std::env::args().find(|s| s.as_str() == "--version").is_some() {
        println!(env!("VERSION"));
        return;
    }

    setup_logger(std::io::stderr(), true);
    notify::init();

    // check if another instance is live
    let (tx_client, rx_client) =
        ipc::channel::<mumlib::error::Result<Option<CommandResponse>>>().unwrap();
    if let Ok(server_name) = fs::read_to_string(mumlib::SOCKET_PATH) {
        if let Ok(tx0) = IpcSender::connect(server_name) {
            if tx0.send((Command::Ping, tx_client)).is_ok() {
                match rx_client.recv() {
                    Ok(Ok(Some(CommandResponse::Pong))) => {
                        error!("Another instance of mumd is already running");
                        return;
                    },
                    resp => {
                        warn!("Ping with weird response. Continuing...");
                        debug!("Response was {:?}", resp);
                    }
                }
            }
        }
    }

    let (command_sender, command_receiver) = mpsc::unbounded_channel::<(
        Command,
        IpcSender<mumlib::error::Result<Option<CommandResponse>>>,
    )>();

    let (_, e) = join!(
        client::handle(command_receiver),
        spawn_blocking(move || {
            // IpcSender is blocking
            receive_oneshot_commands(command_sender);
        }),
    );
    e.unwrap();
}

fn receive_oneshot_commands(
    command_sender: mpsc::UnboundedSender<(
        Command,
        IpcSender<mumlib::error::Result<Option<CommandResponse>>>,
    )>,
) {
    loop {
        // create listener
        let (server, server_name): (
            IpcOneShotServer<(
                Command,
                IpcSender<mumlib::error::Result<Option<CommandResponse>>>,
            )>,
            String,
        ) = IpcOneShotServer::new().unwrap();
        fs::write(mumlib::SOCKET_PATH, &server_name).unwrap();
        debug!("Listening to {}", server_name);

        // receive command and response channel
        let (_, conn): (
            _,
            (
                Command,
                IpcSender<mumlib::error::Result<Option<CommandResponse>>>,
            ),
        ) = server.accept().unwrap();
        debug!("Sending command {:?} to command handler", conn.0);
        command_sender.send(conn).unwrap();
    }
}