X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fminecraft_controller.rs;h=545e8c228a3ded8038ea12653ca12f31051066f8;hb=3c7277e35a204d40d9e72794503a68530fcec682;hp=7af13dd9a4b02b73a383f813180fde1ffa6ef406;hpb=56bda46ddd9202d7d5485792fc2062fc69b6ed07;p=minecraft_web.git diff --git a/backend/src/minecraft_controller.rs b/backend/src/minecraft_controller.rs index 7af13dd..545e8c2 100644 --- a/backend/src/minecraft_controller.rs +++ b/backend/src/minecraft_controller.rs @@ -1,7 +1,5 @@ -use sysinfo::{ ProcessExt, SystemExt }; - use std::{ fs, time::SystemTime }; - +use sysinfo::{ ProcessExt, SystemExt }; use chrono::{ DateTime, offset::Local }; #[derive(Clone, Debug)] @@ -11,6 +9,7 @@ pub struct MinecraftExe { uptime: u64, // [s]. world_size: u64, // [B]. active_players: Vec, + version: String, last_backup: Option, } @@ -42,6 +41,10 @@ impl MinecraftExe { } } + pub fn format_version(&self) -> String { + self.version.clone() + } + pub fn format_last_backup(&self) -> String { match self.last_backup { Some(t) => { @@ -75,6 +78,7 @@ const MINECRAFT_PROCESS_NAME: &str = "java"; struct StatusFromRcon { players: Vec, + version: String, } fn get_status_from_rcon(rcon_password: &str) -> StatusFromRcon { @@ -100,12 +104,20 @@ fn get_status_from_rcon(rcon_password: &str) -> StatusFromRcon { println!("Error from 'list' command"); Vec::new() }, + }, + version: + match client.send_command("list".to_string()) { + Ok(resp) => resp.body, + Err(_e) => { + println!("Error from 'version' command"); + String::new() + } } } }, Err(_e) => { println!("Authentication error"); - StatusFromRcon { players: Vec::new() } + StatusFromRcon { players: Vec::new(), version: String::new() } }, }; @@ -132,12 +144,10 @@ fn get_last_backup_datetime(backup_path: &str) -> Option { pub fn get_minecraft_executable_information(world_path: &str, backup_path: &str, rcon_password: &str) -> Option { let mut system = sysinfo::System::new_all(); system.refresh_system(); - let processes = system.get_process_by_name(MINECRAFT_PROCESS_NAME); + let mut processes = system.processes_by_name(MINECRAFT_PROCESS_NAME); // TODO: find the correct process by checking the correct jar name in parameters. - if processes.len() >= 1 { - let process = processes.first().unwrap(); - + if let Some(process) = processes.next() { let world_size = match fs_extra::dir::get_size(world_path) { Ok(l) => l, Err(_) => 0u64 }; let status_from_rcon = get_status_from_rcon(rcon_password); @@ -145,10 +155,11 @@ pub fn get_minecraft_executable_information(world_path: &str, backup_path: &str, Some( MinecraftExe { memory: process.memory() / 3, // Because the Java garbage collector ZGC reports three times more the real memory usage: https://stackoverflow.com/a/62934057/212675 - load_average_5min: system.get_load_average().five / system.get_processors().len() as f64 * 100., + load_average_5min: system.load_average().five / system.processors().len() as f64 * 100., uptime: std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs() - process.start_time(), world_size, active_players: status_from_rcon.players, + version: status_from_rcon.version, last_backup: get_last_backup_datetime(backup_path) } )