use crate::network::Network;
use crate::protocol::Protocol;
use crate::adversary::Adversary;
use crate::simulation::threshold::Threshold;
use crate::simulation::recorder::Recorder;
pub mod threshold;
pub mod recorder;
pub struct Simulation<P, A, T> where P: Protocol, A: Adversary, T: Threshold {
network: Network,
protocol: P,
adversary: A,
threshold: T,
recorders: Vec<Box<dyn Recorder>>,
}
impl<P, A, T> Simulation<P, A, T> where
P: Protocol,
A: Adversary,
T: Threshold,
{
pub fn new(
network: Network,
protocol: P,
adversary: A,
threshold: T,
recorders: Vec<Box<dyn Recorder>>,
) -> Self {
Simulation { network, protocol, adversary, threshold, recorders }
}
pub fn run(&mut self) {
let mut rd = 1;
loop {
let mut packets_to_inject = self.adversary.get_next_packets(&self.network, rd);
let num_to_inject = packets_to_inject.len();
for _ in 0..num_to_inject {
let p = packets_to_inject.remove(0);
self.protocol.add_packet(p, &mut self.network);
}
for recorder in &mut self.recorders {
recorder.record(rd, false, &self.network);
}
self.protocol.forward_packets(&mut self.network);
for recorder in &mut self.recorders {
recorder.record(rd, true, &self.network);
}
if self.threshold.check_termination(rd, &self.network) { break };
rd += 1;
}
for recorder in &mut self.recorders { recorder.close() }
}
}