Новые модули: McProcessServer и McScreenServer
Данные модули реализуют один из способов создания процесса сервера Minecraft. Process - это создание контролируемого процесса, который завязан на самом ASys. Проще говоря, выключится ASys - "упадет" процесс. Screen - подходит только для Unix-based систем (или запуск в Cygwin). Для запуска процесса используется утилита screen. Для контроля работы процесса так же задействуются утилиты pgrep и ps. Созданный процесс является неконтролируемым и при отключении ASys, созданный процесс продолжит "жизнь".
This commit is contained in:
73
McProcessServer/pom.xml
Normal file
73
McProcessServer/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 MC-Server Process implementation</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.mcserver</groupId>
|
||||
<artifactId>process-impl</artifactId>
|
||||
<version>0.1</version>
|
||||
<packaging>bundle</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>asys</groupId>
|
||||
<artifactId>api</artifactId>
|
||||
<version>0.6</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.mcserver.Activator</Bundle-Activator>
|
||||
<Import-Package>asys.api, *</Import-Package>
|
||||
</instructions>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
32
McProcessServer/src/main/java/asys/mcserver/Activator.java
Normal file
32
McProcessServer/src/main/java/asys/mcserver/Activator.java
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2016-08-15
|
||||
*/
|
||||
package asys.mcserver;
|
||||
|
||||
import asys.api.MinecraftServer;
|
||||
import asys.api.MinecraftServerFactory;
|
||||
import org.osgi.framework.BundleActivator;
|
||||
import org.osgi.framework.BundleContext;
|
||||
import org.osgi.framework.ServiceRegistration;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class Activator implements BundleActivator, MinecraftServerFactory {
|
||||
private ServiceRegistration<?> service;
|
||||
|
||||
@Override
|
||||
public void start(BundleContext bundleContext) throws Exception {
|
||||
service = bundleContext.registerService(MinecraftServerFactory.class.getName(), this, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop(BundleContext bundleContext) throws Exception {
|
||||
service.unregister();
|
||||
}
|
||||
|
||||
@Override
|
||||
public MinecraftServer createServer(String name, File directory, String mainJar, String jvmArgs, String params) {
|
||||
return new MinecraftProcessServer(name, directory, mainJar, jvmArgs, params);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* DmitriyMX <mail@dmitriymx.ru>
|
||||
* 2016-08-15
|
||||
*/
|
||||
package asys.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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user