2016-12-24 05:55:17 +01:00
|
|
|
package net.ME1312.SubServers.Bungee.Host.Internal;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
2016-12-24 05:55:17 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Event.*;
|
|
|
|
import net.ME1312.SubServers.Bungee.Host.Executable;
|
2017-01-09 20:37:57 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Host.SubLogger;
|
2016-12-24 05:55:17 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Library.Container;
|
|
|
|
import net.ME1312.SubServers.Bungee.Library.Exception.InvalidServerException;
|
|
|
|
import net.ME1312.SubServers.Bungee.Host.Host;
|
|
|
|
import net.ME1312.SubServers.Bungee.Host.SubServer;
|
2017-01-21 17:49:37 +01:00
|
|
|
import net.ME1312.SubServers.Bungee.Library.NamedContainer;
|
2016-12-05 04:21:04 +01:00
|
|
|
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStreamWriter;
|
2017-01-21 17:49:37 +01:00
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.LinkedList;
|
2016-12-05 04:21:04 +01:00
|
|
|
import java.util.UUID;
|
|
|
|
|
2017-01-07 20:06:54 +01:00
|
|
|
/**
|
|
|
|
* Internal SubServer Class
|
|
|
|
*/
|
2016-12-05 04:21:04 +01:00
|
|
|
public class InternalSubServer extends SubServer {
|
|
|
|
private InternalHost host;
|
|
|
|
private boolean enabled;
|
|
|
|
private Container<Boolean> log;
|
2017-01-01 20:34:46 +01:00
|
|
|
private String dir;
|
2016-12-05 04:21:04 +01:00
|
|
|
private File directory;
|
|
|
|
private Executable executable;
|
|
|
|
private String stopcmd;
|
2017-01-21 17:49:37 +01:00
|
|
|
private LinkedList<LoggedCommand> history;
|
2016-12-05 04:21:04 +01:00
|
|
|
private Process process;
|
2017-01-09 20:37:57 +01:00
|
|
|
private InternalSubLogger logger;
|
2016-12-20 00:31:01 +01:00
|
|
|
private Thread thread;
|
2016-12-15 22:04:39 +01:00
|
|
|
private BufferedWriter command;
|
2016-12-05 04:21:04 +01:00
|
|
|
private boolean restart;
|
2016-12-15 22:04:39 +01:00
|
|
|
private boolean allowrestart;
|
2016-12-05 04:21:04 +01:00
|
|
|
private boolean temporary;
|
|
|
|
|
2017-01-07 20:06:54 +01:00
|
|
|
/**
|
|
|
|
* Creates an Internal SubServer
|
|
|
|
*
|
|
|
|
* @param host Host
|
|
|
|
* @param name Name
|
|
|
|
* @param enabled Enabled Status
|
|
|
|
* @param port Port Number
|
|
|
|
* @param motd MOTD
|
|
|
|
* @param log Logging Status
|
|
|
|
* @param directory Directory
|
|
|
|
* @param executable Executable String
|
|
|
|
* @param stopcmd Stop Command
|
|
|
|
* @param start Auto-Start
|
|
|
|
* @param restart Auto-Restart
|
|
|
|
* @param hidden Hidden Status
|
|
|
|
* @param restricted Restricted Status
|
|
|
|
* @param temporary Temporary Status
|
|
|
|
* @throws InvalidServerException
|
|
|
|
*/
|
|
|
|
public InternalSubServer(InternalHost host, String name, boolean enabled, int port, String motd, boolean log, String directory, Executable executable, String stopcmd, boolean start, boolean restart, boolean hidden, boolean restricted, boolean temporary) throws InvalidServerException {
|
2016-12-19 01:38:02 +01:00
|
|
|
super(host, name, port, motd, hidden, restricted);
|
2017-01-07 20:06:54 +01:00
|
|
|
this.host = host;
|
2016-12-05 04:21:04 +01:00
|
|
|
this.enabled = enabled;
|
|
|
|
this.log = new Container<Boolean>(log);
|
2017-01-01 20:34:46 +01:00
|
|
|
this.dir = directory;
|
2016-12-15 22:04:39 +01:00
|
|
|
this.directory = new File(host.getDirectory(), directory);
|
2016-12-05 04:21:04 +01:00
|
|
|
this.executable = executable;
|
|
|
|
this.stopcmd = stopcmd;
|
2017-01-21 17:49:37 +01:00
|
|
|
this.history = new LinkedList<LoggedCommand>();
|
2016-12-05 04:21:04 +01:00
|
|
|
this.process = null;
|
2017-01-11 04:23:29 +01:00
|
|
|
this.logger = new InternalSubLogger(null, this, getName(), this.log, null);
|
2016-12-20 00:31:01 +01:00
|
|
|
this.thread = null;
|
2016-12-15 22:04:39 +01:00
|
|
|
this.command = null;
|
2016-12-05 04:21:04 +01:00
|
|
|
this.restart = restart;
|
2017-01-21 17:49:37 +01:00
|
|
|
this.temporary = !((start || temporary) && !start()) && temporary;
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void run() {
|
2017-01-06 21:44:34 +01:00
|
|
|
allowrestart = true;
|
|
|
|
try {
|
|
|
|
process = Runtime.getRuntime().exec(executable.toString(), null, directory);
|
|
|
|
System.out.println("SubServers > Now starting " + getName());
|
2017-01-09 20:37:57 +01:00
|
|
|
logger.process = process;
|
|
|
|
logger.start();
|
2017-01-06 21:44:34 +01:00
|
|
|
command = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
|
2017-01-21 17:49:37 +01:00
|
|
|
for (LoggedCommand command : history) if (process.isAlive()) {
|
|
|
|
this.command.write(command.getCommand());
|
|
|
|
this.command.newLine();
|
|
|
|
this.command.flush();
|
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
|
2017-01-21 17:49:37 +01:00
|
|
|
if (process.isAlive()) process.waitFor();
|
2017-01-06 21:44:34 +01:00
|
|
|
} catch (IOException | InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
allowrestart = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
SubStoppedEvent event = new SubStoppedEvent(this);
|
|
|
|
host.plugin.getPluginManager().callEvent(event);
|
|
|
|
System.out.println("SubServers > " + getName() + " has stopped");
|
|
|
|
process = null;
|
|
|
|
command = null;
|
2017-01-21 17:49:37 +01:00
|
|
|
history.clear();
|
2016-12-05 04:21:04 +01:00
|
|
|
|
2017-01-06 21:44:34 +01:00
|
|
|
if (isTemporary()) {
|
|
|
|
try {
|
|
|
|
host.removeSubServer(getName());
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (willAutoRestart() && allowrestart) {
|
2017-01-26 23:19:48 +01:00
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
|
|
|
while (thread != null && thread.isAlive()) {
|
|
|
|
Thread.sleep(250);
|
|
|
|
}
|
|
|
|
start();
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
2017-01-06 21:44:34 +01:00
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-12-19 01:38:02 +01:00
|
|
|
public boolean start(UUID player) {
|
2017-01-21 17:49:37 +01:00
|
|
|
if (isEnabled() && !(thread != null && thread.isAlive())) {
|
2016-12-15 22:04:39 +01:00
|
|
|
SubStartEvent event = new SubStartEvent(player, this);
|
2016-12-05 04:21:04 +01:00
|
|
|
host.plugin.getPluginManager().callEvent(event);
|
|
|
|
if (!event.isCancelled()) {
|
2017-01-21 17:49:37 +01:00
|
|
|
(thread = new Thread(this::run)).start();
|
2016-12-19 01:38:02 +01:00
|
|
|
return true;
|
|
|
|
} else return false;
|
|
|
|
} else return false;
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-12-19 01:38:02 +01:00
|
|
|
public boolean stop(UUID player) {
|
2017-01-21 17:49:37 +01:00
|
|
|
if (thread != null && thread.isAlive()) {
|
2016-12-15 22:04:39 +01:00
|
|
|
SubStopEvent event = new SubStopEvent(player, this, false);
|
2016-12-05 04:21:04 +01:00
|
|
|
host.plugin.getPluginManager().callEvent(event);
|
|
|
|
if (!event.isCancelled()) {
|
2016-12-15 22:04:39 +01:00
|
|
|
try {
|
2016-12-19 01:38:02 +01:00
|
|
|
allowrestart = false;
|
2017-01-21 17:49:37 +01:00
|
|
|
history.add(new LoggedCommand(player, stopcmd));
|
|
|
|
if (process != null && process.isAlive()) {
|
|
|
|
command.write(stopcmd);
|
|
|
|
command.newLine();
|
|
|
|
command.flush();
|
|
|
|
}
|
2016-12-19 01:38:02 +01:00
|
|
|
return true;
|
2016-12-15 22:04:39 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2016-12-19 01:38:02 +01:00
|
|
|
return false;
|
2016-12-15 22:04:39 +01:00
|
|
|
}
|
2016-12-19 01:38:02 +01:00
|
|
|
} else return false;
|
|
|
|
} else return false;
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-12-19 01:38:02 +01:00
|
|
|
public boolean terminate(UUID player) {
|
2017-01-21 17:49:37 +01:00
|
|
|
if (thread != null && thread.isAlive()) {
|
2016-12-15 22:04:39 +01:00
|
|
|
SubStopEvent event = new SubStopEvent(player, this, true);
|
2016-12-05 04:21:04 +01:00
|
|
|
host.plugin.getPluginManager().callEvent(event);
|
|
|
|
if (!event.isCancelled()) {
|
2016-12-19 01:38:02 +01:00
|
|
|
allowrestart = false;
|
2017-01-21 17:49:37 +01:00
|
|
|
if (process != null && process.isAlive()) process.destroyForcibly();
|
2016-12-19 01:38:02 +01:00
|
|
|
return true;
|
|
|
|
} else return false;
|
|
|
|
} else return false;
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2016-12-19 01:38:02 +01:00
|
|
|
public boolean command(UUID player, String command) {
|
2017-01-21 17:49:37 +01:00
|
|
|
if (thread != null && thread.isAlive()) {
|
2016-12-15 22:04:39 +01:00
|
|
|
SubSendCommandEvent event = new SubSendCommandEvent(player, this, command);
|
2016-12-05 04:21:04 +01:00
|
|
|
host.plugin.getPluginManager().callEvent(event);
|
|
|
|
if (!event.isCancelled()) {
|
2016-12-15 22:04:39 +01:00
|
|
|
try {
|
2016-12-19 01:38:02 +01:00
|
|
|
if (event.getCommand().equalsIgnoreCase(stopcmd)) allowrestart = false;
|
2017-01-21 17:49:37 +01:00
|
|
|
history.add(new LoggedCommand(player, event.getCommand()));
|
|
|
|
if (process != null && process.isAlive()) {
|
|
|
|
this.command.write(event.getCommand());
|
|
|
|
this.command.newLine();
|
|
|
|
this.command.flush();
|
|
|
|
}
|
2016-12-19 01:38:02 +01:00
|
|
|
return true;
|
2016-12-15 22:04:39 +01:00
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
2016-12-19 01:38:02 +01:00
|
|
|
return false;
|
2016-12-15 22:04:39 +01:00
|
|
|
}
|
2016-12-19 01:38:02 +01:00
|
|
|
} else return false;
|
|
|
|
} else return false;
|
2016-12-15 22:04:39 +01:00
|
|
|
}
|
|
|
|
|
2016-12-05 04:21:04 +01:00
|
|
|
@Override
|
|
|
|
public void waitFor() throws InterruptedException {
|
2016-12-20 00:31:01 +01:00
|
|
|
while (thread != null && thread.isAlive()) {
|
|
|
|
Thread.sleep(250);
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isRunning() {
|
|
|
|
return process != null && process.isAlive();
|
|
|
|
}
|
|
|
|
|
2017-01-26 23:19:48 +01:00
|
|
|
@Override
|
|
|
|
public void setDisplayName(String value) {
|
|
|
|
super.setDisplayName(value);
|
|
|
|
logger.name = getDisplayName();
|
|
|
|
}
|
|
|
|
|
2016-12-05 04:21:04 +01:00
|
|
|
@Override
|
|
|
|
public Host getHost() {
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isEnabled() {
|
|
|
|
return enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setEnabled(boolean value) {
|
|
|
|
enabled = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isLogging() {
|
|
|
|
return log.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setLogging(boolean value) {
|
|
|
|
log.set(value);
|
|
|
|
}
|
|
|
|
|
2017-01-09 20:37:57 +01:00
|
|
|
@Override
|
|
|
|
public SubLogger getLogger() {
|
|
|
|
return logger;
|
|
|
|
}
|
|
|
|
|
2017-01-21 17:49:37 +01:00
|
|
|
@Override
|
|
|
|
public LinkedList<LoggedCommand> getCommandHistory() {
|
|
|
|
return new LinkedList<LoggedCommand>(history);
|
|
|
|
}
|
|
|
|
|
2017-01-01 20:34:46 +01:00
|
|
|
@Override
|
|
|
|
public String getDirectory() {
|
|
|
|
return dir;
|
|
|
|
}
|
|
|
|
|
2016-12-05 04:21:04 +01:00
|
|
|
@Override
|
|
|
|
public String getStopCommand() {
|
|
|
|
return stopcmd;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setStopCommand(String value) {
|
|
|
|
stopcmd = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean willAutoRestart() {
|
|
|
|
return restart;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setAutoRestart(boolean value) {
|
|
|
|
restart = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isTemporary() {
|
|
|
|
return temporary;
|
|
|
|
}
|
2017-01-21 17:49:37 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setTemporary(boolean value) {
|
|
|
|
temporary = !(value && !isRunning() && !start()) && value;
|
|
|
|
}
|
2016-12-05 04:21:04 +01:00
|
|
|
}
|