net.directj.csf.impl
Class ServerImpl

java.lang.Object
  |
  +--net.directj.csf.impl.ServerImpl
All Implemented Interfaces:
java.lang.Runnable, Server

public class ServerImpl
extends java.lang.Object
implements Server

This a simple implementation of network server. It is a quite simple class, but it is powerfull enough to realize any networking application.

Version:
0.1.3
Author:
CARTAPANIS Alexandre <earendil@jprolog.net>

Nested Class Summary
private  class ServerImpl.ShutdownHook
          This class is used to stop the server at the end of the JVM.
 
Field Summary
private  java.net.InetAddress address
          The local endpoint of this server.
private  java.util.Vector clients
          The list of connected clients.
static java.lang.String DEFAULT_NAME
          The default name.
private  ClientHandlerFactory handlerFactory
          The factory that will be used to create the ClientHandler.
private  java.util.Vector listeners
          The list of listeners.
static int MAX_CLIENT
          The default maximum client amount.
private  int maxClient
          The maximum client amount.
private  java.lang.String name
          The server's name.
private  int port
          The port number to which this server is listening.
private  java.lang.Thread runner
          The server thread.
private  boolean running
          Indicates if the server is running or not.
private  java.net.ServerSocket serverSock
          The server's socket.
private  javax.net.ServerSocketFactory socketFactory
          The factory that will be used to create the ServerSocket.
static java.lang.String VERSION
          The server's version.
 
Constructor Summary
ServerImpl()
          Creates a new server implementation.
 
Method Summary
 void addServerListener(ServerListener _listener)
          Adds a server listener to this class.
 void fireEvent(ServerEvent _evt)
          Send a server event to the listeners
private  void getClient()
          Waits for the client to connect.
 java.net.InetAddress getInetAddress()
          Returns the address of the endpoint this server is listening to.
 java.lang.String getInfo()
          Returns the server informations (port, address, version...).
 int getMaximumClient()
          Returns the maximum amount of client the server can handle.
 java.lang.String getName()
          Returns the server's name.
 int getPort()
          Returns the port on which this server is listening.
 java.lang.String getStatus()
          Returns the server status (idle, number of connection, ...).
 boolean isRunning()
          Indicates if the server is running or not.
 void killClient(java.lang.String _address)
          Kills a client identified by his hostname or IP address.
 void removeAllServerListener()
          Removes all the listeners associated with this class.
 void removeServerListener(ServerListener _listener)
          Removes a specified server listener from the list.
 void run()
          Runs the server.
 void setClientHandlerFactory(ClientHandlerFactory _factory)
          Sets the ClientHandlerFactory.
 void setInetAddress(java.net.InetAddress _address)
          Changes the server's address.
 void setMaximumClient(int _max)
          Changes the server's maximum client amount.
 void setName(java.lang.String _name)
          Changes the server's name.
 void setPort(int _port)
          Changes the server's port.
 void setServerSocketFactory(javax.net.ServerSocketFactory _factory)
          Sets the ServerSocketFactory.
 void startServer()
          Starts the server.
 void stopServer()
          Stops the server.
 java.lang.String toString()
          Returns a string representation of this server.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

VERSION

public static final java.lang.String VERSION
The server's version.

See Also:
Constant Field Values

MAX_CLIENT

public static final int MAX_CLIENT
The default maximum client amount.

See Also:
Constant Field Values

DEFAULT_NAME

public static final java.lang.String DEFAULT_NAME
The default name.

See Also:
Constant Field Values

name

private java.lang.String name
The server's name.


serverSock

private java.net.ServerSocket serverSock
The server's socket.


socketFactory

private javax.net.ServerSocketFactory socketFactory
The factory that will be used to create the ServerSocket.


port

private int port
The port number to which this server is listening.


address

private java.net.InetAddress address
The local endpoint of this server.


maxClient

private int maxClient
The maximum client amount.


clients

private java.util.Vector clients
The list of connected clients.


handlerFactory

private ClientHandlerFactory handlerFactory
The factory that will be used to create the ClientHandler.


listeners

private java.util.Vector listeners
The list of listeners.


running

private volatile boolean running
Indicates if the server is running or not.


runner

private java.lang.Thread runner
The server thread.

Constructor Detail

ServerImpl

public ServerImpl()
Creates a new server implementation.

Method Detail

startServer

public void startServer()
Starts the server.

Specified by:
startServer in interface Server

stopServer

public void stopServer()
Stops the server.

Specified by:
stopServer in interface Server

run

public void run()
Runs the server.

Specified by:
run in interface java.lang.Runnable

isRunning

public boolean isRunning()
Indicates if the server is running or not.

Specified by:
isRunning in interface Server
Returns:
boolean - True if the server is running and ready to accept client, false otherwise.

getClient

private void getClient()
Waits for the client to connect.


killClient

public void killClient(java.lang.String _address)
Kills a client identified by his hostname or IP address. If the address does not correspond to any connected client, nothing happens. If the _address correspond to more than one client (for example when the same machine have more than one client connected to this server), then all clients are killed.

