/**
   * This method handle the processing of all Selector's interest op
   * (OP_ACCEPT,OP_READ,OP_WRITE,OP_CONNECT) by delegating to its Handler. By default, all
   * java.nio.channels.Selector operations are implemented using SelectorHandler. All SelectionKey
   * operations are implemented by SelectionKeyHandler. Finally, ProtocolChain creation/re-use are
   * implemented by InstanceHandler.
   *
   * @param selectorHandler - the {@link SelectorHandler}
   */
  protected void doSelect(SelectorHandler selectorHandler) {
    final NIOContext serverCtx = pollContext(null, null);
    serverCtx.setSelectorHandler(selectorHandler);
    try {
      // Set the SelectionKeyHandler only if the SelectorHandler doesn't define one.
      if (selectorHandler.getSelectionKeyHandler() == null) {
        if (logger.isLoggable(Level.FINE)) {
          logger.log(
              Level.FINE, "Set DefaultSelectionKeyHandler to SelectorHandler: " + selectorHandler);
        }
        SelectionKeyHandler assgnSelectionKeyHandler = null;
        if (selectorHandler.getPreferredSelectionKeyHandler() != null) {
          Class<? extends SelectionKeyHandler> keyHandlerClass =
              selectorHandler.getPreferredSelectionKeyHandler();
          try {
            assgnSelectionKeyHandler = keyHandlerClass.newInstance();
            assgnSelectionKeyHandler.setSelectorHandler(selectorHandler);
          } catch (Exception e) {
            if (logger.isLoggable(Level.WARNING)) {
              logger.log(
                  Level.WARNING,
                  "Exception initializing preffered SelectionKeyHandler '"
                      + keyHandlerClass
                      + "' for the SelectorHandler '"
                      + selectorHandler
                      + "'");
            }
          }
        }
        if (assgnSelectionKeyHandler == null) {
          assgnSelectionKeyHandler = new DefaultSelectionKeyHandler(selectorHandler);
        }
        selectorHandler.setSelectionKeyHandler(assgnSelectionKeyHandler);
      }
      selectorHandler.preSelect(serverCtx);

      Set<SelectionKey> readyKeys = selectorHandler.select(serverCtx);
      if (readyKeys.size() != 0
          && stateHolder.getState(false) == State.STARTED
          && selectorHandler.getStateHolder().getState(false) == State.STARTED) {
        handleSelectedKeys(readyKeys, selectorHandler, serverCtx);
        readyKeys.clear();
      }
      selectorHandler.postSelect(serverCtx);
    } catch (Throwable e) {
      handleSelectException(e, selectorHandler, null);
    } finally {
      contexts.offer(serverCtx);
    }
  }
示例#2
0
  /** Shuntdown this instance by closing its Selector and associated channels. */
  public void shutdown() {
    // If shutdown was called for this SelectorHandler
    if (isShutDown.getAndSet(true)) return;

    stateHolder.setState(State.STOPPED);

    if (selector != null) {
      try {
        boolean isContinue = true;
        while (isContinue) {
          try {
            for (SelectionKey selectionKey : selector.keys()) {
              selectionKeyHandler.close(selectionKey);
            }

            isContinue = false;
          } catch (ConcurrentModificationException e) {
            // ignore
          }
        }
      } catch (ClosedSelectorException e) {
        // If Selector is already closed - OK
      }
    }

    try {
      if (serverSocket != null) {
        serverSocket.close();
        serverSocket = null;
      }
    } catch (Throwable ex) {
      Controller.logger().log(Level.SEVERE, "serverSocket.close", ex);
    }

    try {
      if (serverSocketChannel != null) {
        serverSocketChannel.close();
        serverSocketChannel = null;
      }
    } catch (Throwable ex) {
      Controller.logger().log(Level.SEVERE, "serverSocketChannel.close", ex);
    }

    try {
      if (selector != null) selector.close();
    } catch (Throwable ex) {
      Controller.logger().log(Level.SEVERE, "selector.close", ex);
    }

    if (asyncQueueReader != null) {
      asyncQueueReader.close();
      asyncQueueReader = null;
    }

    if (asyncQueueWriter != null) {
      asyncQueueWriter.close();
      asyncQueueWriter = null;
    }

    selectorHandlerTasks.clear();

    attributes = null;
  }
示例#3
0
 /**
  * Invoked after Selector.select().
  *
  * @param ctx {@link Context}
  */
 public void postSelect(Context ctx) {
   selectionKeyHandler.expire(keys().iterator());
 }
 protected void afterCall() {
   SelectionKey currentKey = context.getSelectionKey();
   SelectionKeyHandler selectionKeyHandler = context.getSelectorHandler().getSelectionKeyHandler();
   selectionKeyHandler.postProcess(currentKey);
 }