-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
ParseLiveQueryClientImpl.java
305 lines (270 loc) · 12.1 KB
/
ParseLiveQueryClientImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package com.parse;
import android.util.Log;
import android.util.SparseArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URI;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;
import bolts.Continuation;
import bolts.Task;
import static com.parse.Parse.checkInit;
/* package */ class ParseLiveQueryClientImpl<T extends ParseObject> implements ParseLiveQueryClient<T> {
private static final String LOG_TAG = "ParseLiveQueryClient";
private final Executor taskExecutor;
private final String applicationId;
private final String clientKey;
private final SparseArray<Subscription<T>> subscriptions = new SparseArray<>();
private final URI uri;
private final WebSocketClientFactory webSocketClientFactory;
private final WebSocketClient.WebSocketClientCallback webSocketClientCallback;
private WebSocketClient webSocketClient;
private int requestIdCount = 1;
private boolean userInitiatedDisconnect = false;
/* package */ ParseLiveQueryClientImpl(URI uri) {
this(uri, new TubeSockWebSocketClient.TubeWebSocketClientFactory(), Task.BACKGROUND_EXECUTOR);
}
/* package */ ParseLiveQueryClientImpl(URI uri, WebSocketClientFactory webSocketClientFactory, Executor taskExecutor) {
checkInit();
this.uri = uri;
this.applicationId = ParsePlugins.get().applicationId();
this.clientKey = ParsePlugins.get().clientKey();
this.webSocketClientFactory = webSocketClientFactory;
this.taskExecutor = taskExecutor;
this.webSocketClientCallback = getWebSocketClientCallback();
}
@Override
public SubscriptionHandling<T> subscribe(ParseQuery<T> query) {
int requestId = requestIdGenerator();
Subscription<T> subscription = new Subscription<>(requestId, query);
subscriptions.append(requestId, subscription);
if (webSocketClient == null || (webSocketClient.getState() != WebSocketClient.State.CONNECTING && webSocketClient.getState() != WebSocketClient.State.CONNECTED)) {
if (!userInitiatedDisconnect) {
reconnect();
} else {
Log.w(LOG_TAG, "Warning: The client was explicitly disconnected! You must explicitly call .reconnect() in order to process your subscriptions.");
}
} else if (webSocketClient.getState() == WebSocketClient.State.CONNECTED) {
sendSubscription(subscription);
}
return subscription;
}
@Override
public void unsubscribe(final ParseQuery<T> query) {
if (query != null) {
for (int i = 0; i < subscriptions.size(); i++) {
Subscription subscription = subscriptions.valueAt(i);
if (query.equals(subscription.getQuery())) {
sendUnsubscription(subscription);
}
}
}
}
@Override
public void unsubscribe(final ParseQuery<T> query, final SubscriptionHandling subscriptionHandling) {
if (query != null && subscriptionHandling != null) {
for (int i = 0; i < subscriptions.size(); i++) {
Subscription subscription = subscriptions.valueAt(i);
if (query.equals(subscription.getQuery()) && subscriptionHandling.equals(subscription)) {
sendUnsubscription(subscription);
}
}
}
}
@Override
public void reconnect() {
if (webSocketClient != null) {
webSocketClient.close();
}
this.webSocketClient = webSocketClientFactory.createInstance(webSocketClientCallback, uri);
this.webSocketClient.open();
userInitiatedDisconnect = false;
}
@Override
public void disconnect() {
if (webSocketClient != null) {
webSocketClient.close();
webSocketClient = null;
userInitiatedDisconnect = true;
}
}
// Private methods
private synchronized int requestIdGenerator() {
return requestIdCount++;
}
private Task<Void> handleOperationAsync(final String message) {
return Task.call(new Callable<Void>() {
public Void call() throws Exception {
parseMessage(message);
return null;
}
}, taskExecutor);
}
private Task<Void> sendOperationAsync(final ClientOperation clientOperation) {
return Task.call(new Callable<Void>() {
public Void call() throws Exception {
JSONObject jsonEncoded = clientOperation.getJSONObjectRepresentation();
String jsonString = jsonEncoded.toString();
webSocketClient.send(jsonString);
return null;
}
}, taskExecutor);
}
private void parseMessage(String message) throws LiveQueryException {
try {
JSONObject jsonObject = new JSONObject(message);
String rawOperation = jsonObject.getString("op");
switch (rawOperation) {
case "connected":
Log.v(LOG_TAG, "Connected, sending pending subscription");
for (int i = 0; i < subscriptions.size(); i++) {
sendSubscription(subscriptions.valueAt(i));
}
break;
case "redirect":
String url = jsonObject.getString("url");
// TODO: Handle redirect.
Log.d(LOG_TAG, "Redirect is not yet handled");
break;
case "subscribed":
handleSubscribedEvent(jsonObject);
break;
case "unsubscribed":
handleUnsubscribedEvent(jsonObject);
break;
case "enter":
handleObjectEvent(Subscription.Event.ENTER, jsonObject);
break;
case "leave":
handleObjectEvent(Subscription.Event.LEAVE, jsonObject);
break;
case "update":
handleObjectEvent(Subscription.Event.UPDATE, jsonObject);
break;
case "create":
handleObjectEvent(Subscription.Event.CREATE, jsonObject);
break;
case "delete":
handleObjectEvent(Subscription.Event.DELETE, jsonObject);
break;
case "error":
handleErrorEvent(jsonObject);
break;
default:
throw new LiveQueryException.InvalidResponseException(message);
}
} catch (JSONException e) {
throw new LiveQueryException.InvalidResponseException(message);
}
}
private void handleSubscribedEvent(JSONObject jsonObject) throws JSONException {
final int requestId = jsonObject.getInt("requestId");
final Subscription<T> subscription = subscriptionForRequestId(requestId);
if (subscription != null) {
subscription.didSubscribe(subscription.getQuery());
}
}
private void handleUnsubscribedEvent(JSONObject jsonObject) throws JSONException {
final int requestId = jsonObject.getInt("requestId");
final Subscription<T> subscription = subscriptionForRequestId(requestId);
if (subscription != null) {
subscription.didUnsubscribe(subscription.getQuery());
subscriptions.remove(requestId);
}
}
private void handleObjectEvent(Subscription.Event event, JSONObject jsonObject) throws JSONException {
final int requestId = jsonObject.getInt("requestId");
final Subscription<T> subscription = subscriptionForRequestId(requestId);
if (subscription != null) {
T object = ParseObject.fromJSON(jsonObject.getJSONObject("object"), subscription.getQueryState().className(), subscription.getQueryState().selectedKeys() == null);
subscription.didReceive(event, subscription.getQuery(), object);
}
}
private void handleErrorEvent(JSONObject jsonObject) throws JSONException {
int requestId = jsonObject.getInt("requestId");
int code = jsonObject.getInt("code");
String error = jsonObject.getString("error");
Boolean reconnect = jsonObject.getBoolean("reconnect");
final Subscription<T> subscription = subscriptionForRequestId(requestId);
if (subscription != null) {
subscription.didEncounter(new LiveQueryException.ServerReportedException(code, error, reconnect), subscription.getQuery());
}
}
private Subscription<T> subscriptionForRequestId(int requestId) {
return subscriptions.get(requestId);
}
private void sendSubscription(final Subscription<T> subscription) {
ParseUser.getCurrentSessionTokenAsync().onSuccess(new Continuation<String, Void>() {
@Override
public Void then(Task<String> task) throws Exception {
String sessionToken = task.getResult();
SubscribeClientOperation<T> op = new SubscribeClientOperation<>(subscription.getRequestId(), subscription.getQueryState(), sessionToken);
// dispatch errors
sendOperationAsync(op).continueWith(new Continuation<Void, Void>() {
public Void then(Task<Void> task) {
Exception error = task.getError();
if (error != null) {
if (error instanceof RuntimeException) {
subscription.didEncounter(new LiveQueryException.UnknownException(
"Error when subscribing", (RuntimeException) error), subscription.getQuery());
}
}
return null;
}
});
return null;
}
});
}
private void sendUnsubscription(Subscription subscription) {
sendOperationAsync(new UnsubscribeClientOperation(subscription.getRequestId()));
}
private WebSocketClient.WebSocketClientCallback getWebSocketClientCallback() {
return new WebSocketClient.WebSocketClientCallback() {
@Override
public void onOpen() {
Log.v(LOG_TAG, "Socket opened");
ParseUser.getCurrentSessionTokenAsync().onSuccessTask(new Continuation<String, Task<Void>>() {
@Override
public Task<Void> then(Task<String> task) throws Exception {
String sessionToken = task.getResult();
return sendOperationAsync(new ConnectClientOperation(applicationId, sessionToken));
}
}).continueWith(new Continuation<Void, Void>() {
public Void then(Task<Void> task) {
Exception error = task.getError();
if (error != null) {
Log.e(LOG_TAG, "Error when connection client", error);
}
return null;
}
});
}
@Override
public void onMessage(String message) {
Log.v(LOG_TAG, "Socket onMessage " + message);
handleOperationAsync(message).continueWith(new Continuation<Void, Void>() {
public Void then(Task<Void> task) {
Exception error = task.getError();
if (error != null) {
Log.e(LOG_TAG, "Error handling message", error);
}
return null;
}
});
}
@Override
public void onClose() {
Log.v(LOG_TAG, "Socket onClose");
}
@Override
public void onError(Exception exception) {
Log.e(LOG_TAG, "Socket onError", exception);
}
@Override
public void stateChanged() {
Log.v(LOG_TAG, "Socket stateChanged");
}
};
}
}