diff --git a/src/main/ProjectSWG.java b/src/main/ProjectSWG.java index d14340d8..2c35b730 100644 --- a/src/main/ProjectSWG.java +++ b/src/main/ProjectSWG.java @@ -28,6 +28,7 @@ public class ProjectSWG { System.err.println("ProjectSWG: Shutting down - unknown error."); } server.terminate(); + System.out.println("ProjectSWG: Server shut down."); } /** @@ -52,13 +53,14 @@ public class ProjectSWG { GalaxyStatus status = manager.getGalaxyStatus(); new ServerStatusIntent(status==GalaxyStatus.UP?ServerStatus.OPEN:ServerStatus.LOCKED).broadcast(); loop(); + System.out.println("ProjectSWG: Shutting down server."); new ServerStatusIntent(ServerStatus.TERMINATING).broadcast(); terminate(); new ServerStatusIntent(ServerStatus.OFFLINE).broadcast(); - if (!shutdownRequested) { + /*if (!shutdownRequested) { // Should this be here? It can only be reached at this point if there is a shutdown due to loop() method System.out.println("ProjectSWG: Cleaning up memory..."); cleanup(); - } + }*/ } } @@ -84,7 +86,7 @@ public class ProjectSWG { private void loop() { long loop = 0; - while (!shutdownRequested) { + while (!shutdownRequested && !manager.isShutdownRequested()) { try { manager.flushPackets(); // Sends any packets that weren't sent Thread.sleep(50); // Checks the state of the server every 10ms @@ -108,6 +110,7 @@ public class ProjectSWG { System.out.println("ProjectSWG: Terminated. Time: " + manager.getCoreTime() + "ms"); } + @SuppressWarnings("unused") private void cleanup() { manager = null; Runtime.getRuntime().gc(); diff --git a/src/services/CoreManager.java b/src/services/CoreManager.java index 2329ebc3..7e886bda 100644 --- a/src/services/CoreManager.java +++ b/src/services/CoreManager.java @@ -14,6 +14,7 @@ import network.packets.swg.SWGPacket; import network.packets.swg.zone.baselines.Baseline; import intents.InboundPacketIntent; import intents.OutboundPacketIntent; +import intents.ServerManagementIntent; import resources.Galaxy; import resources.Galaxy.GalaxyStatus; import resources.config.ConfigFile; @@ -72,6 +73,7 @@ public class CoreManager extends Manager { startTime = System.nanoTime(); registerForIntent(InboundPacketIntent.TYPE); registerForIntent(OutboundPacketIntent.TYPE); + registerForIntent(ServerManagementIntent.TYPE); return galaxy != null && super.initialize(); } @@ -88,6 +90,21 @@ public class CoreManager extends Manager { outputPacket(1, out.getPacket()); } } + if (i instanceof ServerManagementIntent) + handleServerManagementIntent((ServerManagementIntent) i); + } + + private void handleServerManagementIntent(ServerManagementIntent i) { + switch(i.getEvent()) { + case SHUTDOWN: initiateShutdownSequence(); break; + default: break; + } + + } + + private void initiateShutdownSequence() { + System.out.println("Beginning server shutdown sequence..."); + shutdownRequested = true; } public GalaxyStatus getGalaxyStatus() {