com.vodafone360.people.service.transport.http
Class HttpConnectionThread

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

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

HTTP connection thread handles issuing of RPG requests over HTTP to server.


Field Summary
private  DecoderThread mDecoder
           
private  HttpClient mHttpClient
           
private  boolean mIsConnectionRunning
           
private  boolean mIsFirstTimePoll
           
private  boolean mIsPolling
           
private  PollThread mPollThread
           
private  int mRetryCount
           
private  java.net.URI mRpgUrl
           
private  java.lang.Object mRunLock
           
private  java.lang.Object mSendLock
           
private  java.lang.Thread mThread
           
 
Constructor Summary
HttpConnectionThread(DecoderThread decoder)
           
 
Method Summary
 void addErrorToResponseQueue(java.util.List<java.lang.Integer> reqIds)
          Adds errors to the response queue whenever there is an HTTP error on the backend.
private  void addToDecoder(byte[] input, java.util.List<java.lang.Integer> reqIds)
          Adds a response to the response decoder.
private  void finishResponse(HttpResponse response)
          Finishes reading the response in order to unblock the current connection.
 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.
 void handleApiResponse(HttpResponse response, java.util.List<java.lang.Integer> reqIds)
          Handles the synchronous responses for the authentication calls which go against the API directly by adding it to the queue and checking if the response code was a HTTP 200.
private  boolean handleRpgResponse(HttpResponse response, java.util.List<java.lang.Integer> reqIds)
          Checks if the response to an RPG request was fired off correctly.
static void logD(java.lang.String tag, java.lang.String message)
           
static void logE(java.lang.String tag, java.lang.String message, java.lang.Throwable error)
           
static void logI(java.lang.String tag, java.lang.String message)
           
static void logV(java.lang.String tag, java.lang.String message)
           
static void logW(java.lang.String tag, java.lang.String message)
           
 void notifyOfItemInRequestQueue()
          Kicks the request queue as soon as there are more requests on the queue.
 void notifyOfRegainedNetworkCoverage()
          Called whenever the device regains network coverage.
 void notifyOfUiActivity()
          If the UI is currently being used by the user this method gets called.
 void onLoginStateChanged(boolean isLoggedIn)
          Triggered by the ConnectionManager whenever the login engine has detected a change in the currently held session.
 HttpResponse postHTTPRequest(byte[] postData, java.net.URI uri, java.lang.String contentType)
          Posts the serialized data to the RPG and synchronously grabs the response.
private  byte[] prepareRPGRequests(java.util.List<Request> requests, java.util.List<java.lang.Integer> reqIds)
          Takes all requests objects and writes its serialized data to a byte array for further posting to the RPG.
 void run()
           Sends out synchronous requests (for authentication) to the API and asynchronous calls to the RPG as soon as there are requests on the request queue.
 void setHttpClient()
          Sets HTTP settings.
private  void startPollThread()
          This method is called when log-in is detected.
 void startThread()
          Starts the RPG connection as a thread and also launches the polling thread.
private  void stopPollThread()
          Stops the polling thread.
 void stopThread()
          Stops the current thread.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mThread

private java.lang.Thread mThread

mIsConnectionRunning

private volatile boolean mIsConnectionRunning

mPollThread

private PollThread mPollThread

mDecoder

private DecoderThread mDecoder

mHttpClient

private HttpClient mHttpClient

mRetryCount

private int mRetryCount

mRpgUrl

private java.net.URI mRpgUrl

mIsPolling

private boolean mIsPolling

mIsFirstTimePoll

private boolean mIsFirstTimePoll

mSendLock

private final java.lang.Object mSendLock

mRunLock

private final java.lang.Object mRunLock
Constructor Detail

HttpConnectionThread

public HttpConnectionThread(DecoderThread decoder)
Method Detail

startThread

public void startThread()
Starts the RPG connection as a thread and also launches the polling thread.

Specified by:
startThread in interface IConnection

setHttpClient

public void setHttpClient()
Sets HTTP settings.


stopThread

public void stopThread()
Stops the current thread.

Specified by:
stopThread in interface IConnection

run

public void run()

Sends out synchronous requests (for authentication) to the API and asynchronous calls to the RPG as soon as there are requests on the request queue.

