|
|
|
@ -1,17 +1,17 @@
|
|
|
|
|
#![allow(dead_code)]
|
|
|
|
|
#![allow(unused_variables)]
|
|
|
|
|
use std::time::{Duration, Instant};
|
|
|
|
|
use std::{marker::PhantomData, time::{Duration, Instant}};
|
|
|
|
|
use sysinfo::System;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
enum ProcState {
|
|
|
|
|
NeverSeen,
|
|
|
|
|
Seen,
|
|
|
|
|
NotSeen,
|
|
|
|
|
NeverSeen,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
struct ProcessWatch {
|
|
|
|
|
struct Process {
|
|
|
|
|
first_seen: Option<Instant>,
|
|
|
|
|
last_seen: Option<Instant>,
|
|
|
|
|
state: ProcState,
|
|
|
|
@ -19,7 +19,7 @@ struct ProcessWatch {
|
|
|
|
|
pid: Option<usize>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl ProcessWatch {
|
|
|
|
|
impl Process {
|
|
|
|
|
fn new() -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
first_seen: None,
|
|
|
|
@ -30,23 +30,84 @@ impl ProcessWatch {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn refresh(&mut self, sysinfo: &System, last_refresh: Instant) {}
|
|
|
|
|
fn refresh(&mut self, sysinfo: &System, last_refresh: Instant) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Matcher<ConditionSeen> for Process {
|
|
|
|
|
type Condition = LifetimeCond<ConditionSeen>;
|
|
|
|
|
|
|
|
|
|
fn matches(&self, c: Self::Condition) -> bool {
|
|
|
|
|
if let Some(last_seen) = self.last_seen {
|
|
|
|
|
last_seen.elapsed() > c.span
|
|
|
|
|
} else { false }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct ConditionSeen {}
|
|
|
|
|
impl ConditionSeen {
|
|
|
|
|
fn from_duration(d: Duration) -> LifetimeCond<Self> {
|
|
|
|
|
LifetimeCond{span: d, ty: PhantomData{}}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct ConditionNotSeen {}
|
|
|
|
|
impl ConditionNotSeen {
|
|
|
|
|
fn from_duration(d: Duration) -> LifetimeCond<Self> {
|
|
|
|
|
LifetimeCond{span: d, ty: PhantomData{}}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait Condition<Type> {}
|
|
|
|
|
impl<T> Condition<T> for LifetimeCond<T>{}
|
|
|
|
|
|
|
|
|
|
struct LifetimeCond<CondType> {
|
|
|
|
|
span: Duration,
|
|
|
|
|
ty: PhantomData<CondType>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T> LifetimeCond<T> {
|
|
|
|
|
fn new(span: Duration) -> Self {
|
|
|
|
|
Self{ span, ty: PhantomData{} }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
trait Matcher<CondType> {
|
|
|
|
|
type Condition: Condition<CondType>;
|
|
|
|
|
|
|
|
|
|
enum Condition {
|
|
|
|
|
Seen(Duration),
|
|
|
|
|
NotSeen(Duration),
|
|
|
|
|
fn matches(&self, c: Self::Condition) -> bool;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trait CheckCondition {
|
|
|
|
|
fn check_condition(&self, c: Condition) -> bool;
|
|
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
unimplemented!()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// fn main() {
|
|
|
|
|
// let p = ProcessWatch::new();
|
|
|
|
|
// println!("{:?}", p);
|
|
|
|
|
// for cmd in watched_cmds {
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
//
|
|
|
|
|
// }
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test {
|
|
|
|
|
use sysinfo::{ProcessRefreshKind, RefreshKind, UpdateKind};
|
|
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn process_watch_condition() {
|
|
|
|
|
let cond = ConditionSeen::from_duration(Duration::from_secs(5));
|
|
|
|
|
|
|
|
|
|
let process_refresh_kind = ProcessRefreshKind::new()
|
|
|
|
|
.with_cmd(UpdateKind::Always)
|
|
|
|
|
.with_cwd(UpdateKind::Always)
|
|
|
|
|
.with_exe(UpdateKind::Always);
|
|
|
|
|
let process_refresh = RefreshKind::new().with_processes(process_refresh_kind);
|
|
|
|
|
let mut sys_info = System::new();
|
|
|
|
|
sys_info.refresh_specifics(process_refresh);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut p = Process::new();
|
|
|
|
|
p.refresh(&sys_info, Instant::now());
|
|
|
|
|
assert!(!p.matches(cond));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|