net.directj.csf.impl
Class ClientImpl

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

public class ClientImpl
extends java.lang.Object
implements Client

This a simple interface to implements a network client. It is a quite simple class, but it is powerfull enough to realize any networking application.

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

Nested Class Summary
private  class ClientImpl.ShutdownHook
          This class is used to stop the client at the end of the JVM.
 
Field Summary
private  java.net.InetAddress address
          The server's address.
private  java.io.BufferedReader in
          The input reader.
private  java.util.Vector listeners
          The list of listeners.
private  java.io.PrintWriter out
          The output writer.
private  int port
          The server's port.
private  java.lang.Thread runner
          The handler thread.
private  boolean running
          Indicates if the handler is running or not.
private  java.net.Socket sock
          The socket.
private  javax.net.SocketFactory socketFactory
          The factory that will be used to create the socket.
 
Constructor Summary
ClientImpl(int _port, java.net.InetAddress _address)
          Creates a new client implementation.
 
Method Summary
 void addClientListener(ClientListener _listener)
          Adds a client listener to this class.
 void doRequest(byte[] _data)
          Send an request to the server.
 void fireEvent(ClientEvent _evt)
          Send a client event to the listeners
 java.net.InetAddress getInetAddress()
          Returns the address of the server this client is bounded to.
 java.lang.String getInfo()
          Returns the client's informations (port, address, version...).
 int getPort()
          Returns the port on which this client is bounded to.
 java.lang.String getStatus()
          Returns the client's status (state, ...).
 boolean isRunning()
          Indicates if the client is running or not.
 void onRequest(byte[] _data)
          Called when the client receive a request from the server.
 void removeAllClientListener()
          Removes all the listeners associated with this class.
 void removeClientListener(ClientListener _listener)
          Removes a specified client listener from the list.
 void run()
          Runs the client.
 void setInetAddress(java.net.InetAddress _address)
          Changes the server's address.
 void setPort(int _port)
          Changes the client's port.
 void setSocketFactory(javax.net.SocketFactory _factory)
          Sets the SocketFactory.
 void startClient()
          Starts the client.
 void stopClient()
          Stops the client.
 java.lang.String toString()
          Returns a string representation of this client.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

in

private java.io.BufferedReader in
The input reader.


out

private java.io.PrintWriter out
The output writer.


running

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


runner

private java.lang.Thread runner
The handler thread.


port

private int port
The server's port.


address

private java.net.InetAddress address
The server's address.


sock

private java.net.Socket sock
The socket.


listeners

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


socketFactory

private javax.net.SocketFactory socketFactory
The factory that will be used to create the socket.

Constructor Detail

ClientImpl

public ClientImpl(int _port,
                  java.net.InetAddress _address)
Creates a new client implementation.

Parameters:
_port - The port on wich the client will bind to (on wich the server is listening).
_address - The address on wich the client will bind to (on wich the server is listening).
Method Detail

onRequest

public void onRequest(byte[] _data)
               throws java.lang.IllegalStateException,
                      InvalidRequestException
Called when the client receive a request from the server. In fact, this must fire a ClientEvent and use the client's fireEvent method.

Specified by:
onRequest in interface Client
Parameters:
_data - The data received from the server.
Throws:
java.lang.IllegalStateException - If the client is not running.
InvalidRequestException - If the request received from the server is not valid.
See Also:
Client.fireEvent(ClientEvent)

doRequest

public void doRequest(byte[] _data)
               throws java.lang.IllegalStateException,
                      InvalidRequestException
Send an request to the server.

Specified by:
doRequest in interface Client
Parameters:
_data - The answer to send to the server.
Throws:
java.lang.IllegalStateException - If the client is not running.
InvalidRequestException - If the request received from the server is not valid.

startClient

public void startClient()
Starts the client.

Specified by:
startClient in interface Client

stopClient

public void stopClient()
Stops the client.

Specified by:
stopClient in interface Client

isRunning

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

Specified by:
isRunning in interface Client
Returns:
boolean - True if the client is running, false otherwise.

run

public void run()
Runs the client.

Specified by:
run in interface java.lang.Runnable

setSocketFactory

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

Specified by:
setSocketFactory in interface Client
Parameters:
_factory - The socket factory.
Throws:
java.lang.IllegalStateException - If the client is running.
java.lang.IllegalArgumentException - If the SocketFactory is not valid or has not been initialized.

addClientListener

public void addClientListener(ClientListener _listener)
Adds a client listener to this class.

Specified by:
addClientListener in interface Client
Parameters:
_listener - The Listener to add.

removeClientListener

public void removeClientListener(ClientListener _listener)
Removes a specified client listener from the list.

Specified by:
removeClientListener in interface Client
Parameters:
_listener - The Listener to remove.

removeAllClientListener

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

Specified by:
removeAllClientListener in interface Client

fireEvent

public void fireEvent(ClientEvent _evt)
Send a client event to the listeners

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

getInfo

public java.lang.String getInfo()
Returns the client's informations (port, address, version...).

Specified by:
getInfo in interface Client
Returns:
String - The client's informations.

getStatus

public java.lang.String getStatus()
Returns the client's status (state, ...).

Specified by:
getStatus in interface Client
Returns:
String - The client's status.

toString

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

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

getPort

public int getPort()
Returns the port on which this client is bounded to.

Specified by:
getPort in interface Client
Returns:
int - The port number to which this client is bounded to.

setPort

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

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

getInetAddress

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

Specified by:
getInetAddress in interface Client
Returns:
InetAddress - A InetAddress representing the address of the server this client is bouded to.

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 client is running.

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


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