add `--timeout-ms` for client

Empirical testing shows that 200 ms is probably enough on my LAN, so
I set the default to 500 ms.
main
_ 2021-12-09 15:34:42 +00:00
parent 30ebb528eb
commit 18e38f0611
5 changed files with 40 additions and 8 deletions

View File

@ -65,6 +65,12 @@ Run a client to ping all servers in the same multi-cast domain:
lookaround client lookaround client
``` ```
Use a longer timeout if some servers need longer than 500 ms to respond:
```bash
lookaround client --timeout-ms 1000
```
Check which IP addresses LookAround will auto-detect: Check which IP addresses LookAround will auto-detect:
```bash ```bash

View File

@ -1,5 +1,6 @@
Cool ideas that can be done but probably won't be. Cool ideas that can be done but probably won't be.
- Exit faster if the user only wants to see known servers
- Command for shell substituting IPs into commands - Command for shell substituting IPs into commands
- Arbitrary TCP forwarding of (stdin? stdout? TCP?) - Arbitrary TCP forwarding of (stdin? stdout? TCP?)
- Netcat replacement "Just send a file" _including filename_ - Netcat replacement "Just send a file" _including filename_

View File

@ -17,6 +17,8 @@ pub enum AppError {
#[error (transparent)] #[error (transparent)]
Message (#[from] crate::message::MessageError), Message (#[from] crate::message::MessageError),
#[error (transparent)] #[error (transparent)]
ParseInt (#[from] std::num::ParseIntError),
#[error (transparent)]
Tlv (#[from] crate::tlv::TlvError), Tlv (#[from] crate::tlv::TlvError),
} }

View File

@ -10,15 +10,22 @@ pub async fn client <I : Iterator <Item=String>> (mut args: I) -> Result <(), Ap
let common_params = app_common::Params::default (); let common_params = app_common::Params::default ();
let mut bind_addrs = vec! []; let mut bind_addrs = vec! [];
let mut timeout_ms = 500;
while let Some (arg) = args.next () { while let Some (arg) = args.next () {
match arg.as_str () { match arg.as_str () {
"--bind-addr" => { "--bind-addr" => {
bind_addrs.push (match args.next () { bind_addrs.push (match args.next () {
None => return Err (CliArgError::MissingArgumentValue (arg).into ()), None => return Err (CliArgError::MissingArgumentValue (arg).into ()),
Some (x) => Ipv4Addr::from_str (&x)? Some (x) => Ipv4Addr::from_str (&x)?,
}); });
}, },
"--timeout-ms" => {
timeout_ms = match args.next () {
None => return Err (CliArgError::MissingArgumentValue (arg).into ()),
Some (x) => u64::from_str (&x)?,
};
},
_ => return Err (CliArgError::UnrecognizedArgument (arg).into ()), _ => return Err (CliArgError::UnrecognizedArgument (arg).into ()),
} }
} }
@ -43,14 +50,21 @@ pub async fn client <I : Iterator <Item=String>> (mut args: I) -> Result <(), Ap
mac: None, mac: None,
}.to_vec ()?; }.to_vec ()?;
for _ in 0..10 { let socket = Arc::new (socket);
socket.send_to (&msg, (common_params.multicast_addr, common_params.server_port)).await?; let socket2 = Arc::clone (&socket);
sleep (Duration::from_millis (100)).await;
} tokio::spawn (async move {
for _ in 0..10 {
socket2.send_to (&msg, (common_params.multicast_addr, common_params.server_port)).await?;
sleep (Duration::from_millis (100)).await;
}
Ok::<_, AppError> (())
});
let mut peers = HashMap::with_capacity (10); let mut peers = HashMap::with_capacity (10);
timeout (Duration::from_secs (2), listen_for_responses (&socket, &mut peers)).await.ok (); timeout (Duration::from_millis (timeout_ms), listen_for_responses (&*socket, &mut peers)).await.ok ();
let mut peers: Vec <_> = peers.into_iter ().collect (); let mut peers: Vec <_> = peers.into_iter ().collect ();
peers.sort_by_key (|(_, v)| v.mac); peers.sort_by_key (|(_, v)| v.mac);
@ -83,6 +97,8 @@ async fn listen_for_responses (
socket: &UdpSocket, socket: &UdpSocket,
peers: &mut HashMap <SocketAddr, ServerResponse> peers: &mut HashMap <SocketAddr, ServerResponse>
) { ) {
let start_time = Instant::now ();
loop { loop {
let (msgs, remote_addr) = match recv_msg_from (socket).await { let (msgs, remote_addr) = match recv_msg_from (socket).await {
Err (_) => continue, Err (_) => continue,
@ -102,6 +118,9 @@ async fn listen_for_responses (
} }
} }
peers.insert (remote_addr, resp); if peers.insert (remote_addr, resp).is_none () {
let now = Instant::now ();
// println! ("Added peer at {} ms", (now - start_time).as_millis ());
}
} }
} }

View File

@ -7,7 +7,11 @@ pub use std::{
SocketAddrV4, SocketAddrV4,
}, },
str::FromStr, str::FromStr,
time::Duration, sync::Arc,
time::{
Duration,
Instant,
},
}; };
pub use mac_address::{ pub use mac_address::{