X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fminecraft_controller.rs;h=690f3d990fe6dae94834c774952e051d8c386fd9;hb=29f7678190211ea660f922f731fcb8993b08cfb9;hp=f04ed24d974085fc1dbde4d832cbe0150373b602;hpb=026120042fe286bc09af4b5f38a345954d73f681;p=minecraft_web.git diff --git a/backend/src/minecraft_controller.rs b/backend/src/minecraft_controller.rs index f04ed24..690f3d9 100644 --- a/backend/src/minecraft_controller.rs +++ b/backend/src/minecraft_controller.rs @@ -1,22 +1,21 @@ -use sysinfo::{ ProcessExt, SystemExt }; - use std::{ fs, time::SystemTime }; - +use sysinfo::{ ProcessExt, SystemExt }; use chrono::{ DateTime, offset::Local }; #[derive(Clone, Debug)] pub struct MinecraftExe { - memory: u64, // [kB]. + memory: u64, // [B]. load_average_5min: f64, // [%]. uptime: u64, // [s]. world_size: u64, // [B]. active_players: Vec, + version: String, last_backup: Option, } impl MinecraftExe { pub fn format_memory(&self) -> String { - format_byte_size(self.memory * 1024, 2) + format_byte_size(self.memory, 2) } pub fn format_load_average(&self) -> String { @@ -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) => { @@ -73,37 +76,45 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { const MINECRAFT_PROCESS_NAME: &str = "java"; -// It doesn't work for the moment, it only scan the connection event and do not treat disconnections. -fn get_active_players(rcon_password: &str) -> Vec { +struct StatusFromRcon { + players: Vec +} + +fn get_status_from_rcon(rcon_password: &str) -> StatusFromRcon { let mut client = minecraft_client_rs::Client::new("127.0.0.1:25575".to_string()).unwrap(); - let players = + let status = match client.authenticate(rcon_password.to_string()) { Ok(_) => { - match client.send_command("list".to_string()) { - Ok(resp) => { - resp.body - .split('\n') - .skip(1) - .filter(|n| !n.is_empty()) - .map(|n| n.to_string()) - .collect() - }, - Err(_e) => { - println!("Error from 'list' command"); - Vec::new() - }, + StatusFromRcon { + players: + match client.send_command("list".to_string()) { + Ok(resp) => + match resp.body.find(':') { + Some(i) if i < resp.body.len() -1 => + resp.body[i + 1..resp.body.len()] + .split(',') + .map(|nick| nick.trim().to_string()) + .filter(|nick| !nick.is_empty()) + .collect(), + _ => Vec::new() + }, + Err(_e) => { + println!("Error from 'list' command"); + Vec::new() + }, + } } }, Err(_e) => { println!("Authentication error"); - Vec::new() + StatusFromRcon { players: Vec::new() } }, }; client.close().unwrap(); - players + status } fn get_last_backup_datetime(backup_path: &str) -> Option { @@ -124,21 +135,23 @@ 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 world_size = match std::fs::metadata(world_path) { Ok(f) => f.len(), Err(_) => 0u64 }; + let status_from_rcon = get_status_from_rcon(rcon_password); Some( MinecraftExe { + // 'memory' was divided by 3 but it not necessary any more (the Java garbage collector ZGC reports three times more the real memory usage: https://stackoverflow.com/a/62934057/212675). memory: process.memory(), - load_average_5min: system.get_load_average().five / system.get_processors().len() as f64 * 100., + load_average_5min: system.load_average().five / system.cpus().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: get_active_players(rcon_password), + active_players: status_from_rcon.players, + version: process.cmd()[process.cmd().len() - 2].clone(), // TODO: Extract the version from the .jar filename. last_backup: get_last_backup_datetime(backup_path) } )