Search in sources :

Example 1 with SpokeInvocationResults

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

the class UnifiedMessenger method messageReceived.

public void messageReceived(final Serializable msg, final INode from) {
    if (msg instanceof SpokeInvoke) {
        // if this isn't the server, something is wrong
        // maybe an attempt to spoof a message
        assertIsServer(from);
        final SpokeInvoke invoke = (SpokeInvoke) msg;
        final EndPoint local;
        synchronized (endPointMutex) {
            local = localEndPoints.get(invoke.call.getRemoteName());
        }
        // regardless, the other side is expecting our reply
        if (local == null) {
            if (invoke.needReturnValues) {
                send(new HubInvocationResults(new RemoteMethodCallResults(new RemoteNotFoundException("No implementors for " + invoke.call + ", inode: " + from + ", msg: " + msg)), invoke.methodCallId), from);
            }
            return;
        }
        // very important
        // we are guaranteed that here messages will be
        // read in the same order that they are sent from the client
        // however, once we delegate to the thread pool, there is no
        // guarantee that the thread pool task will run before
        // we get the next message notification
        // get the number for the invocation here
        final long methodRunNumber = local.takeANumber();
        // we don't want to block the message thread, only one thread is
        // reading messages per connection, so run with out thread pool
        final EndPoint localFinal = local;
        threadPool.execute(() -> {
            final List<RemoteMethodCallResults> results = localFinal.invokeLocal(invoke.call, methodRunNumber, invoke.getInvoker());
            if (invoke.needReturnValues) {
                final RemoteMethodCallResults result;
                if (results.size() == 1) {
                    result = results.get(0);
                } else {
                    result = new RemoteMethodCallResults(new IllegalStateException("Invalid result count" + results.size()) + " for end point:" + localFinal);
                }
                send(new HubInvocationResults(result, invoke.methodCallId), from);
            }
        });
    } else if (msg instanceof SpokeInvocationResults) {
        // a remote machine is returning results
        // if this isn't the server, something is wrong
        // maybe an attempt to spoof a message
        assertIsServer(from);
        final SpokeInvocationResults spokeInvocationResults = (SpokeInvocationResults) msg;
        final GUID methodId = spokeInvocationResults.methodCallId;
        // all
        synchronized (pendingLock) {
            results.put(methodId, spokeInvocationResults.results);
            final CountDownLatch latch = pendingInvocations.remove(methodId);
            Preconditions.checkNotNull(latch, String.format("method id: %s, was not present in pending invocations: %s, unified messenger addr: %s", methodId, pendingInvocations, super.toString()));
            latch.countDown();
        }
    }
}
Also used : HubInvocationResults(games.strategy.engine.message.HubInvocationResults) RemoteMethodCallResults(games.strategy.engine.message.RemoteMethodCallResults) RemoteNotFoundException(games.strategy.engine.message.RemoteNotFoundException) SpokeInvocationResults(games.strategy.engine.message.SpokeInvocationResults) GUID(games.strategy.net.GUID) SpokeInvoke(games.strategy.engine.message.SpokeInvoke) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

HubInvocationResults (games.strategy.engine.message.HubInvocationResults)1 RemoteMethodCallResults (games.strategy.engine.message.RemoteMethodCallResults)1 RemoteNotFoundException (games.strategy.engine.message.RemoteNotFoundException)1 SpokeInvocationResults (games.strategy.engine.message.SpokeInvocationResults)1 SpokeInvoke (games.strategy.engine.message.SpokeInvoke)1 GUID (games.strategy.net.GUID)1 CountDownLatch (java.util.concurrent.CountDownLatch)1