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

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

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


Field Summary
protected static byte ACTIVE_MODE
           
protected static int DEFAULT_BATCHSIZE
           
private static byte IDLE_MODE
           
private static int LONG_POLLING_INTERVAL
           
private  int mBatchsize
           
private  int mBlankHeaderCount
           
private  DecoderThread mDecoder
           
private  boolean mHasErrorOccured
           
private  RpgHeader mHeader
           
private  HttpClient mHttpClient
           
private  boolean mIsConnectionRunning
           
private  byte mMode
           
private  java.lang.Object mPollLock
           
private  int mRetryCount
           
private  HttpConnectionThread mRpgRequesterThread
           
private  java.lang.Object mRunLock
           
private  java.net.URI mUrl
           
private static int NETWORK_NO_COVERAGE_RETRIES
           
private static long NETWORK_RETRY_INTERVAL
           
private static java.lang.String RPG_BATCH_SIZE_KEY
           
private static java.lang.String RPG_MODE_ACTIVE
           
private static java.lang.String RPG_MODE_KEY
           
private static java.lang.String RPG_POLLING_INTERVAL_KEY
           
protected static int SHORT_POLLING_INTERVAL
           
 
Constructor Summary
protected PollThread(HttpConnectionThread connThread)
           
 
Method Summary
private  void consumeResponse(HttpResponse response)
           
 boolean getHasCoverage()
          Returns true if the device has coverage and the servers are not down.
private  void handleResponse(HttpResponse response)
           Looks at the response and adds it to the necessary decoder.
protected  void invokePoll(int pollInterval, int batchSize, byte mode)
          Invokes a poll on the RPG with the passed arguments.
private  HttpResponse postHTTPRequest(byte[] postData, java.net.URI uri, java.lang.String contentType)
          Posts an HTTP request with data to a URL under a certain content type.
private  void retryConnection()
          Retries to connect to the backend (if in active mode) every NETWORK_RETRY_INTERVAL seconds.
 void run()
          Carries out an initial poll with a short interval to have the RPG set up the presence roosters then does poll after poll with the default polling interval to keep the connection alive.
private  byte[] serializeRPGPoll(int pollInterval, int batchSize, byte mode)
          Serializes a poll to hessian and includes the RPG header.
private  void setHttpClient()
           
protected  void startConnection(DecoderThread decoder)
          Starts the connection.
protected  void startRpgPolling()
          Starts the RPG polling in active mode.
protected  void stopConnection()
          Attempts to stop the connection.
private  void stopRpgPolling()
          We need to inform controller about the error here.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RPG_MODE_KEY

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

RPG_MODE_ACTIVE

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

RPG_BATCH_SIZE_KEY

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

RPG_POLLING_INTERVAL_KEY

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

ACTIVE_MODE

protected static final byte ACTIVE_MODE
See Also:
Constant Field Values

IDLE_MODE

private static final byte IDLE_MODE
See Also:
Constant Field Values

LONG_POLLING_INTERVAL

private static final int LONG_POLLING_INTERVAL
See Also:
Constant Field Values

SHORT_POLLING_INTERVAL

protected static final int SHORT_POLLING_INTERVAL
See Also:
Constant Field Values

DEFAULT_BATCHSIZE

protected static final int DEFAULT_BATCHSIZE
See Also:
Constant Field Values

NETWORK_NO_COVERAGE_RETRIES

private static final int NETWORK_NO_COVERAGE_RETRIES
See Also:
Constant Field Values

NETWORK_RETRY_INTERVAL

private static final long NETWORK_RETRY_INTERVAL
See Also:
Constant Field Values

mRpgRequesterThread

private HttpConnectionThread mRpgRequesterThread

mDecoder

private DecoderThread mDecoder

mIsConnectionRunning

private boolean mIsConnectionRunning

mHasErrorOccured

private boolean mHasErrorOccured

mBlankHeaderCount

