Ejemplo n.º 1
0
  protected boolean success() {
    HttpExchange exchange = connection.getExchange();
    if (exchange == null) return false;

    AtomicMarkableReference<Result> completion = exchange.responseComplete(null);
    if (!completion.isMarked()) return false;

    parser.reset();
    decoder = null;

    if (!updateState(State.RECEIVE, State.IDLE)) throw new IllegalStateException();

    exchange.terminateResponse();

    HttpResponse response = exchange.getResponse();
    List<Response.ResponseListener> listeners = exchange.getConversation().getResponseListeners();
    ResponseNotifier notifier = connection.getDestination().getResponseNotifier();
    notifier.notifySuccess(listeners, response);
    LOG.debug("Received {}", response);

    Result result = completion.getReference();
    if (result != null) {
      connection.complete(exchange, !result.isFailed());
      notifier.notifyComplete(listeners, result);
    }

    return true;
  }
Ejemplo n.º 2
0
  protected boolean fail(Throwable failure) {
    HttpExchange exchange = connection.getExchange();
    // In case of a response error, the failure has already been notified
    // and it is possible that a further attempt to read in the receive
    // loop throws an exception that reenters here but without exchange;
    // or, the server could just have timed out the connection.
    if (exchange == null) return false;

    AtomicMarkableReference<Result> completion = exchange.responseComplete(failure);
    if (!completion.isMarked()) return false;

    parser.close();
    decoder = null;

    while (true) {
      State current = state.get();
      if (updateState(current, State.FAILURE)) break;
    }

    exchange.terminateResponse();

    HttpResponse response = exchange.getResponse();
    HttpConversation conversation = exchange.getConversation();
    ResponseNotifier notifier = connection.getDestination().getResponseNotifier();
    notifier.notifyFailure(conversation.getResponseListeners(), response, failure);
    LOG.debug("Failed {} {}", response, failure);

    Result result = completion.getReference();
    if (result != null) {
      connection.complete(exchange, false);

      notifier.notifyComplete(conversation.getResponseListeners(), result);
    }

    return true;
  }