remove game ipc
This commit is contained in:
parent
63591fcb6f
commit
26a15a0b15
1 changed files with 0 additions and 146 deletions
|
@ -1,146 +0,0 @@
|
|||
package game;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.File;
|
||||
import java.io.FileFilter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.io.PrintWriter;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import game.log.Log;
|
||||
import game.util.Tuple;
|
||||
import game.util.Util;
|
||||
|
||||
public class ServerProcess {
|
||||
private final Consumer<String> monitor;
|
||||
private final ProcessBuilder builder;
|
||||
|
||||
private Process process;
|
||||
private PrintWriter writer;
|
||||
private boolean running;
|
||||
private String message;
|
||||
private int total;
|
||||
private int progress = -1;
|
||||
private int lastTick;
|
||||
|
||||
public ServerProcess(Consumer<String> monitor, File dir, int minMem, int maxMem, int port) {
|
||||
this.monitor = monitor;
|
||||
File jar = new File("game.jar");
|
||||
String path;
|
||||
if(jar.exists()) {
|
||||
path = jar.getAbsolutePath();
|
||||
}
|
||||
else {
|
||||
path = new File("../bin").getAbsolutePath() + File.pathSeparator + new File("../data").getAbsolutePath();
|
||||
for(File file : new File("../lib").listFiles(new FileFilter() {
|
||||
public boolean accept(File pathname) {
|
||||
return pathname.getName().endsWith(".jar") && !pathname.getName().endsWith("-sources.jar");
|
||||
}
|
||||
})) {
|
||||
path += File.pathSeparator + file.getAbsolutePath();
|
||||
}
|
||||
}
|
||||
this.builder = new ProcessBuilder("java", "-Xms" + minMem + "M", "-Xmx" + maxMem + "M", "-cp", path,
|
||||
"-Dserver.port=" + port, dir == null ? "-Dserver.debug" : "-Dserver.pipe", "game.Server")
|
||||
.directory(dir).redirectErrorStream(true);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
if(this.builder.directory() != null)
|
||||
this.builder.directory().mkdirs();
|
||||
try {
|
||||
this.process = this.builder.start();
|
||||
}
|
||||
catch(IOException e) {
|
||||
Log.SYSTEM.error(e, "Konnte Server nicht starten");
|
||||
this.process = null;
|
||||
}
|
||||
// this.builder = null;
|
||||
if(this.process == null)
|
||||
return;
|
||||
Thread con = new Thread(new Runnable() {
|
||||
private final BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(ServerProcess.this.process.getInputStream())));
|
||||
|
||||
public void run() {
|
||||
while(true) {
|
||||
String line;
|
||||
try {
|
||||
line = this.reader.readLine();
|
||||
}
|
||||
catch(IOException e) {
|
||||
line = null;
|
||||
}
|
||||
if(line == null)
|
||||
break;
|
||||
if(line.startsWith("#")) {
|
||||
line = line.substring(1);
|
||||
Tuple<String, String> data = Util.getKeyValue(line);
|
||||
if(data.first.equals("running"))
|
||||
ServerProcess.this.running = Boolean.parseBoolean(data.second);
|
||||
else if(data.first.equals("message"))
|
||||
ServerProcess.this.message = data.second;
|
||||
else if(data.first.equals("total"))
|
||||
ServerProcess.this.total = Integer.parseInt(data.second);
|
||||
else if(data.first.equals("progress"))
|
||||
ServerProcess.this.progress = Integer.parseInt(data.second);
|
||||
else if(data.first.equals("tick"))
|
||||
ServerProcess.this.lastTick = Integer.parseInt(data.second);
|
||||
else if(data.first.equals("console"))
|
||||
ServerProcess.this.monitor.accept(data.second);
|
||||
Log.SYSTEM.trace("server cmd %s -- %s", data.first, data.second);
|
||||
continue;
|
||||
}
|
||||
System.err.println(line);
|
||||
}
|
||||
}
|
||||
}, "Server console listener");
|
||||
con.setDaemon(true);
|
||||
con.start();
|
||||
this.writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(new BufferedOutputStream(this.process.getOutputStream()))), true);
|
||||
}
|
||||
|
||||
public boolean isStarted() {
|
||||
return this.running;
|
||||
}
|
||||
|
||||
public boolean isStopped() {
|
||||
return !this.running;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return this.message;
|
||||
}
|
||||
|
||||
public int getProgress() {
|
||||
return this.progress;
|
||||
}
|
||||
|
||||
public int getTotal() {
|
||||
return this.total;
|
||||
}
|
||||
|
||||
public int getLastTick() {
|
||||
return this.lastTick;
|
||||
}
|
||||
|
||||
public void shutdown() {
|
||||
try {
|
||||
this.writer.println("#end");
|
||||
}
|
||||
catch(Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
public void runCommand(String cmd) {
|
||||
try {
|
||||
this.writer.println(cmd);
|
||||
}
|
||||
catch(Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue