Search in sources :

Example 1 with SpokeInvoke

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

the class ServerMessenger method bareBonesSendChatMessage.

private void bareBonesSendChatMessage(final String message, final INode to) {
    final RemoteName rn = new RemoteName(isLobby() ? ChatController.getChatChannelName("_LOBBY_CHAT") : ChatController.getChatChannelName("games.strategy.engine.framework.ui.ServerStartup.CHAT_NAME"), IChatChannel.class);
    final RemoteMethodCall call = new RemoteMethodCall(rn.getName(), "chatOccured", new Object[] { message }, new Class<?>[] { String.class }, IChatChannel.class);
    final SpokeInvoke spokeInvoke = new SpokeInvoke(null, false, call, getServerNode());
    send(spokeInvoke, to);
}
Also used : RemoteName(games.strategy.engine.message.RemoteName) RemoteMethodCall(games.strategy.engine.message.RemoteMethodCall) SpokeInvoke(games.strategy.engine.message.SpokeInvoke)

Example 2 with SpokeInvoke

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

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