Commit 26615672 authored by Yusuke Iwaki's avatar Yusuke Iwaki

fix keepalive.

parent 5e11a6bd
...@@ -112,6 +112,7 @@ public class MethodCallHelper { ...@@ -112,6 +112,7 @@ public class MethodCallHelper {
.put("sessionId", Session.DEFAULT_ID) .put("sessionId", Session.DEFAULT_ID)
.put("token", task.getResult()) .put("token", task.getResult())
.put("tokenVerified", true) .put("tokenVerified", true)
.put("error", JSONObject.NULL)
)); ));
} }
......
...@@ -13,9 +13,7 @@ import chat.rocket.android.realm_helper.RealmListObserver; ...@@ -13,9 +13,7 @@ import chat.rocket.android.realm_helper.RealmListObserver;
import chat.rocket.android.realm_helper.RealmStore; import chat.rocket.android.realm_helper.RealmStore;
import io.realm.RealmResults; import io.realm.RealmResults;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* Background service for Rocket.Chat.Application class. * Background service for Rocket.Chat.Application class.
...@@ -42,7 +40,7 @@ public class RocketChatService extends Service { ...@@ -42,7 +40,7 @@ public class RocketChatService extends Service {
.isNotNull("hostname") .isNotNull("hostname")
.equalTo("state", ServerConfig.STATE_READY) .equalTo("state", ServerConfig.STATE_READY)
.findAll()) .findAll())
.setOnUpdateListener(this::syncWebSocketThreadsWith); .setOnUpdateListener(this::connectToServerWithServerConfig);
refreshServerConfigState(); refreshServerConfigState();
} }
...@@ -51,17 +49,37 @@ public class RocketChatService extends Service { ...@@ -51,17 +49,37 @@ public class RocketChatService extends Service {
realmHelper.executeTransaction(realm -> { realmHelper.executeTransaction(realm -> {
RealmResults<ServerConfig> configs = realm.where(ServerConfig.class).findAll(); RealmResults<ServerConfig> configs = realm.where(ServerConfig.class).findAll();
for (ServerConfig config: configs) { for (ServerConfig config: configs) {
if (config.getState() != ServerConfig.STATE_READY) {
config.setState(ServerConfig.STATE_READY); config.setState(ServerConfig.STATE_READY);
} }
}
return null; return null;
}).continueWith(new LogcatIfError());; }).continueWith(new LogcatIfError());;
} }
@Override public int onStartCommand(Intent intent, int flags, int startId) { @Override public int onStartCommand(Intent intent, int flags, int startId) {
List<ServerConfig> configs = realmHelper.executeTransactionForReadResults(realm ->
realm.where(ServerConfig.class)
.equalTo("state", ServerConfig.STATE_CONNECTED)
.findAll());
for (ServerConfig config: configs) {
String serverConfigId = config.getServerConfigId();
if (webSocketThreads.containsKey(serverConfigId)) {
RocketChatWebSocketThread thread = webSocketThreads.get(serverConfigId);
if (thread != null) {
thread.keepalive();
}
}
}
realmHelper.executeTransaction(realm -> { realmHelper.executeTransaction(realm -> {
RealmResults<ServerConfig> targetConfigs = realm RealmResults<ServerConfig> targetConfigs = realm
.where(ServerConfig.class) .where(ServerConfig.class)
.beginGroup()
.equalTo("state", ServerConfig.STATE_CONNECTION_ERROR) .equalTo("state", ServerConfig.STATE_CONNECTION_ERROR)
.or()
.isNotNull("error")
.endGroup()
.isNotNull("session") .isNotNull("session")
.findAll(); .findAll();
for (ServerConfig config : targetConfigs) { for (ServerConfig config : targetConfigs) {
...@@ -76,31 +94,13 @@ public class RocketChatService extends Service { ...@@ -76,31 +94,13 @@ public class RocketChatService extends Service {
return START_STICKY; return START_STICKY;
} }
private void syncWebSocketThreadsWith(List<ServerConfig> configList) { private void connectToServerWithServerConfig(List<ServerConfig> configList) {
final Iterator<Map.Entry<String, RocketChatWebSocketThread>> iterator = if (configList.isEmpty()) {
webSocketThreads.entrySet().iterator(); return;
while (iterator.hasNext()) {
Map.Entry<String, RocketChatWebSocketThread> entry = iterator.next();
String serverConfigId = entry.getKey();
boolean found = false;
for (ServerConfig config : configList) {
if (serverConfigId.equals(config.getServerConfigId())) {
found = true;
break;
}
}
if (!found) {
RocketChatWebSocketThread thread = entry.getValue();
if (thread != null) {
RocketChatWebSocketThread.destroy(thread);
}
iterator.remove();
}
} }
for (ServerConfig config : configList) { ServerConfig config = configList.get(0);
findOrCreateWebSocketThread(config).onSuccess(task -> { createWebSocketThread(config).onSuccess(task -> {
RocketChatWebSocketThread thread = task.getResult(); RocketChatWebSocketThread thread = task.getResult();
if (thread != null) { if (thread != null) {
thread.keepalive(); thread.keepalive();
...@@ -108,19 +108,13 @@ public class RocketChatService extends Service { ...@@ -108,19 +108,13 @@ public class RocketChatService extends Service {
return null; return null;
}); });
} }
}
private Task<RocketChatWebSocketThread> findOrCreateWebSocketThread(final ServerConfig config) { private Task<RocketChatWebSocketThread> createWebSocketThread(final ServerConfig config) {
final String serverConfigId = config.getServerConfigId(); final String serverConfigId = config.getServerConfigId();
if (webSocketThreads.containsKey(serverConfigId)) {
return ServerConfig.updateState(serverConfigId, ServerConfig.STATE_CONNECTED)
.onSuccessTask(_task -> Task.forResult(webSocketThreads.get(serverConfigId)));
} else {
return ServerConfig.updateState(serverConfigId, ServerConfig.STATE_CONNECTING)
.onSuccessTask(_task -> {
webSocketThreads.put(serverConfigId, null); webSocketThreads.put(serverConfigId, null);
return RocketChatWebSocketThread.getStarted(getApplicationContext(), config); return ServerConfig.updateState(serverConfigId, ServerConfig.STATE_CONNECTING)
}) .onSuccessTask(_task ->
RocketChatWebSocketThread.getStarted(getApplicationContext(), config))
.onSuccessTask(task -> .onSuccessTask(task ->
ServerConfig.updateState(serverConfigId, ServerConfig.STATE_CONNECTED) ServerConfig.updateState(serverConfigId, ServerConfig.STATE_CONNECTED)
.onSuccessTask(_task -> task)) .onSuccessTask(_task -> task))
...@@ -129,6 +123,14 @@ public class RocketChatService extends Service { ...@@ -129,6 +123,14 @@ public class RocketChatService extends Service {
return task; return task;
}); });
} }
private Task<RocketChatWebSocketThread> findOrCreateWebSocketThread(final ServerConfig config) {
final String serverConfigId = config.getServerConfigId();
if (webSocketThreads.containsKey(serverConfigId)) {
return Task.forResult(webSocketThreads.get(serverConfigId));
} else {
return createWebSocketThread(config);
}
} }
@Nullable @Nullable
......
...@@ -45,7 +45,6 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -45,7 +45,6 @@ public class RocketChatWebSocketThread extends HandlerThread {
private final RealmHelper serverConfigRealm; private final RealmHelper serverConfigRealm;
private final ArrayList<Registerable> listeners = new ArrayList<>(); private final ArrayList<Registerable> listeners = new ArrayList<>();
private DDPClientWraper ddpClient; private DDPClientWraper ddpClient;
private boolean socketExists;
private boolean listenersRegistered; private boolean listenersRegistered;
private RocketChatWebSocketThread(Context appContext, String serverConfigId) { private RocketChatWebSocketThread(Context appContext, String serverConfigId) {
...@@ -116,22 +115,24 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -116,22 +115,24 @@ public class RocketChatWebSocketThread extends HandlerThread {
} }
} }
private Task<Void> ensureConnection() {
if (ddpClient == null || !ddpClient.isConnected()) {
return connect();
} else {
return Task.forResult(null);
}
}
/** /**
* synchronize the state of the thread with ServerConfig. * synchronize the state of the thread with ServerConfig.
*/ */
@DebugLog public void keepalive() { @DebugLog public void keepalive() {
ensureConnection().continueWith(task -> { if (ddpClient == null || !ddpClient.isConnected()) {
new Handler(getLooper()).post(this::keepaliveListeners); defaultRealm.executeTransaction(realm -> {
ServerConfig config = realm.where(ServerConfig.class)
.equalTo("serverConfigId", serverConfigId)
.findFirst();
if (config != null && config.getState() == ServerConfig.STATE_CONNECTED) {
config.setState(ServerConfig.STATE_READY);
quit();
}
return null; return null;
}); });
} else {
new Handler(getLooper()).post(this::keepaliveListeners);
}
} }
private void prepareWebSocket(String hostname) { private void prepareWebSocket(String hostname) {
...@@ -141,11 +142,6 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -141,11 +142,6 @@ public class RocketChatWebSocketThread extends HandlerThread {
} }
@DebugLog private Task<Void> connect() { @DebugLog private Task<Void> connect() {
if (socketExists) {
return Task.forResult(null);
}
socketExists = true;
final ServerConfig config = defaultRealm.executeTransactionForRead(realm -> final ServerConfig config = defaultRealm.executeTransactionForRead(realm ->
realm.where(ServerConfig.class).equalTo("serverConfigId", serverConfigId).findFirst()); realm.where(ServerConfig.class).equalTo("serverConfigId", serverConfigId).findFirst());
...@@ -229,7 +225,7 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -229,7 +225,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
//@DebugLog //@DebugLog
private void keepaliveListeners() { private void keepaliveListeners() {
if (!socketExists || !listenersRegistered) { if (!listenersRegistered) {
return; return;
} }
...@@ -240,7 +236,7 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -240,7 +236,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
@DebugLog @DebugLog
private void unregisterListeners() { private void unregisterListeners() {
if (!socketExists || !listenersRegistered) { if (!listenersRegistered) {
return; return;
} }
...@@ -255,6 +251,5 @@ public class RocketChatWebSocketThread extends HandlerThread { ...@@ -255,6 +251,5 @@ public class RocketChatWebSocketThread extends HandlerThread {
ddpClient = null; ddpClient = null;
} }
listenersRegistered = false; listenersRegistered = false;
socketExists = false;
} }
} }
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