Exemple #1
0
  protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String action = request.getParameter("action");
    String name = request.getParameter("name");

    String redirect = "/user.jsp?name=" + name;
    Client client = ClientRepository.getInstance().getClient(name);

    if ("kick".equals(action)) {
      logger.info(
          client.getUser().getName()
              + " ("
              + client.getInetAddress()
              + ") has been kicked by "
              + request.getRemoteUser()
              + " ("
              + request.getRemoteHost()
              + ")");
    } else if ("ban".equals(action)) {
      Banlist banlist = Banlist.getInstance();
      banlist.ban(client.getInetAddress().getHostAddress());

      logger.info(
          client.getUser().getName()
              + " ("
              + client.getInetAddress()
              + ") has been banned by "
              + request.getRemoteUser()
              + " ("
              + request.getRemoteHost()
              + ")");

      // save the server configuration
      Server.getInstance().getConfig().save();
    }

    client.disconnect();

    response.sendRedirect("/channel.jsp?name=" + client.getChannel().getConfig().getName());
  }
  /**
   *
   * <li>方法名:write
   * <li>@param sk
   * <li>@return
   * <li>返回类型:boolean
   * <li>说明:返回true表示传输没有发生异常,返回false表示发生异常
   * <li>创建人:CshBBrain;技术博客:http://cshbbrain.iteye.com/
   * <li>创建日期:2012-3-10
   * <li>修改人:
   * <li>修改日期:
   */
  public boolean write(Client sk) {
    SocketChannel socketChannel = sk.getChannel();
    if (!this.isBuffered) { // 将内容放入到缓冲区中去
      this.bufferedContent();
    }

    // 处理响应头,响应体的传输
    ByteBuffer buffer = this.currentBufferQueue.poll();
    if (buffer != null) {
      log.info("the postion of the data in write: " + buffer.position());
      buffer.flip(); // 反转缓存区

      while (buffer != null) {
        int transCount = 0;
        try {
          transCount = socketChannel.write(buffer);
          log.info("translate size:" + transCount);
        } catch (IOException e) {
          e.printStackTrace();
          this.closeChannel();
          sk.close();
          return false;
        } // 非阻塞的sockect连接一次可能写不完数据

        this.callCount.incrementAndGet(); // 回写计数器
        if (buffer.position() + transCount >= buffer.limit()) { // 传完
          buffer.clear();
          BufferPool.getInstance().releaseBuffer(buffer); // 将用完的缓冲区放回缓冲区池
          this.currentBufferQueue.poll(); // 从队列中删除缓冲区

          // 获取下一个缓冲区进行传输
          buffer = this.currentBufferQueue.poll();
          if (buffer != null) {
            buffer.flip(); // 反转缓存区
          }

        } else { // 没有传完,下次再传输
          buffer.position(buffer.position() + transCount);
          return true;
        }

        log.info("向客户端传输数据的长度 : " + transCount);
      }
    }

    // 如果有文件则传输文件
    if (!MyStringUtil.isBlank(this.filePath)) {
      try {
        if (this.fileChannel == null) {
          this.createFileChannel(); // 处理文件不存在的问题
        }

        // 直接通过文件管道将文件内容输出到客户端
        if (transelateSize < fileChannel.size()) {
          Long transferLength =
              fileChannel.transferTo(
                  transelateSize, (fileChannel.size() - transelateSize), socketChannel);
          transelateSize += transferLength;
        } else { // 文件传输完毕
          this.closeChannel();
          this.isWriteFinished = true;
        }

        this.callCount.incrementAndGet(); // 回写计数器
      } catch (IOException e) {
        e.printStackTrace();
        this.closeChannel(); // 关闭文件
        sk.close();
        return false;
      }
    } else {
      this.isWriteFinished = true;
    }

    return true; // 正常处理
  }