This repository has been archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 37
/
Connection.java
401 lines (357 loc) · 14.4 KB
/
Connection.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
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
package io.deepstream;
import com.google.j2objc.annotations.ObjectiveCName;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.*;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Establishes a connection to a deepstream server, either
* using TCP or via a WebSocket
*/
class Connection implements IConnection {
private final DeepstreamClient client;
private final String originalUrl;
private final ArrayList<ConnectionStateListener> connectStateListeners;
private final DeepstreamConfig options;
private final EndpointFactory endpointFactory;
private Endpoint endpoint;
private boolean tooManyAuthAttempts;
private boolean challengeDenied;
private boolean deliberateClose;
private boolean redirecting;
private Timer reconnectTimeout;
private int reconnectionAttempt;
private StringBuilder messageBuffer;
private String url;
private ConnectionState connectionState;
private DeepstreamClient.LoginCallback loginCallback;
private JsonElement authParameters;
private ExecutorService rpcThread;
private ExecutorService recordThread;
private ExecutorService eventThread;
private ExecutorService presenceThread;
/**
* Creates an endpoint and passed it to {@link Connection#Connection(String, DeepstreamConfig, DeepstreamClient, EndpointFactory, Endpoint)}
*
* @see Connection#Connection(String, DeepstreamConfig, DeepstreamClient, EndpointFactory, Endpoint)
*
* @param url The endpoint url
* @param options The options used to initialise the deepstream client
* @param endpointFactory The factory to create endpoints
* @param client The deepstream client
* @throws URISyntaxException An exception if an invalid url is passed in
*/
Connection(final String url, final DeepstreamConfig options, DeepstreamClient client, EndpointFactory endpointFactory) throws URISyntaxException {
this( url, options, client, endpointFactory, null );
this.endpoint = createEndpoint();
}
/**
* Creates a connection, that is responsible for handling all the connection related logic related to state
* and messages
* @param url The endpoint url* @param options The options used to initialise the deepstream client
* @param client The deepstream client
* @param endpoint The endpoint, whether Websocket, mock or anything else
*/
Connection(final String url, final DeepstreamConfig options, DeepstreamClient client, Endpoint endpoint) {
this( url, options, client, null, endpoint);
}
public Connection(String url, DeepstreamConfig options, DeepstreamClient client, EndpointFactory endpointFactory, Endpoint endpoint) {
this.client = client;
this.connectStateListeners = new ArrayList<>();
this.originalUrl = url;
this.url = url;
this.connectionState = ConnectionState.CLOSED;
this.messageBuffer = new StringBuilder();
this.tooManyAuthAttempts = false;
this.challengeDenied = false;
this.deliberateClose = false;
this.redirecting = false;
this.reconnectTimeout = null;
this.reconnectionAttempt = 0;
this.options = options;
this.endpoint = endpoint;
this.endpointFactory = endpointFactory;
this.recordThread = Executors.newSingleThreadExecutor();
this.eventThread = Executors.newSingleThreadExecutor();
this.rpcThread = Executors.newSingleThreadExecutor();
this.presenceThread = Executors.newSingleThreadExecutor();
}
/**
* Authenticate the user connection
* @param authParameters The authentication parameters to send to deepstream
* @param loginCallback The callback for a successful / unsuccessful login attempt
* connection
*/
@ObjectiveCName("authenticate:loginCallback:")
void authenticate(JsonElement authParameters, DeepstreamClient.LoginCallback loginCallback) {
this.loginCallback = loginCallback;
if(authParameters != null) {
this.authParameters = authParameters;
} else {
this.authParameters = new JsonObject();
}
if( this.tooManyAuthAttempts || this.challengeDenied ) {
this.client.onError( Topic.ERROR, Event.IS_CLOSED, "The client\'s connection was closed" );
this.loginCallback.loginFailed(Event.IS_CLOSED, "The client\'s connection was closed");
return;
}
if( this.connectionState == ConnectionState.AWAITING_AUTHENTICATION ) {
this.sendAuthMessage();
}
}
@Override
@ObjectiveCName("send:")
public void send( String message ) {
if( this.connectionState != ConnectionState.OPEN ) {
this.messageBuffer.append( message );
} else {
this.endpoint.send( message );
}
}
@Override
@ObjectiveCName("sendMsg:action:data:")
public void sendMsg( Topic topic, Actions action, String[] data ) {
this.send( MessageBuilder.getMsg( topic, action, data ) );
}
/**
*
*/
private void sendAuthMessage() {
setState( ConnectionState.AUTHENTICATING );
String authMessage;
if( this.authParameters == null ) {
authMessage = MessageBuilder.getMsg(Topic.AUTH, Actions.REQUEST, new JsonObject().toString());
} else {
authMessage = MessageBuilder.getMsg(Topic.AUTH, Actions.REQUEST, authParameters.toString());
}
this.endpoint.send( authMessage );
}
void addConnectionChangeListener( ConnectionStateListener connectionStateListener) {
this.connectStateListeners.add(connectionStateListener);
}
void removeConnectionChangeListener( ConnectionStateListener connectionStateListener) {
this.connectStateListeners.remove(connectionStateListener);
}
ConnectionState getConnectionState() {
return this.connectionState;
}
public void close(boolean forceClose) {
this.deliberateClose = true;
if(forceClose && endpoint != null) {
endpoint.forceClose();
} else if(this.endpoint != null) {
endpoint.close();
endpoint = null;
}
if( this.reconnectTimeout != null ) {
this.reconnectTimeout.cancel();
this.reconnectTimeout = null;
}
}
void onOpen() {
this.setState( ConnectionState.AWAITING_CONNECTION );
}
@ObjectiveCName("onError:")
void onError(final String error ) {
this.setState( ConnectionState.ERROR );
/*
* If the implementation isn't listening on the error event this will throw
* an error. So let's defer it to allow the reconnection to kick in.
*/
Timer timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
client.onError( null, Event.CONNECTION_ERROR, error);
}
}, 1000);
}
@ObjectiveCName("onMessage:")
void onMessage(String rawMessage) {
List<Message> parsedMessages = MessageParser.parse( rawMessage, this.client );
for (final Message message : parsedMessages) {
if (message.topic == Topic.CONNECTION) {
handleConnectionResponse(message);
} else if (message.topic == Topic.AUTH) {
handleAuthResponse(message);
} else if (message.topic == Topic.EVENT) {
this.eventThread.execute(new Runnable() {
@Override
public void run() {
client.event.handle(message);
}
});
} else if (message.topic == Topic.RPC) {
this.rpcThread.execute(new Runnable() {
@Override
public void run() {
client.rpc.handle(message);
}
});
} else if ( message.topic == Topic.RECORD ) {
this.recordThread.execute(new Runnable() {
@Override
public void run() {
client.record.handle(message);
}
});
} else if ( message.topic == Topic.PRESENCE ) {
this.presenceThread.execute(new Runnable() {
@Override
public void run() {
client.presence.handle( message );
}
});
} else {
this.client.onError(Topic.ERROR, Event.UNSOLICITED_MESSAGE, message.action.toString());
}
}
}
void onClose() throws URISyntaxException {
if( this.redirecting ) {
this.redirecting = false;
this.endpoint = this.createEndpoint();
}
else if( this.deliberateClose ) {
this.setState( ConnectionState.CLOSED );
}
else {
if(!this.originalUrl.equals(this.url)) {
this.url = this.originalUrl;
this.endpoint = this.createEndpoint();
return;
}
this.tryReconnect();
}
}
@ObjectiveCName("handleConnectionResponse:")
private void handleConnectionResponse( Message message ) {
if( message.action == Actions.PING ) {
this.endpoint.send( MessageBuilder.getMsg( Topic.CONNECTION, Actions.PONG ) );
}
else if( message.action == Actions.ACK ) {
this.setState( ConnectionState.AWAITING_AUTHENTICATION );
}
else if( message.action == Actions.CHALLENGE ) {
this.setState( ConnectionState.CHALLENGING );
this.endpoint.send( MessageBuilder.getMsg( Topic.CONNECTION, Actions.CHALLENGE_RESPONSE, this.originalUrl ) );
}
else if( message.action == Actions.REJECTION ) {
this.challengeDenied = true;
this.close(false);
}
else if( message.action == Actions.REDIRECT ) {
this.url = message.data[ 0 ];
this.redirecting = true;
this.endpoint.close();
this.endpoint = null;
}
}
@ObjectiveCName("handleAuthResponse:")
private void handleAuthResponse( Message message ) {
if( message.action == Actions.ERROR ) {
if( message.data[0].equals( Event.TOO_MANY_AUTH_ATTEMPTS.name() ) ) {
this.deliberateClose = true;
this.tooManyAuthAttempts = true;
} else {
this.authParameters = null;
this.setState( ConnectionState.AWAITING_AUTHENTICATION );
}
if( this.loginCallback != null ) {
this.loginCallback.loginFailed(
Event.getEvent( message.data[ 0 ] ),
MessageParser.convertTyped( message.data[ 1 ], this.client )
);
}
}
else if( message.action == Actions.ACK ) {
this.setState( ConnectionState.OPEN );
if( this.messageBuffer.length() > 0 ) {
this.endpoint.send( this.messageBuffer.toString() );
this.messageBuffer = new StringBuilder();
}
if( this.loginCallback != null ) {
try {
Object data = MessageParser.convertTyped(message.data[0], this.client);
this.loginCallback.loginSuccess(data);
} catch (IndexOutOfBoundsException e) {
this.loginCallback.loginSuccess(null);
}
}
}
}
@ObjectiveCName("setState:")
private void setState( ConnectionState connectionState ) {
this.connectionState = connectionState;
for (ConnectionStateListener connectStateListener : this.connectStateListeners) {
connectStateListener.connectionStateChanged(connectionState);
}
if( connectionState == ConnectionState.AWAITING_AUTHENTICATION && this.authParameters != null ) {
this.sendAuthMessage();
}
}
/**
* Take the url passed when creating the client and ensure the correct
* protocol is provided
* @param {String} url Url passed in by client
* @param {String} defaultPath Default path to concatenate if one doest not exist
* @return {String} Url with supported protocol
*/
private URI parseUri(String url, String defaultPath) throws URISyntaxException {
if (url.startsWith("http:") || url.startsWith("https:")) {
throw new URISyntaxException(url, "HTTP/HTTPS is not supported, please use ws or wss instead");
}
if (url.startsWith("//")) {
url = "ws:" + url;
} else if (!url.startsWith("ws:") && !url.startsWith("wss:")) {
url = "ws://" + url;
}
URI uri = new URI(url);
if (uri.getPath().isEmpty()) {
uri = uri.resolve(defaultPath);
}
return uri;
}
private Endpoint createEndpoint() throws URISyntaxException {
URI uri = parseUri(url, this.options.getPath());
Endpoint endpoint = this.endpointFactory.createEndpoint(uri, this);
endpoint.open();
return endpoint;
}
private void tryReconnect() {
if( this.reconnectTimeout != null ) {
return;
}
int maxReconnectAttempts = options.getMaxReconnectAttempts();
int reconnectIntervalIncrement = options.getReconnectIntervalIncrement();
int maxReconnectInterval = options.getMaxReconnectInterval();
if( this.reconnectionAttempt < maxReconnectAttempts ) {
this.setState( ConnectionState.RECONNECTING );
this.reconnectTimeout = new Timer();
this.reconnectTimeout.schedule(new TimerTask() {
public void run() {
tryOpen();
}
}, Math.min(
reconnectIntervalIncrement * this.reconnectionAttempt,
maxReconnectInterval
));
this.reconnectionAttempt++;
} else {
this.clearReconnect();
this.close(true);
}
}
private void tryOpen() {
this.reconnectTimeout.cancel();
this.reconnectTimeout = null;
this.endpoint.open();
}
private void clearReconnect() {
this.reconnectTimeout = null;
this.reconnectionAttempt = 0;
}
}