Finish impl
This commit is contained in:
parent
37755fdcb6
commit
5e84030cf4
|
@ -2,6 +2,224 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "ansi_term"
|
||||
version = "0.12.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "atty"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
||||
dependencies = [
|
||||
"hermit-abi",
|
||||
"libc",
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "2.34.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
|
||||
dependencies = [
|
||||
"ansi_term",
|
||||
"atty",
|
||||
"bitflags",
|
||||
"strsim",
|
||||
"textwrap",
|
||||
"unicode-width",
|
||||
"vec_map",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "heck"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
|
||||
dependencies = [
|
||||
"unicode-segmentation",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "hermit-abi"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.117"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e74d72e0f9b65b5b4ca49a346af3976df0f9c61d550727f349ecd559f251a26c"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
|
||||
dependencies = [
|
||||
"proc-macro-error-attr",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error-attr"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c7342d5883fbccae1cc37a2353b09c87c9b0f3afd73f5fb9bba687a1f733b029"
|
||||
dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "864d3e96a899863136fc6e99f3d7cae289dafe43bf2c5ac19b70df7210c0a145"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rust_network_status_rate"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"structopt",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "strsim"
|
||||
version = "0.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
|
||||
|
||||
[[package]]
|
||||
name = "structopt"
|
||||
version = "0.3.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0c6b5c64445ba8094a6ab0c3cd2ad323e07171012d9c98b0b15651daf1787a10"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"lazy_static",
|
||||
"structopt-derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "structopt-derive"
|
||||
version = "0.4.18"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcb5ae327f9cc13b68763b5749770cb9e048a99bd9dfdfa58d0cf05d5f64afe0"
|
||||
dependencies = [
|
||||
"heck",
|
||||
"proc-macro-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.86"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8a65b3f4ffa0092e9887669db0eae07941f023991ab58ea44da8fe8e2d511c6b"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "textwrap"
|
||||
version = "0.11.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
|
||||
dependencies = [
|
||||
"unicode-width",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-segmentation"
|
||||
version = "1.9.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7e8820f5d777f6224dc4be3632222971ac30164d4a258d595640799554ebfd99"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-width"
|
||||
version = "0.1.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
|
||||
[[package]]
|
||||
name = "vec_map"
|
||||
version = "0.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
||||
|
||||
[[package]]
|
||||
name = "winapi"
|
||||
version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
|
||||
dependencies = [
|
||||
"winapi-i686-pc-windows-gnu",
|
||||
"winapi-x86_64-pc-windows-gnu",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "winapi-i686-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||
|
||||
[[package]]
|
||||
name = "winapi-x86_64-pc-windows-gnu"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
|
|
@ -6,3 +6,4 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
structopt = "0.3"
|
||||
|
|
94
src/main.rs
94
src/main.rs
|
@ -1,8 +1,18 @@
|
|||
use std::env::var;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Write};
|
||||
use std::ops::Sub;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::thread::sleep;
|
||||
use std::time::{Duration, Instant};
|
||||
use structopt::StructOpt;
|
||||
|
||||
const INTERVAL_SECONDS: u64 = 5;
|
||||
|
||||
#[derive(StructOpt, Debug, Clone)]
|
||||
struct Opt {
|
||||
net_dev: String,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
struct ByteState {
|
||||
|
@ -41,7 +51,7 @@ fn read_proc_net_dev(net_device: &str) -> Result<ByteState, String> {
|
|||
for line in file_string.lines() {
|
||||
if line.trim().starts_with(net_device) {
|
||||
let mut count = 0u32;
|
||||
for word in line.split(' ') {
|
||||
for word in line.split_ascii_whitespace() {
|
||||
if count == 1 {
|
||||
byte_state.recv = u64::from_str_radix(word, 10).map_err(|_| {
|
||||
String::from("Failed to parse recv bytes from \"/proc/net/dev\"")
|
||||
|
@ -66,8 +76,8 @@ fn read_proc_net_dev(net_device: &str) -> Result<ByteState, String> {
|
|||
|
||||
fn write_compare_state(
|
||||
net_device: &str,
|
||||
send_filename: &str,
|
||||
recv_filename: &str,
|
||||
send_filename: &Path,
|
||||
recv_filename: &Path,
|
||||
) -> Result<ByteState, String> {
|
||||
let mut prev_byte_state = ByteState { send: 0, recv: 0 };
|
||||
|
||||
|
@ -102,23 +112,50 @@ fn write_compare_state(
|
|||
|
||||
{
|
||||
let mut send_file = File::create(send_filename)
|
||||
.map_err(|_| format!("Failed to create \"{}\"", send_filename))?;
|
||||
.map_err(|_| format!("Failed to create \"{:?}\"", send_filename))?;
|
||||
send_file
|
||||
.write_all(byte_state.send.to_string().as_bytes())
|
||||
.map_err(|_| format!("Failed to write into \"{}\"", send_filename))?;
|
||||
.map_err(|_| format!("Failed to write into \"{:?}\"", send_filename))?;
|
||||
}
|
||||
|
||||
{
|
||||
let mut recv_file = File::create(recv_filename)
|
||||
.map_err(|_| format!("Failed to create \"{}\"", recv_filename))?;
|
||||
.map_err(|_| format!("Failed to create \"{:?}\"", recv_filename))?;
|
||||
recv_file
|
||||
.write_all(byte_state.recv.to_string().as_bytes())
|
||||
.map_err(|_| format!("Failed to write into \"{}\"", recv_filename))?;
|
||||
.map_err(|_| format!("Failed to write into \"{:?}\"", recv_filename))?;
|
||||
}
|
||||
|
||||
Ok(byte_state - prev_byte_state)
|
||||
}
|
||||
|
||||
fn do_set_states(
|
||||
net_device: &str,
|
||||
send_interval_filename: &Path,
|
||||
recv_interval_filename: &Path,
|
||||
send_total_filename: &Path,
|
||||
recv_total_filename: &Path,
|
||||
) -> Result<(), String> {
|
||||
let state = write_compare_state(net_device, send_total_filename, recv_total_filename)?;
|
||||
|
||||
{
|
||||
let mut send_interval_file = File::create(send_interval_filename)
|
||||
.map_err(|_| format!("Failed to create \"{:?}\"", send_interval_filename))?;
|
||||
send_interval_file
|
||||
.write_all(state.send.to_string().as_bytes())
|
||||
.map_err(|_| format!("Failed to write into \"{:?}\"", send_interval_filename))?;
|
||||
}
|
||||
{
|
||||
let mut recv_interval_file = File::create(recv_interval_filename)
|
||||
.map_err(|_| format!("Failed to create \"{:?}\"", recv_interval_filename))?;
|
||||
recv_interval_file
|
||||
.write_all(state.recv.to_string().as_bytes())
|
||||
.map_err(|_| format!("Failed to write into \"{:?}\"", recv_interval_filename))?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn timer_execute<F>(func: F, sleep_seconds: u64) -> Result<(), String>
|
||||
where
|
||||
F: std::ops::Fn() -> Result<(), String>,
|
||||
|
@ -135,6 +172,45 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
println!("Hello, world!");
|
||||
fn main() -> Result<(), String> {
|
||||
let opt = Opt::from_args();
|
||||
|
||||
println!("Using net_dev == \"{}\"", opt.net_dev);
|
||||
|
||||
let xdg_runtime_dir = var("XDG_RUNTIME_DIR").map_err(|e| format!("{}", e))?;
|
||||
|
||||
let mut send_total_path = PathBuf::new();
|
||||
send_total_path.push(&xdg_runtime_dir);
|
||||
send_total_path.push("rust_send_total");
|
||||
let send_total_path = send_total_path;
|
||||
|
||||
let mut recv_total_path = PathBuf::new();
|
||||
recv_total_path.push(&xdg_runtime_dir);
|
||||
recv_total_path.push("rust_recv_total");
|
||||
let recv_total_path = recv_total_path;
|
||||
|
||||
let mut send_interval_path = PathBuf::new();
|
||||
send_interval_path.push(&xdg_runtime_dir);
|
||||
send_interval_path.push("rust_send_interval");
|
||||
let send_interval_path = send_interval_path;
|
||||
|
||||
let mut recv_interval_path = PathBuf::new();
|
||||
recv_interval_path.push(&xdg_runtime_dir);
|
||||
recv_interval_path.push("rust_recv_interval");
|
||||
let recv_interval_path = recv_interval_path;
|
||||
|
||||
timer_execute(
|
||||
move || {
|
||||
do_set_states(
|
||||
&opt.net_dev,
|
||||
&send_interval_path,
|
||||
&recv_interval_path,
|
||||
&send_total_path,
|
||||
&recv_total_path,
|
||||
)
|
||||
},
|
||||
INTERVAL_SECONDS,
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue