Search in sources :

Example 6 with RemoteMethodCallResults

use of games.strategy.engine.message.RemoteMethodCallResults 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)

Example 7 with RemoteMethodCallResults

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

the class UnifiedMessenger method messengerInvalid.

private void messengerInvalid() {
    synchronized (pendingLock) {
        for (final GUID id : pendingInvocations.keySet()) {
            final CountDownLatch latch = pendingInvocations.remove(id);
            latch.countDown();
            results.put(id, new RemoteMethodCallResults(new ConnectionLostException("Connection Lost")));
        }
    }
}
Also used : ConnectionLostException(games.strategy.engine.message.ConnectionLostException) RemoteMethodCallResults(games.strategy.engine.message.RemoteMethodCallResults) GUID(games.strategy.net.GUID) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

RemoteMethodCallResults (games.strategy.engine.message.RemoteMethodCallResults)7 GUID (games.strategy.net.GUID)4 SpokeInvoke (games.strategy.engine.message.SpokeInvoke)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 HubInvoke (games.strategy.engine.message.HubInvoke)2 ConnectionLostException (games.strategy.engine.message.ConnectionLostException)1 HubInvocationResults (games.strategy.engine.message.HubInvocationResults)1 RemoteMethodCall (games.strategy.engine.message.RemoteMethodCall)1 RemoteNotFoundException (games.strategy.engine.message.RemoteNotFoundException)1 SpokeInvocationResults (games.strategy.engine.message.SpokeInvocationResults)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)1 Test (org.junit.jupiter.api.Test)1