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

java.lang.Object
  extended by com.vodafone360.people.service.transport.tcp.TcpConnectionThread
All Implemented Interfaces:
IConnection, IQueueListener, java.lang.Runnable

public class TcpConnectionThread
extends java.lang.Object
implements java.lang.Runnable, IConnection


Field Summary
private static int BYTE_ARRAY_OUTPUT_STREAM_SIZE
           
private static int CONNECTION_RESTART_INTERVAL
          If we have a connection error we try to restart after 60 seconds earliest
private static int ERROR_RETRY_INTERVAL
           
private static int MAX_NUMBER_RETRIES
          The maximum number of retries to reestablish a connection until we sleep until the user uses the UI or Settings.TCP_RETRY_BROKEN_CONNECTION_INTERVAL calls another retry.
private  java.io.ByteArrayOutputStream mBaos
           
private  boolean mConnectionShouldBeRunning
           
private  DecoderThread mDecoder
           
private  boolean mDidCriticalErrorOccur
           
private  HeartbeatSenderThread mHeartbeatSender
           
private  java.io.InputStream mIs
           
private  long mLastErrorRetryTime
           
private  long mLastErrorTimestamp
           
private  java.io.OutputStream mOs
           
private  ResponseReaderThread mResponseReader
           
private  int mRpgTcpPort
           
private  java.lang.String mRpgTcpUrl
           
private  RemoteService mService
           
private  java.net.Socket mSocket
           
private  java.lang.Thread mThread
           
private static int RPG_DEFAULT_TCP_PORT
           
private static java.lang.String RPG_FALLBACK_TCP_URL
           
private static int TCP_DEFAULT_TIMEOUT
           
 
Constructor Summary
TcpConnectionThread(DecoderThread decoder, RemoteService service)
           
 
Method Summary
 boolean getIsConnected()
          Returns true if the current connection thread is connected.
 boolean getIsRpgConnectionActive()
          Returns true if we have an open RPG connection or false if we do not have one.
private  void haltAndRetryConnection(int numberOfRetries)
           Retries to establish a network connection after a network error has occurred or the coverage of the network was lost.
private  void invalidateRequests()
          Invalidates all the requests so that the engines can either resend or post an error message for the user.
 void notifyOfItemInRequestQueue()
          Notifies the implementation that a new request is available in the queue and should trigger its delivery to the backend.
protected  void notifyOfNetworkProblems()
          Called back by the response reader, which should notice network problems first
 void notifyOfRegainedNetworkCoverage()
          Called whenever the network coverage has been reestablished...
 void notifyOfUiActivity()
          Gets notified whenever the user is using the UI.
 void onLoginStateChanged(boolean isLoggedIn)
          Triggered by the ConnectionManager whenever the login engine has detected a change in the currently held session.
private  void reconnectSocket()
          Attempts to reconnect the socket if it has been closed for some reason.
 void run()
           
private  void startHelperThreads()
          Starts the helper threads in order to be able to read responses and send heartbeats and passes them the needed input and output streams.
 void startThread()
          Starts the main connection thread.
private  void stopConnection()
          Stops the connection and its underlying socket implementation.
private  void stopHelperThreads()
          Stops the helper threads and closes the input and output streams.
 void stopThread()
          Stops the current connection thread. this should also stop any running sub-connection threads such as heartbeats or polls.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RPG_FALLBACK_TCP_URL

private static final java.lang.String RPG_FALLBACK_TCP_URL
See Also:
Constant Field Values

RPG_DEFAULT_TCP_PORT

private static final int RPG_DEFAULT_TCP_PORT
See Also:
Constant Field Values

TCP_DEFAULT_TIMEOUT

private static final int TCP_DEFAULT_TIMEOUT
See Also:
Constant Field Values

ERROR_RETRY_INTERVAL

private static final int ERROR_RETRY_INTERVAL
See Also:
Constant Field Values

CONNECTION_RESTART_INTERVAL

private static final int CONNECTION_RESTART_INTERVAL
If we have a connection error we try to restart after 60 seconds earliest

See Also:
Constant Field Values

MAX_NUMBER_RETRIES

private static final int MAX_NUMBER_RETRIES
The maximum number of retries to reestablish a connection until we sleep until the user uses the UI or Settings.TCP_RETRY_BROKEN_CONNECTION_INTERVAL calls another retry.

See Also:
Constant Field Values

BYTE_ARRAY_OUTPUT_STREAM_SIZE

