ocsf.server
Class ConnectionToClient

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--ocsf.server.ConnectionToClient

public class ConnectionToClient
extends java.lang.Thread

An instance of this class is created by the server when a client connects. It accepts messages coming from the client and is responsible for sending data to the client since the socket is private to this class. The AbstractServer contains a set of instances of this class and is responsible for adding and deleting them.

Project Name: OCSF (Object Client-Server Framework)


Field Summary
private  java.net.Socket clientSocket
          Sockets are used in the operating system as channels of communication between two processes.
private  java.io.ObjectInputStream input
          Stream used to read from the client.
private  java.io.ObjectOutputStream output
          Stream used to write to the client.
private  boolean readyToStop
          Indicates if the thread is ready to stop.
private  java.util.HashMap savedInfo
          Map to save information about the client such as its login ID.
private  AbstractServer server
          A reference to the Server that created this instance.
 
Fields inherited from class java.lang.Thread
contextClassLoader, daemon, eetop, group, inheritedAccessControlContext, MAX_PRIORITY, MIN_PRIORITY, name, NORM_PRIORITY, priority, single_step, stillborn, stopThreadPermission, target, threadInitNumber, threadQ, values
 
Constructor Summary
(package private) ConnectionToClient(java.lang.ThreadGroup group, java.net.Socket clientSocket, AbstractServer server)
          Constructs a new connection to a client.
 
Method Summary
 void close()
          Closes the client.
private  void closeAll()
          Closes all connection to the server.
protected  void finalize()
          This method is called by garbage collection.
 java.net.InetAddress getInetAddress()
          Returns the address of the client.
 java.lang.Object getInfo(java.lang.String infoType)
          Returns information about the client saved using setInfo.
 void run()
          Constantly reads the client's input stream.
 void sendToClient(java.lang.Object msg)
          Sends an object to the client.
 void setInfo(java.lang.String infoType, java.lang.Object info)
          Saves arbitrary information about this client.
 java.lang.String toString()
          Returns a string representation of the client.
 
Methods inherited from class java.lang.Thread
, activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, exit, getContextClassLoader, getName, getPriority, getThreadGroup, init, interrupt, interrupt0, interrupted, isAlive, isDaemon, isInterrupted, isInterrupted, join, join, join, nextThreadNum, registerNatives, resume, resume0, setContextClassLoader, setDaemon, setName, setPriority, setPriority0, sleep, sleep, start, stop, stop, stop0, suspend, suspend0, yield
 
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

server

private AbstractServer server
A reference to the Server that created this instance.

clientSocket

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

input

private java.io.ObjectInputStream input
Stream used to read from the client.

output

private java.io.ObjectOutputStream output
Stream used to write to the client.

readyToStop

private boolean readyToStop
Indicates if the thread is ready to stop. Set to true when closing of the connection is initiated.

savedInfo

private java.util.HashMap savedInfo
Map to save information about the client such as its login ID. The initial size of the map is small since it is not expected that concrete servers will want to store many different types of information about each client. Used by the setInfo and getInfo methods.
Constructor Detail

ConnectionToClient

ConnectionToClient(java.lang.ThreadGroup group,
                   java.net.Socket clientSocket,
                   AbstractServer server)
             throws java.io.IOException
Constructs a new connection to a client.
Parameters:
group - the thread group that contains the connections.
clientSocket - contains the client's socket.
server - a reference to the server that created this instance
Throws:
java.io.IOException - if an I/O error occur when creating the connection.
Method Detail

sendToClient

public final void sendToClient(java.lang.Object msg)
                        throws java.io.IOException
Sends an object to the client.
Parameters:
msg - the message to be sent.
Throws:
java.io.IOException - if an I/O error occur when sending the message.

close

public final void close()
                 throws java.io.IOException
Closes the client. If the connection is already closed, this call has no effect.
Throws:
java.io.IOException - if an error occurs when closing the socket.

getInetAddress

public final java.net.InetAddress getInetAddress()
Returns the address of the client.
Returns:
the client's Internet address.

toString

public java.lang.String toString()
Returns a string representation of the client.
Returns:
the client's description.
Overrides:
toString in class java.lang.Thread

setInfo

public void setInfo(java.lang.String infoType,
                    java.lang.Object info)
Saves arbitrary information about this client. Designed to be used by concrete subclasses of AbstractServer. Based on a hash map.
Parameters:
infoType - identifies the type of information
info - the information itself.

getInfo

public java.lang.Object getInfo(java.lang.String infoType)
Returns information about the client saved using setInfo. Based on a hash map.
Parameters:
infoType - identifies the type of information

run

public final void run()
Constantly reads the client's input stream. Sends all objects that are read to the server. Not to be called.
Overrides:
run in class java.lang.Thread

closeAll

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

finalize

protected void finalize()
This method is called by garbage collection.
Overrides:
finalize in class java.lang.Object