Debugging messages added

This commit is contained in:
Martin Brodbeck 2023-10-03 22:11:06 +02:00
parent f80f4b6035
commit dedc2d8ff9
3 changed files with 190 additions and 4 deletions

177
Cargo.lock generated
View file

@ -2,6 +2,15 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 3 version = 3
[[package]]
name = "aho-corasick"
version = "1.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea5d730647d4fadd988536d06fecce94b7b4f2a7efdae548f1cf4b63205518ab"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "android-tzdata" name = "android-tzdata"
version = "0.1.1" version = "0.1.1"
@ -23,6 +32,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "bitflags"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635"
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.14.0" version = "3.14.0"
@ -64,6 +79,52 @@ version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
[[package]]
name = "env_logger"
version = "0.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0"
dependencies = [
"humantime",
"is-terminal",
"log",
"regex",
"termcolor",
]
[[package]]
name = "errno"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "add4f07d43996f76ef320709726a556a9d4f965d9410d8d0271132d2f8293480"
dependencies = [
"errno-dragonfly",
"libc",
"windows-sys",
]
[[package]]
name = "errno-dragonfly"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf"
dependencies = [
"cc",
"libc",
]
[[package]]
name = "hermit-abi"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "iana-time-zone" name = "iana-time-zone"
version = "0.1.57" version = "0.1.57"
@ -87,6 +148,17 @@ dependencies = [
"cc", "cc",
] ]
[[package]]
name = "is-terminal"
version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
dependencies = [
"hermit-abi",
"rustix",
"windows-sys",
]
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.64" version = "0.3.64"
@ -102,6 +174,12 @@ version = "0.2.148"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b" checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b"
[[package]]
name = "linux-raw-sys"
version = "0.4.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3852614a3bd9ca9804678ba6be5e3b8ce76dfc902cae004e3e0c44051b6e88db"
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.20" version = "0.4.20"
@ -113,8 +191,16 @@ name = "m32chat"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"chrono", "chrono",
"env_logger",
"log",
] ]
[[package]]
name = "memchr"
version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.16" version = "0.2.16"
@ -148,6 +234,48 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "regex"
version = "1.9.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ebee201405406dbf528b8b672104ae6d6d63e6d118cb10e4d51abbc7b58044ff"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59b23e92ee4318893fa3fe3e6fb365258efbfe6ac6ab30f090cdcbb7aa37efa9"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.7.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da"
[[package]]
name = "rustix"
version = "0.38.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2f9da0cbd88f9f09e7814e388301c8414c51c62aa6ce1e4b5c551d49d96e531"
dependencies = [
"bitflags",
"errno",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]] [[package]]
name = "syn" name = "syn"
version = "2.0.37" version = "2.0.37"
@ -159,6 +287,15 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "termcolor"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6093bad37da69aab9d123a8091e4be0aa4a03e4d601ec641c327398315f62b64"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.12" version = "1.0.12"
@ -219,6 +356,37 @@ version = "0.2.87"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1" checksum = "ca6ad05a4870b2bf5fe995117d3728437bd27d7cd5f06f13c17443ef369775a1"
[[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-util"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596"
dependencies = [
"winapi",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]] [[package]]
name = "windows" name = "windows"
version = "0.48.0" version = "0.48.0"
@ -228,6 +396,15 @@ dependencies = [
"windows-targets", "windows-targets",
] ]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets",
]
[[package]] [[package]]
name = "windows-targets" name = "windows-targets"
version = "0.48.5" version = "0.48.5"

View file

@ -7,3 +7,5 @@ edition = "2021"
[dependencies] [dependencies]
chrono = "0.4.31" chrono = "0.4.31"
env_logger = "0.10.0"
log = "0.4.20"

View file

@ -1,5 +1,6 @@
use chrono::{DateTime, Duration, Local}; use chrono::{DateTime, Duration, Local};
use std::{collections::HashMap, net::UdpSocket}; use std::{collections::HashMap, net::UdpSocket};
use log::debug;
const MAX_CLIENTS: usize = 10; const MAX_CLIENTS: usize = 10;
const CLIENT_TIMEOUT: u32 = 300; const CLIENT_TIMEOUT: u32 = 300;
@ -118,11 +119,15 @@ fn broadcast(
} }
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
env_logger::init();
let socket = UdpSocket::bind("0.0.0.0:7373")?; let socket = UdpSocket::bind("0.0.0.0:7373")?;
socket socket
.set_read_timeout(None) .set_read_timeout(None)
.expect("Could not set read timeout"); .expect("Could not set read timeout");
debug!("Morserino chat server started.");
let mut receivers2: HashMap<String, DateTime<Local>> = HashMap::new(); let mut receivers2: HashMap<String, DateTime<Local>> = HashMap::new();
loop { loop {
@ -133,9 +138,9 @@ fn main() -> std::io::Result<()> {
let data = &buf[0..number_of_bytes]; let data = &buf[0..number_of_bytes];
if receivers2.contains_key(&client_addr) { if receivers2.contains_key(&client_addr) {
//println!("Client already known."); debug!("Client known: {client_addr}");
if strip_header(data) == strip_header(mopp(speed, b":bye").as_slice()) { if strip_header(data) == strip_header(mopp(speed, b":bye").as_slice()) {
println!("TSCHAU"); debug!("Removing client {client_addr} as requested.");
socket socket
.send_to(mopp(speed, b":bye").as_slice(), &client_addr) .send_to(mopp(speed, b":bye").as_slice(), &client_addr)
.unwrap(); .unwrap();
@ -145,7 +150,7 @@ fn main() -> std::io::Result<()> {
receivers2.insert(client_addr.to_owned(), Local::now()); receivers2.insert(client_addr.to_owned(), Local::now());
} }
} else if strip_header(data) == strip_header(mopp(speed, b"hi").as_slice()) { } else if strip_header(data) == strip_header(mopp(speed, b"hi").as_slice()) {
//println!("Welcome!"); debug!("Adding new client {client_addr}");
if receivers2.len() < MAX_CLIENTS { if receivers2.len() < MAX_CLIENTS {
receivers2.insert(client_addr.to_owned(), Local::now()); receivers2.insert(client_addr.to_owned(), Local::now());
socket socket
@ -156,17 +161,19 @@ fn main() -> std::io::Result<()> {
) )
.unwrap(); .unwrap();
} else { } else {
debug!("There are already too many clients connected.");
socket socket
.send_to(mopp(speed, b":qrl").as_slice(), &client_addr) .send_to(mopp(speed, b":qrl").as_slice(), &client_addr)
.unwrap(); .unwrap();
} }
} else { } else {
//println!("Unknown client - Ignoring"); debug!("Ignoring unknown client {client_addr}");
} }
let timestamp = Local::now(); let timestamp = Local::now();
for cl in &receivers2 { for cl in &receivers2 {
if *cl.1 + Duration::seconds(CLIENT_TIMEOUT as i64) < timestamp { if *cl.1 + Duration::seconds(CLIENT_TIMEOUT as i64) < timestamp {
debug!("Removing outdated client {}", cl.0);
socket socket
.send_to(mopp(MY_SPEED, b":bye").as_slice(), &client_addr) .send_to(mopp(MY_SPEED, b":bye").as_slice(), &client_addr)
.unwrap(); .unwrap();