private static final int BYTE_ARRAY_OUTPUT_STREAM_SIZE
See Also:
Constant Field Values

mThread

private java.lang.Thread mThread

mService

private RemoteService mService

mDecoder

private DecoderThread mDecoder

mConnectionShouldBeRunning

private boolean mConnectionShouldBeRunning

mDidCriticalErrorOccur

private boolean mDidCriticalErrorOccur

mIs

private java.io.InputStream mIs

mOs

private java.io.OutputStream mOs

mRpgTcpUrl

private java.lang.String mRpgTcpUrl

mRpgTcpPort

private int mRpgTcpPort

mSocket

private java.net.Socket mSocket

mHeartbeatSender

private HeartbeatSenderThread mHeartbeatSender

mResponseReader

private ResponseReaderThread mResponseReader

mLastErrorRetryTime

private long mLastErrorRetryTime

mLastErrorTimestamp

private long mLastErrorTimestamp

mBaos

private java.io.ByteArrayOutputStream mBaos
Constructor Detail

TcpConnectionThread

public TcpConnectionThread(DecoderThread decoder,
                           RemoteService service)
Method Detail

run

public void run()
Specified by:
run in interface java.lang.Runnable

notifyOfUiActivity

public void notifyOfUiActivity()
Gets notified whenever the user is using the UI. In this special case whenever an error occured with the connection and this method was not called a short time before

Specified by:
notifyOfUiActivity in interface IConnection

notifyOfRegainedNetworkCoverage

public void notifyOfRegainedNetworkCoverage()
Description copied from interface: IConnection
Called whenever the network coverage has been reestablished...

Specified by:
notifyOfRegainedNetworkCoverage in interface IConnection

notifyOfNetworkProblems

protected void notifyOfNetworkProblems()
Called back by the response reader, which should notice network problems first


reconnectSocket

private void reconnectSocket()
                      throws java.io.IOException
Attempts to reconnect the socket if it has been closed for some reason.

Throws:
java.io.IOException - Thrown if something goes wrong while reconnecting the socket.

haltAndRetryConnection

private void haltAndRetryConnection(int numberOfRetries)

Retries to establish a network connection after a network error has occurred or the coverage of the network was lost. The amount of retries depends on MAX_NUMBER_RETRIES.

A new retry is carried out each time an exception is thrown until the limit of retries has been reached.

Parameters:
numberOfRetries - The amount of retries carried out until the connection is given up.

invalidateRequests

private void invalidateRequests()
Invalidates all the requests so that the engines can either resend or post an error message for the user.


startThread

public void startThread()
Description copied from interface: IConnection
Starts the main connection thread.

Specified by:
startThread in interface IConnection

stopThread

public void stopThread()
Description copied from interface: IConnection
Stops the current connection thread. this should also stop any running sub-connection threads such as heartbeats or polls.

Specified by:
stopThread in interface IConnection

startHelperThreads

private void startHelperThreads()
Starts the helper threads in order to be able to read responses and send heartbeats and passes them the needed input and output streams.


stopHelperThreads

private void stopHelperThreads()
Stops the helper threads and closes the input and output streams. As the response reader is at this point in time probably in a blocking read()-state an IOException will need to be caught.


stopConnection

private void stopConnection()
Stops the connection and its underlying socket implementation. Keeps the thread running to allow further logins from the user.


notifyOfItemInRequestQueue

public void notifyOfItemInRequestQueue()
Description copied from interface: IQueueListener
Notifies the implementation that a new request is available in the queue and should trigger its delivery to the backend.

Specified by:
notifyOfItemInRequestQueue in interface IQueueListener

getIsConnected

public boolean getIsConnected()
Description copied from interface: IConnection
Returns true if the current connection thread is connected.

Specified by:
getIsConnected in interface IConnection
Returns:
True if the connection thread is connected to the backend.

getIsRpgConnectionActive

public boolean getIsRpgConnectionActive()
Description copied from interface: IConnection
Returns true if we have an open RPG connection or false if we do not have one.

Specified by:
getIsRpgConnectionActive in interface IConnection
Returns:
True if the RPG connection is currently active or false otherwise.

onLoginStateChanged

public void onLoginStateChanged(boolean isLoggedIn)
Description copied from interface: IConnection
Triggered by the ConnectionManager whenever the login engine has detected a change in the currently held session. If the user has signed off for example, false will be passed as a parameter.

Specified by:
onLoginStateChanged in interface IConnection
Parameters:
isLoggedIn - True if the user was just logged in, false if he was logged out.


JavaDoc