net.directj.csf.impl
Class ClientHandlerImpl

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

public class ClientHandlerImpl
extends java.lang.Object
implements ClientHandler

This class is in charge of a client connection. It allow the user to do answer. This implementation is VERY simple, and must not be used as is.

Version:
0.1.2
Author:
CARTAPANIS Alexandre <earendil@jprolog.net>
To do:
Modify the run młethod for testing socket each timeout exception.

Field Summary
private  java.lang.String hostname
          The client's hostname.
private  java.io.BufferedReader in
          The input reader.
private  java.lang.String ip
          The client's IP.
private  java.io.PrintWriter out
          The output writer.
private  java.lang.Thread runner
          The handler thread.
private  boolean running
          Indicates if the handler is running or not.
private  Server server
          The server that stored the handler.
private  java.net.Socket sock
          The socket.
 
Constructor Summary
ClientHandlerImpl(java.net.Socket _sock, Server _server)
          Creates a ClientHandler implementation.
ClientHandlerImpl(java.net.Socket _sock, Server _server, int _timeout)
          Creates a ClientHandler implementation
 
Method Summary
 void doRequest(byte[] _data)
          Send an request to the client.
 java.lang.String getClientHostAddress()
          Returns the client's IP.
 java.lang.String getClientHostName()
          Returns the client's hostname.
 boolean isRunning()
          Indicates if the handler is running or not.
 void onRequest(byte[] _data)
          Called when the handler receive a request from the client.
 void run()
          Runs the handler.
 void startHandler()
          Starts the handler.
 void stopHandler()
          Stops the handler.
 java.lang.String toString()
          Returns a string representation of this ClientHandler.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

server

private Server server
The server that stored the handler.


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.


ip

private java.lang.String ip
The client's IP.


hostname

private java.lang.String hostname
The client's hostname.


sock

private java.net.Socket sock
The socket.

Constructor Detail

ClientHandlerImpl

public ClientHandlerImpl(java.net.Socket _sock,
                         Server _server)
Creates a ClientHandler implementation. The timeout is 2000 ms.

Parameters:
_sock - The socket on wich the handler is attached.
_server - The server that is in charge of the handler.

ClientHandlerImpl

public ClientHandlerImpl(java.net.Socket _sock,
                         Server _server,
                         int _timeout)
Creates a ClientHandler implementation

Parameters:
_sock - The socket on wich the handler is attached.
_server - The server that is in charge of the handler.
_timeout - The socket timeout, in millisecond.
Method Detail

onRequest

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

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

doRequest

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

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

startHandler

public void startHandler()
Starts the handler.

Specified by:
startHandler in interface ClientHandler

stopHandler

public void stopHandler()
Stops the handler.

Specified by:
stopHandler in interface ClientHandler

run

public void run()
Runs the handler.

Specified by:
run in interface java.lang.Runnable

isRunning

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

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

getClientHostAddress

public java.lang.String getClientHostAddress()
Returns the client's IP.

Specified by:
getClientHostAddress in interface ClientHandler
Returns:
String - The client's IP, as a string.

getClientHostName

public java.lang.String getClientHostName()
Returns the client's hostname.

Specified by:
getClientHostName in interface ClientHandler
Returns:
String - The client's hostname.

toString

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

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


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