File size: 1,099 Bytes
da27866
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
//! Mock sensor: a scripted stand-in for the kernel adapters.
//!
//! Real builds replace this with eBPF ringbuffer readers (Linux) or
//! ETW/WFP consumers (Windows) that emit the same normalized `Event`s.

use crate::event::{Event, EventKind};
use crate::rtid::Rtid;

pub struct ScriptedSensor {
    script: Vec<(u64, Rtid, EventKind, String)>,
    cursor: usize,
}

impl ScriptedSensor {
    pub fn new(mut script: Vec<(u64, Rtid, EventKind, String)>) -> Self {
        script.sort_by_key(|s| s.0);
        Self { script, cursor: 0 }
    }

    /// Emit every scripted event whose time has arrived.
    pub fn poll(&mut self, now_ms: u64) -> Vec<Event> {
        let mut out = Vec::new();
        while self.cursor < self.script.len() && self.script[self.cursor].0 <= now_ms {
            let (at_ms, rtid, kind, detail) = self.script[self.cursor].clone();
            out.push(Event { rtid, kind, at_ms, detail });
            self.cursor += 1;
        }
        out
    }

    pub fn exhausted(&self) -> bool {
        self.cursor >= self.script.len()
    }
}