Make basic server-cli mode default to not reading input and add interactive option

This commit is contained in:
Imbris 2020-09-19 22:40:26 -04:00
parent ef74d395a6
commit 873ea1ec46
2 changed files with 47 additions and 42 deletions

View File

@ -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));

View File

@ -86,26 +86,13 @@ pub const COMMANDS: [Command; 4] = [
];
pub struct Tui {
pub msg_r: mpsc::Receiver<Message>,
background: Option<std::thread::JoinHandle<()>>,
basic: bool,
msg_s: Option<mpsc::Sender<Message>>,
running: Arc<AtomicBool>,
}
impl Tui {
pub fn new() -> (Self, mpsc::Receiver<Message>) {
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<Message>) {
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,
}
}