ocsf.client
Class AbstractClient

java.lang.Object
  |
  +--ocsf.client.AbstractClient
All Implemented Interfaces:
java.lang.Runnable

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)

* * @author Dr. Robert Laganière * @author Dr. Timothy C. Lethbridge * @author François Bél;langer * @author Paul Holden * @version February 2001 (2.12)


Constructor Summary
AbstractClient(java.lang.String host, int port)
          Constructs the client.
 
Method Summary
 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, toString, wait, wait, wait
 

Constructor Detail

AbstractClient

public AbstractClient(java.lang.String host,
                      int port)
Constructs the client. * * @param host the server's host name. * @param 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. * * @exception IOException if an I/O error occurs when opening.

java.io.IOException

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. * * @param msg The message to be sent. * @exception IOException if an I/O error occurs when sending

java.io.IOException

closeConnection

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

java.io.IOException

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(). * * @param 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(). * * @param host the host name.


getInetAddress

public final java.net.InetAddress getInetAddress()
returns the client's description. * * @return 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. * * @param 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. * * @param msg the message sent.