Add GameService#shutdown, remove comment on overridden method, rebase some sloppy code.

This commit is contained in:
atomicint
2015-03-23 16:44:15 -04:00
parent e136d7847b
commit 9569bf374d
2 changed files with 33 additions and 18 deletions
+5 -4
View File
@@ -25,7 +25,7 @@ public final class GamePulseHandler implements Runnable {
* *
* @param service The {@link GameService}. * @param service The {@link GameService}.
*/ */
GamePulseHandler(GameService service) { protected GamePulseHandler(GameService service) {
this.service = service; this.service = service;
} }
@@ -33,10 +33,11 @@ public final class GamePulseHandler implements Runnable {
public void run() { public void run() {
try { try {
service.pulse(); service.pulse();
} catch (Throwable t) { } catch (Throwable reason) {
logger.log(Level.SEVERE, "Exception during pulse.", t); logger.log(Level.SEVERE, "Exception occured during pulse!", reason);
} finally {
service.shutdown(false);
} }
} }
} }
+28 -14
View File
@@ -125,18 +125,11 @@ public final class GameService extends Service {
*/ */
public void pulse() { public void pulse() {
synchronized (this) { synchronized (this) {
LoginService loginService = getContext().getService(LoginService.class); finalizeUnregisters();
World world = World.getWorld(); World world = World.getWorld();
for (Player player : world.getPlayerRepository()) {
int unregistered = 0; GameSession session = player.getSession();
Player old;
while (unregistered < UNREGISTERS_PER_CYCLE && (old = oldPlayers.poll()) != null) {
loginService.submitSaveRequest(old.getSession(), old);
unregistered++;
}
for (Player p : world.getPlayerRepository()) {
GameSession session = p.getSession();
if (session != null) { if (session != null) {
session.handlePendingMessages(chainGroup); session.handlePendingMessages(chainGroup);
} }
@@ -147,6 +140,30 @@ public final class GameService extends Service {
} }
} }
/**
* Finalizes the unregistration of Player's queued to be unregistered.
*/
private void finalizeUnregisters() {
LoginService loginService = getContext().getService(LoginService.class);
for (int count = 0; count < UNREGISTERS_PER_CYCLE; count++) {
Player player = oldPlayers.poll();
if (player == null) {
break;
}
loginService.submitSaveRequest(player.getSession(), player);
}
}
/**
* Shuts down this game service.
*/
public void shutdown(boolean natural) {
scheduledExecutor.shutdownNow();
// TODO: Other events that should happen upon natural or unexpected shutdown.
}
/** /**
* Registers a {@link Player} (may block!). * Registers a {@link Player} (may block!).
* *
@@ -170,9 +187,6 @@ public final class GameService extends Service {
} }
} }
/**
* Starts the game service.
*/
@Override @Override
public void start() { public void start() {
scheduledExecutor.scheduleAtFixedRate(new GamePulseHandler(this), GameConstants.PULSE_DELAY, GameConstants.PULSE_DELAY, TimeUnit.MILLISECONDS); scheduledExecutor.scheduleAtFixedRate(new GamePulseHandler(this), GameConstants.PULSE_DELAY, GameConstants.PULSE_DELAY, TimeUnit.MILLISECONDS);