Added very basic proof-of-concept
This commit is contained in:
parent
905f973324
commit
f8a65ba2f3
3 changed files with 76 additions and 0 deletions
59
src/main.rs
Normal file
59
src/main.rs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue