♻️ refactor: make it a little more idiomatic

main
_ 2021-12-09 00:45:18 +00:00
parent 5d8bb3282b
commit b620bcfe06
3 changed files with 35 additions and 45 deletions

12
Cargo.lock generated
View File

@ -236,21 +236,9 @@ dependencies = [
"libc",
"mio",
"pin-project-lite",
"tokio-macros",
"winapi",
]
[[package]]
name = "tokio-macros"
version = "1.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9efc1aba077437943f7515666aa2b882dfabfbfdf89c819ea75a8d6e9eaba5e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"

View File

@ -15,7 +15,7 @@ version = "0.1.4"
mac_address = "1.1.2"
rand = "0.8.4"
thiserror = "1.0.30"
tokio = { version = "1.14.0", features = ["fs", "macros", "net", "rt", "time"] }
tokio = { version = "1.14.0", features = ["fs", "net", "rt", "time"] }
[profile.release]
codegen-units = 1

View File

@ -7,7 +7,7 @@ use std::{
SocketAddrV4,
},
str::FromStr,
time::{Duration, Instant},
time::{Duration},
};
use mac_address::{
@ -17,8 +17,10 @@ use mac_address::{
use thiserror::Error;
use tokio::{
net::UdpSocket,
select,
time::sleep,
time::{
sleep,
timeout,
},
};
mod ip;
@ -184,39 +186,12 @@ async fn client <I : Iterator <Item=String>> (mut args: I) -> Result <(), AppErr
for _ in 0..10 {
socket.send_to (&msg, (common_params.multicast_addr, common_params.server_port)).await?;
std::thread::sleep (Duration::from_millis (100));
sleep (Duration::from_millis (100)).await;
}
let start_time = Instant::now ();
let mut peers = HashMap::with_capacity (10);
while Instant::now () < start_time + Duration::from_secs (2) {
let x = select! {
x = recv_msg_from (&socket) => x,
() = sleep (Duration::from_millis (1_000)) => continue,
};
let (msgs, remote_addr) = match x {
Err (_) => continue,
Ok (x) => x,
};
let mut resp = ServerResponse {
mac: None,
nickname: None,
};
for msg in msgs.into_iter () {
match msg {
Message::Response1 (x) => resp.mac = x,
Message::Response2 (x) => resp.nickname = Some (x.nickname),
_ => (),
}
}
peers.insert (remote_addr, resp);
}
timeout (Duration::from_secs (2), listen_for_responses (&socket, &mut peers)).await.ok ();
let mut peers: Vec <_> = peers.into_iter ().collect ();
peers.sort_by_key (|(_, v)| v.mac);
@ -245,6 +220,33 @@ async fn client <I : Iterator <Item=String>> (mut args: I) -> Result <(), AppErr
Ok (())
}
async fn listen_for_responses (
socket: &UdpSocket,
peers: &mut HashMap <SocketAddr, ServerResponse>
) {
loop {
let (msgs, remote_addr) = match recv_msg_from (socket).await {
Err (_) => continue,
Ok (x) => x,
};
let mut resp = ServerResponse {
mac: None,
nickname: None,
};
for msg in msgs.into_iter () {
match msg {
Message::Response1 (x) => resp.mac = x,
Message::Response2 (x) => resp.nickname = Some (x.nickname),
_ => (),
}
}
peers.insert (remote_addr, resp);
}
}
async fn server <I: Iterator <Item=String>> (mut args: I) -> Result <(), AppError>
{
let common_params = CommonParams::default ();