X-Git-Url: http://git.euphorik.ch/index.cgi?a=blobdiff_plain;f=backend%2Fsrc%2Fminecraft_controller.rs;h=451b0164961d5ca249d6a96cc2eb65e4eabc1f92;hb=2e6831744641c8d8ebeea0a47c933448e8499ddb;hp=260a6e410fc2d0975539548aa3c953a95065fde8;hpb=16f93d7c5d110a92f90a4596e4fb3b994ae12932;p=minecraft_web.git diff --git a/backend/src/minecraft_controller.rs b/backend/src/minecraft_controller.rs index 260a6e4..451b016 100644 --- a/backend/src/minecraft_controller.rs +++ b/backend/src/minecraft_controller.rs @@ -1,12 +1,7 @@ -use sysinfo::{ ProcessExt, SystemExt }; - use std::{ fs, time::SystemTime }; - +use sysinfo::{ ProcessExt, SystemExt }; use chrono::{ DateTime, offset::Local }; -#[cfg(target_os = "linux")] -use systemd::journal; - #[derive(Clone, Debug)] pub struct MinecraftExe { memory: u64, // [kB]. @@ -14,6 +9,7 @@ pub struct MinecraftExe { uptime: u64, // [s]. world_size: u64, // [B]. active_players: Vec, + version: String, last_backup: Option, } @@ -38,13 +34,15 @@ impl MinecraftExe { } pub fn format_active_players(&self) -> String { - /* Commented because the player list isn't correct (the number is). if self.active_players.len() == 0 { String::from("") } else { self.active_players.join(", ") - }*/ - self.active_players.len().to_string() + } + } + + pub fn format_version(&self) -> String { + self.version.clone() } pub fn format_last_backup(&self) -> String { @@ -78,39 +76,45 @@ fn format_byte_size(bytes: u64, precision: usize) -> String { const MINECRAFT_PROCESS_NAME: &str = "java"; -#[cfg(target_os = "linux")] -const STRING_BEFORE_CHARACTER_NAME: &str = "Got character ZDOID from"; - -#[cfg(target_os = "linux")] -const STRING_BEFORE_NB_OF_CONNECTIONS: &str = "Connections"; +struct StatusFromRcon { + players: Vec +} -// 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 { +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) => { - println!("{}", resp.body); - Vec::new() - }, - Err(_e) => { - println!("Error asking seed"); - 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 { @@ -131,21 +135,22 @@ 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: process.memory(), - load_average_5min: system.get_load_average().five / system.get_processors().len() as f64 * 100., + 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.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) } )