Savarese.Org

org.savarese.barehttp
Class HTTPServer

java.lang.Object
  extended by org.savarese.barehttp.HTTPServer

public class HTTPServer
extends java.lang.Object

Implements a server that listens for incoming client connections and services each with HTTPSession instances. A port number, bind address, and maximum number of client connections to service may be specified.

Author:
Daniel F. Savarese

Field Summary
static int DEFAULT_MAX_CONNECTIONS
          The default maximum number of concurrent client connections (10) that will be accepted if not specified.
static int DEFAULT_PORT
          The default port number (8080) to bind to if not specified.
 
Constructor Summary
HTTPServer(java.lang.String documentRoot)
          Same as HTTPServer(documentRoot, DEFAULT_PORT, DEFAULT_MAX_CONNECTIONS);
HTTPServer(java.lang.String root, int port, int maxConnections)
          Creates an HTTPServer instance.
 
Method Summary
 java.net.InetAddress getBindAddress()
          Returns the network interface address the server will bind to.
 java.net.InetAddress getBoundAddress()
          If the server is running, returns the address currently bound to.
 int getBoundPort()
          If the server is running, returns the port number currently bound to.
 int getConnectionCount()
          Returns the number of client connections currently established.
 java.lang.String getDocumentRoot()
          Returns the document root directory pathname.
 int getMaxConnections()
          Returns the maximum number of concurrent client connections that will be accepted.
 int getPort()
          The port number the server will bind to.
 boolean isRunning()
          Returns true if the server is in a running state, false if not.
 void setBindAddress(java.net.InetAddress bindAddr)
          Sets the network interface address the server should bind to.
 void setMaxConnections(int maxConnections)
          Sets the maximum number of concurrent client connections the server should accept.
 void setPort(int port)
          Sets the port number the server should bind to.
 void start()
          Starts listening for incoming connectons in an asynchronously initiated thread.
 boolean stop(long timeout, java.util.concurrent.TimeUnit unit)
          Schedules termination of the server, closes the server socket, and waits for the specified amount of time or until the server is terminated before returning.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_MAX_CONNECTIONS

public static final int DEFAULT_MAX_CONNECTIONS
The default maximum number of concurrent client connections (10) that will be accepted if not specified.

See Also:
Constant Field Values

DEFAULT_PORT

public static final int DEFAULT_PORT
The default port number (8080) to bind to if not specified.

See Also:
Constant Field Values
Constructor Detail

HTTPServer

public HTTPServer(java.lang.String documentRoot)
Same as HTTPServer(documentRoot, DEFAULT_PORT, DEFAULT_MAX_CONNECTIONS);


HTTPServer

public HTTPServer(java.lang.String root,
                  int port,
                  int maxConnections)
Creates an HTTPServer instance.

Parameters:
root - The fully qualified document root directory pathname.
port - The port number the server should bind to.
maxConnections - The maximum number of client connections the server should accept.
Method Detail

getDocumentRoot

public java.lang.String getDocumentRoot()
Returns the document root directory pathname.

Returns:
The document root directory pathname.

setPort

public void setPort(int port)
Sets the port number the server should bind to. By default, the server binds to DEFAULT_PORT. The new port takes effect the next time start() is invoked (after a stop(long, java.util.concurrent.TimeUnit) if already running).

Parameters:
port - The port number the server should bind to.

getPort

public int getPort()
The port number the server will bind to.

Returns:
The port number the server will bind to.

getBoundPort

public int getBoundPort()
If the server is running, returns the port number currently bound to. Otherwise, returns -1.

Returns:
The port number currently bound to or -1 if not bound.

setMaxConnections

public void setMaxConnections(int maxConnections)
Sets the maximum number of concurrent client connections the server should accept.

Parameters:
maxConnections - The maximum number of concurrent client connections the server should accept.

getMaxConnections

public int getMaxConnections()
Returns the maximum number of concurrent client connections that will be accepted.

Returns:
The maximum number of concurrent client connections that will be accepted.

getConnectionCount

public int getConnectionCount()
Returns the number of client connections currently established.

Returns:
The number of client connections currently established.

setBindAddress

public void setBindAddress(java.net.InetAddress bindAddr)
Sets the network interface address the server should bind to. By default, the server binds to the wildcard address. The new bind address takes effect the next time start() is invoked (after a stop(long, java.util.concurrent.TimeUnit) if already running).

Parameters:
bindAddr - The network interface the server should bind to. It may be null to reset to the wildcard.

getBindAddress

public java.net.InetAddress getBindAddress()
Returns the network interface address the server will bind to. A null return value signifies the wildcard address.

Returns:
The network interface address the server will bind to.

getBoundAddress

public java.net.InetAddress getBoundAddress()
If the server is running, returns the address currently bound to. Otherwise, returns null.

Returns:
The port number currently bound to or -1 if not bound.

isRunning

public boolean isRunning()
Returns true if the server is in a running state, false if not.

Returns:
True if the server is in a running state, false if not.

start

public void start()
           throws java.io.IOException,
                  java.lang.IllegalStateException
Starts listening for incoming connectons in an asynchronously initiated thread. The method returns immediately after the listening thread is established, making the HTTPServer instance an active object.

Throws:
java.io.IOException - If the server socket cannot be bound.
java.lang.IllegalStateException - If the server is already running.

stop

public boolean stop(long timeout,
                    java.util.concurrent.TimeUnit unit)
             throws java.io.IOException
Schedules termination of the server, closes the server socket, and waits for the specified amount of time or until the server is terminated before returning.

Parameters:
timeout - The maximum amount of time to wait for termination.
unit - The unit of time for the timeout.
Returns:
True if the server terminated before the method returned, false if not. If false is returned, the server will not be completely terminated untl isRunning() returns false. Subsequent calls to stop will have no effect while terminating.
Throws:
java.io.IOException

Savarese.Org

Copyright © 2001,2006,2010,2012 Daniel F. Savarese. All Rights Reserved.