aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGustav Sörnäs <gustav@sornas.net>2021-04-10 17:10:29 +0200
committerGustav Sörnäs <gustav@sornas.net>2021-04-10 19:57:09 +0200
commitcda329d361cdf4d1d13dbdb58e38c1e5e049762e (patch)
tree09b7be7c209e15936146be02aa2490a4df053c3c
parent28ada847437be5d8215d529ad48eff289dc32085 (diff)
downloadmum-cda329d361cdf4d1d13dbdb58e38c1e5e049762e.tar.gz
send parallel ping commands
-rw-r--r--mumctl/src/main.rs58
1 files changed, 36 insertions, 22 deletions
diff --git a/mumctl/src/main.rs b/mumctl/src/main.rs
index 29c9e44..07e5064 100644
--- a/mumctl/src/main.rs
+++ b/mumctl/src/main.rs
@@ -3,7 +3,11 @@ use log::*;
use mumlib::command::{Command as MumCommand, CommandResponse};
use mumlib::config::{self, Config, ServerConfig};
use mumlib::state::Channel as MumChannel;
-use std::{fmt,io::{self, BufRead, Read, Write}, iter, os::unix::net::UnixStream};
+use std::fmt;
+use std::io::{self, BufRead, Read, Write};
+use std::iter;
+use std::os::unix::net::UnixStream;
+use std::thread;
use structopt::{clap::Shell, StructOpt};
const INDENTATION: &str = " ";
@@ -363,7 +367,7 @@ fn match_opt() -> Result<(), Error> {
Ok(())
}
-fn match_server_command(server_command: Server, config: &mut Config) -> Result<(), CliError> {
+fn match_server_command(server_command: Server, config: &mut Config) -> Result<(), Error> {
match server_command {
Server::Config {
server_name,
@@ -496,29 +500,39 @@ fn match_server_command(server_command: Server, config: &mut Config) -> Result<(
if config.servers.is_empty() {
return Err(CliError::NoServers)?;
}
- let query = config
+ let queries: Vec<_> = config
.servers
.iter()
- .map(|e| {
- let response = send_command(MumCommand::ServerStatus {
- host: e.host.clone(),
- port: e.port.unwrap_or(mumlib::DEFAULT_PORT),
- });
- response.map(|f| (e, f))
+ .map(|s| {
+ let query = MumCommand::ServerStatus {
+ host: s.host.clone(),
+ port: s.port.unwrap_or(mumlib::DEFAULT_PORT),
+ };
+ thread::spawn(move || {
+ send_command(query)
+ })
})
- .collect::<Result<Vec<_>, _>>()?;
- for (server, response) in query
- .into_iter()
- .filter(|e| e.1.is_ok())
- .map(|e| (e.0, e.1.unwrap().unwrap()))
- {
- if let CommandResponse::ServerStatus {
- users, max_users, ..
- } = response
- {
- println!("{} [{}/{}]", server.name, users, max_users)
- } else {
- unreachable!()
+ .collect();
+ for (server, response) in config.servers.iter().zip(queries) {
+ match response.join().unwrap() {
+ Ok(Ok(response)) => {
+ if let Some(CommandResponse::ServerStatus {
+ users, max_users, ..
+ }) = response
+ {
+ println!("{} [{}/{}]", server.name, users, max_users)
+ } else {
+ unreachable!()
+ }
+ }
+ Ok(Err(e)) => {
+ error!("{}", e);
+ return Err(e)?;
+ }
+ Err(e) => {
+ error!("{}", e);
+ return Err(e)?;
+ }
}
}
}