public Future<SessionMessage> requestMessage( Map<String, InputOperation> operations, ExecutionThread ethread) { final SessionMessageFuture future = new SessionMessageNDFuture(operations.keySet().toArray(new String[0])); ethread.cancelIfKilled(future); synchronized (messageQueues) { Deque<SessionMessage> queue = null; SessionMessage message = null; InputOperation operation = null; Iterator<Deque<SessionMessage>> it = messageQueues.values().iterator(); while (operation == null && it.hasNext()) { queue = it.next(); message = queue.peekFirst(); if (message != null) { operation = operations.get(message.message().operationName()); } } if (message == null) { queue = uncorrelatedMessageQueue; message = queue.peekFirst(); if (message != null) { operation = operations.get(message.message().operationName()); } } if (message == null || operation == null) { for (Map.Entry<String, InputOperation> entry : operations.entrySet()) { addMessageWaiter(entry.getValue(), future); } } else { future.setResult(message); queue.removeFirst(); // Check if we unlocked other receives boolean keepRun = true; SessionMessageFuture f; while (keepRun && !queue.isEmpty()) { message = queue.peekFirst(); f = getMessageWaiter(message.message().operationName()); if (f != null) { // We found a waiter for the unlocked message f.setResult(message); queue.removeFirst(); } else { keepRun = false; } } } } return future; }
public void pushMessage(SessionMessage message) { synchronized (messageQueues) { Deque<SessionMessage> queue; CorrelationSet cset = interpreter().getCorrelationSetForOperation(message.message().operationName()); if (cset != null) { queue = messageQueues.get(cset); } else { queue = uncorrelatedMessageQueue; } SessionMessageFuture future = getMessageWaiter(message.message().operationName()); if (future != null && queue.isEmpty()) { future.setResult(message); } else { queue.addLast(message); } } }
@Override protected void setResult(SessionMessage sessionMessage) { for (String operationName : operationNames) { if (operationName.equals(sessionMessage.message().operationName()) == false) { Deque<SessionMessageFuture> waitersList = messageWaiters.get(operationName); if (waitersList != null) { waitersList.remove(this); } } } super.setResult(sessionMessage); }
public Future<SessionMessage> requestMessage(InputOperation operation, ExecutionThread ethread) { SessionMessageFuture future = new SessionMessageFuture(); ethread.cancelIfKilled(future); CorrelationSet cset = interpreter().getCorrelationSetForOperation(operation.id()); synchronized (messageQueues) { Deque<SessionMessage> queue; if (cset == null) { queue = uncorrelatedMessageQueue; } else { queue = messageQueues.get(cset); } SessionMessage message = queue.peekFirst(); if (message == null || message.message().operationName().equals(operation.id()) == false) { addMessageWaiter(operation, future); } else { future.setResult(message); queue.removeFirst(); // Check if we unlocked other receives boolean keepRun = true; SessionMessageFuture currFuture; while (keepRun && !queue.isEmpty()) { message = queue.peekFirst(); currFuture = getMessageWaiter(message.message().operationName()); if (currFuture != null) { // We found a waiter for the unlocked message currFuture.setResult(message); queue.removeFirst(); } else { keepRun = false; } } } } return future; }