コード例 #1
0
ファイル: HttpRpcServer.java プロジェクト: cameronbraid/rox
  private void resetClientTimer(Socket socket) {
    if (this.idleClientTimer == null) {
      if (log.logTrace()) {
        log.trace("No idle client timeout configured, skipping timer reset");
      }
      return;
    }

    if (log.logTrace()) {
      log.trace("Resetting idle client timer: " + System.identityHashCode(socket));
    }

    // Store this in a local so we don't have to worry about
    // the value changing underneath us.
    long timeout = this.idleClientTimeout;

    // Cancel the existing task for this socket ...
    TimerTask curTask = (TimerTask) this.socketActivity.get(socket);
    if (curTask != null) {
      curTask.cancel();
    }

    // And schedule a new one.
    TimerTask task = new IdleClientTimerTask(socket);
    this.socketActivity.put(socket, task);
    this.idleClientTimer.schedule(task, timeout);
  }
コード例 #2
0
ファイル: HttpRpcServer.java プロジェクト: cameronbraid/rox
  /**
   * Called when a new connection is pending on the underlying {@link ServerSocketChannel}.
   *
   * @param key The {@link SelectionKey} for the socket on which a connection is pending.
   * @throws IOException if an error occurs while accepting the new connection.
   */
  protected void accept(SelectionKey key) throws IOException {
    // Pull out the socket channel that has a connection pending
    ServerSocketChannel serverSocketChannel = (ServerSocketChannel) key.channel();

    // Accept the connection
    SocketChannel socketChannel = serverSocketChannel.accept();
    Socket socket = socketChannel.socket();

    // Check if our AcceptPolicy will allow this new connection
    if (this.acceptPolicy != null
        && !this.acceptPolicy.shouldRetain(socketChannel, this.getSocketSelector().keys().size())) {
      if (log.logTrace()) {
        log.trace("Closing accepted connection (accept policy enforced)");
      }
      socketChannel.close();
      return;
    }

    this.registerChannel(socketChannel);

    // Register the new socket. This will promote it to an SSLSocket
    // if we're configured for HTTPS.
    this.registerSocket(socket, this.host, this.port, false);

    // Add the new SocketChannel to our Selector
    socketChannel.configureBlocking(false);
    SelectionKey acceptKey = socketChannel.register(this.getSocketSelector(), SelectionKey.OP_READ);

    this.resetClientTimer(socketChannel.socket());
  }
コード例 #3
0
ファイル: HttpRpcServer.java プロジェクト: cameronbraid/rox
 /**
  * Converts an exception raised while processing an HTTP request into a suitable HTTP response.
  *
  * <p>The response is marshalled and queued for writing on the socket associated with the original
  * request.
  *
  * @param msg The HTTP request being processed when the exception occurred.
  * @param e The exception that was raised.
  * @throws IOException if an error occurs marshalling or writing the response.
  */
 protected void handleMessageException(HttpMessageBuffer msg, Exception e) throws IOException {
   HttpResponse httpRsp;
   if (e instanceof HttpResponseException) {
     if (log.logWarn()) {
       log.warn("HttpResponseException", e);
     }
     httpRsp = this.newHttpResponse(msg, (HttpResponseException) e);
     this.queueWrite(msg.getSocket(), httpRsp.marshal(), true);
   } else if (e instanceof RemoteSocketClosedException) {
     if (log.logTrace()) {
       log.trace("Remote entity closed connection", e);
     }
   } else {
     if (log.logError()) {
       log.error("Internal Server Error", e);
     }
     httpRsp =
         this.newHttpResponse(
             msg,
             new HttpResponseException(
                 HttpConstants.StatusCodes._500_INTERNAL_SERVER_ERROR,
                 "Internal Server Error",
                 e));
     this.queueWrite(msg.getSocket(), httpRsp.marshal(), true);
   }
 }
コード例 #4
0
ファイル: HttpRpcServer.java プロジェクト: cameronbraid/rox
  private RequestHandler lookupHandler(String uri, String methodName) {
    // We don't normalize the URI here because it is normalized when
    // the HTTP request comes in.
    if (log.logDebug()) {
      log.debug("Look up handler for URI [" + uri + "] and method [" + methodName + "]");
    }

    RequestHandler handler = null;
    Map patternMap = (Map) this.uriHandlers.get(uri);
    if (patternMap == null) {
      if (log.logDebug()) {
        log.debug("Nothing registered for uri [" + uri + "]");
      }
      return null;
    }
    Iterator patterns = patternMap.entrySet().iterator();
    while (patterns.hasNext()) {
      Map.Entry entry = (Map.Entry) patterns.next();
      Pattern pattern = (Pattern) this.globalPatternMap.get(entry.getKey());
      if (pattern.matcher(methodName).find()) {
        if (log.logDebug()) {
          log.debug("Handler matched on pattern [" + pattern + "]");
        }
        handler = (RequestHandler) entry.getValue();
      }
    }
    return handler;
  }
コード例 #5
0
ファイル: HttpRpcServer.java プロジェクト: cameronbraid/rox
 protected void handleTimeout(Socket socket, Exception cause) {
   log.debug("Timeout on " + Utils.toString(socket), cause);
 }
コード例 #6
0
ファイル: HttpRpcServer.java プロジェクト: cameronbraid/rox
 protected void handleProcessingException(Socket socket, Exception e) {
   log.error("Exception on selecting thread (socket=" + Utils.toString(socket) + ")", e);
 }