Commit 17a940d6 authored by Yusuke Iwaki's avatar Yusuke Iwaki

set state CONNECTED after thread.keepalive()

parent a0af78fd
...@@ -47,11 +47,11 @@ public class RocketChatService extends Service { ...@@ -47,11 +47,11 @@ public class RocketChatService extends Service {
private void refreshServerConfigState() { private void refreshServerConfigState() {
realmHelper.executeTransaction(realm -> { realmHelper.executeTransaction(realm -> {
RealmResults<ServerConfig> configs = realm.where(ServerConfig.class).findAll(); RealmResults<ServerConfig> configs = realm.where(ServerConfig.class)
.notEqualTo("state", ServerConfig.STATE_READY)
.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());;
...@@ -100,39 +100,28 @@ public class RocketChatService extends Service { ...@@ -100,39 +100,28 @@ public class RocketChatService extends Service {
} }
ServerConfig config = configList.get(0); ServerConfig config = configList.get(0);
createWebSocketThread(config).onSuccess(task -> { final String serverConfigId = config.getServerConfigId();
RocketChatWebSocketThread thread = task.getResult(); ServerConfig.updateState(serverConfigId, ServerConfig.STATE_CONNECTING)
if (thread != null) { .onSuccessTask(task -> createWebSocketThread(config))
thread.keepalive(); .onSuccessTask(task -> {
} RocketChatWebSocketThread thread = task.getResult();
return null; if (thread != null) {
}); thread.keepalive();
}
return ServerConfig.updateState(serverConfigId, ServerConfig.STATE_CONNECTED);
}).continueWith(new LogcatIfError());
} }
private Task<RocketChatWebSocketThread> createWebSocketThread(final ServerConfig config) { private Task<RocketChatWebSocketThread> createWebSocketThread(final ServerConfig config) {
final String serverConfigId = config.getServerConfigId(); final String serverConfigId = config.getServerConfigId();
webSocketThreads.put(serverConfigId, null); webSocketThreads.put(serverConfigId, null);
return ServerConfig.updateState(serverConfigId, ServerConfig.STATE_CONNECTING) return RocketChatWebSocketThread.getStarted(getApplicationContext(), config)
.onSuccessTask(_task ->
RocketChatWebSocketThread.getStarted(getApplicationContext(), config))
.onSuccessTask(task ->
ServerConfig.updateState(serverConfigId, ServerConfig.STATE_CONNECTED)
.onSuccessTask(_task -> task))
.onSuccessTask(task -> { .onSuccessTask(task -> {
webSocketThreads.put(serverConfigId, task.getResult()); webSocketThreads.put(serverConfigId, task.getResult());
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);
}
}
@Override public void onDestroy() { @Override public void onDestroy() {
if (connectionRequiredServerConfigObserver != null) { if (connectionRequiredServerConfigObserver != null) {
connectionRequiredServerConfigObserver.unsub(); connectionRequiredServerConfigObserver.unsub();
......
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