SocketIOSocketImpl.java
12.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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.annotation.Nonnull
* javax.annotation.Nullable
* org.apache.sling.commons.json.JSONArray
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.socketio.impl;
import com.adobe.granite.socketio.SocketIOAck;
import com.adobe.granite.socketio.SocketIOAckListener;
import com.adobe.granite.socketio.SocketIOEmitter;
import com.adobe.granite.socketio.SocketIONamespace;
import com.adobe.granite.socketio.SocketIOSocket;
import com.adobe.granite.socketio.SocketIOSocketListener;
import com.adobe.granite.socketio.impl.Client;
import com.adobe.granite.socketio.impl.Packet;
import com.adobe.granite.socketio.impl.PacketType;
import com.adobe.granite.socketio.impl.SocketIONamespaceImpl;
import com.adobe.granite.socketio.impl.SocketIOServiceImpl;
import java.io.IOException;
import java.security.Principal;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ScheduledFuture;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import org.apache.sling.commons.json.JSONArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SocketIOSocketImpl
implements SocketIOSocket {
private static final SocketIOSocketListener NOP_LISTENER = new SocketIOSocketListener(){
@Override
public void onEvent(@Nonnull String name, @Nonnull JSONArray data, @Nullable SocketIOAck ack) {
}
@Override
public void onDisconnect(String reason) {
}
@Override
public void onError(JSONArray data) {
}
};
private static final Logger log = LoggerFactory.getLogger(SocketIOSocketImpl.class);
private final SocketIOEmitter emitter;
private final Client client;
private final String sig;
private final SocketIONamespaceImpl namespace;
private final Set<String> rooms = new HashSet<String>();
private final ConcurrentMap<Long, AckRequest> ackRequests = new ConcurrentHashMap<Long, AckRequest>();
private SocketIOSocketListener listener = NOP_LISTENER;
public SocketIOSocketImpl(Client client, SocketIONamespaceImpl namespace) {
this.client = client;
this.namespace = namespace;
this.emitter = namespace.createEmitter(client.getId(), true);
this.sig = client.getId() + "@" + namespace.getName();
}
public void onConnect() {
log.debug("[{}] socket connected - writing packet", (Object)this.sig);
this.join(this.client.getId());
try {
this.client.send(new Packet(PacketType.CONNECT, this.namespace.getName(), -1, null, 0));
}
catch (IOException e) {
log.error("[{}] error sending packet", (Object)this.sig, (Object)e);
}
this.namespace.bind(this);
this.client.getSocketIOService().onConnect(this);
}
private void onError(Packet p) {
try {
this.listener.onError(p.data);
}
catch (Exception e) {
log.error("listener threw exception", (Throwable)e);
}
}
private void onAck(Packet p) {
AckRequest ackRequest = this.ackRequests.remove(p.id);
if (ackRequest == null) {
log.warn("[{}] Received illegal ack response with id={}.", (Object)this.sig, (Object)p.id);
} else {
ackRequest.onAck(p.data);
}
}
private void onEvent(Packet p) {
if (p.data == null || p.data.length() == 0) {
log.error("[{}] Invalid payload. data array missing or empty for type: {}", (Object)this.sig, (Object)p.type);
return;
}
JSONArray args = new JSONArray();
String name = p.data.optString(0);
for (int i = 1; i < p.data.length(); ++i) {
args.put(p.data.opt(i));
}
AckImpl ack = p.id >= 0 ? new AckImpl(p.id) : null;
try {
this.listener.onEvent(name, args, ack);
}
catch (Exception e) {
log.error("listener threw exception", (Throwable)e);
}
if (ack != null && !ack.sent) {
log.warn("[{}] client requested ack response for event '{}' which was not handled.", (Object)name, (Object)this.sig);
}
}
private void onDisconnect() {
log.debug("[{}] got disconnect packet", (Object)this.sig);
this.onClose("client namespace disconnect");
}
private void onClose(String reason) {
log.debug("[{}] closing socket. reason: {}", (Object)this.sig, (Object)reason);
this.leaveAll();
this.namespace.unbind(this);
this.client.detach(this);
try {
this.listener.onDisconnect(reason);
}
catch (Exception e) {
log.error("listener threw exception", (Throwable)e);
}
for (AckRequest ack : this.ackRequests.values()) {
ack.close();
}
this.ackRequests.clear();
this.client.getSocketIOService().onDisconnect(this, reason);
}
public void onPacket(Packet p) {
log.trace("[{}] incoming packet {}: {}", new Object[]{this.sig, p.type, p.data});
switch (p.type) {
case DISCONNECT: {
this.onDisconnect();
break;
}
case EVENT: {
this.onEvent(p);
break;
}
case ACK: {
this.onAck(p);
break;
}
case ERROR: {
this.onError(p);
break;
}
case BINARY_EVENT: {
break;
}
case BINARY_ACK: {
break;
}
default: {
log.error("[{}] Unexpected packet type: {}", (Object)this.sig, (Object)p.type);
}
}
}
public void send(Packet p) throws IOException {
this.client.send(p);
}
@Override
public SocketIOSocket setListener(SocketIOSocketListener listener) {
if (listener == null) {
throw new NullPointerException();
}
if (this.listener != NOP_LISTENER) {
throw new IllegalArgumentException("Overriding existing listener not allowed.");
}
this.listener = listener;
return this;
}
protected boolean hasListener() {
return this.listener != NOP_LISTENER;
}
@Override
public SocketIOSocket removeListener(SocketIOSocketListener listener) {
if (this.listener != listener && this.listener != NOP_LISTENER) {
throw new IllegalArgumentException("Unable to remove foreign listener " + listener);
}
this.listener = NOP_LISTENER;
return this;
}
@Override
public SocketIOSocket disconnect(boolean close) {
if (close) {
this.client.disconnect();
} else {
try {
this.client.send(new Packet(PacketType.DISCONNECT, this.namespace.getName(), -1, null, 0));
}
catch (IOException e) {
log.error("[{}] error sending packet", (Object)this.sig, (Object)e);
}
this.onClose("server namespace disconnect");
}
return this;
}
@Override
public String getId() {
return this.client.getId();
}
@Override
public SocketIOEmitter broadcast() {
return this.namespace.createEmitter(this.getId(), false);
}
@Override
public SocketIOSocket leave(String name) {
this.rooms.remove(name);
this.namespace.leave(this, name);
return this;
}
private SocketIOSocket leaveAll() {
for (String room : this.rooms) {
this.namespace.leave(this, room);
}
this.rooms.clear();
return this;
}
@Override
public SocketIOSocket join(String name) {
this.rooms.add(name);
this.namespace.join(this, name);
return this;
}
@Override
public Set<String> getRooms() {
return Collections.unmodifiableSet(this.rooms);
}
@Override
public SocketIONamespace getNamespace() {
return this.namespace;
}
@Nonnull
@Override
public /* varargs */ SocketIOSocket emit(@Nonnull String eventName, @Nonnull SocketIOAckListener ackListener, @Nonnull Object ... arguments) throws IOException {
JSONArray data = new JSONArray();
data.put((Object)eventName);
for (Object arg : arguments) {
data.put(arg);
}
return this.emit(eventName, ackListener, data);
}
@Nonnull
@Override
public SocketIOSocket emit(@Nonnull String name, @Nonnull SocketIOAckListener ack, @Nonnull JSONArray data) throws IOException {
AckRequest ackRequest;
while (this.ackRequests.putIfAbsent((ackRequest = new AckRequest(this.ackRequests.size(), ack)).ackId, ackRequest) != null) {
}
log.debug("[{}] Scheduling callback timeout for ack id: {}", (Object)ackRequest.ackId, (Object)this.sig);
ackRequest.timeoutHandle = this.client.getSocketIOService().scheduleCallbackTimeout(ackRequest, ack.getCallbackTimeout());
this.send(new Packet(PacketType.EVENT, this.getNamespace().getName(), ackRequest.ackId, data, 0));
return this;
}
@Override
public /* varargs */ SocketIOEmitter emit(String eventName, Object ... arguments) throws IOException {
return this.emitter.emit(eventName, arguments);
}
@Override
public SocketIOEmitter emit(String eventName, JSONArray arguments) throws IOException {
return this.emitter.emit(eventName, arguments);
}
@Override
public /* varargs */ SocketIOEmitter to(String ... room) {
return this.emitter.to(room);
}
@Override
public Principal getUserPrincipal() {
return this.client.getUserPrincipal();
}
private class AckRequest
implements Runnable {
private final long ackId;
private final SocketIOAckListener listener;
private ScheduledFuture<?> timeoutHandle;
private AckRequest(long ackId, SocketIOAckListener listener) {
this.ackId = ackId;
this.listener = listener;
}
private void onAck(@Nonnull JSONArray data) {
log.debug("[{}] Ack event received for ack id: {}", (Object)SocketIOSocketImpl.this.sig, (Object)this.ackId);
this.timeoutHandle.cancel(false);
try {
this.listener.onAck(data);
}
catch (Exception e) {
log.error("listener threw exception", (Throwable)e);
}
}
private void close() {
this.timeoutHandle.cancel(true);
}
@Override
public void run() {
log.debug("[{}] Callback timeout for ack id: {}", (Object)SocketIOSocketImpl.this.sig, (Object)this.ackId);
SocketIOSocketImpl.this.ackRequests.remove(this.ackId);
try {
this.listener.onTimeout();
}
catch (Exception e) {
log.error("listener threw exception", (Throwable)e);
}
}
}
private class AckImpl
implements SocketIOAck {
private final long ackId;
private boolean sent;
private AckImpl(long ackId) {
this.ackId = ackId;
}
@Override
public boolean isAcknowledged() {
return this.sent;
}
@Override
public /* varargs */ void send(@Nonnull Object ... arguments) throws IOException {
JSONArray data = new JSONArray();
for (Object arg : arguments) {
data.put(arg);
}
this.send(data);
}
@Override
public void send(@Nonnull JSONArray data) throws IOException {
if (this.sent) {
throw new IllegalStateException("Acknowledge packet already sent");
}
Packet p = new Packet(PacketType.ACK, SocketIOSocketImpl.this.namespace.getName(), this.ackId, data, 0);
SocketIOSocketImpl.this.client.send(p);
this.sent = true;
}
}
}