use std::time::Duration;
use rand::Rng;
use rand_pcg::Pcg64Mcg as Random;
#[derive(Clone, Debug)]
pub struct LinkConditioner {
packet_loss: f64,
latency: Duration,
random: Random,
}
impl LinkConditioner {
#[allow(dead_code)]
pub fn new() -> LinkConditioner {
LinkConditioner {
packet_loss: 0.0,
latency: Duration::default(),
random: Random::new(0),
}
}
#[allow(dead_code)]
pub fn set_packet_loss(&mut self, rate: f64) {
self.packet_loss = rate;
}
#[allow(dead_code)]
pub fn set_latency(&mut self, latency: Duration) {
self.latency = latency
}
pub fn should_send(&mut self) -> bool {
self.random.gen_range(0.0..1.0) >= self.packet_loss
}
}
impl Default for LinkConditioner {
fn default() -> Self {
Self::new()
}
}