Code example for SelectionKey

Methods: attach, cancel, interestOps, isConnectable, isValid

0
    key.attach(this);
  } 
 
  void registerForFirstWrite(SelectionKey key) throws IOException {
    state = State.WRITING_REQUEST_SIZE;
    key.interestOps(SelectionKey.OP_WRITE);
  } 
 
  protected ByteBuffer getFrameBuffer() {
    return frameBuffer;
  } 
 
  /** 
   * Transition to next state, doing whatever work is required. Since this 
   * method is only called by the selector thread, we can make changes to our 
   * select interests without worrying about concurrency. 
   * @param key 
   */ 
  protected void transition(SelectionKey key) {
    // Ensure key is valid 
    if (!key.isValid()) {
      key.cancel();
      Exception e = new TTransportException("Selection key not valid!");
      onError(e);
      return; 
    } 
 
    // Transition function 
    try { 
      switch (state) {
        case CONNECTING:
          doConnecting(key);
          break; 
        case WRITING_REQUEST_SIZE:
          doWritingRequestSize(); 
          break; 
        case WRITING_REQUEST_BODY:
          doWritingRequestBody(key);
          break; 
        case READING_RESPONSE_SIZE:
          doReadingResponseSize(); 
          break; 
        case READING_RESPONSE_BODY:
          doReadingResponseBody(key);
          break; 
        default: // RESPONSE_READ, ERROR, or bug 
          throw new IllegalStateException("Method call in state " + state
              + " but selector called transition method. Seems like a bug..."); 
      } 
      lastTransitionTime = System.currentTimeMillis();
    } catch (Throwable e) {
      key.cancel();
      key.attach(null);
      onError(e);
    } 
  } 
 
  protected void onError(Throwable e) {
    client.onError(e);
    callback.onError(e);
    state = State.ERROR;
  } 
 
  private void doReadingResponseBody(SelectionKey key) throws IOException {
    if (transport.read(frameBuffer) < 0) {
      throw new IOException("Read call frame failed");
    } 
    if (frameBuffer.remaining() == 0) {
      cleanUpAndFireCallback(key);
    } 
  } 
 
  private void cleanUpAndFireCallback(SelectionKey key) {
    state = State.RESPONSE_READ;
    key.interestOps(0);
    // this ensures that the TAsyncMethod instance doesn't hang around 
    key.attach(null);
    client.onComplete();
    callback.onComplete((T)this);
  } 
 
  private void doReadingResponseSize() throws IOException { 
    if (transport.read(sizeBuffer) < 0) {
      throw new IOException("Read call frame size failed");
    } 
    if (sizeBuffer.remaining() == 0) {
      state = State.READING_RESPONSE_BODY;
      frameBuffer = ByteBuffer.allocate(TFramedTransport.decodeFrameSize(sizeBufferArray));
    } 
  } 
 
  private void doWritingRequestBody(SelectionKey key) throws IOException {
    if (transport.write(frameBuffer) < 0) {
      throw new IOException("Write call frame failed");
    } 
    if (frameBuffer.remaining() == 0) {
      if (isOneway) {
        cleanUpAndFireCallback(key);
      } else { 
        state = State.READING_RESPONSE_SIZE;
        sizeBuffer.rewind();  // Prepare to read incoming frame size
        key.interestOps(SelectionKey.OP_READ);
      } 
    } 
  } 
 
  private void doWritingRequestSize() throws IOException { 
    if (transport.write(sizeBuffer) < 0) {
      throw new IOException("Write call frame size failed");
    } 
    if (sizeBuffer.remaining() == 0) {
      state = State.WRITING_REQUEST_BODY;
    } 
  } 
 
  private void doConnecting(SelectionKey key) throws IOException {
    if (!key.isConnectable() || !transport.finishConnect()) {
      throw new IOException("not connectable or finishConnect returned false after we got an OP_CONNECT");
    } 
    registerForFirstWrite(key);
  } 
}