infoservice.mailsystem.central.server
Class GenericServer

java.lang.Object
  extended by infoservice.mailsystem.central.server.GenericServer
All Implemented Interfaces:
java.lang.Runnable

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

This class is a generic server implementation. It waits for incoming connections and forwards them to the specified connection handler.


Field Summary
private  java.net.InetAddress m_serverAddress
          Stores the local address the server will listen on.
private  IServerImplementationFactory m_serverImplementationFactory
          Stores the factory which creates the server implementations for handling the accepted connections from the clients.
private  int m_serverPort
          Stores the local port the server will listen on.
private  java.net.ServerSocket m_serverSocket
          Stores the ServerSocket used for listening.
private  java.lang.Thread m_serverSocketThread
          Stores the thread which handles all requests reaching the server socket.
private  java.lang.Object m_synchronizer
          This object is used for internal synchronization with the server socket handling thread.
 
Constructor Summary
GenericServer(int a_serverPort, java.net.InetAddress a_serverAddress)
          Creates a new instance of GenericServer.
 
Method Summary
private  IServerImplementationFactory getServerImplementationFactory()
          Returns the current server implementation factory.
 void run()
          This is the implementation of the ServerSocket management thread.
 void setConnectionHandlerFactory(IServerImplementationFactory a_serverImplementationFactory)
          Changes the factory used for creating the server implementations for the accepted connections from the clients.
 void startServer()
          Starts the GenericServer.
 void stopServer()
          Stops listening for new requests.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_serverAddress

private java.net.InetAddress m_serverAddress
Stores the local address the server will listen on. If this value is null, the server will listen on all local addresses.


m_serverPort

private int m_serverPort
Stores the local port the server will listen on.


m_serverSocket

private java.net.ServerSocket m_serverSocket
Stores the ServerSocket used for listening. If this value is null, the server is currently not listening for new requests.


m_serverSocketThread

private java.lang.Thread m_serverSocketThread
Stores the thread which handles all requests reaching the server socket. If the server isn't running, this value is null.


m_synchronizer

private java.lang.Object m_synchronizer
This object is used for internal synchronization with the server socket handling thread.


m_serverImplementationFactory

private IServerImplementationFactory m_serverImplementationFactory
Stores the factory which creates the server implementations for handling the accepted connections from the clients. If this value is null, new connections are not handled and are closed immediately.

Constructor Detail

GenericServer

public GenericServer(int a_serverPort,
                     java.net.InetAddress a_serverAddress)
Creates a new instance of GenericServer. Only some initialization is done here, startServer() has to be called in order to start listening for requests.

Parameters:
a_serverPort - The port the server will listen on.
a_serverAddress - The local address the server will be bind on. If this value is null, the server will accept requests on all local addresses.
Method Detail

startServer

public void startServer()
                 throws java.io.IOException
Starts the GenericServer. The server is bound to the port and address specified in the constructor and will listen for requests. If the server is already listening, nothing happens. If there occurs an exception while creating the ServerSocket for the server, the server isn't started and that exception is thrown.

Throws:
java.io.IOException

stopServer

public void stopServer()
Stops listening for new requests. If the server isn't running, nothing happens. Attention: Already handled connections are not terminated by a call of this method.


setConnectionHandlerFactory

public void setConnectionHandlerFactory(IServerImplementationFactory a_serverImplementationFactory)
Changes the factory used for creating the server implementations for the accepted connections from the clients. The factory specified here is used immediately with the next accepted connection (but already handled connections are not influenced).

Parameters:
a_serverImplementationFactory - The new server implementation factory. If this value is null, any new arriving connection is not handled and is closed immediately.

run

public void run()
This is the implementation of the ServerSocket management thread. It waits for new connections from the clients and forwards them to the connection handlers.

Specified by:
run in interface java.lang.Runnable

getServerImplementationFactory

private IServerImplementationFactory getServerImplementationFactory()
Returns the current server implementation factory. It is used to create the server implementation for every new accpeted connection from the clients.

Returns:
The current server implementation factory. If this value is null, new connections are are not handled and are closed immediately.