author | prappo |
Thu, 22 Mar 2018 09:48:27 +0000 | |
branch | http-client-branch |
changeset 56341 | 10fcbe13cd19 |
parent 56337 | 58e16ad3fb98 |
child 56389 | 0ba90c4f1e3f |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
56024
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
55992
diff
changeset
|
2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
26 |
package jdk.internal.net.http.websocket; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
28 |
import jdk.internal.net.http.common.Demand; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
29 |
import jdk.internal.net.http.common.Log; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
30 |
import jdk.internal.net.http.common.MinimalFuture; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
31 |
import jdk.internal.net.http.common.SequentialScheduler; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
32 |
import jdk.internal.net.http.common.Utils; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
33 |
import jdk.internal.net.http.websocket.OpeningHandshake.Result; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
34 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.io.IOException; |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
36 |
import java.io.InterruptedIOException; |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
37 |
import java.lang.System.Logger.Level; |
48083 | 38 |
import java.lang.ref.Reference; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.net.ProtocolException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
import java.net.URI; |
56263 | 41 |
import java.net.http.WebSocket; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
import java.nio.ByteBuffer; |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
43 |
import java.nio.CharBuffer; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
44 |
import java.nio.charset.CharacterCodingException; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
45 |
import java.nio.charset.CharsetEncoder; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
46 |
import java.nio.charset.CodingErrorAction; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
47 |
import java.nio.charset.StandardCharsets; |
56045 | 48 |
import java.util.Objects; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
import java.util.concurrent.CompletionStage; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
import java.util.concurrent.atomic.AtomicBoolean; |
56293 | 52 |
import java.util.concurrent.atomic.AtomicLong; |
48083 | 53 |
import java.util.concurrent.atomic.AtomicReference; |
56263 | 54 |
import java.util.function.BiConsumer; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
import java.util.function.Function; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
import static java.util.Objects.requireNonNull; |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
58 |
import static jdk.internal.net.http.common.MinimalFuture.completedFuture; |
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
59 |
import static jdk.internal.net.http.common.MinimalFuture.failedFuture; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
60 |
import static jdk.internal.net.http.websocket.StatusCodes.CLOSED_ABNORMALLY; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
61 |
import static jdk.internal.net.http.websocket.StatusCodes.NO_STATUS_CODE; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
62 |
import static jdk.internal.net.http.websocket.StatusCodes.isLegalToSendFromClient; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
63 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.BINARY; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
64 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.CLOSE; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
65 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.ERROR; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
66 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.IDLE; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
67 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.OPEN; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
68 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.PING; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
69 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.PONG; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
70 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.TEXT; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
71 |
import static jdk.internal.net.http.websocket.WebSocketImpl.State.WAITING; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
73 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
* A WebSocket client. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
75 |
*/ |
48083 | 76 |
public final class WebSocketImpl implements WebSocket { |
77 |
||
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
78 |
private static final boolean DEBUG = Utils.DEBUG_WS; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
79 |
private static final System.Logger debug = |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
80 |
Utils.getWebSocketLogger("[WebSocket]"::toString, DEBUG); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
81 |
private final AtomicLong sendCounter = new AtomicLong(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
82 |
private final AtomicLong receiveCounter = new AtomicLong(); |
56263 | 83 |
|
48083 | 84 |
enum State { |
85 |
OPEN, |
|
86 |
IDLE, |
|
87 |
WAITING, |
|
88 |
TEXT, |
|
89 |
BINARY, |
|
90 |
PING, |
|
91 |
PONG, |
|
92 |
CLOSE, |
|
93 |
ERROR; |
|
94 |
} |
|
95 |
||
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
96 |
private final AtomicReference<ByteBuffer> lastAutomaticPong = new AtomicReference<>(); |
56263 | 97 |
private final MinimalFuture<WebSocket> DONE = MinimalFuture.completedFuture(this); |
48083 | 98 |
private volatile boolean inputClosed; |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
99 |
private final AtomicBoolean outputClosed = new AtomicBoolean(); |
48083 | 100 |
|
101 |
private final AtomicReference<State> state = new AtomicReference<>(OPEN); |
|
102 |
||
103 |
/* Components of calls to Listener's methods */ |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
104 |
private boolean last; |
48083 | 105 |
private ByteBuffer binaryData; |
106 |
private CharSequence text; |
|
107 |
private int statusCode; |
|
108 |
private String reason; |
|
109 |
private final AtomicReference<Throwable> error = new AtomicReference<>(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
111 |
private final URI uri; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
112 |
private final String subprotocol; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
113 |
private final Listener listener; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
114 |
|
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
115 |
private final AtomicBoolean pendingTextOrBinary = new AtomicBoolean(); |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
116 |
private final AtomicBoolean pendingPingOrPong = new AtomicBoolean(); |
56263 | 117 |
private final Transport transport; |
118 |
private final SequentialScheduler receiveScheduler |
|
119 |
= new SequentialScheduler(new ReceiveTask()); |
|
48083 | 120 |
private final Demand demand = new Demand(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
|
48083 | 122 |
public static CompletableFuture<WebSocket> newInstanceAsync(BuilderImpl b) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
Function<Result, WebSocket> newWebSocket = r -> { |
48083 | 124 |
WebSocket ws = newInstance(b.getUri(), |
125 |
r.subprotocol, |
|
126 |
b.getListener(), |
|
127 |
r.transport); |
|
128 |
// Make sure we don't release the builder until this lambda |
|
129 |
// has been executed. The builder has a strong reference to |
|
130 |
// the HttpClientFacade, and we want to keep that live until |
|
131 |
// after the raw channel is created and passed to WebSocketImpl. |
|
132 |
Reference.reachabilityFence(b); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
133 |
return ws; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
}; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
OpeningHandshake h; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
h = new OpeningHandshake(b); |
48083 | 138 |
} catch (Throwable e) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
return failedFuture(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
return h.send().thenApply(newWebSocket); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
143 |
|
48083 | 144 |
/* Exposed for testing purposes */ |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
145 |
static WebSocketImpl newInstance(URI uri, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
146 |
String subprotocol, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
147 |
Listener listener, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
148 |
TransportFactory transport) { |
48083 | 149 |
WebSocketImpl ws = new WebSocketImpl(uri, subprotocol, listener, transport); |
150 |
// This initialisation is outside of the constructor for the sake of |
|
151 |
// safe publication of WebSocketImpl.this |
|
152 |
ws.signalOpen(); |
|
153 |
return ws; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
154 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
155 |
|
48083 | 156 |
private WebSocketImpl(URI uri, |
157 |
String subprotocol, |
|
158 |
Listener listener, |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
159 |
TransportFactory transportFactory) { |
48083 | 160 |
this.uri = requireNonNull(uri); |
161 |
this.subprotocol = requireNonNull(subprotocol); |
|
162 |
this.listener = requireNonNull(listener); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
163 |
this.transport = transportFactory.createTransport( |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
164 |
new SignallingMessageConsumer()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
165 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
166 |
|
56263 | 167 |
// FIXME: add to action handling of errors -> signalError() |
168 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
169 |
@Override |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
170 |
public CompletableFuture<WebSocket> sendText(CharSequence message, |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
171 |
boolean last) { |
56045 | 172 |
Objects.requireNonNull(message); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
173 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
174 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
175 |
id = sendCounter.incrementAndGet(); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
176 |
debug.log(Level.DEBUG, "enter send text %s payload length=%s last=%s", |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
177 |
id, message.length(), last); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
178 |
} |
56293 | 179 |
CompletableFuture<WebSocket> result; |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
180 |
if (!setPendingTextOrBinary()) { |
56293 | 181 |
result = failedFuture(new IllegalStateException("Send pending")); |
182 |
} else { |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
183 |
result = transport.sendText(message, last, this, |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
184 |
(r, e) -> clearPendingTextOrBinary()); |
56293 | 185 |
} |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
186 |
debug.log(Level.DEBUG, "exit send text %s returned %s", id, result); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
187 |
|
56293 | 188 |
return replaceNull(result); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
189 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
190 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
191 |
@Override |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
192 |
public CompletableFuture<WebSocket> sendBinary(ByteBuffer message, |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
193 |
boolean last) { |
56045 | 194 |
Objects.requireNonNull(message); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
195 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
196 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
197 |
id = sendCounter.incrementAndGet(); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
198 |
debug.log(Level.DEBUG, "enter send binary %s payload=%s last=%s", |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
199 |
id, message, last); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
200 |
} |
56293 | 201 |
CompletableFuture<WebSocket> result; |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
202 |
if (!setPendingTextOrBinary()) { |
56293 | 203 |
result = failedFuture(new IllegalStateException("Send pending")); |
204 |
} else { |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
205 |
result = transport.sendBinary(message, last, this, |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
206 |
(r, e) -> clearPendingTextOrBinary()); |
56293 | 207 |
} |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
208 |
debug.log(Level.DEBUG, "exit send binary %s returned %s", id, result); |
56293 | 209 |
return replaceNull(result); |
56263 | 210 |
} |
211 |
||
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
212 |
private void clearPendingTextOrBinary() { |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
213 |
pendingTextOrBinary.set(false); |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
214 |
} |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
215 |
|
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
216 |
private boolean setPendingTextOrBinary() { |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
217 |
return pendingTextOrBinary.compareAndSet(false, true); |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
218 |
} |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
219 |
|
56263 | 220 |
private CompletableFuture<WebSocket> replaceNull( |
221 |
CompletableFuture<WebSocket> cf) |
|
222 |
{ |
|
223 |
if (cf == null) { |
|
224 |
return DONE; |
|
225 |
} else { |
|
226 |
return cf; |
|
227 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
public CompletableFuture<WebSocket> sendPing(ByteBuffer message) { |
56263 | 232 |
Objects.requireNonNull(message); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
233 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
234 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
235 |
id = sendCounter.incrementAndGet(); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
236 |
debug.log(Level.DEBUG, "enter send ping %s payload=%s", id, message); |
56293 | 237 |
} |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
238 |
CompletableFuture<WebSocket> result; |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
239 |
if (!setPendingPingOrPong()) { |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
240 |
result = failedFuture(new IllegalStateException("Send pending")); |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
241 |
} else { |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
242 |
result = transport.sendPing(message, this, |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
243 |
(r, e) -> clearPendingPingOrPong()); |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
244 |
} |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
245 |
debug.log(Level.DEBUG, "exit send ping %s returned %s", id, result); |
56293 | 246 |
return replaceNull(result); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
247 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
248 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
249 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
250 |
public CompletableFuture<WebSocket> sendPong(ByteBuffer message) { |
56263 | 251 |
Objects.requireNonNull(message); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
252 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
253 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
254 |
id = sendCounter.incrementAndGet(); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
255 |
debug.log(Level.DEBUG, "enter send pong %s payload=%s", id, message); |
56293 | 256 |
} |
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
257 |
CompletableFuture<WebSocket> result; |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
258 |
if (!setPendingPingOrPong()) { |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
259 |
result = failedFuture(new IllegalStateException("Send pending")); |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
260 |
} else { |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
261 |
result = transport.sendPong(message, this, |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
262 |
(r, e) -> clearPendingPingOrPong()); |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
263 |
} |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
264 |
debug.log(Level.DEBUG, "exit send pong %s returned %s", id, result); |
56293 | 265 |
return replaceNull(result); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
266 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
267 |
|
56318
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
268 |
private boolean setPendingPingOrPong() { |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
269 |
return pendingPingOrPong.compareAndSet(false, true); |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
270 |
} |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
271 |
|
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
272 |
private void clearPendingPingOrPong() { |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
273 |
pendingPingOrPong.set(false); |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
274 |
} |
2a96e88888b2
http-client-branch: (WebSocket) API changes + test;
prappo
parents:
56314
diff
changeset
|
275 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
276 |
@Override |
56263 | 277 |
public CompletableFuture<WebSocket> sendClose(int statusCode, |
278 |
String reason) { |
|
56058 | 279 |
Objects.requireNonNull(reason); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
280 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
281 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
282 |
id = sendCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
283 |
debug.log(Level.DEBUG, |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
284 |
"enter send close %s statusCode=%s reason.length=%s", |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
285 |
id, statusCode, reason.length()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
286 |
} |
56293 | 287 |
CompletableFuture<WebSocket> result; |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
288 |
// Close message is the only type of message whose validity is checked |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
289 |
// in the corresponding send method. This is made in order to close the |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
290 |
// output in place. Otherwise the number of Close messages in queue |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
291 |
// would not be bounded. |
56293 | 292 |
if (!isLegalToSendFromClient(statusCode)) { |
293 |
result = failedFuture(new IllegalArgumentException("statusCode")); |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
294 |
} else if (!isLegalReason(reason)) { |
56341
10fcbe13cd19
http-client-branch: (WebSocket) from IOE to IAE in sendClose with bad reason
prappo
parents:
56337
diff
changeset
|
295 |
result = failedFuture(new IllegalArgumentException("reason")); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
296 |
} else if (!outputClosed.compareAndSet(false, true)){ |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
297 |
result = failedFuture(new IOException("Output closed")); |
56293 | 298 |
} else { |
299 |
result = sendClose0(statusCode, reason); |
|
300 |
} |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
301 |
debug.log(Level.DEBUG, "exit send close %s returned %s", id, result); |
56293 | 302 |
return replaceNull(result); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
305 |
private static boolean isLegalReason(String reason) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
306 |
if (reason.length() > 123) { // quick check |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
307 |
return false; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
308 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
309 |
CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder() |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
310 |
.onMalformedInput(CodingErrorAction.REPORT) |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
311 |
.onUnmappableCharacter(CodingErrorAction.REPORT); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
312 |
ByteBuffer bytes; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
313 |
try { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
314 |
bytes = encoder.encode(CharBuffer.wrap(reason)); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
315 |
} catch (CharacterCodingException ignored) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
316 |
return false; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
317 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
318 |
return bytes.remaining() <= 123; |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
319 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
320 |
|
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
321 |
/* |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
322 |
* The implementation uses this method internally to send Close messages |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
323 |
* with codes that are not allowed to be sent through the API. |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
324 |
*/ |
56263 | 325 |
private CompletableFuture<WebSocket> sendClose0(int statusCode, |
326 |
String reason) { |
|
56327
efd50952acd9
http-client-branch: (WebSocket) remove close timer
prappo
parents:
56326
diff
changeset
|
327 |
return transport.sendClose(statusCode, reason, this, |
efd50952acd9
http-client-branch: (WebSocket) remove close timer
prappo
parents:
56326
diff
changeset
|
328 |
(r, e) -> processCloseError(e)); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
329 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
330 |
|
56327
efd50952acd9
http-client-branch: (WebSocket) remove close timer
prappo
parents:
56326
diff
changeset
|
331 |
private void processCloseError(Throwable e) { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
332 |
if (e == null) { |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
333 |
debug.log(Level.DEBUG, "send close completed successfully"); |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
334 |
} else { |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
335 |
debug.log(Level.DEBUG, "send close completed with error", e); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
336 |
} |
56327
efd50952acd9
http-client-branch: (WebSocket) remove close timer
prappo
parents:
56326
diff
changeset
|
337 |
outputClosed.set(true); |
efd50952acd9
http-client-branch: (WebSocket) remove close timer
prappo
parents:
56326
diff
changeset
|
338 |
try { |
efd50952acd9
http-client-branch: (WebSocket) remove close timer
prappo
parents:
56326
diff
changeset
|
339 |
transport.closeOutput(); |
efd50952acd9
http-client-branch: (WebSocket) remove close timer
prappo
parents:
56326
diff
changeset
|
340 |
} catch (IOException ignored) { } |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
341 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
public void request(long n) { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
345 |
debug.log(Level.DEBUG, "request %s", n); |
48083 | 346 |
if (demand.increase(n)) { |
347 |
receiveScheduler.runOrSchedule(); |
|
348 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
351 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
352 |
public String getSubprotocol() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
353 |
return subprotocol; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
354 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
355 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
356 |
@Override |
48083 | 357 |
public boolean isOutputClosed() { |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
358 |
return outputClosed.get(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
@Override |
48083 | 362 |
public boolean isInputClosed() { |
363 |
return inputClosed; |
|
364 |
} |
|
365 |
||
366 |
@Override |
|
367 |
public void abort() { |
|
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
368 |
debug.log(Level.DEBUG, "abort"); |
48083 | 369 |
inputClosed = true; |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
370 |
outputClosed.set(true); |
48083 | 371 |
receiveScheduler.stop(); |
372 |
close(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
373 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
374 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
375 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
376 |
public String toString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
377 |
return super.toString() |
48083 | 378 |
+ "[uri=" + uri |
379 |
+ (!subprotocol.isEmpty() ? ", subprotocol=" + subprotocol : "") |
|
380 |
+ "]"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
381 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
382 |
|
48083 | 383 |
/* |
384 |
* The assumptions about order is as follows: |
|
385 |
* |
|
386 |
* - state is never changed more than twice inside the `run` method: |
|
387 |
* x --(1)--> IDLE --(2)--> y (otherwise we're loosing events, or |
|
388 |
* overwriting parts of messages creating a mess since there's no |
|
389 |
* queueing) |
|
390 |
* - OPEN is always the first state |
|
391 |
* - no messages are requested/delivered before onOpen is called (this |
|
392 |
* is implemented by making WebSocket instance accessible first in |
|
393 |
* onOpen) |
|
394 |
* - after the state has been observed as CLOSE/ERROR, the scheduler |
|
395 |
* is stopped |
|
396 |
*/ |
|
397 |
private class ReceiveTask extends SequentialScheduler.CompleteRestartableTask { |
|
398 |
||
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
399 |
// Transport only asked here and nowhere else because we must make sure |
48083 | 400 |
// onOpen is invoked first and no messages become pending before onOpen |
401 |
// finishes |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
402 |
|
48083 | 403 |
@Override |
404 |
public void run() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
405 |
debug.log(Level.DEBUG, "enter receive task"); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
406 |
loop: |
56322
316be30d078d
http-client-branch: (WebSocket) no error after abort
prappo
parents:
56320
diff
changeset
|
407 |
while (!receiveScheduler.isStopped()) { |
48083 | 408 |
State s = state.get(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
409 |
debug.log(Level.DEBUG, "receive state: %s", s); |
48083 | 410 |
try { |
411 |
switch (s) { |
|
412 |
case OPEN: |
|
413 |
processOpen(); |
|
414 |
tryChangeState(OPEN, IDLE); |
|
415 |
break; |
|
416 |
case TEXT: |
|
417 |
processText(); |
|
418 |
tryChangeState(TEXT, IDLE); |
|
419 |
break; |
|
420 |
case BINARY: |
|
421 |
processBinary(); |
|
422 |
tryChangeState(BINARY, IDLE); |
|
423 |
break; |
|
424 |
case PING: |
|
425 |
processPing(); |
|
426 |
tryChangeState(PING, IDLE); |
|
427 |
break; |
|
428 |
case PONG: |
|
429 |
processPong(); |
|
430 |
tryChangeState(PONG, IDLE); |
|
431 |
break; |
|
432 |
case CLOSE: |
|
433 |
processClose(); |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
434 |
break loop; |
48083 | 435 |
case ERROR: |
436 |
processError(); |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
437 |
break loop; |
48083 | 438 |
case IDLE: |
439 |
if (demand.tryDecrement() |
|
440 |
&& tryChangeState(IDLE, WAITING)) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
441 |
transport.request(1); |
48083 | 442 |
} |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
443 |
break loop; |
48083 | 444 |
case WAITING: |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
445 |
// For debugging spurious signalling: when there was |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
446 |
// a signal, but apparently nothing has changed |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
447 |
break loop; |
48083 | 448 |
default: |
449 |
throw new InternalError(String.valueOf(s)); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
450 |
} |
48083 | 451 |
} catch (Throwable t) { |
452 |
signalError(t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
453 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
454 |
} |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
455 |
debug.log(Level.DEBUG, "exit receive task"); |
48083 | 456 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
457 |
|
48083 | 458 |
private void processError() throws IOException { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
459 |
debug.log(Level.DEBUG, "processError"); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
460 |
transport.closeInput(); |
48083 | 461 |
receiveScheduler.stop(); |
462 |
Throwable err = error.get(); |
|
463 |
if (err instanceof FailWebSocketException) { |
|
464 |
int code1 = ((FailWebSocketException) err).getStatusCode(); |
|
465 |
err = new ProtocolException().initCause(err); |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
466 |
debug.log(Level.DEBUG, "failing %s with error=%s statusCode=%s", |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
467 |
WebSocketImpl.this, err, code1); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
468 |
sendCloseSilently(code1); |
48083 | 469 |
} |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
470 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
471 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
472 |
id = receiveCounter.incrementAndGet(); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
473 |
debug.log(Level.DEBUG, "enter onError %s error=%s", id, err); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
474 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
475 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
476 |
listener.onError(WebSocketImpl.this, err); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
477 |
} finally { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
478 |
debug.log(Level.DEBUG, "exit onError %s", id); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
479 |
} |
48083 | 480 |
} |
481 |
||
482 |
private void processClose() throws IOException { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
483 |
debug.log(Level.DEBUG, "processClose"); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
484 |
transport.closeInput(); |
48083 | 485 |
receiveScheduler.stop(); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
486 |
CompletionStage<?> cs = null; // when the listener is ready to close |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
487 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
488 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
489 |
id = receiveCounter.incrementAndGet(); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
490 |
debug.log(Level.DEBUG, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
491 |
"enter onClose %s statusCode=%s reason.length=%s", |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
492 |
id, statusCode, reason.length()); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
493 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
494 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
495 |
cs = listener.onClose(WebSocketImpl.this, statusCode, reason); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
496 |
} finally { |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
497 |
debug.log(Level.DEBUG, "exit onClose %s returned %s", id, cs); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
498 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
499 |
if (cs == null) { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
500 |
cs = DONE; |
48083 | 501 |
} |
502 |
int code; |
|
503 |
if (statusCode == NO_STATUS_CODE || statusCode == CLOSED_ABNORMALLY) { |
|
504 |
code = NORMAL_CLOSURE; |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
505 |
debug.log(Level.DEBUG, "using statusCode %s instead of %s", |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
506 |
statusCode, code); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
507 |
|
48083 | 508 |
} else { |
509 |
code = statusCode; |
|
510 |
} |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
511 |
cs.whenComplete((r, e) -> { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
512 |
debug.log(Level.DEBUG, |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
513 |
"CompletionStage returned by onClose completed result=%s error=%s", |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
514 |
r, e); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
515 |
sendCloseSilently(code); |
48083 | 516 |
}); |
517 |
} |
|
518 |
||
519 |
private void processPong() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
520 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
521 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
522 |
id = receiveCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
523 |
debug.log(Level.DEBUG, "enter onPong %s payload=%s", |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
524 |
id, binaryData); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
525 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
526 |
CompletionStage<?> cs = null; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
527 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
528 |
cs = listener.onPong(WebSocketImpl.this, binaryData); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
529 |
} finally { |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
530 |
debug.log(Level.DEBUG, "exit onPong %s returned %s", id, cs); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
531 |
} |
48083 | 532 |
} |
533 |
||
534 |
private void processPing() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
535 |
debug.log(Level.DEBUG, "processPing"); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
536 |
// A full copy of this (small) data is made. This way sending a |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
537 |
// replying Pong could be done in parallel with the listener |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
538 |
// handling this Ping. |
56323
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
539 |
ByteBuffer slice = binaryData.slice(); |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
540 |
if (!outputClosed.get()) { |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
541 |
ByteBuffer copy = ByteBuffer.allocate(binaryData.remaining()) |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
542 |
.put(binaryData) |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
543 |
.flip(); |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
544 |
if (!trySwapAutomaticPong(copy)) { |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
545 |
// Non-exclusive send; |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
546 |
BiConsumer<WebSocketImpl, Throwable> reporter = (r, e) -> { |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
547 |
if (e != null) { // TODO: better error handing. What if already closed? |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
548 |
signalError(Utils.getCompletionCause(e)); |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
549 |
} |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
550 |
}; |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
551 |
transport.sendPong(WebSocketImpl.this::clearAutomaticPong, |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
552 |
WebSocketImpl.this, |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
553 |
reporter); |
cf43d0ee8959
http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents:
56322
diff
changeset
|
554 |
} |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
555 |
} |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
556 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
557 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
558 |
id = receiveCounter.incrementAndGet(); |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
559 |
debug.log(Level.DEBUG, "enter onPing %s payload=%s", id, slice); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
560 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
561 |
CompletionStage<?> cs = null; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
562 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
563 |
cs = listener.onPing(WebSocketImpl.this, slice); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
564 |
} finally { |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
565 |
debug.log(Level.DEBUG, "exit onPing %s returned %s", id, cs); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
566 |
} |
48083 | 567 |
} |
568 |
||
569 |
private void processBinary() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
570 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
571 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
572 |
id = receiveCounter.incrementAndGet(); |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
573 |
debug.log(Level.DEBUG, "enter onBinary %s payload=%s last=%s", |
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
574 |
id, binaryData, last); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
575 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
576 |
CompletionStage<?> cs = null; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
577 |
try { |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
578 |
cs = listener.onBinary(WebSocketImpl.this, binaryData, last); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
579 |
} finally { |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
580 |
debug.log(Level.DEBUG, "exit onBinary %s returned %s", id, cs); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
581 |
} |
48083 | 582 |
} |
583 |
||
584 |
private void processText() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
585 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
586 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
587 |
id = receiveCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
588 |
debug.log(Level.DEBUG, |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
589 |
"enter onText %s payload.length=%s last=%s", |
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
590 |
id, text.length(), last); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
591 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
592 |
CompletionStage<?> cs = null; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
593 |
try { |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
594 |
cs = listener.onText(WebSocketImpl.this, text, last); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
595 |
} finally { |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
596 |
debug.log(Level.DEBUG, "exit onText %s returned %s", id, cs); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
597 |
} |
48083 | 598 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
599 |
|
48083 | 600 |
private void processOpen() { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
601 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
602 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
603 |
id = receiveCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
604 |
debug.log(Level.DEBUG, "enter onOpen %s", id); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
605 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
606 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
607 |
listener.onOpen(WebSocketImpl.this); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
608 |
} finally { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
609 |
debug.log(Level.DEBUG, "exit onOpen %s", id); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
610 |
} |
48083 | 611 |
} |
612 |
} |
|
613 |
||
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
614 |
private void sendCloseSilently(int statusCode) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
615 |
sendClose0(statusCode, "").whenComplete((r, e) -> { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
616 |
if (e != null) { |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
617 |
debug.log(Level.DEBUG, "automatic closure completed with error", |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
618 |
(Object) e); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
619 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
620 |
}); |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
621 |
} |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
622 |
|
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
623 |
private ByteBuffer clearAutomaticPong() { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
624 |
ByteBuffer data; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
625 |
do { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
626 |
data = lastAutomaticPong.get(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
627 |
if (data == null) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
628 |
// This method must never be called unless a message that is |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
629 |
// using it has been added previously |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
630 |
throw new InternalError(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
631 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
632 |
} while (!lastAutomaticPong.compareAndSet(data, null)); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
633 |
return data; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
634 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
635 |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
636 |
// bound pings |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
637 |
private boolean trySwapAutomaticPong(ByteBuffer copy) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
638 |
ByteBuffer message; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
639 |
boolean swapped; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
640 |
while (true) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
641 |
message = lastAutomaticPong.get(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
642 |
if (message == null) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
643 |
if (!lastAutomaticPong.compareAndSet(null, copy)) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
644 |
// It's only this method that can change null to ByteBuffer, |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
645 |
// and this method is invoked at most by one thread at a |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
646 |
// time. Thus no failure in the atomic operation above is |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
647 |
// expected. |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
648 |
throw new InternalError(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
649 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
650 |
swapped = false; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
651 |
break; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
652 |
} else if (lastAutomaticPong.compareAndSet(message, copy)) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
653 |
swapped = true; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
654 |
break; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
655 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
656 |
} |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
657 |
debug.log(Level.DEBUG, "swapped automatic pong from %s to %s", |
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
658 |
message, copy); |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
659 |
return swapped; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
660 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
661 |
|
48083 | 662 |
private void signalOpen() { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
663 |
debug.log(Level.DEBUG, "signalOpen"); |
48083 | 664 |
receiveScheduler.runOrSchedule(); |
665 |
} |
|
666 |
||
667 |
private void signalError(Throwable error) { |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
668 |
debug.log(Level.DEBUG, "signalError %s", (Object) error); |
48083 | 669 |
inputClosed = true; |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
670 |
outputClosed.set(true); |
48083 | 671 |
if (!this.error.compareAndSet(null, error) || !trySetState(ERROR)) { |
56326
63422db47911
http-client-branch: RawChannel implementation based on FlowTube
dfuchs
parents:
56323
diff
changeset
|
672 |
debug.log(Level.DEBUG, "signalError", error); |
48083 | 673 |
Log.logError(error); |
674 |
} else { |
|
675 |
close(); |
|
676 |
} |
|
677 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
678 |
|
48083 | 679 |
private void close() { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
680 |
debug.log(Level.DEBUG, "close"); |
56263 | 681 |
Throwable first = null; |
48083 | 682 |
try { |
56263 | 683 |
transport.closeInput(); |
684 |
} catch (Throwable t1) { |
|
685 |
first = t1; |
|
686 |
} finally { |
|
687 |
Throwable second = null; |
|
48083 | 688 |
try { |
56263 | 689 |
transport.closeOutput(); |
690 |
} catch (Throwable t2) { |
|
691 |
second = t2; |
|
48083 | 692 |
} finally { |
56263 | 693 |
Throwable e = null; |
694 |
if (first != null && second != null) { |
|
695 |
first.addSuppressed(second); |
|
696 |
e = first; |
|
697 |
} else if (first != null) { |
|
698 |
e = first; |
|
699 |
} else if (second != null) { |
|
700 |
e = second; |
|
701 |
} |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
702 |
if (e != null) { |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
703 |
debug.log(Level.DEBUG, "exception in close", e); |
56263 | 704 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
705 |
} |
48083 | 706 |
} |
707 |
} |
|
708 |
||
709 |
private void signalClose(int statusCode, String reason) { |
|
56263 | 710 |
// FIXME: make sure no race reason & close are not intermixed |
48083 | 711 |
inputClosed = true; |
712 |
this.statusCode = statusCode; |
|
713 |
this.reason = reason; |
|
56263 | 714 |
boolean managed = trySetState(CLOSE); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
715 |
debug.log(Level.DEBUG, |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
716 |
"signalClose statusCode=%s reason.length=%s: %s", |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
717 |
statusCode, reason.length(), managed); |
56263 | 718 |
if (managed) { |
48083 | 719 |
try { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
720 |
transport.closeInput(); |
48083 | 721 |
} catch (Throwable t) { |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
722 |
debug.log(Level.DEBUG, "exception closing input", (Object) t); |
48083 | 723 |
} |
724 |
} |
|
725 |
} |
|
726 |
||
727 |
private class SignallingMessageConsumer implements MessageStreamConsumer { |
|
728 |
||
729 |
@Override |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
730 |
public void onText(CharSequence data, boolean last) { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
731 |
transport.acknowledgeReception(); |
48083 | 732 |
text = data; |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
733 |
WebSocketImpl.this.last = last; |
48083 | 734 |
tryChangeState(WAITING, TEXT); |
735 |
} |
|
736 |
||
737 |
@Override |
|
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
738 |
public void onBinary(ByteBuffer data, boolean last) { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
739 |
transport.acknowledgeReception(); |
48083 | 740 |
binaryData = data; |
56320
f82729ca8660
http-client-branch: (WebSocket) removed MessagePart
prappo
parents:
56318
diff
changeset
|
741 |
WebSocketImpl.this.last = last; |
48083 | 742 |
tryChangeState(WAITING, BINARY); |
743 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
744 |
|
48083 | 745 |
@Override |
746 |
public void onPing(ByteBuffer data) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
747 |
transport.acknowledgeReception(); |
48083 | 748 |
binaryData = data; |
749 |
tryChangeState(WAITING, PING); |
|
750 |
} |
|
751 |
||
752 |
@Override |
|
753 |
public void onPong(ByteBuffer data) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
754 |
transport.acknowledgeReception(); |
48083 | 755 |
binaryData = data; |
756 |
tryChangeState(WAITING, PONG); |
|
757 |
} |
|
758 |
||
759 |
@Override |
|
760 |
public void onClose(int statusCode, CharSequence reason) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
761 |
transport.acknowledgeReception(); |
48083 | 762 |
signalClose(statusCode, reason.toString()); |
763 |
} |
|
764 |
||
765 |
@Override |
|
766 |
public void onComplete() { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
767 |
transport.acknowledgeReception(); |
48083 | 768 |
signalClose(CLOSED_ABNORMALLY, ""); |
769 |
} |
|
770 |
||
771 |
@Override |
|
772 |
public void onError(Throwable error) { |
|
773 |
signalError(error); |
|
774 |
} |
|
775 |
} |
|
776 |
||
777 |
private boolean trySetState(State newState) { |
|
56263 | 778 |
State currentState; |
779 |
boolean success = false; |
|
48083 | 780 |
while (true) { |
56263 | 781 |
currentState = state.get(); |
48083 | 782 |
if (currentState == ERROR || currentState == CLOSE) { |
56263 | 783 |
break; |
48083 | 784 |
} else if (state.compareAndSet(currentState, newState)) { |
785 |
receiveScheduler.runOrSchedule(); |
|
56263 | 786 |
success = true; |
787 |
break; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
788 |
} |
48083 | 789 |
} |
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
790 |
debug.log(Level.DEBUG, "set state %s (previous %s) %s", |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
791 |
newState, currentState, success); |
56263 | 792 |
return success; |
48083 | 793 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
794 |
|
48083 | 795 |
private boolean tryChangeState(State expectedState, State newState) { |
796 |
State witness = state.compareAndExchange(expectedState, newState); |
|
56263 | 797 |
boolean success = false; |
48083 | 798 |
if (witness == expectedState) { |
799 |
receiveScheduler.runOrSchedule(); |
|
56263 | 800 |
success = true; |
801 |
} else if (witness != ERROR && witness != CLOSE) { |
|
802 |
// This should be the only reason for inability to change the state |
|
803 |
// from IDLE to WAITING: the state has changed to terminal |
|
48083 | 804 |
throw new InternalError(); |
805 |
} |
|
56314
f92e7a8a189f
http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents:
56306
diff
changeset
|
806 |
debug.log(Level.DEBUG, "change state from %s to %s %s", |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
807 |
expectedState, newState, success); |
56263 | 808 |
return success; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
809 |
} |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
810 |
|
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
811 |
/* Exposed for testing purposes */ |
56263 | 812 |
protected Transport transport() { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
813 |
return transport; |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
814 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
815 |
} |