com.vodafone360.people.service.transport.tcp
Class ResponseReaderThread

java.lang.Object
  extended by com.vodafone360.people.service.transport.tcp.ResponseReaderThread
All Implemented Interfaces:
java.lang.Runnable

public class ResponseReaderThread
extends java.lang.Object
implements java.lang.Runnable

Reads responses from a TCP sockets and adds them to the response decoder. Errors are called and passed back to the RpgTcpConnectionThread.

Author:
Rudy Norff (rudy.norff@vodafone.com)

Field Summary
private  TcpConnectionThread mConnThread
          The main connection thread to call back when an error occured.
protected static ResponseReaderThread mCurrentThread
          The ResponseReaderThread that was created by the TcpConnectionThread.
private  DecoderThread mDecoder
          Decodes all responses coming in.
protected  java.io.DataInputStream mIs
          The input stream to read responses from.
protected  boolean mIsConnectionRunning
          Indicates that the thread is active and connected.
private  java.net.Socket mSocket
          The socket to read from.
protected  java.lang.Thread mThread
          Represents the thread that reads responses.
private static long THREAD_SLEEP_TIME
          Sleep time (in milliseconds) of the thread in between reads.
 
Constructor Summary
ResponseReaderThread(TcpConnectionThread connThread, DecoderThread decoder, java.net.Socket socket)
          Constructs a new response reader used for reading bytes from a socket connection.
 
Method Summary
private  void checkForDuplicateThreads()
          Checks whether the current thread is not the same as the thread that should be running.
private  byte[] readNextResponse()
           Attempts to read all the bytes from the DataInputStream and writes them to a byte array where they are processed for further use.
 void run()
          Overrides the Thread.run() method and continuously calls the readResponses() method which blocks and reads responses or throws an exception that needs to be handled by reconnecting the sockets.
 void setInputStream(java.io.InputStream inputStream)
          Sets the input stream that will be used to read responses from.
 void startConnection()
          Starts the connection by setting the connection flag and spawning a new thread in which responses can be read without interfering with the rest of the client.
 void stopConnection()
          Stops the connection by closing the input stream and setting it to null.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

THREAD_SLEEP_TIME

private static final long THREAD_SLEEP_TIME
Sleep time (in milliseconds) of the thread in between reads.

See Also:
Constant Field Values

mCurrentThread

protected static ResponseReaderThread mCurrentThread
The ResponseReaderThread that was created by the TcpConnectionThread. It will be compared to this thread. If they differ this thread must be a lock thread that got stuck when the user changed APNs or switched from WiFi to another data connection type.


mDecoder

private DecoderThread mDecoder
Decodes all responses coming in.


mConnThread

private TcpConnectionThread mConnThread
The main connection thread to call back when an error occured.


mIs

protected java.io.DataInputStream mIs
The input stream to read responses from.


mIsConnectionRunning

protected boolean mIsConnectionRunning
Indicates that the thread is active and connected.


mThread

protected java.lang.Thread mThread
Represents the thread that reads responses.


mSocket

private java.net.Socket mSocket
The socket to read from.

Constructor Detail

ResponseReaderThread

public ResponseReaderThread(TcpConnectionThread connThread,
                            DecoderThread decoder,
                            java.net.Socket socket)
Constructs a new response reader used for reading bytes from a socket connection.

Parameters:
connThread - The connection thread that manages this and the heartbeat sender thread. It is called back whenever an error occured reading from the socket input stream.
decoder - Used to decode all incoming responses.
Method Detail

startConnection

public void startConnection()
Starts the connection by setting the connection flag and spawning a new thread in which responses can be read without interfering with the rest of the client.


stopConnection

public void stopConnection()
Stops the connection by closing the input stream and setting it to null. Attempts to stop the thread and sets it to null.


setInputStream

public void setInputStream(java.io.InputStream inputStream)
Sets the input stream that will be used to read responses from.

Parameters:
inputStream - The input stream to read from. Should not be null as this would trigger a reconnect of the whole socket and cause all transport-threads to be restarted.

run

public void run()
Overrides the Thread.run() method and continuously calls the readResponses() method which blocks and reads responses or throws an exception that needs to be handled by reconnecting the sockets.

Specified by:
run in interface java.lang.Runnable

readNextResponse

private byte[] readNextResponse()
                         throws java.io.IOException,
                                java.io.EOFException

Attempts to read all the bytes from the DataInputStream and writes them to a byte array where they are processed for further use.

As this method uses InputStream.read() it blocks the execution until a byte has been read, the socket was closed (at which point an IOException will be thrown), or the end of the file has been reached (resulting in a EOFException being thrown).

Returns:
A byte-array if the response was read successfully or null if the response could not be read.
Throws:
java.io.IOException - Thrown if something went wrong trying to read or write a byte from or to a stream.
java.io.EOFException - Thrown if the end of the stream has been reached unexpectedly.

checkForDuplicateThreads

private void checkForDuplicateThreads()
Checks whether the current thread is not the same as the thread that should be running. If it is not the connection and the thread is stopped.



JavaDoc