summaryrefslogtreecommitdiffstats
path: root/src/network/utilization.rs
blob: 3b67a0a6d0ab1fc762c9e44342fccd02a512d5fd (plain)
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
use crate::network::{Connection, Direction, Segment};

use ::std::collections::HashMap;

pub struct TotalBandwidth {
    pub total_bytes_downloaded: u128,
    pub total_bytes_uploaded: u128,
}

impl TotalBandwidth {
    pub fn increment_bytes_downloaded(&mut self, ip_length: u128) {
        self.total_bytes_downloaded += ip_length;
    }
    pub fn increment_bytes_uploaded(&mut self, ip_length: u128) {
        self.total_bytes_uploaded += ip_length;
    }
}

pub struct Utilization {
    pub connections: HashMap<Connection, TotalBandwidth>,
}

impl Utilization {
    pub fn new() -> Self {
        let connections = HashMap::new();
        Utilization { connections }
    }
    pub fn reset(&mut self) {
        self.connections.clear();
    }
    pub fn update(&mut self, seg: &Segment) {
        let total_bandwidth =
            self.connections
                .entry(seg.connection.clone())
                .or_insert(TotalBandwidth {
                    total_bytes_downloaded: 0,
                    total_bytes_uploaded: 0,
                });
        match seg.direction {
            Direction::Download => total_bandwidth.increment_bytes_downloaded(seg.ip_length),
            Direction::Upload => total_bandwidth.increment_bytes_uploaded(seg.ip_length),
        }
    }
}