ocsf.client
Class AbstractClient

java.lang.Object
  |
  +--ocsf.client.AbstractClient
Direct Known Subclasses:
AdaptableClient, ChatClient, SimpleClient

public abstract class AbstractClient
extends java.lang.Object
implements java.lang.Runnable

The AbstractClient contains all the methods necessary to set up the client side of a client-server architecture. When a client is thus connected to the server, the two programs can then exchange Object instances.

Method handleMessageFromServer must be defined by a concrete subclass. Several other hook methods may also be overriden.

Several public service methods are provided to application that use this framework.

Project Name: OCSF (Object Client-Server Framework)


Field Summary
private  java.lang.Thread clientReader
          The thread created to read data from the server.
private  java.net.Socket clientSocket
          Sockets are used in the operating system as channels of communication between two processes.
private  java.lang.String host
          The server's host name.
private  java.io.ObjectInputStream input
          The stream to handle data from the server.
private  java.io.ObjectOutputStream output
          The stream to handle data going to the server.
private  int port
          The port number.
private  boolean readyToStop
          Indicates if the thread is ready to stop.
 
Constructor Summary
AbstractClient(java.lang.String host, int port)
          Constructs the client.
 
Method Summary
private  void closeAll()
          Closes all aspects of the connection to the server.
 void closeConnection()
          Closes the connection to the server.
protected  void connectionClosed()
          Hook method called after the connection has been closed.
protected  void connectionEstablished()
          Hook method called after a connection has been established.
protected  void connectionException(java.lang.Exception exception)
          Hook method called each time an exception is thrown by the client's thread that is waiting for messages from the server.
 java.lang.String getHost()
           
 java.net.InetAddress getInetAddress()
          returns the client's description.
 int getPort()
           
protected abstract  void handleMessageFromServer(java.lang.Object msg)
          Handles a message sent from the server to this client.
 boolean isConnected()
           
 void openConnection()
          Opens the connection with the server.
 void run()
          Waits for messages from the server.
 void sendToServer(java.lang.Object msg)
          Sends an object to the server.
 void setHost(java.lang.String host)
          Sets the server host for the next connection.
 void setPort(int port)
          Sets the server port number for the next connection.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

clientSocket

private java.net.Socket clientSocket
Sockets are used in the operating system as channels of communication between two processes.
See Also:
Socket

output

private java.io.ObjectOutputStream output
The stream to handle data going to the server.

input

private java.io.ObjectInputStream input
The stream to handle data from the server.

clientReader

private java.lang.Thread clientReader
The thread created to read data from the server.

readyToStop

private boolean readyToStop
Indicates if the thread is ready to stop. Needed so that the loop in the run method knows when to stop waiting for incoming messages.

host

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

port

private int port
The port number.
Constructor Detail

AbstractClient

public AbstractClient(java.lang.String host,
                      int port)
Constructs the client.
Parameters:
host - the server's host name.
port - the port number.
Method Detail

openConnection

public final void openConnection()
                          throws java.io.IOException
Opens the connection with the server. If the connection is already opened, this call has no effect.
Throws:
java.io.IOException - if an I/O error occurs when opening.

sendToServer

public final void sendToServer(java.lang.Object msg)
                        throws java.io.IOException
Sends an object to the server. This is the only way that methods should communicate with the server.
Parameters:
msg - The message to be sent.
Throws:
java.io.IOException - if an I/O error occurs when sending

closeConnection

public final void closeConnection()
                           throws java.io.IOException
Closes the connection to the server.
Throws:
java.io.IOException - if an I/O error occurs when closing.

isConnected

public final boolean isConnected()
Returns:
true if the client is connnected.

getPort

public final int getPort()
Returns:
the port number.

setPort

public final void setPort(int port)
Sets the server port number for the next connection. The change in port only takes effect at the time of the next call to openConnection().
Parameters:
port - the port number.

getHost

public final java.lang.String getHost()
Returns:
the host name.

setHost

public final void setHost(java.lang.String host)
Sets the server host for the next connection. The change in host only takes effect at the time of the next call to openConnection().
Parameters:
host - the host name.

getInetAddress

public final java.net.InetAddress getInetAddress()
returns the client's description.
Returns:
the client's Inet address.

run

public final void run()
Waits for messages from the server. When each arrives, a call is made to handleMessageFromServer(). Not to be explicitly called.
Specified by:
run in interface java.lang.Runnable

connectionClosed

protected void connectionClosed()
Hook method called after the connection has been closed. The default implementation does nothing. The method may be overriden by subclasses to perform special processing such as cleaning up and terminating, or attempting to reconnect.

connectionException

protected void connectionException(java.lang.Exception exception)
Hook method called each time an exception is thrown by the client's thread that is waiting for messages from the server. The method may be overridden by subclasses.
Parameters:
exception - the exception raised.

connectionEstablished

protected void connectionEstablished()
Hook method called after a connection has been established. The default implementation does nothing. It may be overridden by subclasses to do anything they wish.

handleMessageFromServer

protected abstract void handleMessageFromServer(java.lang.Object msg)
Handles a message sent from the server to this client. This MUST be implemented by subclasses, who should respond to messages.
Parameters:
msg - the message sent.

closeAll

private void closeAll()
               throws java.io.IOException
Closes all aspects of the connection to the server.
Throws:
java.io.IOException - if an I/O error occurs when closing.