dist1.acb.common.transport
Class AbstractClientSocket

java.lang.Object
  extended byjava.lang.Thread
      extended bydist1.acb.common.transport.AbstractClientSocket
All Implemented Interfaces:
java.lang.Runnable
Direct Known Subclasses:
AdminSocket, Backup, ClientSocket, DownloadFile

public abstract class AbstractClientSocket
extends java.lang.Thread

Abstract class for threaded model of client socket. The class manages outcoming requests.

Version:
$Revision: 1.44 $
Author:
Alexander Stasiv

Nested Class Summary
static class AbstractClientSocket.SocketAndStreams
          Container for three objects: Socket, ObjectOutputStream and ObjectInputStream
 
Field Summary
protected  boolean isShutdowned
           
protected  java.util.logging.Logger log
           
protected  ObjectStreamFactory objectStreamFactory
           
protected  java.net.Socket socket
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
AbstractClientSocket(java.lang.ThreadGroup group)
          Creates a new instance of AbstractClientSocket
AbstractClientSocket(java.lang.ThreadGroup group, boolean doNotConnectInParentThread)
          Creates a new AbstractClientSocket object.
 
Method Summary
 void connect()
          Connect to the next server
static AbstractClientSocket.SocketAndStreams connect(java.net.InetSocketAddress serverAddr, java.lang.String className, java.util.logging.Logger log)
          Create connected socket and related streams
static AbstractClientSocket.SocketAndStreams connect(java.net.InetSocketAddress serverAddr, java.lang.String className, java.util.logging.Logger log, ObjectStreamFactory osf)
          Create connected socket and related streams
 Response getResponse(Request request)
          Get respons to the request.
 java.net.InetSocketAddress getServerAddr()
          Getter for property serverAddr.
 java.util.LinkedList getServersAddresses()
          Getter for property serversAddresses.
protected abstract  void handleConnectionEstablished()
           
protected abstract  boolean handleConnectionLost()
           
protected  void handleResponseToNoRequest(Response response)
           
protected abstract  void handleUnrecoverableError(java.lang.Throwable e)
           
 boolean isConnected()
          Returns the connection state of the socket.
protected  Response receiveResponse()
          Private static function for receiving a response.
 Response request(Request request)
          Send the request and receive a response from the server in same thread.
static Response request(Request request, java.io.ObjectInputStream in, java.io.ObjectOutputStream out, java.util.logging.Logger log)
          Used by a modles which use direct exchange of messages
 void run()
          Main processing method for the ConnectionServer object
 void scheduleRequest(Request request)
          Schedules equest to the server.
 void setServersAddresses(java.util.LinkedList serversAddresses)
          Setter for property serversAddresses.
 void start()
           Initiate connection to the server and if succeed, causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

protected java.util.logging.Logger log

objectStreamFactory

protected ObjectStreamFactory objectStreamFactory

socket

protected java.net.Socket socket

isShutdowned

protected boolean isShutdowned
Constructor Detail

AbstractClientSocket

public AbstractClientSocket(java.lang.ThreadGroup group)
Creates a new instance of AbstractClientSocket

Parameters:
group - Thread group to which current thread will be related

AbstractClientSocket

public AbstractClientSocket(java.lang.ThreadGroup group,
                            boolean doNotConnectInParentThread)
Creates a new AbstractClientSocket object.

Parameters:
group - DOCUMENT ME!
doNotConnectInParentThread - DOCUMENT ME!
Method Detail

isConnected

public boolean isConnected()
Returns the connection state of the socket.

Returns:
true if the socket successfuly connected to a server

getResponse

public Response getResponse(Request request)
Get respons to the request. The method checks list of responses in order to find requered response. If found, delete the response from the list and return it.

Parameters:
request - A request to search for response
Returns:
A response to the request. NULL if no response received yet.

getServerAddr

public java.net.InetSocketAddress getServerAddr()
Getter for property serverAddr.

Returns:
Value of property serverAddr.

setServersAddresses

public void setServersAddresses(java.util.LinkedList serversAddresses)
                         throws java.lang.RuntimeException
