Example #1
0
  /** {@inheritDoc} */
  public void resume() {
    if (!State.PAUSED.equals(stateHolder.getState(false))) {
      throw new IllegalStateException(
          "SelectorHandler is not in PAUSED state, but: " + stateHolder.getState(false));
    }

    stateHolder.setState(State.STARTED);
  }
  /** Resume this {@link Controller} and associated {@link SelectorHandler}s */
  public void resume() throws IOException {
    if (!State.PAUSED.equals(stateHolder.getState(false))) {
      throw new IllegalStateException(
          "Controller is not in PAUSED state, but: " + stateHolder.getState(false));
    }

    stateHolder.setState(State.STARTED);
  }
  /**
   * Stop the Controller by canceling all the registered keys.
   *
   * @param isAsync, true if controller should be stopped asynchrounously and control returned
   *     immediately. If false - control will be returned after Controller will be completely
   *     stoped.
   */
  public void stop(boolean isAsync) throws IOException {
    final CountDownLatch latch = new CountDownLatch(1);
    stateHolder.getStateLocker().writeLock().lock();
    try {
      if (stateHolder.getState(false) == State.STOPPED) {
        logger.log(Level.FINE, "Controller is already in stopped state");
        return;
      }

      if (!isAsync) {
        addStateListener(
            new ControllerStateListenerAdapter() {
              @Override
              public void onException(Throwable e) {
                removeStateListener(this);
                latch.countDown();
              }

              @Override
              public void onStopped() {
                removeStateListener(this);
                latch.countDown();
              }
            });
      }
      stateHolder.setState(State.STOPPED, false);
    } finally {
      stateHolder.getStateLocker().writeLock().unlock();
    }

    if (!isAsync) {
      try {
        latch.await();
      } catch (InterruptedException e) {
      }
    }
  }
Example #4
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;
  }
Example #5
0
 /** {@inheritDoc} */
 public void pause() {
   stateHolder.setState(State.PAUSED);
 }
 /** Pause this {@link Controller} and associated {@link SelectorHandler}s */
 public void pause() throws IOException {
   stateHolder.setState(State.PAUSED);
 }
  /**
   * Start the Controller. If the thread pool and/or Handler has not been defined, the default will
   * be used.
   */
  public void start() throws IOException {
    stateHolder.getStateLocker().writeLock().lock();
    boolean isUnlocked = false;
    try {
      if (stateHolder.getState(false) == null || stateHolder.getState(false) == State.STOPPED) {
        // if selectorHandlers were not set by user explicitly,
        // add TCPSelectorHandler by default
        if (selectorHandlers.isEmpty()) {
          SelectorHandler selectorHandler = new TCPSelectorHandler();
          selectorHandlers.add(selectorHandler);
        }

        if (readThreadsCount > 0) {
          initReadThreads();
          multiReadThreadSelectorHandler = new RoundRobinSelectorHandler(readThreadControllers);
        }

        stateHolder.setState(State.STARTED, false);
        notifyStarted();

        int selectorHandlerCount = selectorHandlers.size();
        readySelectorHandlerCounter = new AtomicInteger(selectorHandlerCount);
        stoppedSelectorHandlerCounter = new AtomicInteger(selectorHandlerCount);

        Iterator<SelectorHandler> it = selectorHandlers.iterator();
        if (selectorHandlerCount > 1) {
          for (; it.hasNext() && selectorHandlerCount-- > 0; ) {
            SelectorHandler selectorHandler = it.next();
            startSelectorHandlerRunner(selectorHandler, true);
          }
        } else if (it.hasNext()) {
          SelectorHandler selectorHandler = it.next();
          stateHolder.getStateLocker().writeLock().unlock();
          isUnlocked = true;
          startSelectorHandlerRunner(selectorHandler, false);
        }
      }
    } finally {
      if (!isUnlocked) {
        stateHolder.getStateLocker().writeLock().unlock();
      }
    }

    waitUntilSeletorHandlersStop();

    if (readThreadsCount > 0) {
      multiReadThreadSelectorHandler.shutdown();
      multiReadThreadSelectorHandler = null;

      for (Controller readController : readThreadControllers) {
        try {
          readController.stop();
        } catch (IOException e) {
          logger.log(Level.WARNING, "Exception occured when stopping read Controller!", e);
        }
      }

      readThreadControllers = null;
    }

    selectorHandlers.clear();
    threadPool.shutdown();
    attributes = null;

    // Notify Controller listeners
    for (ControllerStateListener stateListener : stateListeners) {
      stateListener.onStopped();
    }
  }