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

rename RocketChatWebSocketAPI to DDPClientWrapper

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