use of com.box.androidsdk.content.models.BoxSimpleMessage 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;
}
Aggregations