예제 #1
0
 /**
  * Stops the Reactor activity, including the Reactor thread and the Worker Threads in the Thread
  * Pool.
  */
 public synchronized void stopReactor() {
   if (!_shouldRun) return;
   _shouldRun = false;
   _data.getSelector().wakeup(); // Force select() to return
   _data.getExecutor().shutdown();
   try {
     _data.getExecutor().awaitTermination(2000, TimeUnit.MILLISECONDS);
   } catch (InterruptedException e) {
     // Someone didn't have patience to wait for the executor pool to
     // close
     e.printStackTrace();
   }
 }
예제 #2
0
  /**
   * Reads incoming data from the client:
   *
   * <UL>
   *   <LI>Reads some bytes from the SocketChannel
   *   <LI>create a protocolTask, to process this data, possibly generating an answer
   *   <LI>Inserts the Task to the ThreadPool
   * </UL>
   *
   * @throws
   * @throws IOException in case of an IOException during reading
   */
  public void read() {
    // do not read if OLD.protocol has terminated. only write of pending data is
    // allowed
    if (_protocol.shouldClose()) {
      return;
    }

    SocketAddress address = _sChannel.socket().getRemoteSocketAddress();
    logger.info("Reading from " + address);

    ByteBuffer buf = ByteBuffer.allocate(BUFFER_SIZE);
    int numBytesRead = 0;
    try {
      numBytesRead = _sChannel.read(buf);
    } catch (IOException e) {
      numBytesRead = -1;
    }
    // is the channel closed??
    if (numBytesRead == -1) {
      // No more bytes can be read from the channel
      logger.info("client on " + address + " has disconnected");
      closeConnection();
      // tell the OLD.protocol that the connection terminated.
      _protocol.connectionTerminated();
      return;
    }

    // add the buffer to the OLD.protocol task
    buf.flip();
    _task.addBytes(buf);
    // add the OLD.protocol task to the OLD.reactor
    _data.getExecutor().execute(_task);
  }