java.lang.Object
org.cybergarage.http.HTTPServer
- All Implemented Interfaces:
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 Summary
Modifier and TypeFieldDescriptionprivate InetAddressAddress the server listens on.private intPort the server listens on.static final intPort the server listens on when none is given.static final intDefault timeout connection for HTTP communicationprivate ListenerListListeners notified of each request.private Threadrunstatic final StringConstantsprivate ServerSocketServerSocketprotected intStore the current TCP timeout value The variable should be accessed by getter and setter methodsstatic final StringVersion of this HTTP server implementation.Constructor Summary
ConstructorsMethod Summary
Modifier and TypeMethodDescriptionaccept()Accepts an incoming connection from the server socket.voidaddRequestListener(HTTPRequestListener listener) Adds a request listener to receive HTTP request notifications.booleanclose()Closes the server socket and cleans up resources.Gets the bind address as a string.intGets the bind port.static StringgetName()Builds the Server response header value from OS and library names.Gets the server socket.intGets the current socket timeout value.booleanisOpened()Checks if the server socket is opened.booleanOpens the server on specified port with default bind address.booleanopen(InetAddress addr, int port) Opens the server on specified address and port.voidperformRequestListener(HTTPRequest httpReq) Notifies all registered request listeners of an HTTP request.voidremoveRequestListener(HTTPRequestListener listener) Removes a request listener.voidrun()Serves queued requests while this thread owns the server.voidsetTimeout(int timeout) Sets the socket timeout value.booleanstart()Starts the HTTP server thread.booleanstop()Stops the HTTP server thread.toString()Returns the bind address as string representation.
Field Details
NAME
VERSION
Version of this HTTP server implementation.- See Also:
DEFAULT_PORT
public static final int DEFAULT_PORTPort the server listens on when none is given.- See Also:
DEFAULT_TIMEOUT
public static final int DEFAULT_TIMEOUTDefault timeout connection for HTTP communication- See Also:
serverSock
ServerSocketbindAddr
Address the server listens on.bindPort
private int bindPortPort the server listens on.timeout
protected int timeoutStore the current TCP timeout value The variable should be accessed by getter and setter methodshttpRequestListenerList
Listeners notified of each request.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
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
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
Opens the server on specified address and port.- Parameters:
addr- bind address for the serverport- port number to bind to- Returns:
- true if server was opened successfully, false otherwise
open
Opens the server on specified port with default bind address.- Parameters:
addr- bind address as hostname or IP address stringport- 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
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
Adds a request listener to receive HTTP request notifications.- Parameters:
listener- the request listener to add
removeRequestListener
Removes a request listener.- Parameters:
listener- the request listener to remove
performRequestListener
Notifies all registered request listeners of an HTTP request.- Parameters:
httpReq- the HTTP request to notify listeners about
run
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