use crate::rng::Rng;
#[derive(Clone, Copy, Debug)]
pub struct NetworkModel {
pub latency_base: u64,
pub latency_jitter: u64,
pub drop_prob: f64,
}
impl NetworkModel {
pub fn perfect() -> Self {
Self {
latency_base: 1,
latency_jitter: 0,
drop_prob: 0.0,
}
}
pub fn new(latency_base: u64, latency_jitter: u64, drop_prob: f64) -> Self {
Self {
latency_base,
latency_jitter,
drop_prob,
}
}
pub fn delay(&self, rng: &mut Rng) -> u64 {
self.latency_base + rng.below(self.latency_jitter + 1)
}
pub fn drops(&self, rng: &mut Rng) -> bool {
rng.chance(self.drop_prob)
}
}
#[derive(Clone, Debug)]
pub struct Partition {
side: Vec<u8>,
}
impl Partition {
pub fn split(replicas: usize, group_a: &[usize]) -> Self {
let mut side = vec![0u8; replicas];
for &r in group_a {
if r < replicas {
side[r] = 1;
}
}
Self { side }
}
pub fn connected(&self, a: usize, b: usize) -> bool {
self.side.get(a) == self.side.get(b)
}
}