Search in sources :

Example 1 with BoxIteratorRealTimeServers

use of com.box.androidsdk.content.models.BoxIteratorRealTimeServers in project box-android-sdk by box.

the class RealTimeServerConnection method connect.

/**
     * Returns a message once a change has been detected or error occurs. Otherwise this method will continue reconnecting.
     * @return A BoxSimpleMessage with a simple message indicating a change in the user's account.
     */
public BoxSimpleMessage connect() {
    mRetries = 0;
    try {
        BoxIteratorRealTimeServers servers = (BoxIteratorRealTimeServers) mRequest.send();
        mBoxRealTimeServer = servers.get(0);
    } catch (BoxException e) {
        mChangeListener.onException(e, this);
        return null;
    }
    BoxRequestsEvent.LongPollMessageRequest messageRequest = new BoxRequestsEvent.LongPollMessageRequest(mBoxRealTimeServer.getUrl(), mSession);
    messageRequest.setTimeOut(mBoxRealTimeServer.getFieldRetryTimeout().intValue() * 1000);
    boolean shouldRetry = true;
    do {
        BoxFutureTask<BoxSimpleMessage> task = null;
        try {
            task = messageRequest.toTask().addOnCompletedListener(this);
            mExecutor.submit(task);
            BoxResponse<BoxSimpleMessage> response = task.get(mBoxRealTimeServer.getFieldRetryTimeout().intValue(), TimeUnit.SECONDS);
            if (response.isSuccess() && !response.getResult().getMessage().equals(BoxSimpleMessage.MESSAGE_RECONNECT)) {
                return response.getResult();
            }
        } catch (TimeoutException e) {
            if (task != null) {
                try {
                    // if the timeout is coming from the task then cancel the task (as httpurlconnection timeout is unreliable).
                    task.cancel(true);
                } catch (CancellationException e1) {
                }
            }
        } catch (InterruptedException e) {
            mChangeListener.onException(e, this);
        } catch (ExecutionException e) {
            mChangeListener.onException(e, this);
        }
        mRetries++;
        if (mBoxRealTimeServer.getMaxRetries() < mRetries) {
            shouldRetry = false;
        }
    } while (shouldRetry);
    mChangeListener.onException(new BoxException.MaxAttemptsExceeded("Max retries exceeded, ", mRetries), this);
    return null;
}
Also used : BoxException(com.box.androidsdk.content.BoxException) BoxRequestsEvent(com.box.androidsdk.content.requests.BoxRequestsEvent) BoxIteratorRealTimeServers(com.box.androidsdk.content.models.BoxIteratorRealTimeServers) BoxSimpleMessage(com.box.androidsdk.content.models.BoxSimpleMessage) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) SocketTimeoutException(java.net.SocketTimeoutException)

Aggregations

BoxException (com.box.androidsdk.content.BoxException)1 BoxIteratorRealTimeServers (com.box.androidsdk.content.models.BoxIteratorRealTimeServers)1 BoxSimpleMessage (com.box.androidsdk.content.models.BoxSimpleMessage)1 BoxRequestsEvent (com.box.androidsdk.content.requests.BoxRequestsEvent)1 SocketTimeoutException (java.net.SocketTimeoutException)1 CancellationException (java.util.concurrent.CancellationException)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1