diff options
Diffstat (limited to 'mumctl/src')
| -rw-r--r-- | mumctl/src/main.rs | 58 |
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)?; + } } } } |