private int mBlankHeaderCount

mRetryCount

private int mRetryCount

mMode

private byte mMode

mBatchsize

private int mBatchsize

mUrl

private java.net.URI mUrl

mHttpClient

private HttpClient mHttpClient

mHeader

private RpgHeader mHeader

mPollLock

private final java.lang.Object mPollLock

mRunLock

private final java.lang.Object mRunLock
Constructor Detail

PollThread

protected PollThread(HttpConnectionThread connThread)
Method Detail

startConnection

protected void startConnection(DecoderThread decoder)
Starts the connection.


setHttpClient

private void setHttpClient()

stopConnection

protected void stopConnection()
Attempts to stop the connection. This method might only take effect after a maximum of 30 seconds when the poll returns.


run

public void run()
Carries out an initial poll with a short interval to have the RPG set up the presence roosters then does poll after poll with the default polling interval to keep the connection alive.

Specified by:
run in interface java.lang.Runnable

invokePoll

protected void invokePoll(int pollInterval,
                          int batchSize,
                          byte mode)
                   throws java.lang.Exception
Invokes a poll on the RPG with the passed arguments.

Parameters:
pollInterval - The polling interval the server takes as an argument. LONG_POLLING_INTERVAL should be used for all normal polls, SHORT_POLLING_INTERVAL for the initial poll.
batchSize - The maximum batch size of the client. DEFAULT_BATCHSIZE should be used by default.
mode - The mode to use. ACTIVE_MODE and IDLE_MODE are available.
Throws:
java.lang.Exception

postHTTPRequest

private HttpResponse postHTTPRequest(byte[] postData,
                                     java.net.URI uri,
                                     java.lang.String contentType)
                              throws java.lang.Exception
Posts an HTTP request with data to a URL under a certain content type. The method will retry HTTP_MAX_RETRY_COUNT number of times until it throws an exception.

Parameters:
postData - A byte array representing the data to be posted.
uri - The URI to post the data to.
contentType - The content type to post under.
Returns:
The response of the server.
Throws:
java.lang.Exception - Thrown if the request failed for HTTP_MAX_RETRY_COUNT number of times.

handleResponse

private void handleResponse(HttpResponse response)
                     throws java.lang.Exception

Looks at the response and adds it to the necessary decoder.

TODO: this method should be worked on. The decoder should take care of deciding which methods are decoded in which way.

Parameters:
response - The server response to decode.
Throws:
java.lang.Exception - Thrown if the returned status line was null or if the response was null.

consumeResponse

private void consumeResponse(HttpResponse response)

serializeRPGPoll

private byte[] serializeRPGPoll(int pollInterval,
                                int batchSize,
                                byte mode)
                         throws java.io.IOException
Serializes a poll to hessian and includes the RPG header.

Parameters:
pollInterval - The polling interval to use. LONG_POLLING_INTERVAL or SHORT_POLLING_INTERVAL.
batchSize - The batch size to use on the RPG.
mode - The mode to use. Possible values: RPG_MODE_ACTIVE, RPG_MODE_IDLE
Returns:
The serialized hessian-data as a byte-array.
Throws:
java.io.IOException

stopRpgPolling

private void stopRpgPolling()
We need to inform controller about the error here. EngineManager should retry it's requests.


startRpgPolling

protected void startRpgPolling()
                        throws java.net.MalformedURLException,
                               java.net.URISyntaxException
Starts the RPG polling in active mode.

Throws:
java.net.MalformedURLException - Thrown if the URL is not in the correct format.
java.net.URISyntaxException - Thrown if the URL is not in the correct format.

retryConnection

private void retryConnection()
Retries to connect to the backend (if in active mode) every NETWORK_RETRY_INTERVAL seconds.


getHasCoverage

public boolean getHasCoverage()
Returns true if the device has coverage and the servers are not down.

Returns:
True if there is coverage.


JavaDoc