package net.knarcraft.minecraftserverlauncher.server; import net.knarcraft.minecraftserverlauncher.Main; import net.knarcraft.minecraftserverlauncher.profile.Collection; import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI; import javax.naming.ConfigurationException; import java.io.BufferedWriter; import java.io.IOException; import java.util.concurrent.TimeUnit; public class ServerHandler { /** * Available ram sizes. For ServerLauncherGUI combo */ private static final String[] ramList = {"512M", "1G", "2G", "3G", "4G", "5G", "6G", "7G", "8G", "9G", "10G", "11G", "12G", "13G", "14G", "15G", "16G"}; private static boolean stoppingServers = false; private static final ServerLauncherGUI gui = Main.getController().getGUI(); /** * Gets the list of available RAM choices allowed * * @return

All available RAM choices

*/ public static String[] getRamList() { return ramList; } /** * Gets whether the servers are currently in the process of stopping * * @return

True if the servers are in the process of stopping.

*/ public static boolean stoppingServers() { return stoppingServers; } /** * Marks the servers as finished stopping when a stop is confirmed */ public static void serversStopped() { if (stoppingServers) { stoppingServers = false; } } /** * Tries to stop all enabled servers * * @throws IOException

If a writer's process is already closed but not null

*/ public static void stop() throws IOException, InterruptedException { if (stoppingServers) { killServers(); return; } stoppingServers = true; int serversRunning = 0; for (Collection collection : Main.getController().getCurrentProfile().getCollections()) { Server server = collection.getServer(); BufferedWriter writer = server.getWriter(); if (writer != null) { serversRunning++; if (server.isProxy()) { writer.write("end\n"); } else { writer.write("stop\n"); } writer.flush(); server.setWriter(null); server.setStopped(); } } if (serversRunning == 0) { stoppingServers = false; } } /** * Kills all server processes * * @throws InterruptedException

If interrupted waiting for any of the servers to stop

*/ private static void killServers() throws InterruptedException { for (Collection collection : Main.getController().getCurrentProfile().getCollections()) { Server server = collection.getServer(); killServer(server); } } /** * Kills the given server after waiting 30 seconds for it to terminate normally * * @param server

The server to kill

* @throws InterruptedException

If interrupted waiting for the server to stop

*/ private static void killServer(Server server) throws InterruptedException { Process serverProcess = server.getProcess(); if (serverProcess != null) { if (!serverProcess.waitFor(30, TimeUnit.SECONDS)) { serverProcess.destroyForcibly(); serverProcess.waitFor(); } } } /** * Runs all enabled servers with their settings */ public static void startServers() { ServerLauncherController controller = Main.getController(); gui.setStatus("Starting servers"); Server previouslyStartedServer = null; for (Collection collection : controller.getCurrentProfile().getCollections()) { Server server = collection.getServer(); if (!server.runServer(previouslyStartedServer == null || previouslyStartedServer.isProxy())) { gui.showError("An error occurred. Start aborted. Please check relevant log files."); try { stop(); } catch (IOException | InterruptedException e) { e.printStackTrace(); } gui.updateGUIElementsWhenServersStartOrStop(false); return; } if (server.isEnabled()) { previouslyStartedServer = server; } } } /** * Gets a server object from a server save string * * @param saveString

The string containing necessary data regarding the server

* @return

A server in the same state it was saved in

*/ public static Server fromString(String saveString) throws ConfigurationException { String[] data = saveString.split(";"); return new Server(data[0], data[1], Boolean.parseBoolean(data[2]), data[3], data[4], data[5]); } }