Новый модуль: Single server
Модуль предназначен для работы с одним сервером Minecraft
This commit is contained in:
73
SingleServer/pom.xml
Normal file
73
SingleServer/pom.xml
Normal file
@@ -0,0 +1,73 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
|
||||
http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<name>ASys Single server</name>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<name>DmitriyMX</name>
|
||||
<email>mail@dmiriymx.ru</email>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<groupId>asys</groupId>
|
||||
<artifactId>singleserver</artifactId>
|
||||
<version>0.1</version>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>asys</groupId>
|
||||
<artifactId>api</artifactId>
|
||||
<version>0.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.osgi</groupId>
|
||||
<artifactId>org.osgi.core</artifactId>
|
||||
<version>6.0.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>org.apache.felix.gogo.runtime</artifactId>
|
||||
<version>0.10.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.groupId}.${project.artifactId}-${project.version}</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<encoding>${project.build.sourceEncoding}</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.felix</groupId>
|
||||
<artifactId>maven-bundle-plugin</artifactId>
|
||||
<version>3.0.1</version>
|
||||
<extensions>true</extensions>
|
||||
<configuration>
|
||||
<instructions>
|
||||
<Bundle-Name>${project.name}</Bundle-Name>
|
||||
<Bundle-SymbolicName>${project.groupId}.${project.artifactId}</Bundle-SymbolicName>
|
||||
<Bundle-Activator>asys.singleserver.Activator</Bundle-Activator>
|
||||
<Import-Package>asys.api, *</Import-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
81
SingleServer/src/main/java/asys/singleserver/Activator.java
Normal file
81
SingleServer/src/main/java/asys/singleserver/Activator.java
Normal file
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2016-08-15
|
||||
*/
|
||||
package asys.singleserver;
|
||||
|
||||
import asys.api.ASysUtils;
|
||||
import asys.api.BankObject;
|
||||
import org.osgi.framework.BundleActivator;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceRegistration;
|
||||
import org.osgi.util.tracker.ServiceTracker;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Properties;
|
||||
|
||||
public class Activator implements BundleActivator {
|
||||
private ServiceRegistration<?> serviceCmd;
|
||||
private ServiceRegistration<?> serviceServerManager;
|
||||
private ServiceTracker<?, BankObject> bankObjectTracker;
|
||||
private SingleServer singleServer;
|
||||
|
||||
public void start(BundleContext bundleContext) throws Exception {
|
||||
singleServer = new SingleServer(loadProperties(ASysUtils.GetProperty(bundleContext, "asys.config.dir", "conf")));
|
||||
|
||||
bankObjectTracker = new ServiceTracker<>(bundleContext, BankObject.class.getName(), null);
|
||||
bankObjectTracker.open();
|
||||
BankObject bankObject = bankObjectTracker.getService();
|
||||
if (bankObject != null) singleServer.loadState(bankObject);
|
||||
|
||||
serviceServerManager = bundleContext.registerService(asys.api.ServerManager.class.getName(), singleServer, null);
|
||||
serviceCmd = ASysUtils.RegisterCommands(bundleContext, new Commands(singleServer), "asys.server");
|
||||
}
|
||||
|
||||
public void stop(BundleContext bundleContext) throws Exception {
|
||||
serviceCmd.unregister();
|
||||
serviceServerManager.unregister();
|
||||
|
||||
BankObject bankObject = bankObjectTracker.getService();
|
||||
if (bankObject != null) singleServer.saveState(bankObject);
|
||||
|
||||
bankObjectTracker.close();
|
||||
}
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
private Properties loadProperties(String confDir) {
|
||||
final String propsFileName = "asys-singleserver.properties";
|
||||
Path propsPath = Paths.get(confDir).resolve(propsFileName);
|
||||
if (Files.notExists(propsPath)) {
|
||||
propsPath.toFile().getParentFile().mkdirs();
|
||||
|
||||
InputStream resourceAsStream = getClass().getResourceAsStream("/"+propsFileName);
|
||||
try {
|
||||
BufferedWriter bw = new BufferedWriter(new FileWriter(propsPath.toFile()));
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(resourceAsStream));
|
||||
String line;
|
||||
while ((line = br.readLine()) != null) {
|
||||
bw.write(line);
|
||||
bw.write("\n");
|
||||
}
|
||||
bw.flush();
|
||||
bw.close();
|
||||
br.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
Properties properties = new Properties();
|
||||
try {
|
||||
properties.load(new java.io.FileReader(propsPath.toFile()));
|
||||
return properties;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace(); }
|
||||
return null;
|
||||
}
|
||||
}
|
||||
65
SingleServer/src/main/java/asys/singleserver/Commands.java
Normal file
65
SingleServer/src/main/java/asys/singleserver/Commands.java
Normal file
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2016-08-15
|
||||
*/
|
||||
package asys.singleserver;
|
||||
|
||||
import asys.api.Command;
|
||||
import asys.api.MinecraftServer;
|
||||
import asys.api.ServerManager;
|
||||
import org.apache.felix.service.command.Descriptor;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class Commands {
|
||||
private ServerManager serverManager;
|
||||
|
||||
public Commands(ServerManager serverManager) {
|
||||
this.serverManager = serverManager;
|
||||
}
|
||||
|
||||
@Command
|
||||
@Descriptor("Server start")
|
||||
public void start() {
|
||||
serverManager.getServer().start();
|
||||
}
|
||||
|
||||
@Command
|
||||
@Descriptor("Server stop")
|
||||
public void stop() {
|
||||
serverManager.getServer().stop();
|
||||
serverManager.removeServer();
|
||||
}
|
||||
|
||||
@Command
|
||||
@Descriptor("Server force stop")
|
||||
public void forceStop() {
|
||||
serverManager.getServer().forceStop();
|
||||
serverManager.removeServer();
|
||||
}
|
||||
|
||||
@Command
|
||||
@Descriptor("Server restart")
|
||||
public void restart() {
|
||||
MinecraftServer server = serverManager.getServer();
|
||||
server.stop();
|
||||
server.start();
|
||||
}
|
||||
|
||||
@Command
|
||||
@Descriptor("Server is alive?")
|
||||
public void isAlive() {
|
||||
boolean alive = serverManager.getServer().isAlive();
|
||||
System.out.println(String.format("Server is alive: %s", alive));
|
||||
}
|
||||
|
||||
@Command
|
||||
@Descriptor("Send server command")
|
||||
public void cmd(String... command) {
|
||||
StringJoiner sj = new StringJoiner(" ");
|
||||
Arrays.asList(command).forEach(sj::add);
|
||||
serverManager.getServer().sendCommand(sj.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2016-08-15
|
||||
*/
|
||||
package asys.singleserver;
|
||||
|
||||
import asys.api.BankObject;
|
||||
import asys.api.MinecraftServer;
|
||||
import asys.api.ServerManager;
|
||||
import asys.singleserver.mcserver.MinecraftProcessServer;
|
||||
import asys.singleserver.mcserver.MinecraftScreenServer;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Properties;
|
||||
|
||||
public class SingleServer
|
||||
implements ServerManager
|
||||
{
|
||||
private MinecraftServer mcServer;
|
||||
private Properties configuration;
|
||||
|
||||
public SingleServer(Properties configuration)
|
||||
{
|
||||
this.configuration = configuration;
|
||||
}
|
||||
|
||||
private void prepareMcServer() {
|
||||
Path serverPath = Paths.get(configuration.getProperty("server.path"));
|
||||
if (Files.notExists(serverPath)) {
|
||||
throw new RuntimeException(String.format("Server path \"%s\" not found", serverPath.toString()));
|
||||
}
|
||||
|
||||
Path mainJar = serverPath.resolve(configuration.getProperty("server.mainjar"));
|
||||
if (Files.notExists(mainJar)) {
|
||||
throw new RuntimeException(String.format("Main jar \"%s\" not found", mainJar.toString()));
|
||||
}
|
||||
|
||||
if (configuration.getProperty("server.implement", "process").equalsIgnoreCase("screen")) {
|
||||
System.out.println("McServer implements: screen");
|
||||
mcServer = new MinecraftScreenServer("mcserver", serverPath.toFile(), mainJar.toString(),
|
||||
configuration.getProperty("server.jvm.args", null),
|
||||
configuration.getProperty("server.params", null));
|
||||
} else {
|
||||
System.out.println("McServer implements: process");
|
||||
mcServer = new MinecraftProcessServer("mcserver", serverPath.toFile(), mainJar.toString(),
|
||||
configuration.getProperty("server.jvm.args", null),
|
||||
configuration.getProperty("server.params", null));
|
||||
}
|
||||
}
|
||||
|
||||
public MinecraftServer getServer() {
|
||||
if (mcServer == null) prepareMcServer();
|
||||
return mcServer;
|
||||
}
|
||||
|
||||
public void removeServer() {
|
||||
mcServer = null;
|
||||
}
|
||||
|
||||
public void saveState(BankObject bankObject) {
|
||||
bankObject.save(SingleServer.class.getName() + "#mcServer", mcServer);
|
||||
}
|
||||
|
||||
public void loadState(BankObject bankObject) {
|
||||
mcServer = (MinecraftServer) bankObject.get(SingleServer.class.getName() + "#mcServer");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2016-08-15
|
||||
*/
|
||||
package asys.singleserver.mcserver;
|
||||
|
||||
import asys.api.MinecraftServer;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
public class MinecraftProcessServer implements MinecraftServer {
|
||||
private ProcessBuilder processBuilder;
|
||||
private Process process;
|
||||
private final String name;
|
||||
private final byte[] newLine = "\n".getBytes();
|
||||
|
||||
public MinecraftProcessServer(String name, File directory, String mainJar, String jvmArgs, String params) {
|
||||
processBuilder = new ProcessBuilder();
|
||||
processBuilder.directory(directory);
|
||||
List<String> commandLine = new Vector<>();
|
||||
commandLine.add("java");
|
||||
commandLine.add("-Dasys.server.name=\"" + name + "\"");
|
||||
if ((jvmArgs != null) && (!jvmArgs.trim().isEmpty())) {
|
||||
commandLine.add(jvmArgs);
|
||||
}
|
||||
commandLine.add("-jar");
|
||||
commandLine.add(mainJar);
|
||||
if ((params != null) && (!params.trim().isEmpty())) {
|
||||
commandLine.add(params);
|
||||
}
|
||||
processBuilder.command(commandLine);
|
||||
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
process = processBuilder.start();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
sendCommand("stop");
|
||||
}
|
||||
|
||||
public void forceStop() {
|
||||
process.destroy();
|
||||
process = null;
|
||||
}
|
||||
|
||||
public void sendCommand(String command) {
|
||||
if (!isAlive()) return;
|
||||
try {
|
||||
process.getOutputStream().write(command.getBytes());
|
||||
process.getOutputStream().write(newLine);
|
||||
process.getOutputStream().flush();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
return process != null && process.isAlive();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2016-08-15
|
||||
*/
|
||||
package asys.singleserver.mcserver;
|
||||
|
||||
import asys.api.MinecraftServer;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Vector;
|
||||
|
||||
public class MinecraftScreenServer implements MinecraftServer {
|
||||
private ProcessBuilder startPB;
|
||||
private String name;
|
||||
private String serverPID;
|
||||
|
||||
public MinecraftScreenServer(String name, File directory, String mainJar, String jvmArgs, String params) {
|
||||
startPB = new ProcessBuilder();
|
||||
startPB.directory(directory);
|
||||
List<String> commandLine = new Vector<>(Arrays.asList(new String[] { "screen", "-dmS", name, "java", "-Dasys.server.name=\"" + name + "\"" }));
|
||||
if ((jvmArgs != null) && (!jvmArgs.trim().isEmpty())) {
|
||||
commandLine.add(jvmArgs);
|
||||
}
|
||||
commandLine.add("-jar");
|
||||
commandLine.add(mainJar);
|
||||
if ((params != null) && (!params.trim().isEmpty())) {
|
||||
commandLine.add(params);
|
||||
}
|
||||
this.startPB.command(commandLine);
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void start() {
|
||||
try {
|
||||
startPB.start();
|
||||
|
||||
Thread.sleep(500L);
|
||||
|
||||
Process process = Runtime.getRuntime().exec("bash -c \"screen -ls | sed 's/\\t//g' | grep '\\." + name + "(' | cut -f1 -d.\"");
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
String out = br.readLine();
|
||||
br.close();
|
||||
|
||||
process = Runtime.getRuntime().exec("pgrep -P " + out);
|
||||
br = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
serverPID = br.readLine();
|
||||
br.close();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
sendCommand("stop");
|
||||
}
|
||||
|
||||
public void forceStop() {
|
||||
ProcessBuilder sendCmdPB = new ProcessBuilder();
|
||||
sendCmdPB.command("screen", "-S", this.name, "-X", "quit");
|
||||
try {
|
||||
sendCmdPB.start();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void sendCommand(String command) {
|
||||
ProcessBuilder sendCmdPB = new ProcessBuilder();
|
||||
sendCmdPB.command("screen", "-S", this.name, "-p", "0", "-X", "stuff", command + "\\r");
|
||||
try {
|
||||
sendCmdPB.start();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isAlive() {
|
||||
try {
|
||||
Process process = Runtime.getRuntime().exec("ps -p " + this.serverPID);
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
br.readLine();
|
||||
String line = br.readLine();
|
||||
br.close();
|
||||
return line != null;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace(); }
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
server.path=server
|
||||
server.mainjar=spigot.jar
|
||||
server.params=
|
||||
server.jvm.args=
|
||||
# process, screen
|
||||
server.implement=process
|
||||
Reference in New Issue
Block a user