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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/// Contains configuration required to initialize a LinkConditioner
#[derive(Clone)]
pub struct LinkConditionerConfig {
    /// Delay to receive incoming messages in milliseconds
    pub incoming_latency: u32,
    /// The maximum additional random latency to delay received incoming
    /// messages in milliseconds. This may be added OR subtracted from the
    /// latency determined in the `incoming_latency` property above
    pub incoming_jitter: u32,
    /// The % chance that an incoming packet will be dropped.
    /// Represented as a value between 0 and 1
    pub incoming_loss: f32,
}

impl LinkConditionerConfig {
    /// Creates a new LinkConditionerConfig
    pub fn new(incoming_latency: u32, incoming_jitter: u32, incoming_loss: f32) -> Self {
        Self {
            incoming_latency,
            incoming_jitter,
            incoming_loss,
        }
    }

    pub fn perfect_condition() -> Self {
        Self {
            incoming_latency: 1,
            incoming_jitter: 0,
            incoming_loss: 0.0,
        }
    }

    /// Creates a new LinkConditioner that simulates a connection which is in a
    /// very good condition
    pub fn very_good_condition() -> Self {
        Self {
            incoming_latency: 12,
            incoming_jitter: 3,
            incoming_loss: 0.001,
        }
    }

    /// Creates a new LinkConditioner that simulates a connection which is in a
    /// good condition
    pub fn good_condition() -> Self {
        Self {
            incoming_latency: 40,
            incoming_jitter: 10,
            incoming_loss: 0.002,
        }
    }

    /// Creates a new LinkConditioner that simulates a connection which is in an
    /// average condition
    pub fn average_condition() -> Self {
        Self {
            incoming_latency: 100,
            incoming_jitter: 25,
            incoming_loss: 0.02,
        }
    }

    /// Creates a new LinkConditioner that simulates a connection which is in an
    /// poor condition
    pub fn poor_condition() -> Self {
        Self {
            incoming_latency: 200,
            incoming_jitter: 50,
            incoming_loss: 0.04,
        }
    }

    /// Creates a new LinkConditioner that simulates a connection which is in an
    /// very poor condition
    pub fn very_poor_condition() -> Self {
        Self {
            incoming_latency: 300,
            incoming_jitter: 75,
            incoming_loss: 0.06,
        }
    }
}