Ejemplo n.º 1
0
  /**
   * Signal that the HttpConnection has finished handling the request. For blocking connectors,this
   * call may block if the request has been suspended (startAsync called).
   *
   * @return next actions be handled again (eg because of a resume that happened before unhandle was
   *     called)
   */
  protected Action unhandle() {
    Action action;
    AsyncContextEvent schedule_event = null;
    boolean read_interested = false;

    try (Locker.Lock lock = _locker.lock()) {
      if (DEBUG) LOG.debug("unhandle {}", toStringLocked());

      switch (_state) {
        case COMPLETING:
        case COMPLETED:
          return Action.TERMINATED;

        case THROWN:
          _state = State.DISPATCHED;
          return Action.ERROR_DISPATCH;

        case DISPATCHED:
        case ASYNC_IO:
          break;

        default:
          throw new IllegalStateException(this.getStatusStringLocked());
      }

      if (_async != null) {
        _initial = false;
        switch (_async) {
          case COMPLETE:
            _state = State.COMPLETING;
            _async = null;
            action = Action.COMPLETE;
            break;

          case DISPATCH:
            _state = State.DISPATCHED;
            _async = null;
            action = Action.ASYNC_DISPATCH;
            break;

          case STARTED:
            if (_asyncReadUnready && _asyncReadPossible) {
              _state = State.ASYNC_IO;
              _asyncReadUnready = false;
              action = Action.READ_CALLBACK;
            } else if (_asyncWrite) // TODO refactor same as read
            {
              _asyncWrite = false;
              _state = State.ASYNC_IO;
              action = Action.WRITE_CALLBACK;
            } else {
              schedule_event = _event;
              read_interested = _asyncReadUnready;
              _state = State.ASYNC_WAIT;
              action = Action.WAIT;
            }
            break;

          case EXPIRING:
            // onTimeout callbacks still being called, so just WAIT
            _state = State.ASYNC_WAIT;
            action = Action.WAIT;
            break;

          case EXPIRED:
            // onTimeout handling is complete, but did not dispatch as
            // we were handling.  So do the error dispatch here
            _state = State.DISPATCHED;
            _async = null;
            action = Action.ERROR_DISPATCH;
            break;

          case ERRORED:
            _state = State.DISPATCHED;
            _async = null;
            action = Action.ERROR_DISPATCH;
            break;

          default:
            throw new IllegalStateException(this.getStatusStringLocked());
        }
      } else {
        _state = State.COMPLETING;
        action = Action.COMPLETE;
      }
    }

    if (schedule_event != null) scheduleTimeout(schedule_event);
    if (read_interested) _channel.asyncReadFillInterested();
    return action;
  }