-
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathParseLiveQueryClientImpl.java
More file actions
436 lines (374 loc) · 16.1 KB
/
ParseLiveQueryClientImpl.java
File metadata and controls
436 lines (374 loc) · 16.1 KB
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
package com.parse.livequery;
import android.util.Log;
import com.parse.PLog;
import com.parse.Parse;
import com.parse.ParseDecoder;
import com.parse.ParseObject;
import com.parse.ParsePlugins;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import org.json.JSONException;
import org.json.JSONObject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import com.parse.boltsinternal.Continuation;
import com.parse.boltsinternal.Task;
import okhttp3.OkHttpClient;
class ParseLiveQueryClientImpl implements ParseLiveQueryClient {
private static final String LOG_TAG = "ParseLiveQueryClient";
private final Executor taskExecutor;
private final String applicationId;
private final String clientKey;
private final ConcurrentHashMap<Integer, Subscription<? extends ParseObject>> subscriptions = new ConcurrentHashMap<>();
private final URI uri;
private final WebSocketClientFactory webSocketClientFactory;
private final WebSocketClient.WebSocketClientCallback webSocketClientCallback;
private final List<ParseLiveQueryClientCallbacks> mCallbacks = new ArrayList<>();
private WebSocketClient webSocketClient;
private int requestIdCount = 1;
private boolean userInitiatedDisconnect = false;
private boolean hasReceivedConnected = false;
/* package */ ParseLiveQueryClientImpl() {
this(getDefaultUri());
}
/* package */ ParseLiveQueryClientImpl(URI uri) {
this(uri, new OkHttp3SocketClientFactory(new OkHttpClient()), Task.BACKGROUND_EXECUTOR);
}
/* package */ ParseLiveQueryClientImpl(URI uri, WebSocketClientFactory webSocketClientFactory) {
this(uri, webSocketClientFactory, Task.BACKGROUND_EXECUTOR);
}
/* package */ ParseLiveQueryClientImpl(WebSocketClientFactory webSocketClientFactory) {
this(getDefaultUri(), webSocketClientFactory, Task.BACKGROUND_EXECUTOR);
}
/* package */ ParseLiveQueryClientImpl(URI uri, WebSocketClientFactory webSocketClientFactory, Executor taskExecutor) {
Parse.checkInit();
this.uri = uri;
this.applicationId = ParsePlugins.get().applicationId();
this.clientKey = ParsePlugins.get().clientKey();
this.webSocketClientFactory = webSocketClientFactory;
this.taskExecutor = taskExecutor;
this.webSocketClientCallback = getWebSocketClientCallback();
}
private static URI getDefaultUri() {
String url = ParsePlugins.get().server();
if (url.contains("https")) {
url = url.replaceFirst("https", "wss");
} else {
url = url.replaceFirst("http", "ws");
}
try {
return new URI(url);
} catch (URISyntaxException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage());
}
}
@Override
public <T extends ParseObject> SubscriptionHandling<T> subscribe(ParseQuery<T> query) {
int requestId = requestIdGenerator();
Subscription<T> subscription = new Subscription<>(requestId, query);
subscriptions.put(requestId, subscription);
if (isConnected()) {
sendSubscription(subscription);
} else if (userInitiatedDisconnect) {
Log.w(LOG_TAG, "Warning: The client was explicitly disconnected! You must explicitly call .reconnect() in order to process your subscriptions.");
} else {
connectIfNeeded();
}
return subscription;
}
public void connectIfNeeded() {
switch (getWebSocketState()) {
case CONNECTED:
// nothing to do
break;
case CONNECTING:
// just wait for it to finish connecting
break;
case NONE:
case DISCONNECTING:
case DISCONNECTED:
reconnect();
break;
default:
break;
}
}
@Override
public <T extends ParseObject> void unsubscribe(final ParseQuery<T> query) {
if (query != null) {
for (Subscription<? extends ParseObject> subscription : subscriptions.values()) {
if (query.equals(subscription.getQuery())) {
sendUnsubscription(subscription);
}
}
}
}
@Override
public <T extends ParseObject> void unsubscribe(final ParseQuery<T> query, final SubscriptionHandling<T> subscriptionHandling) {
if (query != null && subscriptionHandling != null) {
for (Subscription<? extends ParseObject> subscription : subscriptions.values()) {
if (query.equals(subscription.getQuery()) && subscriptionHandling.equals(subscription)) {
sendUnsubscription(subscription);
}
}
}
}
@Override
public synchronized void reconnect() {
if (webSocketClient != null) {
webSocketClient.close();
}
userInitiatedDisconnect = false;
hasReceivedConnected = false;
webSocketClient = webSocketClientFactory.createInstance(webSocketClientCallback, uri);
webSocketClient.open();
}
@Override
public synchronized void disconnect() {
if (webSocketClient != null) {
webSocketClient.close();
webSocketClient = null;
}
userInitiatedDisconnect = true;
hasReceivedConnected = false;
}
@Override
public void registerListener(ParseLiveQueryClientCallbacks listener) {
mCallbacks.add(listener);
}
@Override
public void unregisterListener(ParseLiveQueryClientCallbacks listener) {
mCallbacks.remove(listener);
}
// Private methods
private synchronized int requestIdGenerator() {
return requestIdCount++;
}
private WebSocketClient.State getWebSocketState() {
WebSocketClient.State state = webSocketClient == null ? null : webSocketClient.getState();
return state == null ? WebSocketClient.State.NONE : state;
}
private boolean isConnected() {
return hasReceivedConnected && inAnyState(WebSocketClient.State.CONNECTED);
}
private boolean inAnyState(WebSocketClient.State... states) {
return Arrays.asList(states).contains(getWebSocketState());
}
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();
if (Parse.getLogLevel() <= Parse.LOG_LEVEL_DEBUG) {
Log.d(LOG_TAG, "Sending over websocket: " + jsonString);
}
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":
hasReceivedConnected = true;
dispatchConnected();
Log.v(LOG_TAG, "Connected, sending pending subscription");
for (Subscription<? extends ParseObject> subscription : subscriptions.values()) {
sendSubscription(subscription);
}
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 dispatchConnected() {
for (ParseLiveQueryClientCallbacks callback : mCallbacks) {
callback.onLiveQueryClientConnected(this);
}
}
private void dispatchDisconnected() {
for (ParseLiveQueryClientCallbacks callback : mCallbacks) {
callback.onLiveQueryClientDisconnected(this, userInitiatedDisconnect);
}
}
private void dispatchServerError(LiveQueryException exc) {
for (ParseLiveQueryClientCallbacks callback : mCallbacks) {
callback.onLiveQueryError(this, exc);
}
}
private void dispatchSocketError(Throwable reason) {
userInitiatedDisconnect = false;
for (ParseLiveQueryClientCallbacks callback : mCallbacks) {
callback.onSocketError(this, reason);
}
dispatchDisconnected();
}
private <T extends ParseObject> 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 <T extends ParseObject> 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 <T extends ParseObject> 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(), ParseDecoder.get(), subscription.getQueryState().selectedKeys());
subscription.didReceive(event, subscription.getQuery(), object);
}
}
private <T extends ParseObject> 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);
LiveQueryException exc = new LiveQueryException.ServerReportedException(code, error, reconnect);
if (subscription != null) {
subscription.didEncounter(exc, subscription.getQuery());
}
dispatchServerError(exc);
}
private <T extends ParseObject> Subscription<T> subscriptionForRequestId(int requestId) {
//noinspection unchecked
return (Subscription<T>) subscriptions.get(requestId);
}
private <T extends ParseObject> 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() {
hasReceivedConnected = false;
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, clientKey));
}
}).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");
hasReceivedConnected = false;
dispatchDisconnected();
}
@Override
public void onError(Throwable exception) {
PLog.e(LOG_TAG, "Socket onError", exception);
hasReceivedConnected = false;
dispatchSocketError(exception);
}
@Override
public void stateChanged() {
PLog.v(LOG_TAG, "Socket stateChanged");
}
};
}
}