If there are no requests the thread is set to wait().

Specified by:
run in interface java.lang.Runnable

prepareRPGRequests

private byte[] prepareRPGRequests(java.util.List<Request> requests,
                                  java.util.List<java.lang.Integer> reqIds)
Takes all requests objects and writes its serialized data to a byte array for further posting to the RPG.

Parameters:
requests - A list of requests to serialize.
reqIds -
Returns:
The serialized requests with RPG headers. Returns NULL id list of requests is NULL.

postHTTPRequest

public HttpResponse postHTTPRequest(byte[] postData,
                                    java.net.URI uri,
                                    java.lang.String contentType)
                             throws java.lang.Exception
Posts the serialized data to the RPG and synchronously grabs the response.

Parameters:
postData - The post data to send to the RPG.
uri - The URL to send the request to.
contentType - The content type to send as, usually "application/binary)
Returns:
The response data as a byte array.
Throws:
An - exception if the request went wrong after HTTP_MAX_RETRY_COUNT retries.
java.lang.Exception

handleRpgResponse

private boolean handleRpgResponse(HttpResponse response,
                                  java.util.List<java.lang.Integer> reqIds)
                           throws java.lang.Exception
Checks if the response to an RPG request was fired off correctly. Basically this method only checks whether the response is returned under a HTTP 200 status.

Parameters:
response - The response to check for.
reqIds - The request IDs for the response.
Returns:
True if the RPG response returned correctly.
Throws:
java.lang.Exception - Thrown if the response was null or the status line could not be fetched.

finishResponse

private void finishResponse(HttpResponse response)
Finishes reading the response in order to unblock the current connection.

Parameters:
response - The response to finish reading on.

handleApiResponse

public void handleApiResponse(HttpResponse response,
                              java.util.List<java.lang.Integer> reqIds)
                       throws java.lang.Exception
Handles the synchronous responses for the authentication calls which go against the API directly by adding it to the queue and checking if the response code was a HTTP 200. TODO: this should be refactored into a AuthenticationManager class.

Parameters:
response - The response to add to the decoder.
reqIds - The request IDs the response is to be decoded for.
Throws:
java.lang.Exception - Thrown if the status line could not be read or the response is null.

addToDecoder

private void addToDecoder(byte[] input,
                          java.util.List<java.lang.Integer> reqIds)
Adds a response to the response decoder.

Parameters:
input - The data of the response.
reqIds - The request IDs that a response was received for.

addErrorToResponseQueue

public void addErrorToResponseQueue(java.util.List<java.lang.Integer> reqIds)
Adds errors to the response queue whenever there is an HTTP error on the backend.

Parameters:
reqIds - The request IDs the error happened for.

notifyOfItemInRequestQueue

public void notifyOfItemInRequestQueue()
Kicks the request queue as soon as there are more requests on the queue.

Specified by:
notifyOfItemInRequestQueue in interface IQueueListener

notifyOfRegainedNetworkCoverage

public void notifyOfRegainedNetworkCoverage()
Called whenever the device regains network coverage. It will kick the request queue to see if there are more requests to send.

Specified by:
notifyOfRegainedNetworkCoverage in interface IConnection

startPollThread

private void startPollThread()
This method is called when log-in is detected. Polling should not normally start before we are logged in as the backend needs a correct user id.


stopPollThread

private void stopPollThread()
Stops the polling thread. This method is implemented from the IQueueListener-interface. It is called when log-out of the account is detected but a connection is still ongoing. In this case polling needs to stop.


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.

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.

logE

public static void logE(java.lang.String tag,
                        java.lang.String message,
                        java.lang.Throwable error)

logW

public static void logW(java.lang.String tag,
                        java.lang.String message)

logI

public static void logI(java.lang.String tag,
                        java.lang.String message)

logD

public static void logD(java.lang.String tag,
                        java.lang.String message)

logV

public static void logV(java.lang.String tag,
                        java.lang.String message)

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.

notifyOfUiActivity

public void notifyOfUiActivity()
Description copied from interface: IConnection
If the UI is currently being used by the user this method gets called.

Specified by:
notifyOfUiActivity in interface IConnection


JavaDoc