rust/hedgewars-checker/src/main.rs
author unc0rr
Sun, 06 Nov 2022 17:27:28 +0100
changeset 15916 0cd6996cd4c8
parent 15913 fe519de9c270
child 16008 1635ce22b214
permissions -rw-r--r--
Remove stray debugging
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
     1
use anyhow::{bail, Result};
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
     2
use argparse::{ArgumentParser, Store};
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
     3
use hedgewars_network_protocol::{
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
     4
    messages::HwProtocolMessage as ClientMessage, messages::HwServerMessage::*, parser,
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
     5
};
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
     6
use ini::Ini;
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
     7
use log::{debug, info, warn};
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
     8
use netbuf::Buf;
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
     9
use std::{io::Write, str::FromStr};
15913
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
    10
use tokio::{io, io::AsyncWriteExt, net::TcpStream, process::Command, sync::mpsc};
13974
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    11
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
    12
async fn check(executable: &str, data_prefix: &str, buffer: &[String]) -> Result<Vec<String>> {
13974
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    13
    let mut replay = tempfile::NamedTempFile::new()?;
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    14
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
    15
    for line in buffer.into_iter() {
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    16
        replay.write(&base64::decode(line)?)?;
13974
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    17
    }
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    18
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    19
    let temp_file_path = replay.path();
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    20
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    21
    let mut home_dir = dirs::home_dir().unwrap();
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    22
    home_dir.push(".hedgewars");
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    23
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    24
    debug!("Checking replay in {}", temp_file_path.to_string_lossy());
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    25
13974
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    26
    let output = Command::new(executable)
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    27
        .arg("--user-prefix")
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    28
        .arg(&home_dir)
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    29
        .arg("--prefix")
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    30
        .arg(data_prefix)
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    31
        .arg("--nomusic")
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    32
        .arg("--nosound")
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    33
        .arg("--stats-only")
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    34
        .arg(temp_file_path)
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
    35
        //.spawn()?
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
    36
        //.wait_with_output()
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
    37
        .output()
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
    38
        .await?;
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
    39
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
    40
    debug!("Engine finished!");
13974
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    41
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    42
    let mut result = Vec::new();
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    43
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    44
    let mut engine_lines = output
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    45
        .stderr
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    46
        .split(|b| *b == '\n' as u8)
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    47
        .skip_while(|l| *l != b"WINNERS" && *l != b"DRAW");
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    48
15916
0cd6996cd4c8 Remove stray debugging
unc0rr
parents: 15913
diff changeset
    49
    // debug!("Engine lines: {:?}", &engine_lines);
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
    50
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    51
    loop {
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    52
        match engine_lines.next() {
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
    53
            Some(b"DRAW") => result.push("DRAW".to_owned()),
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    54
            Some(b"WINNERS") => {
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
    55
                result.push("WINNERS".to_owned());
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    56
                let winners = engine_lines.next().unwrap();
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    57
                let winners_num = u32::from_str(&String::from_utf8(winners.to_vec())?)?;
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
    58
                result.push(String::from_utf8(winners.to_vec())?);
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    59
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    60
                for _i in 0..winners_num {
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
    61
                    result.push(String::from_utf8(engine_lines.next().unwrap().to_vec())?);
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    62
                }
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    63
            }
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    64
            Some(b"GHOST_POINTS") => {
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
    65
                result.push("GHOST_POINTS".to_owned());
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    66
                let points = engine_lines.next().unwrap();
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    67
                let points_num = u32::from_str(&String::from_utf8(points.to_vec())?)? * 2;
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
    68
                result.push(String::from_utf8(points.to_vec())?);
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    69
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    70
                for _i in 0..points_num {
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
    71
                    result.push(String::from_utf8(engine_lines.next().unwrap().to_vec())?);
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    72
                }
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    73
            }
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    74
            Some(b"ACHIEVEMENT") => {
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
    75
                result.push("ACHIEVEMENT".to_owned());
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    76
                for _i in 0..4 {
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
    77
                    result.push(String::from_utf8(engine_lines.next().unwrap().to_vec())?);
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    78
                }
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    79
            }
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    80
            _ => break,
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    81
        }
13974
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    82
    }
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    83
15913
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
    84
    // println!("Engine lines: {:?}", &result);
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
    85
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    86
    if result.len() > 0 {
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    87
        Ok(result)
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    88
    } else {
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
    89
        bail!("no data from engine")
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
    90
    }
13974
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    91
}
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
    92
15913
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
    93
