java.lang.Object
org.cybergarage.http.HTTPServer
All Implemented Interfaces:
Runnable

public class HTTPServerextends Object implements Runnable
HTTP server implementation for the CyberLink HTTP framework.

This class provides a multithreaded HTTP server that can:

  • Accept incoming HTTP connections
  • Dispatch requests to registered listeners
  • Handle multiple concurrent connections
  • Support both IPv4 and IPv6 addresses

The server must be initialized using open(InetAddress, int) or open(String, int) before starting. Optionally, HTTPRequestListener instances can be registered to handle incoming requests.

  • Field Details

    • NAME

      public static final String NAME
      Constants
      See Also:
    • VERSION

      public static final String VERSION
      Version of this HTTP server implementation.
      See Also:
    • DEFAULT_PORT

      public static final int DEFAULT_PORT
      Port the server listens on when none is given.
      See Also:
    • DEFAULT_TIMEOUT

      public static final int DEFAULT_TIMEOUT
      Default timeout connection for HTTP communication
      See Also:
    • serverSock

      private ServerSocket serverSock
      ServerSocket
    • bindAddr

      private InetAddress bindAddr
      Address the server listens on.
    • bindPort

      private int bindPort
      Port the server listens on.
    • timeout

      protected int timeout
      Store the current TCP timeout value The variable should be accessed by getter and setter methods
    • httpRequestListenerList

      private ListenerList httpRequestListenerList
      Listeners notified of each request.
    • httpServerThread

      private Thread httpServerThread
      run
  • Constructor Details

    • HTTPServer

      public HTTPServer()
      Creates a new HTTP server instance. The server must be opened before it can accept connections.
  • Method Details

    • getName

      public static String getName()
      Builds the Server response header value from OS and library names.
      Returns:
      the Server response header value, built as the OS name and version followed by the CyberHTTP name and version
    • getServerSock

      public ServerSocket getServerSock()
      Gets the server socket.
      Returns:
      the ServerSocket instance
    • getBindAddress

      public String getBindAddress()
      Gets the bind address as a string.
      Returns:
      the bind address string, or empty string if not bound
    • getBindPort

      public int getBindPort()
      Gets the bind port.
      Returns:
      bind port number
    • getTimeout

      public int getTimeout()
      Gets the current socket timeout value.
      Returns:
      timeout in milliseconds
    • setTimeout

      public void setTimeout(int timeout)
      Sets the socket timeout value.
      Parameters:
      timeout - timeout in milliseconds
    • open

      public boolean open(InetAddress addr, int port)
      Opens the server on specified address and port.
      Parameters:
      addr - bind address for the server
      port - port number to bind to
      Returns:
      true if server was opened successfully, false otherwise
    • open

      public boolean open(String addr, int port)
      Opens the server on specified port with default bind address.
      Parameters:
      addr - bind address as hostname or IP address string
      port - port number to bind to
      Returns:
      true if server was opened successfully, false otherwise
    • close

      public boolean close()
      Closes the server socket and cleans up resources.
      Returns:
      true if server was closed successfully, false otherwise
    • accept

      public Socket accept()
      Accepts an incoming connection from the server socket.
      Returns:
      accepted socket with timeout set, or null if no server socket
    • isOpened

      public boolean isOpened()
      Checks if the server socket is opened.
      Returns:
      true if the server socket is not null
    • addRequestListener

      public void addRequestListener(HTTPRequestListener listener)
      Adds a request listener to receive HTTP request notifications.
      Parameters:
      listener - the request listener to add
    • removeRequestListener

      public void removeRequestListener(HTTPRequestListener listener)
      Removes a request listener.
      Parameters:
      listener - the request listener to remove
    • performRequestListener

      public void performRequestListener(HTTPRequest httpReq)
      Notifies all registered request listeners of an HTTP request.
      Parameters:
      httpReq - the HTTP request to notify listeners about
    • run

      public void run()
      Serves queued requests while this thread owns the server.
      Specified by:
      run in interface Runnable
    • start

      public boolean start()
      Starts the HTTP server thread.
      Returns:
      true if started successfully
    • stop

      public boolean stop()
      Stops the HTTP server thread.
      Returns:
      true if stopped successfully
    • toString

      public String toString()
      Returns the bind address as string representation.
      Overrides:
      toString in class Object
      Returns:
      bind address string