➕ 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
parent
30ebb528eb
commit
18e38f0611
|
@ -65,6 +65,12 @@ Run a client to ping all servers in the same multi-cast domain:
|
|||
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:
|
||||
|
||||
```bash
|
||||
|
|
1
ideas.md
1
ideas.md
|
@ -1,5 +1,6 @@
|
|||
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
|
||||
- Arbitrary TCP forwarding of (stdin? stdout? TCP?)
|
||||
- Netcat replacement "Just send a file" _including filename_
|
||||
|
|
|
@ -17,6 +17,8 @@ pub enum AppError {
|
|||
#[error (transparent)]
|
||||
Message (#[from] crate::message::MessageError),
|
||||
#[error (transparent)]
|
||||
ParseInt (#[from] std::num::ParseIntError),
|
||||
#[error (transparent)]
|
||||
Tlv (#[from] crate::tlv::TlvError),
|
||||
}
|
||||
|
||||
|
|
|
@ -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 mut bind_addrs = vec! [];
|
||||
let mut timeout_ms = 500;
|
||||
|
||||
while let Some (arg) = args.next () {
|
||||
match arg.as_str () {
|
||||
"--bind-addr" => {
|
||||
bind_addrs.push (match args.next () {
|
||||
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 ()),
|
||||
}
|
||||
}
|
||||
|
@ -43,14 +50,21 @@ pub async fn client <I : Iterator <Item=String>> (mut args: I) -> Result <(), Ap
|
|||
mac: None,
|
||||
}.to_vec ()?;
|
||||
|
||||
for _ in 0..10 {
|
||||
socket.send_to (&msg, (common_params.multicast_addr, common_params.server_port)).await?;
|
||||
sleep (Duration::from_millis (100)).await;
|
||||
}
|
||||
let socket = Arc::new (socket);
|
||||
let socket2 = Arc::clone (&socket);
|
||||
|
||||
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);
|
||||
|
||||
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 ();
|
||||
peers.sort_by_key (|(_, v)| v.mac);
|
||||
|
@ -83,6 +97,8 @@ async fn listen_for_responses (
|
|||
socket: &UdpSocket,
|
||||
peers: &mut HashMap <SocketAddr, ServerResponse>
|
||||
) {
|
||||
let start_time = Instant::now ();
|
||||
|
||||
loop {
|
||||
let (msgs, remote_addr) = match recv_msg_from (socket).await {
|
||||
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 ());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,7 +7,11 @@ pub use std::{
|
|||
SocketAddrV4,
|
||||
},
|
||||
str::FromStr,
|
||||
time::Duration,
|
||||
sync::Arc,
|
||||
time::{
|
||||
Duration,
|
||||
Instant,
|
||||
},
|
||||
};
|
||||
|
||||
pub use mac_address::{
|
||||
|
|
Loading…
Reference in New Issue