Search in sources :

Example 1 with NoLongerHasEndPointImplementor

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

the class UnifiedMessengerHub method messageReceived.

@Override
public void messageReceived(final Serializable msg, final INode from) {
    if (msg instanceof HasEndPointImplementor) {
        synchronized (endPointMutex) {
            final HasEndPointImplementor hasEndPoint = (HasEndPointImplementor) msg;
            final Collection<INode> nodes = endPoints.computeIfAbsent(hasEndPoint.endPointName, k -> new ArrayList<>());
            if (nodes.contains(from)) {
                throw new IllegalStateException("Already contained, new" + from + " existing, " + nodes + " name " + hasEndPoint.endPointName);
            }
            nodes.add(from);
        }
    } else if (msg instanceof NoLongerHasEndPointImplementor) {
        synchronized (endPointMutex) {
            final NoLongerHasEndPointImplementor hasEndPoint = (NoLongerHasEndPointImplementor) msg;
            final Collection<INode> nodes = endPoints.get(hasEndPoint.endPointName);
            if (nodes != null) {
                if (!nodes.remove(from)) {
                    throw new IllegalStateException("Not removed!");
                }
                if (nodes.isEmpty()) {
                    endPoints.remove(hasEndPoint.endPointName);
                }
            }
        }
    } else if (msg instanceof HubInvoke) {
        final HubInvoke invoke = (HubInvoke) msg;
        final Collection<INode> endPointCols = new ArrayList<>();
        synchronized (endPointMutex) {
            if (endPoints.containsKey(invoke.call.getRemoteName())) {
                endPointCols.addAll(endPoints.get(invoke.call.getRemoteName()));
            }
        }
        // the node will already have routed messages to local invokers
        endPointCols.remove(from);
        if (endPointCols.isEmpty()) {
            if (invoke.needReturnValues) {
                final RemoteMethodCallResults results = new RemoteMethodCallResults(new RemoteNotFoundException("Not found:" + invoke.call.getRemoteName()));
                send(new SpokeInvocationResults(results, invoke.methodCallId), from);
            }
        // no end points, this is ok, we
        // we are a channel with no implementors
        } else {
            invoke(invoke, endPointCols, from);
        }
    } else if (msg instanceof HubInvocationResults) {
        final HubInvocationResults results = (HubInvocationResults) msg;
        results(results, from);
    }
}
Also used : INode(games.strategy.net.INode) ArrayList(java.util.ArrayList) HasEndPointImplementor(games.strategy.engine.message.unifiedmessenger.HasEndPointImplementor) NoLongerHasEndPointImplementor(games.strategy.engine.message.unifiedmessenger.NoLongerHasEndPointImplementor) NoLongerHasEndPointImplementor(games.strategy.engine.message.unifiedmessenger.NoLongerHasEndPointImplementor) Collection(java.util.Collection)

Aggregations

HasEndPointImplementor (games.strategy.engine.message.unifiedmessenger.HasEndPointImplementor)1 NoLongerHasEndPointImplementor (games.strategy.engine.message.unifiedmessenger.NoLongerHasEndPointImplementor)1 INode (games.strategy.net.INode)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1