add new method to Status_obj

This commit is contained in:
primardj 2024-04-11 00:17:51 +01:00
parent 2a11b44fe1
commit a6debb3e51
2 changed files with 148 additions and 4 deletions

View file

@ -1,7 +1,7 @@
pub fn return_u8_in_u64(mut table: Vec<u8>) -> u64 {
// Table make 4 vector u8 in one u32
// Table make 4 vector u8 in one u64
if table.len() != 8 {
return 0
}

View file

@ -1,13 +1,157 @@
use std::path::Path;
pub struct Status_obj {
svname: String, // Service name
svpath: Path, // Service path
time: u64, // 0 -> 7
nano_seconds: u32, // 8 -> 11
pid: u32, // 12 -> 15
pause_run: bool, // 16
up_down: bool, // 17
up_down: char, // 17
term_sig: bool, // 18
run_finish: bool, // 19
is_down: bool, // down file
log_exists: bool, // is log folder exists ?
down: bool, // is down file ?
log: bool, // is log/supervise/ok exists ?
}
impl Status_obj {
pub fn new(path: Path, sv: String ) -> Option<Status_obj> {
// if error when making the status_obj, return None,
// Else, return the status_obj.
if ! set_current_dir(path).is_ok() {
println!("fail: {}: unable to change to service directory: file does not exist", sv);
return None
}
let status_option = fs::OpenOptions::new().read(true).open("supervise/status");
let mut status match status_option {
Ok(file) => file,
Err(..) => {
println!("warning: {}: unable to open supervise/status: file does not exist", sv);
return None;
},
};
let mut contents: Vec<u8> = Vec::new();
let option_size = status.read_to_end(&mut contents);
let size = match option_size {
Ok(size) => size,
Err(..) => {
println!("warning: {}: couldn't read supervise/status", sv);
return None
},
};
if size != 20 {
println!("warning: {}: bad supervise/status format", sv);
return None
}
let is_down;
if Path::new("down").exists() {
is_down = true
} else {
is_down = false
}
let log_exists;
if Path::new("log/supervise/ok").exists() {
log_exists = true
} else {
log_exists = false
}
let time_buf: Vec<u8> = contents[0..8].to_vec();
let nano_buf: Vec<u8> = contents[8..12].to_vec();
let pid_buf: Vec<u8> = contents[12..16].to_vec();
let seconds = return_u8_in_u64(time_buf);
let nano = return_u8_in_u32(nano_buf);
let pid_found = return_u8_in_u32(pid_buf);
let pause_run_raw = make_pause_run(contents[16]); // done
let up_down_raw = make_up_down(contents[17]); // done
let term_sig_raw = make_term_sig(contents[18]); // done
let run_finish_raw = make_run_finish(contents[19]); // done
Status_obj {
svname: sv,
svpath: path,
time: seconds,
nano_seconds: nano,
pid: pid_found,
pause_run: pause_run_raw,
up_down: up_down_raw,
term_sig: term_sig_raw,
run_finish: run_finish_raw,
down: is_down,
log: log_exists,
}
}
pub fn print_status(&mut self) {
println!("Ta gueule.");
}
}
fn make_pause_run(pr_int: u8) -> bool {
if pr_int == 0 {
return false // run
} else {
return true // pause.
}
}
fn make_up_down(ud_int: u8) -> char {
if ud_int == 117 {
return 'u'
} else {
return 'd'
}
}
fn make_term_sig(ts_int: u8) -> bool{
if ts_int == 0 {
return false // Normal
} else {
return true // Got Term
}
}
fn make_run_finish(rf_int: u8) -> bool {
if rf_int == 0 {
return false // service finish.
} else {
return true // service run
}
}
fn return_u8_in_u32(mut table: Vec<u8>) -> u32 {
// Table make 4 values u8 in one u32
if table.len() != 4 {
return 0
}
let mut r: u32 = 0;
table.reverse();
for i in table.iter() {
//println!("{}",i);
r = r * 256;
r += <u8 as Into<u32>>::into(*i);
}
return r
}
fn return_u8_in_u64(mut table: Vec<u8>) -> u64 {
// Table make 8 values u8 in one u64
if table.len() != 8 {
return 0
}
let mut r: u64 = 0;
table.reverse();
for i in table.iter() {
//println!("{}",i);
r = r * 256;
r += <u8 as Into<u64>>::into(*i);
}
return r
}