Client.java
3.77 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.adobe.granite.socketio.impl;
import com.adobe.granite.socketio.SocketIONamespace;
import com.adobe.granite.socketio.SocketIOSocket;
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 com.adobe.granite.socketio.impl.SocketIOSocketImpl;
import com.adobe.granite.socketio.impl.engine.EIOPacket;
import com.adobe.granite.socketio.impl.engine.EIOSocket;
import com.adobe.granite.socketio.impl.engine.EIOSocketListener;
import java.io.IOException;
import java.security.Principal;
import java.util.Collection;
import java.util.concurrent.ConcurrentHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Client
implements EIOSocketListener {
private static final Logger log = LoggerFactory.getLogger(Client.class);
private final String id;
private final SocketIOServiceImpl svc;
private EIOSocket transport;
private final ConcurrentHashMap<String, SocketIOSocketImpl> sockets = new ConcurrentHashMap();
public Client(String id, SocketIOServiceImpl svc, EIOSocket transport) {
this.id = id;
this.svc = svc;
this.transport = transport;
transport.setListener(this);
}
public String getId() {
return this.id;
}
public SocketIOServiceImpl getSocketIOService() {
return this.svc;
}
public void connect(String nspName) {
log.debug("[{}] connecting to namespace {}", (Object)this.id, (Object)nspName);
if (this.sockets.containsKey(nspName)) {
log.error("[{}] already connected to namespace {}", (Object)this.id, (Object)nspName);
return;
}
SocketIONamespaceImpl nsp = this.svc.getNamespace(nspName, true);
SocketIOSocketImpl socket = new SocketIOSocketImpl(this, nsp);
this.sockets.put(nspName, socket);
socket.onConnect();
}
public void detach(SocketIOSocketImpl socket) {
this.sockets.remove(socket.getNamespace().getName());
}
public void disconnect() {
for (SocketIOSocketImpl s : this.sockets.values().toArray(new SocketIOSocketImpl[this.sockets.size()])) {
s.disconnect(false);
}
this.close();
}
public void close() {
if (this.transport.isOpen()) {
log.debug("[{}] forcing transport close");
this.transport.close();
this.onClose("forced server close");
}
this.svc.detach(this);
}
public void send(Packet p) throws IOException {
this.transport.send(p.encode());
}
@Override
public void onOpen() {
}
@Override
public void onPacket(EIOPacket packet) {
}
@Override
public void onHeartbeat() {
}
@Override
public void onMessage(String data) {
Packet p = Packet.decode(data);
if (p.type == PacketType.PARSER_ERROR) {
log.error("[{}] parser error.");
this.close();
} else if (p.type == PacketType.CONNECT) {
this.connect(p.nsp);
} else {
SocketIOSocketImpl socket = this.sockets.get(p.nsp);
if (socket == null) {
log.warn("[{}] No socket on namespace {}", (Object)this.id, (Object)p.nsp);
} else {
socket.onPacket(p);
}
}
}
@Override
public void onClose(String reason) {
log.debug("[{}] client close: {}", (Object)this.id, (Object)reason);
this.disconnect();
}
public Principal getUserPrincipal() {
return this.transport.getUserPrincipal();
}
}