From 873ea1ec46e2f08a679c149a1c02f1ea3573f553 Mon Sep 17 00:00:00 2001 From: Imbris Date: Sat, 19 Sep 2020 22:40:26 -0400 Subject: [PATCH] Make basic server-cli mode default to not reading input and add interactive option --- server-cli/src/main.rs | 46 ++++++++++++++++++++++-------------- server-cli/src/tui_runner.rs | 43 +++++++++++++++------------------ 2 files changed, 47 insertions(+), 42 deletions(-) diff --git a/server-cli/src/main.rs b/server-cli/src/main.rs index 4d2d6fb8b6..c0d7772aab 100644 --- a/server-cli/src/main.rs +++ b/server-cli/src/main.rs @@ -1,5 +1,6 @@ #![deny(unsafe_code)] #![deny(clippy::clone_on_ref_ptr)] +#![feature(bool_to_option)] mod shutdown_coordinator; mod tui_runner; @@ -47,17 +48,23 @@ fn main() -> io::Result<()> { .help("Disables the tui") .takes_value(false), ) + .arg( + Arg::with_name("interactive") + .short("i") + .long("interactive") + .help("Enables command input for basic mode") + .takes_value(false), + ) .get_matches(); let basic = matches.is_present("basic"); + let interactive = matches.is_present("interactive"); let sigusr1_signal = Arc::new(AtomicBool::new(false)); #[cfg(any(target_os = "linux", target_os = "macos"))] let _ = signal_hook::flag::register(SIGUSR1, Arc::clone(&sigusr1_signal)); - let (mut tui, msg_r) = Tui::new(); - // Init logging let base_exceptions = |env: EnvFilter| { env.add_directive("veloren_world::sim=info".parse().unwrap()) @@ -111,7 +118,7 @@ fn main() -> io::Result<()> { hook(info); })); - tui.run(basic); + let tui = (!basic || interactive).then(|| Tui::run(basic)); info!("Starting server..."); @@ -156,22 +163,25 @@ fn main() -> io::Result<()> { #[cfg(feature = "tracy")] common::util::tracy_client::finish_continuous_frame!(); - match msg_r.try_recv() { - Ok(msg) => match msg { - Message::AbortShutdown => shutdown_coordinator.abort_shutdown(&mut server), - Message::Shutdown { grace_period } => { - // TODO: The TUI parser doesn't support quoted strings so it is not currently - // possible to provide a shutdown reason from the console. - let message = "The server is shutting down".to_owned(); - shutdown_coordinator.initiate_shutdown(&mut server, grace_period, message); + if let Some(tui) = tui.as_ref() { + match tui.msg_r.try_recv() { + Ok(msg) => match msg { + Message::AbortShutdown => shutdown_coordinator.abort_shutdown(&mut server), + Message::Shutdown { grace_period } => { + // TODO: The TUI parser doesn't support quoted strings so it is not + // currently possible to provide a shutdown reason + // from the console. + let message = "The server is shutting down".to_owned(); + shutdown_coordinator.initiate_shutdown(&mut server, grace_period, message); + }, + Message::Quit => { + info!("Closing the server"); + break; + }, }, - Message::Quit => { - info!("Closing the server"); - break; - }, - }, - Err(mpsc::TryRecvError::Empty) | Err(mpsc::TryRecvError::Disconnected) => {}, - }; + Err(mpsc::TryRecvError::Empty) | Err(mpsc::TryRecvError::Disconnected) => {}, + } + } // Wait for the next tick. clock.tick(Duration::from_millis(1000 / TPS)); diff --git a/server-cli/src/tui_runner.rs b/server-cli/src/tui_runner.rs index e5c3d127e3..b867d949e8 100644 --- a/server-cli/src/tui_runner.rs +++ b/server-cli/src/tui_runner.rs @@ -86,26 +86,13 @@ pub const COMMANDS: [Command; 4] = [ ]; pub struct Tui { + pub msg_r: mpsc::Receiver, background: Option>, basic: bool, - msg_s: Option>, running: Arc, } impl Tui { - pub fn new() -> (Self, mpsc::Receiver) { - let (msg_s, msg_r) = mpsc::channel(); - ( - Self { - background: None, - basic: false, - msg_s: Some(msg_s), - running: Arc::new(AtomicBool::new(true)), - }, - msg_r, - ) - } - fn handle_events(input: &mut String, msg_s: &mut mpsc::Sender) { use crossterm::event::*; if let Event::Key(event) = read().unwrap() { @@ -132,15 +119,14 @@ impl Tui { } } - pub fn run(&mut self, basic: bool) { - self.basic = basic; + pub fn run(basic: bool) -> Self { + let (mut msg_s, msg_r) = mpsc::channel(); + let running = Arc::new(AtomicBool::new(true)); + let running2 = running.clone(); - let mut msg_s = self.msg_s.take().unwrap(); - let running = Arc::clone(&self.running); - - if self.basic { + let background = if basic { std::thread::spawn(move || { - while running.load(Ordering::Relaxed) { + while running2.load(Ordering::Relaxed) { let mut line = String::new(); match io::stdin().read_line(&mut line) { @@ -163,8 +149,10 @@ impl Tui { } } }); + + None } else { - self.background = Some(std::thread::spawn(move || { + Some(std::thread::spawn(move || { // Start the tui let mut stdout = io::stdout(); execute!(stdout, EnterAlternateScreen, EnableMouseCapture).unwrap(); @@ -180,7 +168,7 @@ impl Tui { error!(?e, "couldn't clean terminal"); }; - while running.load(Ordering::Relaxed) { + while running2.load(Ordering::Relaxed) { if let Err(e) = terminal.draw(|f| { let (log_rect, input_rect) = if f.size().height > 6 { let mut log_rect = f.size(); @@ -227,7 +215,14 @@ impl Tui { Self::handle_events(&mut input, &mut msg_s); }; } - })); + })) + }; + + Self { + msg_r, + background, + basic, + running, } }