org.dhcp4java
Class DHCPCoreServer

java.lang.Object
  extended by org.dhcp4java.DHCPCoreServer
All Implemented Interfaces:
java.lang.Runnable

public class DHCPCoreServer
extends java.lang.Object
implements java.lang.Runnable

A simple generic DHCP Server. The DHCP Server provided is based on a multi-thread model. The main thread listens at the socket, then dispatches work to a pool of threads running the servlet.

Configuration: the Server reads the following properties in "/DHCPd.properties" at the root of the class path. You can however provide a properties set when contructing the server. Default values are:

serverAddress=127.0.0.1:67 [address:port]
serverThreads=2 [number of concurrent threads for servlets]

Note: this class implements Runnable allowing it to be run in a dedicated thread.

Example:

     public static void main(String[] args) {
         try {
             DHCPCoreServer server = DHCPCoreServer.initServer(new DHCPStaticServlet(), null);
             new Thread(server).start();
         } catch (DHCPServerInitException e) {
             // die gracefully
         }
     }
 

Version:
0.51
Author:
Stephan Hadinger

Field Summary
protected static int PACKET_SIZE
          default MTU for ethernet
protected  java.util.Properties properties
          Consolidated parameters of the server.
static java.lang.String SERVER_ADDRESS
           
static java.lang.String SERVER_THREADS
           
static java.lang.String SERVER_THREADS_KEEPALIVE
           
static java.lang.String SERVER_THREADS_MAX
           
protected  DHCPServlet servlet
          the servlet it must run
protected  java.util.concurrent.ThreadPoolExecutor threadPool
          working threads pool.
protected  java.util.Properties userProps
          Reference of user-provided parameters
 
Method Summary
protected  void dispatch()
           
protected  java.net.InetSocketAddress getInetSocketAddress(java.util.Properties props)
          Returns the InetSocketAddress for the server (client-side).
 java.net.InetSocketAddress getSockAddress()
           
protected  void init()
          Initialize the server context from the Properties, and open socket.
static DHCPCoreServer initServer(DHCPServlet servlet, java.util.Properties userProps)
          Creates and initializes a new DHCP Server.
static java.net.InetSocketAddress parseSocketAddress(java.lang.String address)
          Parse a string of the form 'server:port' or '192.168.1.10:67'.
 void run()
          This is the main loop for accepting new request and delegating work to servlets in different threads.
protected  void sendResponse(java.net.DatagramPacket responseDatagram)
          Send back response packet to client.
 void stopServer()
          This method stops the server and closes the socket.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PACKET_SIZE

protected static final int PACKET_SIZE
default MTU for ethernet

See Also:
Constant Field Values

servlet

protected DHCPServlet servlet
the servlet it must run


threadPool

protected java.util.concurrent.ThreadPoolExecutor threadPool
working threads pool.


properties

protected java.util.Properties properties
Consolidated parameters of the server.


userProps

protected java.util.Properties userProps
Reference of user-provided parameters


SERVER_ADDRESS

public static final java.lang.String SERVER_ADDRESS
See Also:
Constant Field Values

SERVER_THREADS

public static final java.lang.String SERVER_THREADS
See Also:
Constant Field Values

SERVER_THREADS_MAX

public static final java.lang.String SERVER_THREADS_MAX
See Also:
Constant Field Values

SERVER_THREADS_KEEPALIVE

public static final java.lang.String SERVER_THREADS_KEEPALIVE
See Also:
Constant Field Values
Method Detail

initServer

public static DHCPCoreServer initServer(DHCPServlet servlet,
                                        java.util.Properties userProps)
                                 throws DHCPServerInitException
Creates and initializes a new DHCP Server.

It instanciates the object, then calls init() method.

Parameters:
servlet - the DHCPServlet instance processing incoming requests, must not be null.
userProps - specific properties, overriding file and default properties, may be null.
Returns:
the new DHCPCoreServer instance (never null).
Throws:
DHCPServerInitException - unable to start the server.

init

protected void init()
             throws DHCPServerInitException
Initialize the server context from the Properties, and open socket.

Throws:
DHCPServerInitException

dispatch

protected void dispatch()

sendResponse

protected void sendResponse(java.net.DatagramPacket responseDatagram)
Send back response packet to client.

This is a callback method used by servlet dispatchers to send back responses.


getInetSocketAddress

protected java.net.InetSocketAddress getInetSocketAddress(java.util.Properties props)
Returns the InetSocketAddress for the server (client-side).

  serverAddress (default 127.0.0.1)
  serverPort (default 67)

 

This method can be overriden to specify an non default socket behaviour

Parameters:
props - Properties loaded from /DHCPd.properties
Returns:
the socket address, null if there was a problem

parseSocketAddress

public static java.net.InetSocketAddress parseSocketAddress(java.lang.String address)
Parse a string of the form 'server:port' or '192.168.1.10:67'.

Parameters:
address - string to parse
Returns:
InetSocketAddress newly created
Throws:
java.lang.IllegalArgumentException - if unable to parse string

run

public void run()
This is the main loop for accepting new request and delegating work to servlets in different threads.

Specified by:
run in interface java.lang.Runnable

stopServer

public void stopServer()
This method stops the server and closes the socket.


getSockAddress

public java.net.InetSocketAddress getSockAddress()
Returns:
Returns the socket address.