use of games.strategy.engine.delegate.IDelegate in project triplea by triplea-game.
the class DefaultPlayerBridge method getRemoteDelegate.
@Override
public IRemote getRemoteDelegate() {
if (game.isGameOver()) {
throw new GameOverException("Game Over");
}
try {
game.getData().acquireReadLock();
try {
final IDelegate delegate = game.getData().getDelegateList().getDelegate(currentDelegate);
if (delegate == null) {
final String errorMessage = "IDelegate in DefaultPlayerBridge.getRemote() cannot be null. CurrentStep: " + stepName + ", and CurrentDelegate: " + currentDelegate;
// for some reason, client isn't getting or seeing the errors, so make sure we print it to err
// too
System.err.println(errorMessage);
// Veqryn: hope that this suffices...?
throw new IllegalStateException(errorMessage);
}
final RemoteName remoteName;
try {
remoteName = ServerGame.getRemoteName(delegate);
} catch (final Exception e) {
final String errorMessage = "IDelegate IRemote interface class returned null or was not correct interface. CurrentStep: " + stepName + ", and CurrentDelegate: " + currentDelegate;
// for some reason, client isn't getting or seeing the errors, so make sure we print it to err
// too
System.err.println(errorMessage);
ClientLogger.logQuietly(errorMessage, e);
throw new IllegalStateException(errorMessage, e);
}
return getRemoteThatChecksForGameOver(game.getRemoteMessenger().getRemote(remoteName));
} finally {
game.getData().releaseReadLock();
}
} catch (final MessengerException me) {
throw new GameOverException("Game Over!");
}
}
use of games.strategy.engine.delegate.IDelegate in project triplea by triplea-game.
the class ServerGame method startStep.
private void startStep(final boolean stepIsRestoredFromSavedGame) {
// dont save if we just loaded
final DefaultDelegateBridge bridge = new DefaultDelegateBridge(gameData, this, new DelegateHistoryWriter(channelMessenger), randomStats, delegateExecutionManager);
if (delegateRandomSource == null) {
delegateRandomSource = (IRandomSource) delegateExecutionManager.createOutboundImplementation(randomSource, new Class<?>[] { IRandomSource.class });
}
bridge.setRandomSource(delegateRandomSource);
// node is created.
if (needToInitialize) {
addPlayerTypesToGameData(gamePlayers.values(), playerManager, bridge);
}
notifyGameStepChanged(stepIsRestoredFromSavedGame);
delegateExecutionManager.enterDelegateExecution();
try {
final IDelegate delegate = getCurrentStep().getDelegate();
delegate.setDelegateBridgeAndPlayer(bridge);
delegate.start();
} finally {
delegateExecutionManager.leaveDelegateExecution();
}
}
use of games.strategy.engine.delegate.IDelegate in project triplea by triplea-game.
the class ServerGame method stopGame.
public void stopGame() {
// we have already shut down
if (isGameOver) {
System.out.println("Game previously stopped, cannot stop again.");
return;
} else if (HeadlessGameServer.headless()) {
System.out.println("Attempting to stop game.");
}
isGameOver = true;
delegateExecutionStoppedLatch.countDown();
// tell the players (especially the AI's) that the game is stopping, so stop doing stuff.
for (final IGamePlayer player : gamePlayers.values()) {
// not sure whether to put this before or after we delegate execution block, but definitely before the game loader
// shutdown
player.stopGame();
}
// block delegate execution to prevent outbound messages to the players while we shut down.
try {
if (!delegateExecutionManager.blockDelegateExecution(16000)) {
System.err.println("Could not stop delegate execution.");
if (HeadlessGameServer.getInstance() != null) {
HeadlessGameServer.getInstance().printThreadDumpsAndStatus();
} else {
ErrorConsole.getConsole().dumpStacks();
}
// Try one more time
if (!delegateExecutionManager.blockDelegateExecution(16000)) {
System.err.println("Exiting...");
System.exit(-1);
}
}
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
}
// shutdown
try {
delegateExecutionManager.setGameOver();
getGameModifiedBroadcaster().shutDown();
randomStats.shutDown();
channelMessenger.unregisterChannelSubscriber(gameModifiedChannel, IGame.GAME_MODIFICATION_CHANNEL);
remoteMessenger.unregisterRemote(SERVER_REMOTE);
vault.shutDown();
for (final IGamePlayer gp : gamePlayers.values()) {
remoteMessenger.unregisterRemote(getRemoteName(gp.getPlayerId(), gameData));
}
for (final IDelegate delegate : gameData.getDelegateList()) {
final Class<? extends IRemote> remoteType = delegate.getRemoteType();
// if its null then it shouldnt be added as an IRemote
if (remoteType == null) {
continue;
}
remoteMessenger.unregisterRemote(getRemoteName(delegate));
}
} catch (final RuntimeException e) {
ClientLogger.logQuietly("Failed to shut down server game", e);
} finally {
delegateExecutionManager.resumeDelegateExecution();
}
gameData.getGameLoader().shutDown();
if (HeadlessGameServer.headless()) {
System.out.println("StopGame successful.");
}
}
use of games.strategy.engine.delegate.IDelegate in project triplea by triplea-game.
the class GameDataExporter method gamePlay.
private void gamePlay(final GameData data) {
xmlfile.append("\n");
xmlfile.append(" <gamePlay>\n");
for (final IDelegate delegate : data.getDelegateList()) {
if (!delegate.getName().equals("edit")) {
xmlfile.append(" <delegate name=\"").append(delegate.getName()).append("\" javaClass=\"").append(delegate.getClass().getCanonicalName()).append("\" display=\"").append(delegate.getDisplayName()).append("\"/>\n");
}
}
sequence(data);
xmlfile.append(" <offset round=\"").append(data.getSequence().getRound() - 1).append("\"/>\n");
xmlfile.append(" </gamePlay>\n");
}
use of games.strategy.engine.delegate.IDelegate in project triplea by triplea-game.
the class GameDataManager method loadDelegates.
private static void loadDelegates(final ObjectInputStream input, final GameData data) throws ClassNotFoundException, IOException {
for (Object endMarker = input.readObject(); !endMarker.equals(DELEGATE_LIST_END); endMarker = input.readObject()) {
final String name = (String) input.readObject();
final String displayName = (String) input.readObject();
final String className = (String) input.readObject();
final IDelegate instance;
try {
instance = (IDelegate) Class.forName(className).getDeclaredConstructor().newInstance();
instance.initialize(name, displayName);
data.getDelegateList().addDelegate(instance);
} catch (final Exception e) {
throw new IOException(e);
}
final String next = (String) input.readObject();
if (next.equals(DELEGATE_DATA_NEXT)) {
instance.loadState((Serializable) input.readObject());
}
}
}
Aggregations