add a helper log

This commit is contained in:
Daniella / Tove 2022-08-27 18:10:13 +02:00
parent 40aa548781
commit 7e7d922129
3 changed files with 76 additions and 6 deletions

22
Cargo.lock generated
View file

@ -308,6 +308,15 @@ dependencies = [
"version_check", "version_check",
] ]
[[package]]
name = "num_threads"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2819ce041d2ee131036f4fc9d6ae7ae125a3a40e97ba64d04fe799ad9dabbb44"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "object" name = "object"
version = "0.29.0" version = "0.29.0"
@ -346,10 +355,11 @@ dependencies = [
[[package]] [[package]]
name = "qft" name = "qft"
version = "0.5.2" version = "0.5.3"
dependencies = [ dependencies = [
"iui", "iui",
"rand", "rand",
"time",
] ]
[[package]] [[package]]
@ -500,6 +510,16 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "time"
version = "0.3.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c3f9a28b618c3a6b9251b6908e9c99e04b9e5c02e6581ccbb67d59c34ef7f9b"
dependencies = [
"libc",
"num_threads",
]
[[package]] [[package]]
name = "toml" name = "toml"
version = "0.5.8" version = "0.5.8"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "qft" name = "qft"
version = "0.5.2" version = "0.5.3"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
@ -8,3 +8,4 @@ edition = "2021"
[dependencies] [dependencies]
iui.git = "https://github.com/rust-native-ui/libui-rs" iui.git = "https://github.com/rust-native-ui/libui-rs"
rand = "0" rand = "0"
time = "0"

View file

@ -6,11 +6,14 @@ use std::{
fs::{File, OpenOptions}, fs::{File, OpenOptions},
io::{stdout, Error, Read, Seek, SeekFrom, Write}, io::{stdout, Error, Read, Seek, SeekFrom, Write},
net::*, net::*,
ops::Mul,
str::FromStr, str::FromStr,
thread, thread,
time::{Duration, SystemTime}, time::{Duration, SystemTime},
}; };
use time::{Date, PrimitiveDateTime, Time};
#[derive(Ord, Eq, PartialOrd, PartialEq)] #[derive(Ord, Eq, PartialOrd, PartialEq)]
enum SafeReadWritePacket { enum SafeReadWritePacket {
Write, Write,
@ -26,6 +29,17 @@ struct SafeReadWrite {
packet_count_in: u64, packet_count_in: u64,
} }
struct Wrap<T>(T);
impl Mul<Wrap<&str>> for u64 {
type Output = String;
fn mul(self, rhs: Wrap<&str>) -> Self::Output {
let strings: Vec<&str> = (0..self).map(|_| rhs.0).collect();
strings.join("")
}
}
impl SafeReadWrite { impl SafeReadWrite {
pub fn new(socket: UdpSocket) -> SafeReadWrite { pub fn new(socket: UdpSocket) -> SafeReadWrite {
SafeReadWrite { SafeReadWrite {
@ -265,7 +279,7 @@ fn main() {
} }
if args.len() == 1 { if args.len() == 1 {
match gui::gui() { match gui::gui() {
Ok(_) => {}, Ok(_) => (),
Err(_) => print_args(&args), Err(_) => print_args(&args),
} }
} }
@ -291,6 +305,14 @@ pub fn helper(args: &Vec<String>) {
let mut map: HashMap<[u8; 200], SocketAddr> = HashMap::new(); let mut map: HashMap<[u8; 200], SocketAddr> = HashMap::new();
let listener = UdpSocket::bind(&bind_addr).expect("unable to create socket"); let listener = UdpSocket::bind(&bind_addr).expect("unable to create socket");
let mut buf = [0 as u8; 200]; let mut buf = [0 as u8; 200];
let mut last_log_time = unix_millis();
let mut amount_since_log = 0;
let mut helper_log = OpenOptions::new()
.create(true)
.write(true)
.append(true)
.open("qft_helper_log.txt")
.expect("unable to create helper log");
loop { loop {
let (l, addr) = listener.recv_from(&mut buf).expect("read error"); let (l, addr) = listener.recv_from(&mut buf).expect("read error");
if l != 200 { if l != 200 {
@ -314,6 +336,27 @@ pub fn helper(args: &Vec<String>) {
{ {
// success! // success!
println!("Helped {} and {}! :D", addr, other); println!("Helped {} and {}! :D", addr, other);
amount_since_log += 1;
if unix_millis() - last_log_time > 10000 {
let d = PrimitiveDateTime::new(
Date::from_calendar_date(1970, time::Month::January, 1).unwrap(),
Time::MIDNIGHT,
) + Duration::from_millis(unix_millis());
helper_log
.write(
format!(
"{} | {} {}>\n",
d,
amount_since_log,
amount_since_log * Wrap("=")
)
.as_bytes(),
)
.expect("error writing to log");
helper_log.flush().expect("error writing to log");
last_log_time = unix_millis();
amount_since_log = 0;
}
} }
map.remove(&buf); map.remove(&buf);
} else { } else {
@ -353,7 +396,8 @@ pub fn sender<F: Fn(f32)>(args: &Vec<String>, on_progress: F) {
let mut bytes_sent: u64 = 0; let mut bytes_sent: u64 = 0;
let mut last_update = unix_millis(); let mut last_update = unix_millis();
let len = file.metadata().expect("bad metadata").len(); let len = file.metadata().expect("bad metadata").len();
sc.write_safe(&len.to_be_bytes()).expect("unable to send file length"); sc.write_safe(&len.to_be_bytes())
.expect("unable to send file length");
println!("Length: {}", &len); println!("Length: {}", &len);
loop { loop {
let read = file.read(&mut buf).expect("file read error"); let read = file.read(&mut buf).expect("file read error");
@ -412,8 +456,13 @@ pub fn receiver<F: Fn(f32)>(args: &Vec<String>, on_progress: F) {
let mut bytes_received: u64 = 0; let mut bytes_received: u64 = 0;
let mut last_update = unix_millis(); let mut last_update = unix_millis();
let mut len_bytes = [0 as u8; 8]; let mut len_bytes = [0 as u8; 8];
let len = sc.read_safe(&mut len_bytes).expect("unable to read length from sender").0; let len = sc
let len = u64::from_be_bytes([len[0], len[1], len[2], len[3], len[4], len[5], len[6], len[7]]); .read_safe(&mut len_bytes)
.expect("unable to read length from sender")
.0;
let len = u64::from_be_bytes([
len[0], len[1], len[2], len[3], len[4], len[5], len[6], len[7],
]);
file.set_len(len).expect("unable to set file length"); file.set_len(len).expect("unable to set file length");
println!("Length: {}", &len); println!("Length: {}", &len);
loop { loop {