Commit 00f44484 authored by Yusuke Iwaki's avatar Yusuke Iwaki

rename RocketChatWebSocketAPI to DDPClientWrapper

parent ab6c9b04
......@@ -13,13 +13,13 @@ import org.json.JSONException;
import rx.Observable;
/**
* API for several POST actions.
* DDP client wrapper.
*/
public class RocketChatWebSocketAPI {
public class DDPClientWraper {
private final DDPClient ddpClient;
private final String hostname;
private RocketChatWebSocketAPI(String hostname) {
private DDPClientWraper(String hostname) {
ddpClient = new DDPClient(OkHttpHelper.getClientForWebSocket());
this.hostname = hostname;
}
......@@ -27,8 +27,8 @@ public class RocketChatWebSocketAPI {
/**
* create new API client instance.
*/
public static RocketChatWebSocketAPI create(String hostname) {
return new RocketChatWebSocketAPI(hostname);
public static DDPClientWraper create(String hostname) {
return new DDPClientWraper(hostname);
}
/**
......
......@@ -24,23 +24,23 @@ import org.json.JSONObject;
public class MethodCallHelper {
private final RealmHelper realmHelper;
private final RocketChatWebSocketAPI api;
private final DDPClientWraper ddpClient;
private static final long TIMEOUT_MS = 4000;
public MethodCallHelper(String serverConfigId) {
this.realmHelper = RealmStore.get(serverConfigId);
api = null;
ddpClient = null;
}
public MethodCallHelper(RealmHelper realmHelper, RocketChatWebSocketAPI api) {
public MethodCallHelper(RealmHelper realmHelper, DDPClientWraper ddpClient) {
this.realmHelper = realmHelper;
this.api = api;
this.ddpClient = ddpClient;
}
@DebugLog
private Task<String> executeMethodCall(String methodName, String param, long timeout) {
if (api != null) {
return api.rpc(UUID.randomUUID().toString(), methodName, param, timeout)
if (ddpClient != null) {
return ddpClient.rpc(UUID.randomUUID().toString(), methodName, param, timeout)
.onSuccessTask(task -> Task.forResult(task.getResult().result));
} else {
return MethodCall.execute(realmHelper, methodName, param, timeout);
......
......@@ -18,7 +18,7 @@ import chat.rocket.android.service.observer.LoadMessageProcedureObserver;
import chat.rocket.android.service.observer.MethodCallObserver;
import chat.rocket.android.service.observer.SessionObserver;
import chat.rocket.android.service.observer.TokenLoginObserver;
import chat.rocket.android.api.RocketChatWebSocketAPI;
import chat.rocket.android.api.DDPClientWraper;
import chat.rocket.android_ddp.DDPClientCallback;
import hugo.weaving.DebugLog;
import java.lang.reflect.Constructor;
......@@ -44,7 +44,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
private final RealmHelper defaultRealm;
private final RealmHelper serverConfigRealm;
private final ArrayList<Registerable> listeners = new ArrayList<>();
private RocketChatWebSocketAPI webSocketAPI;
private DDPClientWraper ddpClient;
private boolean socketExists;
private boolean listenersRegistered;
......@@ -117,7 +117,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
}
private Task<Void> ensureConnection() {
if (webSocketAPI == null || !webSocketAPI.isConnected()) {
if (ddpClient == null || !ddpClient.isConnected()) {
return connect();
} else {
return Task.forResult(null);
......@@ -135,8 +135,8 @@ public class RocketChatWebSocketThread extends HandlerThread {
}
private void prepareWebSocket(String hostname) {
if (webSocketAPI == null || !webSocketAPI.isConnected()) {
webSocketAPI = RocketChatWebSocketAPI.create(hostname);
if (ddpClient == null || !ddpClient.isConnected()) {
ddpClient = DDPClientWraper.create(hostname);
}
}
......@@ -150,7 +150,7 @@ public class RocketChatWebSocketThread extends HandlerThread {
realm.where(ServerConfig.class).equalTo("serverConfigId", serverConfigId).findFirst());
prepareWebSocket(config.getHostname());
return webSocketAPI.connect(config.getSession()).onSuccessTask(task -> {
return ddpClient.connect(config.getSession()).onSuccessTask(task -> {
final String session = task.getResult().session;
defaultRealm.executeTransaction(realm ->
realm.createOrUpdateObjectFromJson(ServerConfig.class, new JSONObject()
......@@ -213,8 +213,8 @@ public class RocketChatWebSocketThread extends HandlerThread {
for (Class clazz : REGISTERABLE_CLASSES) {
try {
Constructor ctor = clazz.getConstructor(Context.class, RealmHelper.class,
RocketChatWebSocketAPI.class);
Object obj = ctor.newInstance(appContext, serverConfigRealm, webSocketAPI);
DDPClientWraper.class);
Object obj = ctor.newInstance(appContext, serverConfigRealm, ddpClient);
if (obj instanceof Registerable) {
Registerable registerable = (Registerable) obj;
......@@ -250,9 +250,9 @@ public class RocketChatWebSocketThread extends HandlerThread {
registerable.unregister();
iterator.remove();
}
if (webSocketAPI != null) {
webSocketAPI.close();
webSocketAPI = null;
if (ddpClient != null) {
ddpClient.close();
ddpClient = null;
}
listenersRegistered = false;
socketExists = false;
......
......@@ -5,7 +5,7 @@ import android.text.TextUtils;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.service.Registerable;
import chat.rocket.android.api.RocketChatWebSocketAPI;
import chat.rocket.android.api.DDPClientWraper;
import chat.rocket.android_ddp.DDPSubscription;
import io.realm.Realm;
import io.realm.RealmObject;
......@@ -18,15 +18,15 @@ import timber.log.Timber;
abstract class AbstractDDPDocEventSubscriber implements Registerable {
protected final Context context;
protected final RealmHelper realmHelper;
protected final RocketChatWebSocketAPI webSocketAPI;
protected final DDPClientWraper ddpClient;
private String subscriptionId;
private Subscription rxSubscription;
protected AbstractDDPDocEventSubscriber(Context context, RealmHelper realmHelper,
RocketChatWebSocketAPI api) {
DDPClientWraper ddpClient) {
this.context = context;
this.realmHelper = realmHelper;
this.webSocketAPI = api;
this.ddpClient = ddpClient;
}
protected abstract String getSubscriptionName();
......@@ -40,7 +40,7 @@ abstract class AbstractDDPDocEventSubscriber implements Registerable {
}
@Override public void register() {
webSocketAPI.subscribe(getSubscriptionName(), null).onSuccess(task -> {
ddpClient.subscribe(getSubscriptionName(), null).onSuccess(task -> {
subscriptionId = task.getResult().id;
return null;
}).continueWith(task -> {
......@@ -60,7 +60,7 @@ abstract class AbstractDDPDocEventSubscriber implements Registerable {
}
private void registerSubscriptionCallback() {
rxSubscription = webSocketAPI.getSubscriptionCallback()
rxSubscription = ddpClient.getSubscriptionCallback()
.filter(event -> event instanceof DDPSubscription.DocEvent)
.cast(DDPSubscription.DocEvent.class)
.filter(event -> getSubscriptionCallbackName().equals(event.collection))
......@@ -146,7 +146,7 @@ abstract class AbstractDDPDocEventSubscriber implements Registerable {
rxSubscription.unsubscribe();
}
if (!TextUtils.isEmpty(subscriptionId)) {
webSocketAPI.unsubscribe(subscriptionId).continueWith(new LogcatIfError());
ddpClient.unsubscribe(subscriptionId).continueWith(new LogcatIfError());
}
}
}
......@@ -3,7 +3,7 @@ package chat.rocket.android.service.ddp;
import android.content.Context;
import chat.rocket.android.model.ddp.User;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.api.RocketChatWebSocketAPI;
import chat.rocket.android.api.DDPClientWraper;
import io.realm.RealmObject;
/**
......@@ -11,8 +11,8 @@ import io.realm.RealmObject;
*/
public class ActiveUsersSubscriber extends AbstractDDPDocEventSubscriber {
public ActiveUsersSubscriber(Context context, RealmHelper realmHelper,
RocketChatWebSocketAPI api) {
super(context, realmHelper, api);
DDPClientWraper ddpClient) {
super(context, realmHelper, ddpClient);
}
@Override protected String getSubscriptionName() {
......
......@@ -3,7 +3,7 @@ package chat.rocket.android.service.ddp;
import android.content.Context;
import chat.rocket.android.model.ddp.MeteorLoginServiceConfiguration;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.api.RocketChatWebSocketAPI;
import chat.rocket.android.api.DDPClientWraper;
import io.realm.RealmObject;
/**
......@@ -11,8 +11,8 @@ import io.realm.RealmObject;
*/
public class LoginServiceConfigurationSubscriber extends AbstractDDPDocEventSubscriber {
public LoginServiceConfigurationSubscriber(Context context, RealmHelper realmHelper,
RocketChatWebSocketAPI api) {
super(context, realmHelper, api);
DDPClientWraper ddpClient) {
super(context, realmHelper, ddpClient);
}
@Override protected String getSubscriptionName() {
......
......@@ -4,7 +4,7 @@ import android.content.Context;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmListObserver;
import chat.rocket.android.service.Registerable;
import chat.rocket.android.api.RocketChatWebSocketAPI;
import chat.rocket.android.api.DDPClientWraper;
import io.realm.RealmObject;
abstract class AbstractModelObserver<T extends RealmObject>
......@@ -12,14 +12,14 @@ abstract class AbstractModelObserver<T extends RealmObject>
protected final Context context;
protected final RealmHelper realmHelper;
protected final RocketChatWebSocketAPI webSocketAPI;
protected final DDPClientWraper ddpClient;
private final RealmListObserver observer;
protected AbstractModelObserver(Context context, RealmHelper realmHelper,
RocketChatWebSocketAPI api) {
DDPClientWraper ddpClient) {
this.context = context;
this.realmHelper = realmHelper;
webSocketAPI = api;
this.ddpClient = ddpClient;
observer = realmHelper.createListObserver(this).setOnUpdateListener(this);
}
......
......@@ -7,7 +7,7 @@ import chat.rocket.android.model.internal.LoadMessageProcedure;
import chat.rocket.android.model.SyncState;
import chat.rocket.android.model.ddp.Message;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.api.RocketChatWebSocketAPI;
import chat.rocket.android.api.DDPClientWraper;
import io.realm.Realm;
import io.realm.RealmResults;
import io.realm.Sort;
......@@ -23,9 +23,9 @@ public class LoadMessageProcedureObserver extends AbstractModelObserver<LoadMess
private final MethodCallHelper methodCall;
public LoadMessageProcedureObserver(Context context, RealmHelper realmHelper,
RocketChatWebSocketAPI api) {
super(context, realmHelper, api);
methodCall = new MethodCallHelper(realmHelper, api);
DDPClientWraper ddpClient) {
super(context, realmHelper, ddpClient);
methodCall = new MethodCallHelper(realmHelper, ddpClient);
}
@Override public RealmResults<LoadMessageProcedure> queryItems(Realm realm) {
......
......@@ -6,7 +6,7 @@ import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.model.SyncState;
import chat.rocket.android.model.internal.MethodCall;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.api.RocketChatWebSocketAPI;
import chat.rocket.android.api.DDPClientWraper;
import chat.rocket.android_ddp.DDPClientCallback;
import io.realm.Realm;
import io.realm.RealmResults;
......@@ -23,8 +23,8 @@ public class MethodCallObserver extends AbstractModelObserver<MethodCall> {
/**
* constructor.
*/
public MethodCallObserver(Context context, RealmHelper realmHelper, RocketChatWebSocketAPI api) {
super(context, realmHelper, api);
public MethodCallObserver(Context context, RealmHelper realmHelper, DDPClientWraper ddpClient) {
super(context, realmHelper, ddpClient);
realmHelper.executeTransaction(realm -> {
// resume pending operations.
RealmResults<MethodCall> pendingMethodCalls = realm.where(MethodCall.class)
......@@ -93,7 +93,7 @@ public class MethodCallObserver extends AbstractModelObserver<MethodCall> {
.put("methodCallId", methodCallId)
.put("syncstate", SyncState.SYNCING))
).onSuccessTask(task ->
webSocketAPI.rpc(methodCallId, methodName, params, timeout)
ddpClient.rpc(methodCallId, methodName, params, timeout)
.onSuccessTask(_task -> realmHelper.executeTransaction(realm -> {
String json = _task.getResult().result;
return realm.createOrUpdateObjectFromJson(MethodCall.class, new JSONObject()
......
......@@ -6,7 +6,7 @@ import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.api.RocketChatWebSocketAPI;
import chat.rocket.android.api.DDPClientWraper;
import hugo.weaving.DebugLog;
import io.realm.Realm;
import io.realm.RealmResults;
......@@ -22,9 +22,9 @@ public class SessionObserver extends AbstractModelObserver<Session> {
/**
* constructor.
*/
public SessionObserver(Context context, RealmHelper realmHelper, RocketChatWebSocketAPI api) {
super(context, realmHelper, api);
methodCall = new MethodCallHelper(realmHelper, api);
public SessionObserver(Context context, RealmHelper realmHelper, DDPClientWraper ddpClient) {
super(context, realmHelper, ddpClient);
methodCall = new MethodCallHelper(realmHelper, ddpClient);
count = 0;
}
......
......@@ -4,7 +4,7 @@ import android.content.Context;
import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.model.internal.Session;
import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.api.RocketChatWebSocketAPI;
import chat.rocket.android.api.DDPClientWraper;
import io.realm.Realm;
import io.realm.RealmResults;
import java.util.List;
......@@ -13,9 +13,9 @@ public class TokenLoginObserver extends AbstractModelObserver<Session> {
private final MethodCallHelper methodCall;
public TokenLoginObserver(Context context, RealmHelper realmHelper, RocketChatWebSocketAPI api) {
super(context, realmHelper, api);
methodCall = new MethodCallHelper(realmHelper, api);
public TokenLoginObserver(Context context, RealmHelper realmHelper, DDPClientWraper ddpClient) {
super(context, realmHelper, ddpClient);
methodCall = new MethodCallHelper(realmHelper, ddpClient);
}
@Override public RealmResults<Session> queryItems(Realm realm) {
......
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