예제 #1
0
  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;
  }
예제 #2
0
 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);
     }
   }
 }
예제 #3
0
 @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);
 }
예제 #4
0
  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;
  }