Search in sources :

Example 1 with GameOverException

use of games.strategy.engine.GameOverException in project triplea by triplea-game.

the class DelegateExecutionManager method createOutboundImplementation.

/**
 * Used to create an object the exits delegate execution.
 *
 * <p>
 * Objects on this method will decrement the thread lock count when called, and will increment it again when execution
 * is finished.
 * </p>
 */
public Object createOutboundImplementation(final Object implementor, final Class<?>[] interfaces) {
    assertGameNotOver();
    final InvocationHandler ih = (proxy, method, args) -> {
        assertGameNotOver();
        final boolean threadLocks = currentThreadHasReadLock();
        if (threadLocks) {
            leaveDelegateExecution();
        }
        try {
            return method.invoke(implementor, args);
        } catch (final MessengerException me) {
            throw new GameOverException("Game Over!");
        } catch (final InvocationTargetException ite) {
            assertGameNotOver();
            throw ite;
        } finally {
            if (threadLocks) {
                enterDelegateExecution();
            }
        }
    };
    return Proxy.newProxyInstance(implementor.getClass().getClassLoader(), interfaces, ih);
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Proxy(java.lang.reflect.Proxy) GameOverException(games.strategy.engine.GameOverException) WrappedInvocationHandler(games.strategy.triplea.util.WrappedInvocationHandler) MessengerException(games.strategy.engine.message.MessengerException) InvocationHandler(java.lang.reflect.InvocationHandler) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) Method(java.lang.reflect.Method) Preconditions.checkState(com.google.common.base.Preconditions.checkState) InvocationTargetException(java.lang.reflect.InvocationTargetException) MessengerException(games.strategy.engine.message.MessengerException) GameOverException(games.strategy.engine.GameOverException) WrappedInvocationHandler(games.strategy.triplea.util.WrappedInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with GameOverException

use of games.strategy.engine.GameOverException 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!");
    }
}
Also used : RemoteName(games.strategy.engine.message.RemoteName) MessengerException(games.strategy.engine.message.MessengerException) GameOverException(games.strategy.engine.GameOverException) IDelegate(games.strategy.engine.delegate.IDelegate) RemoteNotFoundException(games.strategy.engine.message.RemoteNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException) GameOverException(games.strategy.engine.GameOverException) MessengerException(games.strategy.engine.message.MessengerException)

Example 3 with GameOverException

use of games.strategy.engine.GameOverException in project triplea by triplea-game.

the class DefaultPlayerBridge method getRemotePersistentDelegate.

@Override
public IRemote getRemotePersistentDelegate(final String name) {
    if (game.isGameOver()) {
        throw new GameOverException("Game Over");
    }
    try {
        game.getData().acquireReadLock();
        try {
            final IDelegate delegate = game.getData().getDelegateList().getDelegate(name);
            if (delegate == null) {
                final String errorMessage = "IDelegate in DefaultPlayerBridge.getRemote() cannot be null. Looking for delegate named: " + name;
                // for some reason, client isn't getting or seeing the errors, so make sure we print it to err
                System.err.println(errorMessage);
                // too
                throw new IllegalStateException(errorMessage);
            }
            if (!(delegate instanceof IPersistentDelegate)) {
                return null;
            }
            return getRemoteThatChecksForGameOver(game.getRemoteMessenger().getRemote(ServerGame.getRemoteName(delegate)));
        } finally {
            game.getData().releaseReadLock();
        }
    } catch (final MessengerException me) {
        throw new GameOverException("Game Over!");
    }
}
Also used : IPersistentDelegate(games.strategy.engine.delegate.IPersistentDelegate) MessengerException(games.strategy.engine.message.MessengerException) GameOverException(games.strategy.engine.GameOverException) IDelegate(games.strategy.engine.delegate.IDelegate)

Aggregations

GameOverException (games.strategy.engine.GameOverException)3 MessengerException (games.strategy.engine.message.MessengerException)3 IDelegate (games.strategy.engine.delegate.IDelegate)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 IPersistentDelegate (games.strategy.engine.delegate.IPersistentDelegate)1 RemoteName (games.strategy.engine.message.RemoteName)1 RemoteNotFoundException (games.strategy.engine.message.RemoteNotFoundException)1 WrappedInvocationHandler (games.strategy.triplea.util.WrappedInvocationHandler)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 Proxy (java.lang.reflect.Proxy)1 TimeUnit (java.util.concurrent.TimeUnit)1 ReentrantReadWriteLock (java.util.concurrent.locks.ReentrantReadWriteLock)1