Skip to content

Commit 6fccc1c

Browse files
committed
refactor: unify InfoTraffic and InfoTrafficMessage structs
1 parent 4cbbc8f commit 6fccc1c

8 files changed

Lines changed: 78 additions & 159 deletions

File tree

src/chart/manage_chart_data.rs

Lines changed: 9 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,25 @@ use crate::TrafficChart;
44
use crate::networking::types::info_traffic::InfoTraffic;
55

66
impl TrafficChart {
7-
pub fn update_charts_data(&mut self, info_traffic: &InfoTraffic, no_more_packets: bool) {
7+
pub fn update_charts_data(&mut self, info_traffic_msg: &InfoTraffic, no_more_packets: bool) {
88
self.no_more_packets = no_more_packets;
99

1010
if self.ticks == 0 {
11-
self.first_packet_timestamp = info_traffic.last_packet_timestamp;
11+
self.first_packet_timestamp = info_traffic_msg.last_packet_timestamp;
1212
}
1313

1414
#[allow(clippy::cast_precision_loss)]
1515
let tot_seconds = self.ticks as f32;
1616
self.ticks += 1;
1717

1818
#[allow(clippy::cast_precision_loss)]
19-
let out_bytes_entry = -1.0
20-
* (info_traffic.tot_data_info.outgoing_bytes()
21-
- info_traffic.tot_data_info_prev.outgoing_bytes()) as f32;
19+
let out_bytes_entry = -1.0 * info_traffic_msg.tot_data_info.outgoing_bytes() as f32;
2220
#[allow(clippy::cast_precision_loss)]
23-
let in_bytes_entry = (info_traffic.tot_data_info.incoming_bytes()
24-
- info_traffic.tot_data_info_prev.incoming_bytes()) as f32;
21+
let in_bytes_entry = info_traffic_msg.tot_data_info.incoming_bytes() as f32;
2522
#[allow(clippy::cast_precision_loss)]
26-
let out_packets_entry = -1.0
27-
* (info_traffic.tot_data_info.outgoing_packets()
28-
- info_traffic.tot_data_info_prev.outgoing_packets()) as f32;
23+
let out_packets_entry = -1.0 * info_traffic_msg.tot_data_info.outgoing_packets() as f32;
2924
#[allow(clippy::cast_precision_loss)]
30-
let in_packets_entry = (info_traffic.tot_data_info.incoming_packets()
31-
- info_traffic.tot_data_info_prev.incoming_packets())
32-
as f32;
25+
let in_packets_entry = info_traffic_msg.tot_data_info.incoming_packets() as f32;
3326

3427
let out_bytes_point = (tot_seconds, out_bytes_entry);
3528
let in_bytes_point = (tot_seconds, in_bytes_entry);
@@ -168,7 +161,6 @@ mod tests {
168161

169162
use crate::chart::manage_chart_data::{ChartSeries, get_max, get_min};
170163
use crate::networking::types::data_info::DataInfo;
171-
use crate::networking::types::traffic_direction::TrafficDirection;
172164
use crate::utils::types::timestamp::Timestamp;
173165
use crate::{ChartType, InfoTraffic, Language, StyleType, TrafficChart};
174166

@@ -254,16 +246,7 @@ mod tests {
254246
spline: received_spl,
255247
all_time: vec![],
256248
};
257-
let tot_sent = 1000 * 28 + 500;
258-
let tot_received = 21000 * 28 + 1000;
259-
let tot_data_info_prev =
260-
DataInfo::new_for_tests(tot_received, tot_sent, tot_received, tot_sent);
261-
let tot_data_info = DataInfo::new_for_tests(
262-
tot_received + 4444,
263-
tot_sent + 3333,
264-
tot_received + 2222,
265-
tot_sent + 1111,
266-
);
249+
let tot_data_info = DataInfo::new_for_tests(4444, 3333, 2222, 1111);
267250
let mut traffic_chart = TrafficChart {
268251
ticks: 29,
269252
out_bytes: sent.clone(),
@@ -287,7 +270,6 @@ mod tests {
287270
all_packets: 0,
288271
tot_data_info,
289272
dropped_packets: 0,
290-
tot_data_info_prev,
291273
..Default::default()
292274
};
293275

@@ -299,9 +281,6 @@ mod tests {
299281
assert_eq!(get_min(&traffic_chart.out_packets), -3333.0);
300282
assert_eq!(get_max(&traffic_chart.in_bytes), 21000.0);
301283

302-
// prev values aren't updated here anymore: manually set them
303-
info_traffic.tot_data_info_prev = info_traffic.tot_data_info;
304-
305284
let mut sent_bytes = sent.clone();
306285
sent_bytes
307286
.spline
@@ -342,20 +321,9 @@ mod tests {
342321
received_bytes.spline.keys()
343322
);
344323

345-
info_traffic
346-
.tot_data_info
347-
.add_packets(990, 2, TrafficDirection::Incoming);
348-
info_traffic
349-
.tot_data_info
350-
.add_packet(99, TrafficDirection::Outgoing);
324+
info_traffic.tot_data_info = DataInfo::new_for_tests(990, 1, 2, 99);
351325
traffic_chart.update_charts_data(&info_traffic, false);
352-
info_traffic.tot_data_info_prev = info_traffic.tot_data_info;
353-
info_traffic
354-
.tot_data_info
355-
.add_packet(0, TrafficDirection::Incoming);
356-
info_traffic
357-
.tot_data_info
358-
.add_packets(220, 77, TrafficDirection::Outgoing);
326+
info_traffic.tot_data_info = DataInfo::new_for_tests(1, 220, 0, 77);
359327
traffic_chart.update_charts_data(&info_traffic, false);
360328

361329
sent_bytes.spline.remove(0);

src/gui/sniffer.rs

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use crate::networking::types::capture_context::{CaptureContext, CaptureSource, M
4949
use crate::networking::types::filters::Filters;
5050
use crate::networking::types::host::{Host, HostMessage};
5151
use crate::networking::types::host_data_states::HostDataStates;
52-
use crate::networking::types::info_traffic::InfoTrafficMessage;
52+
use crate::networking::types::info_traffic::InfoTraffic;
5353
use crate::networking::types::ip_collection::AddressCollection;
5454
use crate::networking::types::my_device::MyDevice;
5555
use crate::networking::types::port_collection::PortCollection;
@@ -68,7 +68,7 @@ use crate::utils::check_updates::set_newer_release_status;
6868
use crate::utils::error_logger::{ErrorLogger, Location};
6969
use crate::utils::types::file_info::FileInfo;
7070
use crate::utils::types::web_page::WebPage;
71-
use crate::{ConfigSettings, Configs, InfoTraffic, StyleType, TrafficChart, location};
71+
use crate::{ConfigSettings, Configs, StyleType, TrafficChart, location};
7272

7373
pub const FONT_FAMILY_NAME: &str = "Sarasa Mono SC for Sniffnet";
7474
pub const ICON_FONT_FAMILY_NAME: &str = "Icons for Sniffnet";
@@ -686,25 +686,23 @@ impl Sniffer {
686686
}
687687
}
688688

689-
fn refresh_data(&mut self, msg: InfoTrafficMessage, no_more_packets: bool) {
690-
self.info_traffic.refresh(msg, &self.favorite_hosts);
691-
self.update_thresholds();
692-
let info_traffic = &self.info_traffic;
693-
if info_traffic.tot_data_info.tot_packets() == 0 {
694-
return;
695-
}
689+
fn refresh_data(&mut self, msg: InfoTraffic, no_more_packets: bool) {
690+
self.traffic_chart.update_charts_data(&msg, no_more_packets);
696691
let emitted_notifications = notify_and_log(
697692
&mut self.logged_notifications,
698693
self.configs.settings.notifications,
699-
info_traffic,
694+
&msg,
695+
&self.favorite_hosts,
700696
&self.capture_source,
701697
);
702-
self.info_traffic.favorites_last_interval = HashSet::new();
698+
self.info_traffic.refresh(msg);
699+
self.update_thresholds();
700+
if self.info_traffic.tot_data_info.tot_packets() == 0 {
701+
return;
702+
}
703703
if self.thumbnail || self.running_page.ne(&RunningPage::Notifications) {
704704
self.unread_notifications += emitted_notifications;
705705
}
706-
self.traffic_chart
707-
.update_charts_data(&self.info_traffic, no_more_packets);
708706

709707
if let CaptureSource::Device(device) = &self.capture_source {
710708
let current_device_name = device.get_name().clone();
@@ -1117,13 +1115,6 @@ impl Sniffer {
11171115

11181116
// update host data states including the new host
11191117
self.host_data_states.data.update(&host);
1120-
1121-
// check if the newly resolved host was featured in the favorites (possible in case of already existing host)
1122-
if self.favorite_hosts.contains(&host) {
1123-
self.info_traffic
1124-
.favorites_last_interval
1125-
.insert((host, data_info_host));
1126-
}
11271118
}
11281119

11291120
fn register_sigint_handler() -> Task<Message> {
@@ -1160,7 +1151,7 @@ mod tests {
11601151
use crate::gui::types::timing_events::TimingEvents;
11611152
use crate::networking::types::data_info::DataInfo;
11621153
use crate::networking::types::host::Host;
1163-
use crate::networking::types::info_traffic::InfoTrafficMessage;
1154+
use crate::networking::types::info_traffic::InfoTraffic;
11641155
use crate::networking::types::traffic_direction::TrafficDirection;
11651156
use crate::notifications::types::logged_notification::{
11661157
DataThresholdExceeded, LoggedNotification,
@@ -1704,12 +1695,7 @@ mod tests {
17041695
.add_packet(0, TrafficDirection::Outgoing);
17051696

17061697
// Simulate a tick to apply the settings
1707-
sniffer.update(Message::TickRun(
1708-
0,
1709-
InfoTrafficMessage::default(),
1710-
vec![],
1711-
false,
1712-
));
1698+
sniffer.update(Message::TickRun(0, InfoTraffic::default(), vec![], false));
17131699
}
17141700
let mut sniffer = Sniffer::new(Configs::default());
17151701

@@ -1921,14 +1907,14 @@ mod tests {
19211907
#[parallel] // needed to not collide with other tests generating configs files
19221908
fn test_clear_all_notifications() {
19231909
let mut sniffer = Sniffer::new(Configs::default());
1924-
sniffer.logged_notifications =
1925-
VecDeque::from([LoggedNotification::PacketsThresholdExceeded(
1926-
DataThresholdExceeded {
1927-
threshold: 0,
1928-
data_info: DataInfo::default(),
1929-
timestamp: "".to_string(),
1930-
},
1931-
)]);
1910+
sniffer.logged_notifications = VecDeque::from([LoggedNotification::DataThresholdExceeded(
1911+
DataThresholdExceeded {
1912+
chart_type: ChartType::Packets,
1913+
threshold: 0,
1914+
data_info: DataInfo::default(),
1915+
timestamp: "".to_string(),
1916+
},
1917+
)]);
19321918

19331919
assert_eq!(sniffer.modal, None);
19341920
sniffer.update(Message::ShowModal(MyModal::ClearAll));

src/gui/types/message.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::gui::pages::types::running_page::RunningPage;
66
use crate::gui::pages::types::settings_page::SettingsPage;
77
use crate::gui::styles::types::gradient_type::GradientType;
88
use crate::networking::types::host::{Host, HostMessage};
9-
use crate::networking::types::info_traffic::InfoTrafficMessage;
9+
use crate::networking::types::info_traffic::InfoTraffic;
1010
use crate::notifications::types::notifications::Notification;
1111
use crate::report::types::search_parameters::SearchParameters;
1212
use crate::report::types::sort_type::SortType;
@@ -20,7 +20,7 @@ pub enum Message {
2020
/// Run tasks to initialize the app
2121
StartApp(Option<window::Id>),
2222
/// Sent by the backend parsing packets; includes the capture id, new data, new hosts batched data, and whether an offline capture has finished
23-
TickRun(usize, InfoTrafficMessage, Vec<HostMessage>, bool),
23+
TickRun(usize, InfoTraffic, Vec<HostMessage>, bool),
2424
/// Select network device
2525
DeviceSelection(String),
2626
/// Select IP filter

src/networking/manage_packets.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::networking::types::bogon::is_bogon;
1010
use crate::networking::types::capture_context::CaptureSource;
1111
use crate::networking::types::icmp_type::{IcmpType, IcmpTypeV4, IcmpTypeV6};
1212
use crate::networking::types::info_address_port_pair::InfoAddressPortPair;
13-
use crate::networking::types::info_traffic::InfoTrafficMessage;
13+
use crate::networking::types::info_traffic::InfoTraffic;
1414
use crate::networking::types::packet_filters_fields::PacketFiltersFields;
1515
use crate::networking::types::service::Service;
1616
use crate::networking::types::service_query::ServiceQuery;
@@ -249,7 +249,7 @@ pub fn get_service(
249249

250250
/// Function to insert the source and destination of a packet into the map containing the analyzed traffic
251251
pub fn modify_or_insert_in_map(
252-
info_traffic_msg: &mut InfoTrafficMessage,
252+
info_traffic_msg: &mut InfoTraffic,
253253
key: &AddressPortPair,
254254
cs: &CaptureSource,
255255
mac_addresses: (Option<String>, Option<String>),

src/networking/parse_packets.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::networking::types::data_info_host::DataInfoHost;
1717
use crate::networking::types::filters::Filters;
1818
use crate::networking::types::host::{Host, HostMessage};
1919
use crate::networking::types::icmp_type::IcmpType;
20-
use crate::networking::types::info_traffic::InfoTrafficMessage;
20+
use crate::networking::types::info_traffic::InfoTraffic;
2121
use crate::networking::types::my_link_type::MyLinkType;
2222
use crate::networking::types::packet_filters_fields::PacketFiltersFields;
2323
use crate::networking::types::traffic_direction::TrafficDirection;
@@ -48,7 +48,7 @@ pub fn parse_packets(
4848
let my_link_type = capture_context.my_link_type();
4949
let (mut cap, mut savefile) = capture_context.consume();
5050

51-
let mut info_traffic_msg = InfoTrafficMessage::default();
51+
let mut info_traffic_msg = InfoTraffic::default();
5252
let resolutions_state = Arc::new(Mutex::new(AddressesResolutionState::default()));
5353
// list of newly resolved hosts to be sent (batched to avoid UI updates too often)
5454
let new_hosts_to_send = Arc::new(Mutex::new(Vec::new()));
@@ -420,14 +420,14 @@ pub struct AddressesResolutionState {
420420

421421
#[allow(clippy::large_enum_variant)]
422422
pub enum BackendTrafficMessage {
423-
TickRun(usize, InfoTrafficMessage, Vec<HostMessage>, bool),
423+
TickRun(usize, InfoTraffic, Vec<HostMessage>, bool),
424424
PendingHosts(usize, Vec<HostMessage>),
425425
OfflineGap(usize, u32),
426426
}
427427

428428
fn maybe_send_tick_run_live(
429429
cap_id: usize,
430-
info_traffic_msg: &mut InfoTrafficMessage,
430+
info_traffic_msg: &mut InfoTraffic,
431431
new_hosts_to_send: &Arc<Mutex<Vec<HostMessage>>>,
432432
cs: &mut CaptureSource,
433433
first_packet_ticks: &mut Option<Instant>,
@@ -453,7 +453,7 @@ fn maybe_send_tick_run_live(
453453

454454
fn maybe_send_tick_run_offline(
455455
cap_id: usize,
456-
info_traffic_msg: &mut InfoTrafficMessage,
456+
info_traffic_msg: &mut InfoTraffic,
457457
new_hosts_to_send: &Arc<Mutex<Vec<HostMessage>>>,
458458
next_packet_timestamp: Timestamp,
459459
tx: &Sender<BackendTrafficMessage>,

src/networking/types/data_info.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,6 @@ impl DataInfo {
9696
self.final_instant = rhs.final_instant;
9797
}
9898

99-
pub fn subtract(&mut self, rhs: Self) {
100-
self.incoming_packets -= rhs.incoming_packets;
101-
self.outgoing_packets -= rhs.outgoing_packets;
102-
self.incoming_bytes -= rhs.incoming_bytes;
103-
self.outgoing_bytes -= rhs.outgoing_bytes;
104-
}
105-
10699
pub fn compare(&self, other: &Self, sort_type: SortType, chart_type: ChartType) -> Ordering {
107100
match chart_type {
108101
ChartType::Packets => match sort_type {

src/networking/types/info_traffic.rs

Lines changed: 3 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use crate::networking::types::data_info_host::DataInfoHost;
66
use crate::networking::types::host::Host;
77
use crate::networking::types::info_address_port_pair::InfoAddressPortPair;
88
use crate::utils::types::timestamp::Timestamp;
9-
use std::collections::{HashMap, HashSet};
9+
use std::collections::HashMap;
1010

1111
/// Struct containing overall traffic statistics and data.
12-
#[derive(Debug, Default)]
12+
#[derive(Debug, Default, Clone)]
1313
pub struct InfoTraffic {
1414
/// Total amount of exchanged data
1515
pub tot_data_info: DataInfo,
@@ -27,16 +27,10 @@ pub struct InfoTraffic {
2727
pub services: HashMap<Service, DataInfo>,
2828
/// Map of the hosts with their data info
2929
pub hosts: HashMap<Host, DataInfoHost>,
30-
/// Total amount of exchanged data before the current time interval
31-
pub tot_data_info_prev: DataInfo,
32-
/// Collection of favorite hosts that exchanged data in the last interval
33-
pub favorites_last_interval: HashSet<(Host, DataInfoHost)>,
3430
}
3531

3632
impl InfoTraffic {
37-
pub fn refresh(&mut self, msg: InfoTrafficMessage, favorites: &HashSet<Host>) {
38-
self.tot_data_info_prev = self.tot_data_info;
39-
33+
pub fn refresh(&mut self, msg: InfoTraffic) {
4034
self.tot_data_info.refresh(msg.tot_data_info);
4135

4236
self.all_packets += msg.all_packets;
@@ -64,13 +58,6 @@ impl InfoTraffic {
6458
.or_insert(value);
6559
}
6660

67-
self.favorites_last_interval = msg
68-
.hosts
69-
.iter()
70-
.filter(|(h, _)| favorites.contains(h))
71-
.map(|(h, data)| (h.clone(), *data))
72-
.collect();
73-
7461
for (key, value) in msg.hosts {
7562
self.hosts
7663
.entry(key)
@@ -101,30 +88,7 @@ impl InfoTraffic {
10188
)
10289
}
10390
}
104-
}
105-
106-
/// Struct containing traffic statistics and data related to the last time interval.
107-
#[derive(Debug, Clone, Default)]
108-
pub struct InfoTrafficMessage {
109-
/// Total amount of exchanged data
110-
pub tot_data_info: DataInfo,
111-
/// Total packets including those not filtered
112-
pub all_packets: u128,
113-
/// Total bytes including those not filtered
114-
pub all_bytes: u128,
115-
/// Number of dropped packets
116-
pub dropped_packets: u32,
117-
/// Timestamp of the latest parsed packet
118-
pub last_packet_timestamp: Timestamp,
119-
/// Map of the filtered traffic
120-
pub map: HashMap<AddressPortPair, InfoAddressPortPair>,
121-
/// Map of the upper layer services with their data info
122-
pub services: HashMap<Service, DataInfo>,
123-
/// Map of the hosts with their data info
124-
pub hosts: HashMap<Host, DataInfoHost>,
125-
}
12691

127-
impl InfoTrafficMessage {
12892
pub fn take_but_leave_something(&mut self) -> Self {
12993
let info_traffic = Self {
13094
last_packet_timestamp: self.last_packet_timestamp,

0 commit comments

Comments
 (0)