Commit 1ec0835c authored by Leonardo Aramaki's avatar Leonardo Aramaki

Do make connection attempts with exponential backoff retries on all cases

parent 77102f5b
...@@ -12,7 +12,6 @@ import java.util.Iterator; ...@@ -12,7 +12,6 @@ import java.util.Iterator;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import bolts.Task; import bolts.Task;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.DDPClientWrapper; import chat.rocket.android.api.DDPClientWrapper;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.LogIfError; import chat.rocket.android.helper.LogIfError;
...@@ -22,7 +21,6 @@ import chat.rocket.android.log.RCLog; ...@@ -22,7 +21,6 @@ import chat.rocket.android.log.RCLog;
import chat.rocket.android.service.ddp.base.ActiveUsersSubscriber; import chat.rocket.android.service.ddp.base.ActiveUsersSubscriber;
import chat.rocket.android.service.ddp.base.LoginServiceConfigurationSubscriber; import chat.rocket.android.service.ddp.base.LoginServiceConfigurationSubscriber;
import chat.rocket.android.service.ddp.base.UserDataSubscriber; import chat.rocket.android.service.ddp.base.UserDataSubscriber;
import chat.rocket.android.service.ddp.stream.StreamRoomMessage;
import chat.rocket.android.service.observer.CurrentUserObserver; import chat.rocket.android.service.observer.CurrentUserObserver;
import chat.rocket.android.service.observer.FileUploadingToUrlObserver; import chat.rocket.android.service.observer.FileUploadingToUrlObserver;
import chat.rocket.android.service.observer.FileUploadingWithUfsObserver; import chat.rocket.android.service.observer.FileUploadingWithUfsObserver;
...@@ -69,7 +67,6 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -69,7 +67,6 @@ public class RocketChatWebSocketThread extends HandlerThread {
private final ArrayList<Registrable> listeners = new ArrayList<>(); private final ArrayList<Registrable> listeners = new ArrayList<>();
private DDPClientWrapper ddpClient; private DDPClientWrapper ddpClient;
private boolean listenersRegistered; private boolean listenersRegistered;
private RocketChatCache rocketChatCache;
private final DDPClientRef ddpClientRef = new DDPClientRef() { private final DDPClientRef ddpClientRef = new DDPClientRef() {
@Override @Override
public DDPClientWrapper get() { public DDPClientWrapper get() {
...@@ -103,7 +100,6 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -103,7 +100,6 @@ public class RocketChatWebSocketThread extends HandlerThread {
this.hostname = hostname; this.hostname = hostname;
this.realmHelper = RealmStore.getOrCreate(hostname); this.realmHelper = RealmStore.getOrCreate(hostname);
this.connectivityManager = ConnectivityManager.getInstanceForInternal(appContext); this.connectivityManager = ConnectivityManager.getInstanceForInternal(appContext);
this.rocketChatCache = new RocketChatCache(appContext);
} }
/** /**
...@@ -185,7 +181,7 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -185,7 +181,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
@DebugLog @DebugLog
public Single<Boolean> keepAlive() { public Single<Boolean> keepAlive() {
return checkIfConnectionAlive() return checkIfConnectionAlive()
.flatMap(alive -> alive ? Single.just(true) : connect()); .flatMap(alive -> alive ? Single.just(true) : connectWithExponentialBackoff());
} }
private Single<Boolean> checkIfConnectionAlive() { private Single<Boolean> checkIfConnectionAlive() {
...@@ -247,13 +243,11 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -247,13 +243,11 @@ public class RocketChatWebSocketThread extends HandlerThread {
// TODO: Should update to RxJava 2 // TODO: Should update to RxJava 2
final CompositeSubscription subscriptions = new CompositeSubscription(); final CompositeSubscription subscriptions = new CompositeSubscription();
subscriptions.add( subscriptions.add(
connect().retryWhen(RxHelper.exponentialBackoff(3, 500, TimeUnit.MILLISECONDS)) connectWithExponentialBackoff()
.subscribe( .subscribe(
connected -> { connected -> {
if (!connected) { if (!connected) {
connectivityManager.notifyConnectionLost( connectivityManager.notifyConnecting(hostname);
hostname, ConnectivityManagerInternal.REASON_NETWORK_ERROR
);
} }
subscriptions.clear(); subscriptions.clear();
}, },
...@@ -295,6 +289,10 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -295,6 +289,10 @@ public class RocketChatWebSocketThread extends HandlerThread {
subscriptions.clear(); subscriptions.clear();
} }
private Single<Boolean> connectWithExponentialBackoff() {
return connect().retryWhen(RxHelper.exponentialBackoff(Integer.MAX_VALUE, 500, TimeUnit.MILLISECONDS));
}
@DebugLog @DebugLog
private Single<Boolean> connect() { private Single<Boolean> connect() {
return connectDDPClient() return connectDDPClient()
...@@ -343,16 +341,6 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -343,16 +341,6 @@ public class RocketChatWebSocketThread extends HandlerThread {
RCLog.w(exception, "Failed to register listeners!!"); RCLog.w(exception, "Failed to register listeners!!");
} }
} }
// Register for room stream messages
String roomId = rocketChatCache.getSelectedRoomId();
if (roomId != null && !roomId.isEmpty()) {
StreamRoomMessage streamRoomMessage = new StreamRoomMessage(
appContext, hostname, realmHelper, ddpClientRef, roomId
);
streamRoomMessage.register();
listeners.add(streamRoomMessage);
}
} }
@DebugLog @DebugLog
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment