Esempio n. 1
0
 /**
  * Creates a new ConnectionHandler object
  *
  * @param sChannel the SocketChannel of the client
  * @param data a reference to a ReactorData object
  */
 private ConnectionHandler(SocketChannel sChannel, ReactorData<T> data, SelectionKey key) {
   _sChannel = sChannel;
   _data = data;
   _protocol = _data.getProtocolMaker().create();
   _tokenizer = _data.getTokenizerMaker().create();
   _skey = key;
 }
Esempio n. 2
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();
   }
 }
Esempio n. 3
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);
  }
Esempio n. 4
0
 /**
  * switches the handler to write only mode
  *
  * @throws ClosedChannelException if the channel is closed
  */
 public void switchToWriteOnlyMode() {
   _skey.interestOps(SelectionKey.OP_WRITE);
   _data.getSelector().wakeup();
 }
Esempio n. 5
0
 /**
  * switches the handler to read only mode
  *
  * @throws ClosedChannelException if the channel is closed
  */
 public void switchToReadOnlyMode() {
   _skey.interestOps(SelectionKey.OP_READ);
   _data.getSelector().wakeup();
 }
Esempio n. 6
0
 /**
  * switches the handler to read / write TODO Auto-generated catch blockmode
  *
  * @throws ClosedChannelException if the channel is closed
  */
 public void switchToReadWriteMode() {
   _skey.interestOps(SelectionKey.OP_READ | SelectionKey.OP_WRITE);
   _data.getSelector().wakeup();
 }