Specified by:
killClient in interface Server
Parameters:
_address - The address of the client we want to kill.

setClientHandlerFactory

public void setClientHandlerFactory(ClientHandlerFactory _factory)
                             throws java.lang.IllegalStateException,
                                    java.lang.IllegalArgumentException
Sets the ClientHandlerFactory. The factory is used to create the client handler that will be in charge of manage a client's requests and asnwers. Cannot be used if the server is running.

Specified by:
setClientHandlerFactory in interface Server
Parameters:
_factory - The client handler factory.
Throws:
java.lang.IllegalStateException - If the server is running.
java.lang.IllegalArgumentException - If the ClientHandlerFactory is not valid or has not been initialized.

setServerSocketFactory

public void setServerSocketFactory(javax.net.ServerSocketFactory _factory)
                            throws java.lang.IllegalStateException,
                                   java.lang.IllegalArgumentException
Sets the ServerSocketFactory. The factory is used to create the server socket. Cannot be used if the server is running.

Specified by:
setServerSocketFactory in interface Server
Parameters:
_factory - The server socket factory.
Throws:
java.lang.IllegalStateException - If the server is running.
java.lang.IllegalArgumentException - If the ClientHandlerFactory is not valid or has not been initialized.

fireEvent

public void fireEvent(ServerEvent _evt)
Send a server event to the listeners

Specified by:
fireEvent in interface Server
Parameters:
_evt - The event to be fired.

addServerListener

public void addServerListener(ServerListener _listener)
Adds a server listener to this class.

Specified by:
addServerListener in interface Server
Parameters:
_listener - The Listener to add.

removeServerListener

public void removeServerListener(ServerListener _listener)
Removes a specified server listener from the list.

Specified by:
removeServerListener in interface Server
Parameters:
_listener - The Listener to remove.

removeAllServerListener

public void removeAllServerListener()
Removes all the listeners associated with this class.

Specified by:
removeAllServerListener in interface Server

getInfo

public java.lang.String getInfo()
Returns the server informations (port, address, version...).

Specified by:
getInfo in interface Server
Returns:
String - The server's informations.

getStatus

public java.lang.String getStatus()
Returns the server status (idle, number of connection, ...).

Specified by:
getStatus in interface Server
Returns:
String - The server's status.

toString

public java.lang.String toString()
Returns a string representation of this server.

Specified by:
toString in interface Server
Overrides:
toString in class java.lang.Object
Returns:
String - A string representation of this server.

getPort

public int getPort()
Returns the port on which this server is listening.

Specified by:
getPort in interface Server
Returns:
int - The port number to which this server is listening.

setPort

public void setPort(int _port)
             throws java.lang.IllegalStateException,
                    java.lang.IllegalArgumentException
Changes the server's port. Cannot be used if the server is running. The port must be between 0 and 65535, inclusive.

Specified by:
setPort in interface Server
Parameters:
_port - The new server's port.
Throws:
java.lang.IllegalStateException - If the server is running.
java.lang.IllegalArgumentException - If the port is not valid.

getInetAddress

public java.net.InetAddress getInetAddress()
Returns the address of the endpoint this server is listening to.

Specified by:
getInetAddress in interface Server
Returns:
InetAddress - A InetAddress representing the local endpoint of this server.

setInetAddress

public void setInetAddress(java.net.InetAddress _address)
                    throws java.lang.IllegalStateException,
                           java.lang.IllegalArgumentException
Changes the server's address. Cannot be used if the server is running. The _address argument can be used on a multi-homed host for a server that will only accept connect requests to one of its addresses.

Specified by:
setInetAddress in interface Server
Parameters:
_address - The new server's address.
Throws:
java.lang.IllegalStateException - If the server is running.
java.lang.IllegalArgumentException - If the address is not valid.

getName

public java.lang.String getName()
Returns the server's name.

Specified by:
getName in interface Server
Returns:
String - The server's name.

setName

public void setName(java.lang.String _name)
             throws java.lang.IllegalStateException
Changes the server's name. Cannot be used if the server is running.

Specified by:
setName in interface Server
Parameters:
_name - The new server's name.
Throws:
java.lang.IllegalStateException - If the server is running.

getMaximumClient

public int getMaximumClient()
Returns the maximum amount of client the server can handle.

Specified by:
getMaximumClient in interface Server
Returns:
int - The maximum client amount.

setMaximumClient

public void setMaximumClient(int _max)
                      throws java.lang.IllegalArgumentException
Changes the server's maximum client amount. If the new size is greater than the current size, nothing happens. If the new size is less than the current size, all components at index _max and greater are discarded.

Specified by:
setMaximumClient in interface Server
Parameters:
_max - The new maximum client amount.
Throws:
java.lang.IllegalArgumentException - If the specified number is not valid.


Copyright 2002 CARTAPANIS Alexandre
This page was last updated on May 10 2003