async fn check_loop(
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
    94
    executable: &str,
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
    95
    data_prefix: &str,
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
    96
    results_sender: mpsc::Sender<Result<Vec<String>>>,
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
    97
    mut replay_receiver: mpsc::Receiver<Vec<String>>,
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
    98
) -> Result<()> {
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
    99
    while let Some(replay) = replay_receiver.recv().await {
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   100
        results_sender
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   101
            .send(check(executable, data_prefix, &replay).await)
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   102
            .await?;
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   103
    }
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   104
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   105
    Ok(())
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   106
}
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   107
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   108
async fn connect_and_run(
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   109
    username: &str,
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   110
    password: &str,
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   111
    protocol_number: u16,
15913
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   112
    replay_sender: mpsc::Sender<Vec<String>>,
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   113
    mut results_receiver: mpsc::Receiver<Result<Vec<String>>>,
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   114
) -> Result<()> {
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   115
    info!("Connecting...");
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   116
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   117
    let mut stream = TcpStream::connect("hedgewars.org:46631").await?;
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   118
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   119
    let mut buf = Buf::new();
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   120
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   121
    loop {
15913
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   122
        let r = tokio::select! {
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   123
            _ = stream.readable() => None,
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   124
            r = results_receiver.recv() => r
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   125
        };
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   126
15913
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   127
        //println!("Loop: {:?}", &r);
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   128
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   129
        if let Some(execution_result) = r {
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   130
            match execution_result {
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   131
                Ok(result) => {
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   132
                    info!("Checked");
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   133
                    debug!("Check result: [{:?}]", result);
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   134
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   135
                    stream
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   136
                        .write(
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   137
                            ClientMessage::CheckedOk(result)
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   138
                                .to_raw_protocol()
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   139
                                .as_bytes(),
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   140
                        )
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   141
                        .await?;
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   142
                    stream
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   143
                        .write(ClientMessage::CheckerReady.to_raw_protocol().as_bytes())
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   144
                        .await?;
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   145
                }
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   146
                Err(e) => {
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   147
                    info!("Check failed: {:?}", e);
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   148
                    stream
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   149
                        .write(
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   150
                            ClientMessage::CheckedFail("error".to_owned())
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   151
                                .to_raw_protocol()
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   152
                                .as_bytes(),
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   153
                        )
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   154
                        .await?;
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   155
                    stream
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   156
                        .write(ClientMessage::CheckerReady.to_raw_protocol().as_bytes())
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   157
                        .await?;
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   158
                }
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   159
            }
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   160
        } else {
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   161
            let mut msg = [0; 4096];
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   162
            // Try to read data, this may still fail with `WouldBlock`
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   163
            // if the readiness event is a false positive.
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   164
            match stream.try_read(&mut msg) {
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   165
                Ok(n) => {
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   166
                    //println!("{:?}", &msg);
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   167
                    buf.write_all(&msg[0..n])?;
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   168
                }
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   169
                Err(ref e) if e.kind() == io::ErrorKind::WouldBlock => {}
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   170
                Err(e) => {
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   171
                    return Err(e.into());
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   172
                }
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   173
            }
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   174
        }
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   175
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   176
        while let Ok((tail, msg)) = parser::server_message(buf.as_ref()) {
15834
8c39a11f7756 eat warnings
alfadur
parents: 15833
diff changeset
   177
            let tail_len = tail.len();
8c39a11f7756 eat warnings
alfadur
parents: 15833
diff changeset
   178
            buf.consume(buf.len() - tail_len);
13974
e98e2fc556a7 Create replay file, run engine, result from it and send it to the server. Not implemented: filtering of engine output.
unc0rr
parents: 13973
diff changeset
   179
15913
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   180
            // println!("Message from server: {:?}", &msg);
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   181
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   182
            match msg {
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   183
                Connected(_, _) => {
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   184
                    info!("Connected");
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   185
                    stream
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   186
                        .write(
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   187
                            ClientMessage::Checker(
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   188
                                protocol_number,
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   189
                                username.to_owned(),
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   190
                                password.to_owned(),
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   191
                            )
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   192
                            .to_raw_protocol()
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   193
                            .as_bytes(),
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   194
                        )
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   195
                        .await?;
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   196
                }
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   197
                Ping => {
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   198
                    stream
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   199
                        .write(ClientMessage::Pong.to_raw_protocol().as_bytes())
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   200
                        .await?;
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   201
                }
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   202
                LogonPassed => {
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   203
                    stream
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   204
                        .write(ClientMessage::CheckerReady.to_raw_protocol().as_bytes())
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   205
                        .await?;
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   206
                }
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   207
                Replay(lines) => {
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   208
                    info!("Got a replay");
15913
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   209
                    replay_sender.send(lines).await?;
13976
a857cd1cc3f0 Finish hedgewars-checker implementation
unc0rr
parents: 13974
diff changeset
   210
                }
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   211
                Bye(message) => {
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   212
                    warn!("Received BYE: {}", message);
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   213
                    return Ok(());
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   214
                }
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   215
                ChatMsg { nick, msg } => {
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   216
                    info!("Chat [{}]: {}", nick, msg);
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   217
                }
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   218
                RoomAdd(fields) => {
15834
8c39a11f7756 eat warnings
alfadur
parents: 15833
diff changeset
   219
                    let l = fields.into_iter();
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   220
                    info!("Room added: {}", l.skip(1).next().unwrap());
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   221
                }
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   222
                RoomUpdated(name, fields) => {
15834
8c39a11f7756 eat warnings
alfadur
parents: 15833
diff changeset
   223
                    let l = fields.into_iter();
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   224
                    let new_name = l.skip(1).next().unwrap();
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   225
15834
8c39a11f7756 eat warnings
alfadur
parents: 15833
diff changeset
   226
                    if name != new_name {
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   227
                        info!("Room renamed: {}", new_name);
13978
a4877a16564d Also report chat and added rooms in log
unc0rr
parents: 13976
diff changeset
   228
                    }
a4877a16564d Also report chat and added rooms in log
unc0rr
parents: 13976
diff changeset
   229
                }
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   230
                RoomRemove(_) => {
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   231
                    // ignore
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   232
                }
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   233
                Error(message) => {
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   234
                    warn!("Received ERROR: {}", message);
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   235
                    return Ok(());
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   236
                }
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   237
                something => {
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   238
                    warn!("Unexpected protocol command: {:?}", something)
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   239
                }
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   240
            }
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   241
        }
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   242
    }
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   243
}
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   244
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   245
async fn get_protocol_number(executable: &str) -> Result<u16> {
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   246
    let output = Command::new(executable).arg("--protocol").output().await?;
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   247
15833
a855f32ab3ca - Update hedgewars-network-protocol library with messages needed for checker
unc0rr
parents: 14417
diff changeset
   248
    Ok(u16::from_str(&String::from_utf8(output.stdout).unwrap().trim()).unwrap_or(55))
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   249
}
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   250
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   251
#[tokio::main]
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   252
async fn main() -> Result<()> {
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   253
    stderrlog::new()
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   254
        .verbosity(3)
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   255
        .timestamp(stderrlog::Timestamp::Second)
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   256
        .module(module_path!())
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   257
        .init()
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   258
        .unwrap();
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   259
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   260
    let mut frontend_settings = dirs::home_dir().unwrap();
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   261
    frontend_settings.push(".hedgewars/settings.ini");
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   262
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   263
    let i = Ini::load_from_file(frontend_settings.to_str().unwrap()).unwrap();
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   264
    let username = i.get_from(Some("net"), "nick").unwrap();
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   265
    let password = i.get_from(Some("net"), "passwordhash").unwrap();
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   266
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   267
    let mut exe = "/usr/local/bin/hwengine".to_string();
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   268
    let mut prefix = "/usr/local/share/hedgewars/Data".to_string();
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   269
    {
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   270
        let mut ap = ArgumentParser::new();
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   271
        ap.set_description("Game replay checker for hedgewars.");
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   272
        ap.refer(&mut exe)
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   273
            .add_option(&["--exe"], Store, "Path to hwengine executable");
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   274
        ap.refer(&mut prefix)
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   275
            .add_option(&["--prefix"], Store, "Path main Data dir");
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   276
        ap.parse_args_or_exit();
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   277
    }
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   278
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   279
    info!("Executable: {}", exe);
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   280
    info!("Data dir: {}", prefix);
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   281
15835
ad79e5c0885c Begin attempt to convert checker into async using tokio
unc0rr
parents: 15834
diff changeset
   282
    let protocol_number = get_protocol_number(&exe.as_str()).await.unwrap_or_default();
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   283
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   284
    info!("Using protocol number {}", protocol_number);
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   285
15913
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   286
    let (replay_sender, replay_receiver) = mpsc::channel(1);
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   287
    let (results_sender, results_receiver) = mpsc::channel(1);
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   288
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   289
    let (network_result, checker_result) = tokio::join!(
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   290
        connect_and_run(
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   291
            &username,
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   292
            &password,
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   293
            protocol_number,
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   294
            replay_sender,
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   295
            results_receiver
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   296
        ),
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   297
        check_loop(&exe, &prefix, results_sender, replay_receiver)
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   298
    );
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   299
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   300
    network_result?;
fe519de9c270 - Run engine and socket listener in parallel to avoid ping timeouts
unc0rr
parents: 15835
diff changeset
   301
    checker_result
13973
d12ca66054aa Start checker implementation in rust
unc0rr
parents:
diff changeset
   302
}