author | prappo |
Wed, 08 Jun 2016 15:19:58 +0100 | |
changeset 38864 | bf2b41533aed |
parent 37874 | 02589df0999a |
child 39133 | b5641ce64cf7 |
permissions | -rw-r--r-- |
37874 | 1 |
/* |
2 |
* Copyright (c) 2016, 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 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 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 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 |
package java.net.http; |
|
26 |
||
27 |
import java.net.http.WSOutgoingMessage.Binary; |
|
28 |
import java.net.http.WSOutgoingMessage.Close; |
|
29 |
import java.net.http.WSOutgoingMessage.Ping; |
|
30 |
import java.net.http.WSOutgoingMessage.Pong; |
|
31 |
import java.net.http.WSOutgoingMessage.StreamedText; |
|
32 |
import java.net.http.WSOutgoingMessage.Text; |
|
33 |
import java.nio.ByteBuffer; |
|
34 |
import java.nio.CharBuffer; |
|
35 |
import java.nio.charset.CharacterCodingException; |
|
36 |
import java.nio.charset.CoderResult; |
|
37 |
import java.nio.charset.StandardCharsets; |
|
38 |
import java.util.concurrent.BlockingQueue; |
|
39 |
import java.util.concurrent.CompletableFuture; |
|
40 |
import java.util.concurrent.Executor; |
|
41 |
import java.util.concurrent.LinkedBlockingQueue; |
|
42 |
import java.util.function.Consumer; |
|
43 |
import java.util.stream.Stream; |
|
44 |
||
45 |
import static java.lang.String.format; |
|
46 |
import static java.net.http.Pair.pair; |
|
47 |
||
48 |
/* |
|
49 |
* Prepares outgoing messages for transmission. Verifies the WebSocket state, |
|
50 |
* places the message on the outbound queue, and notifies the signal handler. |
|
51 |
*/ |
|
52 |
final class WSTransmitter { |
|
53 |
||
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
54 |
private final BlockingQueue<Pair<WSOutgoingMessage, CompletableFuture<WebSocket>>> |
37874 | 55 |
backlog = new LinkedBlockingQueue<>(); |
56 |
private final WSMessageSender sender; |
|
57 |
private final WSSignalHandler handler; |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
58 |
private final WebSocket webSocket; |
37874 | 59 |
private boolean previousMessageSent = true; |
60 |
private boolean canSendBinary = true; |
|
61 |
private boolean canSendText = true; |
|
62 |
||
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
63 |
WSTransmitter(WebSocket ws, Executor executor, RawChannel channel, Consumer<Throwable> errorHandler) { |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
64 |
this.webSocket = ws; |
37874 | 65 |
this.handler = new WSSignalHandler(executor, this::handleSignal); |
66 |
Consumer<Throwable> sendCompletion = (error) -> { |
|
67 |
synchronized (this) { |
|
68 |
if (error == null) { |
|
69 |
previousMessageSent = true; |
|
70 |
handler.signal(); |
|
71 |
} else { |
|
72 |
errorHandler.accept(error); |
|
73 |
backlog.forEach(p -> p.second.completeExceptionally(error)); |
|
74 |
backlog.clear(); |
|
75 |
} |
|
76 |
} |
|
77 |
}; |
|
78 |
this.sender = new WSMessageSender(channel, sendCompletion); |
|
79 |
} |
|
80 |
||
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
81 |
CompletableFuture<WebSocket> sendText(CharSequence message, boolean isLast) { |
37874 | 82 |
checkAndUpdateText(isLast); |
83 |
return acceptMessage(new Text(isLast, message)); |
|
84 |
} |
|
85 |
||
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
86 |
CompletableFuture<WebSocket> sendText(Stream<? extends CharSequence> message) { |
37874 | 87 |
checkAndUpdateText(true); |
88 |
return acceptMessage(new StreamedText(message)); |
|
89 |
} |
|
90 |
||
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
91 |
CompletableFuture<WebSocket> sendBinary(ByteBuffer message, boolean isLast) { |
37874 | 92 |
checkAndUpdateBinary(isLast); |
93 |
return acceptMessage(new Binary(isLast, message)); |
|
94 |
} |
|
95 |
||
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
96 |
CompletableFuture<WebSocket> sendPing(ByteBuffer message) { |
37874 | 97 |
checkSize(message.remaining(), 125); |
98 |
return acceptMessage(new Ping(message)); |
|
99 |
} |
|
100 |
||
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
101 |
CompletableFuture<WebSocket> sendPong(ByteBuffer message) { |
37874 | 102 |
checkSize(message.remaining(), 125); |
103 |
return acceptMessage(new Pong(message)); |
|
104 |
} |
|
105 |
||
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
106 |
CompletableFuture<WebSocket> sendClose(WebSocket.CloseCode code, CharSequence reason) { |
37874 | 107 |
return acceptMessage(createCloseMessage(code, reason)); |
108 |
} |
|
109 |
||
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
110 |
CompletableFuture<WebSocket> sendClose() { |
37874 | 111 |
return acceptMessage(new Close(ByteBuffer.allocate(0))); |
112 |
} |
|
113 |
||
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
114 |
private CompletableFuture<WebSocket> acceptMessage(WSOutgoingMessage m) { |
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
115 |
CompletableFuture<WebSocket> cf = new CompletableFuture<>(); |
37874 | 116 |
synchronized (this) { |
117 |
backlog.offer(pair(m, cf)); |
|
118 |
} |
|
119 |
handler.signal(); |
|
120 |
return cf; |
|
121 |
} |
|
122 |
||
123 |
/* Callback for pulling messages from the queue, and initiating the send. */ |
|
124 |
private void handleSignal() { |
|
125 |
synchronized (this) { |
|
126 |
while (!backlog.isEmpty() && previousMessageSent) { |
|
127 |
previousMessageSent = false; |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
128 |
Pair<WSOutgoingMessage, CompletableFuture<WebSocket>> p = backlog.peek(); |
37874 | 129 |
boolean sent = sender.trySendFully(p.first); |
130 |
if (sent) { |
|
131 |
backlog.remove(); |
|
38864
bf2b41533aed
8156693: Improve usability of CompletableFuture use in WebSocket API
prappo
parents:
37874
diff
changeset
|
132 |
p.second.complete(webSocket); |
37874 | 133 |
previousMessageSent = true; |
134 |
} |
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
private Close createCloseMessage(WebSocket.CloseCode code, CharSequence reason) { |
|
140 |
// TODO: move to construction of CloseDetail (JDK-8155621) |
|
141 |
ByteBuffer b = ByteBuffer.allocateDirect(125).putChar((char) code.getCode()); |
|
142 |
CoderResult result = StandardCharsets.UTF_8.newEncoder() |
|
143 |
.encode(CharBuffer.wrap(reason), b, true); |
|
144 |
if (result.isError()) { |
|
145 |
try { |
|
146 |
result.throwException(); |
|
147 |
} catch (CharacterCodingException e) { |
|
148 |
throw new IllegalArgumentException("Reason is a malformed UTF-16 sequence", e); |
|
149 |
} |
|
150 |
} else if (result.isOverflow()) { |
|
151 |
throw new IllegalArgumentException("Reason is too long"); |
|
152 |
} |
|
153 |
return new Close(b.flip()); |
|
154 |
} |
|
155 |
||
156 |
private void checkSize(int size, int maxSize) { |
|
157 |
if (size > maxSize) { |
|
158 |
throw new IllegalArgumentException( |
|
159 |
format("The message is too long: %s;" + |
|
160 |
" expected not longer than %s", size, maxSize) |
|
161 |
); |
|
162 |
} |
|
163 |
} |
|
164 |
||
165 |
private void checkAndUpdateText(boolean isLast) { |
|
166 |
if (!canSendText) { |
|
167 |
throw new IllegalStateException("Unexpected text message"); |
|
168 |
} |
|
169 |
canSendBinary = isLast; |
|
170 |
} |
|
171 |
||
172 |
private void checkAndUpdateBinary(boolean isLast) { |
|
173 |
if (!canSendBinary) { |
|
174 |
throw new IllegalStateException("Unexpected binary message"); |
|
175 |
} |
|
176 |
canSendText = isLast; |
|
177 |
} |
|
178 |
} |