Skip to content

Commit

Permalink
Extract Networks from System
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Sep 15, 2023
1 parent 9ee1f2c commit fb746c5
Show file tree
Hide file tree
Showing 18 changed files with 217 additions and 269 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ You have an example into the `examples` folder. You can run it with `cargo run -
Otherwise, here is a little code sample:

```rust
use sysinfo::{NetworkExt, NetworksExt, ProcessExt, System, SystemExt};
use sysinfo::{NetworkExt, Networks, NetworksExt, ProcessExt, System, SystemExt};

// Please note that we use "new_all" to ensure that all list of
// components, network interfaces, disks and users are already
Expand All @@ -51,8 +51,10 @@ for disk in sys.disks().iter() {
}

// Network interfaces name, data received and data transmitted:
let mut networks = Networks::new();
networks.refresh_list();
println!("=> networks:");
for (interface_name, data) in sys.networks() {
for (interface_name, data) in &networks {
println!("{}: {}/{} B", interface_name, data.received(), data.transmitted());
}

Expand Down
14 changes: 8 additions & 6 deletions examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::io::{self, BufRead, Write};
use std::str::FromStr;
use sysinfo::Signal::*;
use sysinfo::{
CpuExt, NetworkExt, NetworksExt, Pid, ProcessExt, Signal, System, SystemExt, UserExt,
CpuExt, NetworkExt, Networks, NetworksExt, Pid, ProcessExt, Signal, System, SystemExt, UserExt,
};

const signals: &[Signal] = &[
Expand Down Expand Up @@ -141,7 +141,7 @@ fn print_help() {
writeln!(&mut io::stdout(), "quit : Exit the program");
}

fn interpret_input(input: &str, sys: &mut System) -> bool {
fn interpret_input(input: &str, sys: &mut System, networks: &mut Networks) -> bool {
match input.trim() {
"help" => print_help(),
"refresh_disks" => {
Expand All @@ -156,7 +156,7 @@ fn interpret_input(input: &str, sys: &mut System) -> bool {
}
"refresh_networks" => {
writeln!(&mut io::stdout(), "Refreshing network list...");
sys.refresh_networks_list();
networks.refresh_list();
writeln!(&mut io::stdout(), "Done.");
}
"signals" => {
Expand Down Expand Up @@ -276,7 +276,7 @@ fn interpret_input(input: &str, sys: &mut System) -> bool {
}
}
"network" => {
for (interface_name, data) in sys.networks().iter() {
for (interface_name, data) in networks.iter() {
writeln!(
&mut io::stdout(),
"{}:\n ether {}\n input data (new / total): {} / {} B\n output data (new / total): {} / {} B",
Expand Down Expand Up @@ -431,8 +431,10 @@ fn interpret_input(input: &str, sys: &mut System) -> bool {
}

fn main() {
println!("Getting processes' information...");
println!("Getting system information...");
let mut t = System::new_all();
let mut n = Networks::new();
n.refresh_list();
println!("Done.");
let t_stin = io::stdin();
let mut stin = t_stin.lock();
Expand All @@ -454,6 +456,6 @@ fn main() {
if (&input as &str).ends_with('\n') {
input.pop();
}
done = interpret_input(input.as_ref(), &mut t);
done = interpret_input(input.as_ref(), &mut t, &mut n);
}
}
70 changes: 59 additions & 11 deletions src/c_interface.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Take a look at the license at the top of the repository in the LICENSE file.

use crate::{CpuExt, NetworkExt, NetworksExt, Pid, Process, ProcessExt, System, SystemExt};
use crate::{
CpuExt, NetworkExt, Networks, NetworksExt, Pid, Process, ProcessExt, System, SystemExt,
};
use libc::{self, c_char, c_float, c_uint, c_void, pid_t, size_t};
use std::borrow::BorrowMut;
use std::ffi::CString;
Expand All @@ -13,6 +15,8 @@ pub type CProcess = *const c_void;
pub type RString = *const c_char;
/// Callback used by [`processes`][crate::System#method.processes].
pub type ProcessLoop = extern "C" fn(pid: pid_t, process: CProcess, data: *mut c_void) -> bool;
/// Equivalent of [`System`][crate::System] struct.
pub type CNetworks = *mut c_void;

/// Equivalent of [`System::new()`][crate::System#method.new].
#[no_mangle]
Expand Down Expand Up @@ -227,32 +231,76 @@ pub extern "C" fn sysinfo_used_swap(system: CSystem) -> size_t {
}
}

/// Equivalent of [`Networks::new()`][crate::Networks#method.new].
#[no_mangle]
pub extern "C" fn sysinfo_networks_init() -> CNetworks {
let networks = Box::new(Networks::new());
Box::into_raw(networks) as CNetworks
}

/// Equivalent of `Networks::drop()`. Important in C to cleanup memory.
#[no_mangle]
pub extern "C" fn sysinfo_networks_destroy(networks: CNetworks) {
assert!(!networks.is_null());
unsafe {
drop(Box::from_raw(networks as *mut Networks));
}
}

/// Equivalent of [`Networks::refresh_list()`][crate::Networks#method.refresh_list].
#[no_mangle]
pub extern "C" fn sysinfo_networks_refresh_list(networks: CNetworks) {
assert!(!networks.is_null());
unsafe {
let mut networks: Box<Networks> = Box::from_raw(networks as *mut Networks);
{
let networks: &mut Networks = networks.borrow_mut();
networks.refresh_list();
}
Box::into_raw(networks);
}
}

/// Equivalent of [`Networks::refresh()`][crate::Networks#method.refresh].
#[no_mangle]
pub extern "C" fn sysinfo_networks_refresh(networks: CNetworks) {
assert!(!networks.is_null());
unsafe {
let mut networks: Box<Networks> = Box::from_raw(networks as *mut Networks);
{
let networks: &mut Networks = networks.borrow_mut();
networks.refresh();
}
Box::into_raw(networks);
}
}

/// Equivalent of
/// `system::networks().iter().fold(0, |acc, (_, data)| acc + data.received() as size_t)`.
#[no_mangle]
pub extern "C" fn sysinfo_networks_received(system: CSystem) -> size_t {
assert!(!system.is_null());
pub extern "C" fn sysinfo_networks_received(networks: CNetworks) -> size_t {
assert!(!networks.is_null());
unsafe {
let system: Box<System> = Box::from_raw(system as *mut System);
let ret = system.networks().iter().fold(0, |acc: size_t, (_, data)| {
let networks: Box<Networks> = Box::from_raw(networks as *mut Networks);
let ret = networks.iter().fold(0, |acc: size_t, (_, data)| {
acc.saturating_add(data.received() as size_t)
});
Box::into_raw(system);
Box::into_raw(networks);
ret
}
}

/// Equivalent of
/// `system::networks().iter().fold(0, |acc, (_, data)| acc + data.transmitted() as size_t)`.
#[no_mangle]
pub extern "C" fn sysinfo_networks_transmitted(system: CSystem) -> size_t {
assert!(!system.is_null());
pub extern "C" fn sysinfo_networks_transmitted(networks: CNetworks) -> size_t {
assert!(!networks.is_null());
unsafe {
let system: Box<System> = Box::from_raw(system as *mut System);
let ret = system.networks().iter().fold(0, |acc: size_t, (_, data)| {
let networks: Box<Networks> = Box::from_raw(networks as *mut Networks);
let ret = networks.iter().fold(0, |acc: size_t, (_, data)| {
acc.saturating_add(data.transmitted() as size_t)
});
Box::into_raw(system);
Box::into_raw(networks);
ret
}
}
Expand Down
41 changes: 11 additions & 30 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,6 @@ impl CpuRefreshKind {
/// [`System`]: crate::System
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct RefreshKind {
networks: bool,
networks_list: bool,
processes: Option<ProcessRefreshKind>,
disks_list: bool,
disks: bool,
Expand All @@ -386,8 +384,6 @@ impl RefreshKind {
///
/// let r = RefreshKind::new();
///
/// assert_eq!(r.networks(), false);
/// assert_eq!(r.networks_list(), false);
/// assert_eq!(r.processes().is_some(), false);
/// assert_eq!(r.disks_list(), false);
/// assert_eq!(r.disks(), false);
Expand All @@ -408,8 +404,6 @@ impl RefreshKind {
///
/// let r = RefreshKind::everything();
///
/// assert_eq!(r.networks(), true);
/// assert_eq!(r.networks_list(), true);
/// assert_eq!(r.processes().is_some(), true);
/// assert_eq!(r.disks_list(), true);
/// assert_eq!(r.disks(), true);
Expand All @@ -421,8 +415,6 @@ impl RefreshKind {
/// ```
pub fn everything() -> Self {
Self {
networks: true,
networks_list: true,
processes: Some(ProcessRefreshKind::everything()),
disks: true,
disks_list: true,
Expand All @@ -441,13 +433,6 @@ impl RefreshKind {
without_processes,
ProcessRefreshKind
);
impl_get_set!(RefreshKind, networks, with_networks, without_networks);
impl_get_set!(
RefreshKind,
networks_list,
with_networks_list,
without_networks_list
);
impl_get_set!(RefreshKind, disks, with_disks, without_disks);
impl_get_set!(RefreshKind, disks_list, with_disks_list, without_disks_list);
impl_get_set!(RefreshKind, memory, with_memory, without_memory);
Expand All @@ -462,28 +447,24 @@ impl RefreshKind {
impl_get_set!(RefreshKind, users_list, with_users_list, without_users_list);
}

/// Networks interfaces.
/// Network interfaces.
///
/// Don't forget to also take a look at the [`NetworksExt`] trait to see the list of available
/// methods.
///
/// ```no_run
/// use sysinfo::{NetworksExt, System, SystemExt};
/// use sysinfo::{Networks, NetworksExt};
///
/// let s = System::new_all();
/// for network in s.networks().iter() {
/// let mut networks = Networks::new();
/// networks.refresh_list();
/// for network in networks.iter() {
/// println!("{:?}", network);
/// }
/// ```
pub struct Networks {
pub(crate) interfaces: HashMap<String, NetworkData>,
}

impl Networks {
pub(crate) fn new() -> Networks {
Networks {
interfaces: HashMap::new(),
}
}
}

impl<'a> IntoIterator for &'a Networks {
type Item = (&'a String, &'a NetworkData);
type IntoIter = NetworksIter<'a>;
Expand All @@ -498,10 +479,10 @@ impl<'a> IntoIterator for &'a Networks {
/// It is returned by [`Networks::iter`][crate::Networks#method.iter].
///
/// ```no_run
/// use sysinfo::{System, SystemExt, NetworksExt};
/// use sysinfo::{Networks, NetworksExt};
///
/// let system = System::new_all();
/// let networks_iter = system.networks().iter();
/// let networks = Networks::new();
/// let networks_iter = networks.iter();
/// ```
pub struct NetworksIter<'a> {
inner: std::collections::hash_map::Iter<'a, String, NetworkData>,
Expand Down
1 change: 0 additions & 1 deletion src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ impl fmt::Debug for System {
.field("total swap", &self.total_swap())
.field("free swap", &self.free_swap())
.field("nb CPUs", &self.cpus().len())
.field("nb network interfaces", &self.networks().iter().count())
.field("nb processes", &self.processes().len())
.field("nb disks", &self.disks().len())
.field("nb components", &self.components().len())
Expand Down
1 change: 0 additions & 1 deletion src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ impl serde::Serialize for crate::System {
state.serialize_field("components", &self.components())?;
state.serialize_field("users", &self.users())?;
state.serialize_field("disks", &self.disks())?;
state.serialize_field("networks", &self.networks())?;

state.serialize_field("uptime", &self.uptime())?;
state.serialize_field("boot_time", &self.boot_time())?;
Expand Down
Loading

0 comments on commit fb746c5

Please sign in to comment.