Added very basic proof-of-concept

signal-handling
Jef Roosens 2021-09-13 12:18:18 +02:00
parent 905f973324
commit f8a65ba2f3
Signed by: Jef Roosens
GPG Key ID: B580B976584B5F30
3 changed files with 76 additions and 0 deletions

5
.gitignore vendored
View File

@ -11,3 +11,8 @@ Cargo.lock
# These are backup files generated by rustfmt # These are backup files generated by rustfmt
**/*.rs.bk **/*.rs.bk
# Added by cargo
/target

12
Cargo.toml 100644
View File

@ -0,0 +1,12 @@
[package]
name = "mc-wrapper"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
# Used for creating tarballs for backups
tar = "0.4.37"
# Used to compress said tarballs using gzip
flate2 = "1.0.21"

59
src/main.rs 100644
View File

@ -0,0 +1,59 @@
use std::io;
use std::io::Write;
use std::process::{Child, Command, Stdio};
struct Server {
child: Child,
}
impl Server {
fn spawn() -> Server {
let child = Command::new("/usr/lib/jvm/java-16-openjdk/bin/java")
.arg("-jar")
.arg("paper-1.17.1-259.jar")
.arg("--universe")
.arg("worlds")
.arg("--nogui")
.stdin(Stdio::piped())
.spawn()
.expect("Couldn't start child process");
Server { child }
}
pub fn send_command(&mut self, cmd: &str) {
match cmd.trim() {
"stop" | "exit" => self.stop(),
s => self.custom(s),
}
}
fn custom(&mut self, cmd: &str) {
let mut stdin = self.child.stdin.as_ref().unwrap();
stdin.write_all(cmd.as_bytes()).unwrap();
stdin.flush().unwrap();
}
pub fn stop(&mut self) {
self.custom("stop");
self.child.wait();
}
}
fn main() {
let mut server = Server::spawn();
let stdin = io::stdin();
let input = &mut String::new();
loop {
input.clear();
stdin.read_line(input);
println!("input: {}", input.trim());
server.send_command(input);
if input.trim() == "stop" {
break;
}
}
}