simple_demo/
simple-demo.rs

1use std::env;
2
3use ethrecv::{PacketHandler, RecvThread};
4
5#[derive(Debug)]
6enum AppError {}
7
8#[derive(Default)]
9struct PktProc {}
10
11impl PacketHandler for PktProc {
12  type Error = AppError;
13
14  fn proc(&mut self, _pkt: pcap::Packet) -> Result<(), Self::Error> {
15    eprintln!("packet!");
16    Ok(())
17  }
18
19  #[cfg(feature = "idle")]
20  fn idle(&mut self) -> Result<(), Self::Error> {
21    eprintln!("idle!");
22    Ok(())
23  }
24
25  #[cfg(feature = "inspect")]
26  fn inspect(&self, info: &ethrecv::RecvInfo) {
27    eprintln!("inspect!");
28    eprintln!("overflow drop: {}", info.overflow_dropped);
29    eprintln!("if drop: {}", info.if_dropped);
30    eprintln!("num raw pkts: {}", info.num_raw_pkts);
31    eprintln!("raw pkt bytes: {}", info.raw_pkt_bytes);
32    eprintln!("uptime: {:?}", info.runtime);
33  }
34}
35
36fn main() {
37  let args: Vec<String> = env::args().skip(1).collect();
38
39  let rt = RecvThread::new(&args[0]);
40
41  #[cfg(feature = "idle")]
42  let rt = rt.idle_duration(std::time::Duration::from_secs(4));
43
44  let pp = PktProc::default();
45
46  #[cfg(feature = "inspect")]
47  let mut ctrl = rt.run(pp).unwrap();
48
49  #[cfg(not(feature = "inspect"))]
50  let ctrl = rt.run(pp).unwrap();
51
52  std::thread::sleep(std::time::Duration::from_secs(10));
53
54  #[cfg(feature = "inspect")]
55  ctrl.signal_inspect().unwrap();
56
57  #[cfg(feature = "inspect")]
58  {
59    let stats = ctrl.inspect().unwrap();
60    println!("{stats:#?}");
61  }
62
63  std::thread::sleep(std::time::Duration::from_secs(30));
64
65  #[cfg(feature = "inspect")]
66  {
67    let stats = ctrl.inspect().unwrap();
68    println!("{stats:#?}");
69  }
70
71  println!("Killing receiver thread..");
72
73  ctrl.shutdown().unwrap();
74}
75
76// vim: set ft=rust et sw=2 ts=2 sts=2 cinoptions=2 tw=79 :