49765
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.internal.net.http.websocket;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.nio.ByteBuffer;
|
|
30 |
import java.nio.CharBuffer;
|
|
31 |
import java.util.concurrent.CompletableFuture;
|
|
32 |
import java.util.concurrent.atomic.AtomicInteger;
|
|
33 |
import java.util.function.BiConsumer;
|
|
34 |
import java.util.function.Supplier;
|
|
35 |
|
|
36 |
import static jdk.internal.net.http.common.Utils.pow2Size;
|
|
37 |
|
|
38 |
/*
|
|
39 |
* A FIFO message storage facility.
|
|
40 |
*
|
|
41 |
* The queue supports at most one consumer and an arbitrary number of producers.
|
|
42 |
* Methods `peek`, `remove` and `isEmpty` must not be invoked concurrently.
|
|
43 |
* Methods `addText`, `addBinary`, `addPing`, `addPong` and `addClose` may be
|
|
44 |
* invoked concurrently.
|
|
45 |
*
|
|
46 |
* This queue is of a bounded size. The queue pre-allocates array of the said
|
|
47 |
* size and fills it with `Message` elements. The resulting structure never
|
|
48 |
* changes. This allows to avoid re-allocation and garbage collection of
|
|
49 |
* elements and arrays thereof. For this reason `Message` elements are never
|
|
50 |
* returned from the `peek` method. Instead their components passed to the
|
|
51 |
* provided callback.
|
|
52 |
*
|
|
53 |
* The queue consists of:
|
|
54 |
*
|
|
55 |
* - a ring array of n + 1 `Message` elements
|
|
56 |
* - indexes H and T denoting the head and the tail elements of the queue
|
|
57 |
* respectively
|
|
58 |
*
|
|
59 |
* Each `Message` element contains a boolean flag. This flag is an auxiliary
|
|
60 |
* communication between the producers and the consumer. The flag shows
|
|
61 |
* whether or not the element is ready to be consumed (peeked at, removed). The
|
|
62 |
* flag is required since updating an element involves many fields and thus is
|
|
63 |
* not an atomic action. An addition to the queue happens in two steps:
|
|
64 |
*
|
|
65 |
* # Step 1
|
|
66 |
*
|
|
67 |
* Producers race with each other to secure an index for the element they add.
|
|
68 |
* T is atomically advanced [1] only if the advanced value doesn't equal to H
|
|
69 |
* (a producer doesn't bump into the head of the queue).
|
|
70 |
*
|
|
71 |
* # Step 2
|
|
72 |
*
|
|
73 |
* Once T is advanced in the previous step, the producer updates the message
|
|
74 |
* fields of the element at the previous value of T and then sets the flag of
|
|
75 |
* this element.
|
|
76 |
*
|
|
77 |
* A removal happens in a single step. The consumer gets the element at index H.
|
|
78 |
* If the flag of this element is set, the consumer clears the fields of the
|
|
79 |
* element, clears the flag and finally advances H.
|
|
80 |
*
|
|
81 |
* ----------------------------------------------------------------------------
|
|
82 |
* [1] To advance the index is to change it from i to (i + 1) % (n + 1).
|
|
83 |
*/
|
|
84 |
public class MessageQueue {
|
|
85 |
|
|
86 |
private final Message[] elements;
|
|
87 |
|
|
88 |
private final AtomicInteger tail = new AtomicInteger();
|
|
89 |
private volatile int head;
|
|
90 |
|
|
91 |
public MessageQueue(int capacity) {
|
|
92 |
if (capacity < 1) {
|
|
93 |
throw new IllegalArgumentException();
|
|
94 |
}
|
|
95 |
int s = pow2Size(capacity + 1);
|
|
96 |
assert s % 2 == 0 : s;
|
|
97 |
Message[] array = new Message[s];
|
|
98 |
for (int i = 0; i < array.length; i++) {
|
|
99 |
array[i] = new Message();
|
|
100 |
}
|
|
101 |
elements = array;
|
|
102 |
}
|
|
103 |
|
|
104 |
/* Exposed for testing purposes */
|
|
105 |
protected static int effectiveCapacityOf(int n) {
|
|
106 |
return pow2Size(n + 1) - 1;
|
|
107 |
}
|
|
108 |
|
|
109 |
public <T> void addText(CharBuffer message,
|
|
110 |
boolean isLast,
|
|
111 |
T attachment,
|
|
112 |
BiConsumer<? super T, ? super Throwable> action,
|
|
113 |
CompletableFuture<T> future)
|
|
114 |
throws IOException
|
|
115 |
{
|
|
116 |
add(MessageQueue.Type.TEXT, null, null, message, isLast, -1, attachment,
|
|
117 |
action, future);
|
|
118 |
}
|
|
119 |
|
|
120 |
private <T> void add(Type type,
|
|
121 |
Supplier<? extends ByteBuffer> binarySupplier,
|
|
122 |
ByteBuffer binary,
|
|
123 |
CharBuffer text,
|
|
124 |
boolean isLast,
|
|
125 |
int statusCode,
|
|
126 |
T attachment,
|
|
127 |
BiConsumer<? super T, ? super Throwable> action,
|
|
128 |
CompletableFuture<? super T> future)
|
|
129 |
throws IOException
|
|
130 |
{
|
|
131 |
// Pong "subtype" is determined by whichever field (data carrier)
|
|
132 |
// is not null. Both fields cannot be null or non-null simultaneously.
|
|
133 |
assert type != Type.PONG || (binary == null ^ binarySupplier == null);
|
|
134 |
int h, currentTail, newTail;
|
|
135 |
do {
|
|
136 |
h = head;
|
|
137 |
currentTail = tail.get();
|
|
138 |
newTail = (currentTail + 1) & (elements.length - 1);
|
|
139 |
if (newTail == h) {
|
|
140 |
throw new IOException("Queue full");
|
|
141 |
}
|
|
142 |
} while (!tail.compareAndSet(currentTail, newTail));
|
|
143 |
Message t = elements[currentTail];
|
|
144 |
if (t.ready) {
|
|
145 |
throw new InternalError();
|
|
146 |
}
|
|
147 |
t.type = type;
|
|
148 |
t.binarySupplier = binarySupplier;
|
|
149 |
t.binary = binary;
|
|
150 |
t.text = text;
|
|
151 |
t.isLast = isLast;
|
|
152 |
t.statusCode = statusCode;
|
|
153 |
t.attachment = attachment;
|
|
154 |
t.action = action;
|
|
155 |
t.future = future;
|
|
156 |
t.ready = true;
|
|
157 |
}
|
|
158 |
|
|
159 |
public <T> void addBinary(ByteBuffer message,
|
|
160 |
boolean isLast,
|
|
161 |
T attachment,
|
|
162 |
BiConsumer<? super T, ? super Throwable> action,
|
|
163 |
CompletableFuture<? super T> future)
|
|
164 |
throws IOException
|
|
165 |
{
|
|
166 |
add(MessageQueue.Type.BINARY, null, message, null, isLast, -1, attachment,
|
|
167 |
action, future);
|
|
168 |
}
|
|
169 |
|
|
170 |
public <T> void addPing(ByteBuffer message,
|
|
171 |
T attachment,
|
|
172 |
BiConsumer<? super T, ? super Throwable> action,
|
|
173 |
CompletableFuture<? super T> future)
|
|
174 |
throws IOException
|
|
175 |
{
|
|
176 |
add(MessageQueue.Type.PING, null, message, null, false, -1, attachment,
|
|
177 |
action, future);
|
|
178 |
}
|
|
179 |
|
|
180 |
public <T> void addPong(ByteBuffer message,
|
|
181 |
T attachment,
|
|
182 |
BiConsumer<? super T, ? super Throwable> action,
|
|
183 |
CompletableFuture<? super T> future)
|
|
184 |
throws IOException
|
|
185 |
{
|
|
186 |
add(MessageQueue.Type.PONG, null, message, null, false, -1, attachment,
|
|
187 |
action, future);
|
|
188 |
}
|
|
189 |
|
|
190 |
public <T> void addPong(Supplier<? extends ByteBuffer> message,
|
|
191 |
T attachment,
|
|
192 |
BiConsumer<? super T, ? super Throwable> action,
|
|
193 |
CompletableFuture<? super T> future)
|
|
194 |
throws IOException
|
|
195 |
{
|
|
196 |
add(MessageQueue.Type.PONG, message, null, null, false, -1, attachment,
|
|
197 |
action, future);
|
|
198 |
}
|
|
199 |
|
|
200 |
public <T> void addClose(int statusCode,
|
|
201 |
CharBuffer reason,
|
|
202 |
T attachment,
|
|
203 |
BiConsumer<? super T, ? super Throwable> action,
|
|
204 |
CompletableFuture<? super T> future)
|
|
205 |
throws IOException
|
|
206 |
{
|
|
207 |
add(MessageQueue.Type.CLOSE, null, null, reason, false, statusCode,
|
|
208 |
attachment, action, future);
|
|
209 |
}
|
|
210 |
|
|
211 |
@SuppressWarnings("unchecked")
|
|
212 |
public <R, E extends Throwable> R peek(QueueCallback<R, E> callback)
|
|
213 |
throws E
|
|
214 |
{
|
|
215 |
Message h = elements[head];
|
|
216 |
if (!h.ready) {
|
|
217 |
return callback.onEmpty();
|
|
218 |
}
|
|
219 |
Type type = h.type;
|
|
220 |
switch (type) {
|
|
221 |
case TEXT:
|
|
222 |
try {
|
|
223 |
return (R) callback.onText(h.text, h.isLast, h.attachment,
|
|
224 |
h.action, h.future);
|
|
225 |
} catch (Throwable t) {
|
|
226 |
// Something unpleasant is going on here with the compiler.
|
|
227 |
// If this seemingly useless catch is omitted, the compiler
|
|
228 |
// reports an error:
|
|
229 |
//
|
|
230 |
// java: unreported exception java.lang.Throwable;
|
|
231 |
// must be caught or declared to be thrown
|
|
232 |
//
|
|
233 |
// My guess is there is a problem with both the type
|
|
234 |
// inference for the method AND @SuppressWarnings("unchecked")
|
|
235 |
// being working at the same time.
|
|
236 |
throw (E) t;
|
|
237 |
}
|
|
238 |
case BINARY:
|
|
239 |
try {
|
|
240 |
return (R) callback.onBinary(h.binary, h.isLast, h.attachment,
|
|
241 |
h.action, h.future);
|
|
242 |
} catch (Throwable t) {
|
|
243 |
throw (E) t;
|
|
244 |
}
|
|
245 |
case PING:
|
|
246 |
try {
|
|
247 |
return (R) callback.onPing(h.binary, h.attachment, h.action,
|
|
248 |
h.future);
|
|
249 |
} catch (Throwable t) {
|
|
250 |
throw (E) t;
|
|
251 |
}
|
|
252 |
case PONG:
|
|
253 |
try {
|
|
254 |
if (h.binarySupplier != null) {
|
|
255 |
return (R) callback.onPong(h.binarySupplier, h.attachment,
|
|
256 |
h.action, h.future);
|
|
257 |
} else {
|
|
258 |
return (R) callback.onPong(h.binary, h.attachment, h.action,
|
|
259 |
h.future);
|
|
260 |
}
|
|
261 |
} catch (Throwable t) {
|
|
262 |
throw (E) t;
|
|
263 |
}
|
|
264 |
case CLOSE:
|
|
265 |
try {
|
|
266 |
return (R) callback.onClose(h.statusCode, h.text, h.attachment,
|
|
267 |
h.action, h.future);
|
|
268 |
} catch (Throwable t) {
|
|
269 |
throw (E) t;
|
|
270 |
}
|
|
271 |
default:
|
|
272 |
throw new InternalError(String.valueOf(type));
|
|
273 |
}
|
|
274 |
}
|
|
275 |
|
|
276 |
public boolean isEmpty() {
|
|
277 |
return !elements[head].ready;
|
|
278 |
}
|
|
279 |
|
|
280 |
public void remove() {
|
|
281 |
int currentHead = head;
|
|
282 |
Message h = elements[currentHead];
|
|
283 |
if (!h.ready) {
|
|
284 |
throw new InternalError("Queue empty");
|
|
285 |
}
|
|
286 |
h.type = null;
|
|
287 |
h.binarySupplier = null;
|
|
288 |
h.binary = null;
|
|
289 |
h.text = null;
|
|
290 |
h.attachment = null;
|
|
291 |
h.action = null;
|
|
292 |
h.future = null;
|
|
293 |
h.ready = false;
|
|
294 |
head = (currentHead + 1) & (elements.length - 1);
|
|
295 |
}
|
|
296 |
|
|
297 |
private enum Type {
|
|
298 |
|
|
299 |
TEXT,
|
|
300 |
BINARY,
|
|
301 |
PING,
|
|
302 |
PONG,
|
|
303 |
CLOSE
|
|
304 |
}
|
|
305 |
|
|
306 |
/*
|
|
307 |
* A callback for consuming a queue element's fields. Can return a result of
|
|
308 |
* type T or throw an exception of type E. This design allows to avoid
|
|
309 |
* "returning" results or "throwing" errors by updating some objects from
|
|
310 |
* the outside of the methods.
|
|
311 |
*/
|
|
312 |
public interface QueueCallback<R, E extends Throwable> {
|
|
313 |
|
|
314 |
<T> R onText(CharBuffer message,
|
|
315 |
boolean isLast,
|
|
316 |
T attachment,
|
|
317 |
BiConsumer<? super T, ? super Throwable> action,
|
|
318 |
CompletableFuture<? super T> future) throws E;
|
|
319 |
|
|
320 |
<T> R onBinary(ByteBuffer message,
|
|
321 |
boolean isLast,
|
|
322 |
T attachment,
|
|
323 |
BiConsumer<? super T, ? super Throwable> action,
|
|
324 |
CompletableFuture<? super T> future) throws E;
|
|
325 |
|
|
326 |
<T> R onPing(ByteBuffer message,
|
|
327 |
T attachment,
|
|
328 |
BiConsumer<? super T, ? super Throwable> action,
|
|
329 |
CompletableFuture<? super T> future) throws E;
|
|
330 |
|
|
331 |
<T> R onPong(ByteBuffer message,
|
|
332 |
T attachment,
|
|
333 |
BiConsumer<? super T, ? super Throwable> action,
|
|
334 |
CompletableFuture<? super T> future) throws E;
|
|
335 |
|
|
336 |
<T> R onPong(Supplier<? extends ByteBuffer> message,
|
|
337 |
T attachment,
|
|
338 |
BiConsumer<? super T, ? super Throwable> action,
|
|
339 |
CompletableFuture<? super T> future) throws E;
|
|
340 |
|
|
341 |
<T> R onClose(int statusCode,
|
|
342 |
CharBuffer reason,
|
|
343 |
T attachment,
|
|
344 |
BiConsumer<? super T, ? super Throwable> action,
|
|
345 |
CompletableFuture<? super T> future) throws E;
|
|
346 |
|
|
347 |
/* The queue is empty*/
|
|
348 |
R onEmpty() throws E;
|
|
349 |
}
|
|
350 |
|
|
351 |
/*
|
|
352 |
* A union of components of all WebSocket message types; also a node in a
|
|
353 |
* queue.
|
|
354 |
*
|
|
355 |
* A `Message` never leaves the context of the queue, thus the reference to
|
|
356 |
* it cannot be retained by anyone other than the queue.
|
|
357 |
*/
|
|
358 |
private static class Message {
|
|
359 |
|
|
360 |
private volatile boolean ready;
|
|
361 |
|
|
362 |
// -- The source message fields --
|
|
363 |
|
|
364 |
private Type type;
|
|
365 |
private Supplier<? extends ByteBuffer> binarySupplier;
|
|
366 |
private ByteBuffer binary;
|
|
367 |
private CharBuffer text;
|
|
368 |
private boolean isLast;
|
|
369 |
private int statusCode;
|
|
370 |
private Object attachment;
|
|
371 |
@SuppressWarnings("rawtypes")
|
|
372 |
private BiConsumer action;
|
|
373 |
@SuppressWarnings("rawtypes")
|
|
374 |
private CompletableFuture future;
|
|
375 |
}
|
|
376 |
}
|