Setter for property serversAddresses.

Parameters:
serversAddresses - New value of property serversAddresses.
Throws:
java.lang.RuntimeException - if contains less then 1 element

getServersAddresses

public java.util.LinkedList getServersAddresses()
Getter for property serversAddresses.

Returns:
Value of property serversAddresses.

connect

public static AbstractClientSocket.SocketAndStreams connect(java.net.InetSocketAddress serverAddr,
                                                            java.lang.String className,
                                                            java.util.logging.Logger log)
                                                     throws java.lang.ClassNotFoundException,
                                                            java.io.IOException
Create connected socket and related streams

Parameters:
serverAddr - Destination
className - Requester of the connection
log - A logger
Returns:
socket and related streams
Throws:
java.lang.ClassNotFoundException - if class with name className was not found
java.io.IOException - if some input-output exception occured

connect

public static AbstractClientSocket.SocketAndStreams connect(java.net.InetSocketAddress serverAddr,
                                                            java.lang.String className,
                                                            java.util.logging.Logger log,
                                                            ObjectStreamFactory osf)
                                                     throws java.lang.ClassNotFoundException,
                                                            java.io.IOException
Create connected socket and related streams

Parameters:
serverAddr - Destination
className - Requester of the connection
log - A logger
osf - ObjectStreamFactory for constructing streams
Returns:
socket and related streams
Throws:
java.lang.ClassNotFoundException - if class with name className was not found
java.io.IOException - if some input-output exception occured

connect

public void connect()
Connect to the next server


request

public Response request(Request request)
                 throws java.lang.InterruptedException,
                        java.lang.ClassNotFoundException,
                        java.io.IOException
Send the request and receive a response from the server in same thread. So the method is blocking. The method tries to hide some errors like link breaks or crashes of the server.

Parameters:
request - Request to send
Returns:
A response from a server
Throws:
java.lang.InterruptedException - The thread was interrupted.
java.lang.ClassNotFoundException - if received from the server class was not found
java.io.IOException - if handleConnectionLost() returns false

request

public static Response request(Request request,
                               java.io.ObjectInputStream in,
                               java.io.ObjectOutputStream out,
                               java.util.logging.Logger log)
                        throws java.lang.InterruptedException,
                               java.lang.ClassNotFoundException,
                               java.io.IOException,
                               java.lang.ClassCastException
Used by a modles which use direct exchange of messages

Parameters:
request - Request
in - Input stream from where to receive a Response
out - Output stream where to send a Request
log - A logger
Returns:
Received response
Throws:
java.lang.InterruptedException - Current thread was interrupted
java.lang.ClassNotFoundException - If we received a class that we haven't localy
java.io.IOException - An input exception
java.lang.ClassCastException - if received object can not be casted to Response

run

public void run()
Main processing method for the ConnectionServer object

Throws:
java.lang.InterruptedException - DOCUMENT ME!

scheduleRequest

public void scheduleRequest(Request request)
Schedules equest to the server. The method does not wait for Response from the server (non-blocking metod). Use getResponse() function to resive the response.

Parameters:
request - Request to send

start

public void start()
 Initiate connection to the server and if succeed, causes this thread to begin execution; the Java Virtual Machine calls the run method of this thread.


handleConnectionEstablished

protected abstract void handleConnectionEstablished()

handleConnectionLost

protected abstract boolean handleConnectionLost()

handleResponseToNoRequest

protected void handleResponseToNoRequest(Response response)

handleUnrecoverableError

protected abstract void handleUnrecoverableError(java.lang.Throwable e)

receiveResponse

protected Response receiveResponse()
                            throws java.lang.InterruptedException,
                                   java.io.IOException,
                                   java.lang.ClassNotFoundException
Private static function for receiving a response. Used by static request().

Returns:
Received response
Throws:
java.lang.InterruptedException - Current thread was interrupted
java.io.IOException - An input exception
java.lang.ClassNotFoundException - If we received a class that we haven't localy


Copyright © 2004 Phoenix Team (CMU/AIT). All Rights Reserved.