forked from actframework/actframework
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebSocketConnectionRegistry.java
More file actions
236 lines (216 loc) · 7.92 KB
/
WebSocketConnectionRegistry.java
File metadata and controls
236 lines (216 loc) · 7.92 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
package act.ws;
/*-
* #%L
* ACT Framework
* %%
* Copyright (C) 2014 - 2017 ActFramework
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import act.util.DestroyableBase;
import act.xio.WebSocketConnection;
import org.osgl.$;
import org.osgl.util.C;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Organize websocket connection by string typed keys. Multiple connections
* can be attached to the same key
*/
public class WebSocketConnectionRegistry extends DestroyableBase {
private ConcurrentMap<String, ConcurrentMap<WebSocketConnection, WebSocketConnection>> registry = new ConcurrentHashMap<>();
/**
* Return a list of websocket connection by key
* @param key the key to find the websocket connection list
* @return a list of websocket connection or an empty list if no websocket connection found by key
*/
public List<WebSocketConnection> get(String key) {
final List<WebSocketConnection> retList = new ArrayList<>();
accept(key, C.F.addTo(retList));
return retList;
}
/**
* Accept a visitor to iterate through the connections attached to the key specified
*
* @param key the key
* @param visitor the visitor
*/
public void accept(String key, $.Function<WebSocketConnection, ?> visitor) {
ConcurrentMap<WebSocketConnection, WebSocketConnection> connections = registry.get(key);
if (null == connections) {
return;
}
if (!connections.isEmpty()) {
List<WebSocketConnection> toBeCleared = null;
for (WebSocketConnection conn : connections.keySet()) {
if (conn.closed()) {
if (null == toBeCleared) {
toBeCleared = new ArrayList<>();
}
toBeCleared.add(conn);
continue;
}
visitor.apply(conn);
}
if (null != toBeCleared) {
ConcurrentMap<WebSocketConnection, WebSocketConnection> originalCopy = registry.get(key);
originalCopy.keySet().removeAll(toBeCleared);
}
}
}
/**
* Alias of {@link #signIn(String, WebSocketConnection)}
*
* Register a connection to the registry by key.
*
* Note multiple connections can be attached to the same key
*
* @param key the key
* @param connection the websocket connection
* @see #signIn(String, WebSocketConnection)
*/
public void register(String key, WebSocketConnection connection) {
signIn(key, connection);
}
/**
* Sign in a connection to the registry by key.
*
* Note multiple connections can be attached to the same key
*
* @param key the key
* @param connection the websocket connection
* @see #register(String, WebSocketConnection)
*/
public void signIn(String key, WebSocketConnection connection) {
ConcurrentMap<WebSocketConnection, WebSocketConnection> bag = ensureConnectionList(key);
bag.put(connection, connection);
}
/**
* Sign in a group of web socket connections to the registry by key
* @param key the key
* @param connections a collection of websocket connections
*/
public void register(String key, Collection<WebSocketConnection> connections) {
signIn(key, connections);
}
/**
* Sign in a group of connections to the registry by key
* @param key the key
* @param connections a collection of websocket connections
*/
public void signIn(String key, Collection<WebSocketConnection> connections) {
if (connections.isEmpty()) {
return;
}
Map<WebSocketConnection, WebSocketConnection> newMap = new HashMap<>();
for (WebSocketConnection conn : connections) {
newMap.put(conn, conn);
}
ConcurrentMap<WebSocketConnection, WebSocketConnection> bag = ensureConnectionList(key);
bag.putAll(newMap);
}
/**
* De-register a connection from the registry by key specified
*
* @param key the key
* @param connection the websocket connection
*/
public void deRegister(String key, WebSocketConnection connection) {
signOff(key, connection);
}
/**
* De-register a group of connections from the registry by key
*
* Note this method is an alias of {@link #signOff(String, Collection)}
*
* @param key the key
* @param connections a collection of websocket connections
* @see #signOff(String, Collection)
*/
public void deRegister(String key, Collection<WebSocketConnection> connections) {
signOff(key, connections);
}
public void signOff(String key, WebSocketConnection connection) {
ConcurrentMap<WebSocketConnection, WebSocketConnection> connections = ensureConnectionList(key);
if (null == connections) {
return;
}
connections.remove(connection);
}
/**
* Sign off a group of connections from the registry by key
*
* @param key the key
* @param connections a collection of websocket connections
*/
public void signOff(String key, Collection<WebSocketConnection> connections) {
if (connections.isEmpty()) {
return;
}
ConcurrentMap<WebSocketConnection, WebSocketConnection> bag = ensureConnectionList(key);
bag.keySet().removeAll(connections);
}
/**
* Returns the connection count in this registry.
*
* Note it might count connections that are closed but not removed from registry yet
*
* @return the connection count
*/
public int count() {
int n = 0;
for (ConcurrentMap<?, ?> bag : registry.values()) {
n += bag.size();
}
return n;
}
/**
* Returns the connection count by key specified in this registry
*
* Note it might count connections that are closed but not removed from registry yet
*
* @param key the key
* @return connection count by key
*/
public int count(String key) {
ConcurrentMap<WebSocketConnection, WebSocketConnection> bag = registry.get(key);
return null == bag ? 0 : bag.size();
}
@Override
protected void releaseResources() {
for (ConcurrentMap<WebSocketConnection, WebSocketConnection> connections : registry.values()) {
for (WebSocketConnection conn : connections.keySet()) {
conn.destroy();
}
}
registry.clear();
}
private ConcurrentMap<WebSocketConnection, WebSocketConnection> ensureConnectionList(String key) {
ConcurrentMap<WebSocketConnection, WebSocketConnection> connections = registry.get(key);
if (null == connections) {
ConcurrentMap<WebSocketConnection, WebSocketConnection> newConnections = newConnectionBag();
connections = registry.putIfAbsent(key, newConnections);
if (null == connections) {
connections = newConnections;
}
}
return connections;
}
private ConcurrentMap<WebSocketConnection, WebSocketConnection> newConnectionBag() {
// TODO find a better strategy to keep track of the connections
// see http://stackoverflow.com/questions/44040637/best-practice-to-track-websocket-connections-in-java/
return new ConcurrentHashMap<>();
}
}