author | prappo |
Wed, 14 Mar 2018 14:42:23 +0000 | |
branch | http-client-branch |
changeset 56306 | 88c1f551d45e |
parent 56304 | 065641767a75 |
child 56314 | f92e7a8a189f |
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; |
48083 | 37 |
import java.lang.ref.Reference; |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
38 |
import java.lang.System.Logger.Level; |
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; |
56045 | 43 |
import java.util.Objects; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
import java.util.concurrent.CompletionStage; |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
46 |
import java.util.concurrent.TimeUnit; |
48083 | 47 |
import java.util.concurrent.TimeoutException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
import java.util.concurrent.atomic.AtomicBoolean; |
56293 | 49 |
import java.util.concurrent.atomic.AtomicLong; |
48083 | 50 |
import java.util.concurrent.atomic.AtomicReference; |
56263 | 51 |
import java.util.function.BiConsumer; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
import java.util.function.Function; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
import static java.util.Objects.requireNonNull; |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
55 |
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
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
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
|
69 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
* A WebSocket client. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
*/ |
48083 | 73 |
public final class WebSocketImpl implements WebSocket { |
74 |
||
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
75 |
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
|
76 |
private static final System.Logger debug = |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
77 |
Utils.getWebSocketLogger("[Websocket]"::toString, DEBUG); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
78 |
private final AtomicLong sendCounter = new AtomicLong(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
79 |
private final AtomicLong receiveCounter = new AtomicLong(); |
56263 | 80 |
|
48083 | 81 |
enum State { |
82 |
OPEN, |
|
83 |
IDLE, |
|
84 |
WAITING, |
|
85 |
TEXT, |
|
86 |
BINARY, |
|
87 |
PING, |
|
88 |
PONG, |
|
89 |
CLOSE, |
|
90 |
ERROR; |
|
91 |
} |
|
92 |
||
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
93 |
private final AtomicReference<ByteBuffer> lastAutomaticPong = new AtomicReference<>(); |
56263 | 94 |
private final MinimalFuture<WebSocket> DONE = MinimalFuture.completedFuture(this); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
95 |
private final long closeTimeout; |
48083 | 96 |
private volatile boolean inputClosed; |
97 |
private volatile boolean outputClosed; |
|
98 |
||
99 |
private final AtomicReference<State> state = new AtomicReference<>(OPEN); |
|
100 |
||
101 |
/* Components of calls to Listener's methods */ |
|
102 |
private MessagePart part; |
|
103 |
private ByteBuffer binaryData; |
|
104 |
private CharSequence text; |
|
105 |
private int statusCode; |
|
106 |
private String reason; |
|
107 |
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
|
108 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
private final URI uri; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
private final String subprotocol; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
111 |
private final Listener listener; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
112 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
113 |
private final AtomicBoolean outstandingSend = new AtomicBoolean(); |
56263 | 114 |
private final Transport transport; |
115 |
private final SequentialScheduler receiveScheduler |
|
116 |
= new SequentialScheduler(new ReceiveTask()); |
|
48083 | 117 |
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
|
118 |
|
48083 | 119 |
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
|
120 |
Function<Result, WebSocket> newWebSocket = r -> { |
48083 | 121 |
WebSocket ws = newInstance(b.getUri(), |
122 |
r.subprotocol, |
|
123 |
b.getListener(), |
|
124 |
r.transport); |
|
125 |
// Make sure we don't release the builder until this lambda |
|
126 |
// has been executed. The builder has a strong reference to |
|
127 |
// the HttpClientFacade, and we want to keep that live until |
|
128 |
// after the raw channel is created and passed to WebSocketImpl. |
|
129 |
Reference.reachabilityFence(b); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
return ws; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
}; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
OpeningHandshake h; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
133 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
h = new OpeningHandshake(b); |
48083 | 135 |
} catch (Throwable e) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
return failedFuture(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
138 |
return h.send().thenApply(newWebSocket); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
|
48083 | 141 |
/* Exposed for testing purposes */ |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
142 |
static WebSocketImpl newInstance(URI uri, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
143 |
String subprotocol, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
144 |
Listener listener, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
145 |
TransportFactory transport) { |
48083 | 146 |
WebSocketImpl ws = new WebSocketImpl(uri, subprotocol, listener, transport); |
147 |
// This initialisation is outside of the constructor for the sake of |
|
148 |
// safe publication of WebSocketImpl.this |
|
149 |
ws.signalOpen(); |
|
150 |
return ws; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
151 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
152 |
|
48083 | 153 |
private WebSocketImpl(URI uri, |
154 |
String subprotocol, |
|
155 |
Listener listener, |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
156 |
TransportFactory transportFactory) { |
48083 | 157 |
this.uri = requireNonNull(uri); |
158 |
this.subprotocol = requireNonNull(subprotocol); |
|
159 |
this.listener = requireNonNull(listener); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
160 |
this.transport = transportFactory.createTransport( |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
161 |
new SignallingMessageConsumer()); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
162 |
closeTimeout = readCloseTimeout(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
163 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
164 |
|
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
165 |
private static int readCloseTimeout() { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
166 |
String property = "jdk.httpclient.websocket.closeTimeout"; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
167 |
int defaultValue = 30; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
168 |
String value = Utils.getNetProperty(property); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
169 |
int v; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
170 |
if (value == null) { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
171 |
v = defaultValue; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
172 |
} else { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
173 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
174 |
v = Integer.parseUnsignedInt(value); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
175 |
} catch (NumberFormatException ignored) { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
176 |
v = defaultValue; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
177 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
178 |
} |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
179 |
debug.log(Level.DEBUG, "%s=%s, using value %s", |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
180 |
property, value, v); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
181 |
return v; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
182 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
|
56263 | 184 |
// FIXME: add to action handling of errors -> signalError() |
185 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
186 |
@Override |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
187 |
public CompletableFuture<WebSocket> sendText(CharSequence message, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
188 |
boolean isLast) { |
56045 | 189 |
Objects.requireNonNull(message); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
190 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
191 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
192 |
id = sendCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
193 |
debug.log(Level.DEBUG, "%s send text: payload length=%s last=%s", |
56293 | 194 |
id, message.length(), isLast); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
195 |
} |
56293 | 196 |
CompletableFuture<WebSocket> result; |
197 |
if (!outstandingSend.compareAndSet(false, true)) { |
|
198 |
result = failedFuture(new IllegalStateException("Send pending")); |
|
199 |
} else { |
|
200 |
result = transport.sendText(message, isLast, this, |
|
201 |
(r, e) -> outstandingSend.set(false)); |
|
202 |
} |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
203 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
204 |
"%s send text: returned %s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
205 |
id, result); |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
206 |
|
56293 | 207 |
return replaceNull(result); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
208 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
209 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
210 |
@Override |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
211 |
public CompletableFuture<WebSocket> sendBinary(ByteBuffer message, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
212 |
boolean isLast) { |
56045 | 213 |
Objects.requireNonNull(message); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
214 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
215 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
216 |
id = sendCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
217 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
218 |
"%s send binary: payload=%s last=%s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
219 |
id, message, isLast); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
220 |
} |
56293 | 221 |
CompletableFuture<WebSocket> result; |
222 |
if (!outstandingSend.compareAndSet(false, true)) { |
|
223 |
result = failedFuture(new IllegalStateException("Send pending")); |
|
224 |
} else { |
|
225 |
result = transport.sendBinary(message, isLast, this, |
|
226 |
(r, e) -> outstandingSend.set(false)); |
|
227 |
} |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
228 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
229 |
"%s send binary: returned %s", id, result); |
56293 | 230 |
return replaceNull(result); |
56263 | 231 |
} |
232 |
||
233 |
private CompletableFuture<WebSocket> replaceNull( |
|
234 |
CompletableFuture<WebSocket> cf) |
|
235 |
{ |
|
236 |
if (cf == null) { |
|
237 |
return DONE; |
|
238 |
} else { |
|
239 |
return cf; |
|
240 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
241 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
244 |
public CompletableFuture<WebSocket> sendPing(ByteBuffer message) { |
56263 | 245 |
Objects.requireNonNull(message); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
246 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
247 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
248 |
id = sendCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
249 |
debug.log(Level.DEBUG, "%s send ping: payload=%s", |
56293 | 250 |
id, message); |
251 |
} |
|
252 |
CompletableFuture<WebSocket> result = transport.sendPing(message, this, |
|
253 |
(r, e) -> { }); |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
254 |
debug.log(Level.DEBUG, "%s send ping: returned %s", id, result); |
56293 | 255 |
return replaceNull(result); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
256 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
257 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
258 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
259 |
public CompletableFuture<WebSocket> sendPong(ByteBuffer message) { |
56263 | 260 |
Objects.requireNonNull(message); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
261 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
262 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
263 |
id = sendCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
264 |
debug.log(Level.DEBUG, "%s send pong: payload=%s", |
56293 | 265 |
id, message); |
266 |
} |
|
267 |
CompletableFuture<WebSocket> result = transport.sendPong(message, this, |
|
268 |
(r, e) -> { }); |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
269 |
debug.log(Level.DEBUG, "%s send pong: returned %s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
270 |
id, result); |
56293 | 271 |
return replaceNull(result); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
272 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
273 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
274 |
@Override |
56263 | 275 |
public CompletableFuture<WebSocket> sendClose(int statusCode, |
276 |
String reason) { |
|
56058 | 277 |
Objects.requireNonNull(reason); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
278 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
279 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
280 |
id = sendCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
281 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
282 |
"%s send close: statusCode=%s, reason.length=%s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
283 |
id, statusCode, reason); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
284 |
} |
56293 | 285 |
CompletableFuture<WebSocket> result; |
286 |
if (!isLegalToSendFromClient(statusCode)) { |
|
287 |
result = failedFuture(new IllegalArgumentException("statusCode")); |
|
288 |
} else { |
|
289 |
// check outputClosed |
|
290 |
result = sendClose0(statusCode, reason); |
|
291 |
} |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
292 |
debug.log(Level.DEBUG, "%s send close: returned %s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
293 |
id, result); |
56293 | 294 |
return replaceNull(result); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
295 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
296 |
|
56263 | 297 |
private CompletableFuture<WebSocket> sendClose0(int statusCode, |
298 |
String reason) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
299 |
outputClosed = true; |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
300 |
CompletableFuture<WebSocket> cf |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
301 |
= transport.sendClose(statusCode, reason, this, (r, e) -> { }); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
302 |
CompletableFuture<WebSocket> closeOrTimeout |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
303 |
= replaceNull(cf).orTimeout(closeTimeout, TimeUnit.SECONDS); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
304 |
// The snippet below, whose purpose might not be immediately obvious, |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
305 |
// is a trick used to complete a dependant stage with an IOException. |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
306 |
// A checked IOException cannot be thrown from inside the BiConsumer |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
307 |
// supplied to the handle method. Instead a CompletionStage completed |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
308 |
// exceptionally with this IOException is returned. |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
309 |
return closeOrTimeout.handle(this::processCloseOutcome) |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
310 |
.thenCompose(Function.identity()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
311 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
312 |
|
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
313 |
private CompletionStage<WebSocket> processCloseOutcome(WebSocket webSocket, |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
314 |
Throwable e) { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
315 |
if (e == null) { |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
316 |
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
|
317 |
} else { |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
318 |
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
|
319 |
} |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
320 |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
321 |
if (e == null) { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
322 |
return completedFuture(webSocket); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
323 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
324 |
Throwable cause = Utils.getCompletionCause(e); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
325 |
if (cause instanceof IllegalArgumentException) { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
326 |
return failedFuture(cause); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
327 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
328 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
329 |
transport.closeOutput(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
330 |
} catch (IOException ignored) { } |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
331 |
|
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
332 |
if (cause instanceof TimeoutException) { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
333 |
inputClosed = true; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
334 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
335 |
transport.closeInput(); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
336 |
} catch (IOException ignored) { } |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
337 |
return failedFuture(new InterruptedIOException( |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
338 |
"Could not send close within a reasonable timeout")); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
339 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
340 |
return failedFuture(cause); |
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() { |
358 |
return outputClosed; |
|
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; |
370 |
outputClosed = true; |
|
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: |
48083 | 407 |
while (true) { |
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); |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
410 |
|
48083 | 411 |
try { |
412 |
switch (s) { |
|
413 |
case OPEN: |
|
414 |
processOpen(); |
|
415 |
tryChangeState(OPEN, IDLE); |
|
416 |
break; |
|
417 |
case TEXT: |
|
418 |
processText(); |
|
419 |
tryChangeState(TEXT, IDLE); |
|
420 |
break; |
|
421 |
case BINARY: |
|
422 |
processBinary(); |
|
423 |
tryChangeState(BINARY, IDLE); |
|
424 |
break; |
|
425 |
case PING: |
|
426 |
processPing(); |
|
427 |
tryChangeState(PING, IDLE); |
|
428 |
break; |
|
429 |
case PONG: |
|
430 |
processPong(); |
|
431 |
tryChangeState(PONG, IDLE); |
|
432 |
break; |
|
433 |
case CLOSE: |
|
434 |
processClose(); |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
435 |
break loop; |
48083 | 436 |
case ERROR: |
437 |
processError(); |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
438 |
break loop; |
48083 | 439 |
case IDLE: |
440 |
if (demand.tryDecrement() |
|
441 |
&& tryChangeState(IDLE, WAITING)) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
442 |
transport.request(1); |
48083 | 443 |
} |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
444 |
break loop; |
48083 | 445 |
case WAITING: |
446 |
// For debugging spurious signalling: when there was a |
|
447 |
// signal, but apparently nothing has changed |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
448 |
break loop; |
48083 | 449 |
default: |
450 |
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
|
451 |
} |
48083 | 452 |
} catch (Throwable t) { |
453 |
signalError(t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
454 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
455 |
} |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
456 |
debug.log(Level.DEBUG, "exit receive task"); |
48083 | 457 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
458 |
|
48083 | 459 |
private void processError() throws IOException { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
460 |
debug.log(Level.DEBUG, "processError"); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
461 |
transport.closeInput(); |
48083 | 462 |
receiveScheduler.stop(); |
463 |
Throwable err = error.get(); |
|
464 |
if (err instanceof FailWebSocketException) { |
|
465 |
int code1 = ((FailWebSocketException) err).getStatusCode(); |
|
466 |
err = new ProtocolException().initCause(err); |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
467 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
468 |
"failing %s with error=%s statusCode=%s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
469 |
WebSocketImpl.this, err, code1); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
470 |
sendClose0(code1, "") // TODO handle errors from here |
48083 | 471 |
.whenComplete( |
472 |
(r, e) -> { |
|
473 |
if (e != null) { |
|
474 |
Log.logError(e); |
|
475 |
} |
|
476 |
}); |
|
477 |
} |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
478 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
479 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
480 |
id = receiveCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
481 |
debug.log(Level.DEBUG, "enter onError %s error=%s", |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
482 |
id, err); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
483 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
484 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
485 |
listener.onError(WebSocketImpl.this, err); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
486 |
} finally { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
487 |
debug.log(Level.DEBUG, "exit onError %s", id); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
488 |
} |
48083 | 489 |
} |
490 |
||
491 |
private void processClose() throws IOException { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
492 |
debug.log(Level.DEBUG, "processClose"); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
493 |
transport.closeInput(); |
48083 | 494 |
receiveScheduler.stop(); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
495 |
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
|
496 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
497 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
498 |
id = receiveCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
499 |
debug.log(Level.DEBUG, "enter onClose %s statusCode=%s reason.length=%s", |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
500 |
id, statusCode, reason.length()); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
501 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
502 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
503 |
cs = listener.onClose(WebSocketImpl.this, statusCode, reason); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
504 |
} finally { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
505 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
506 |
"exit onClose %s returned %s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
507 |
id, cs); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
508 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
509 |
if (cs == null) { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
510 |
cs = DONE; |
48083 | 511 |
} |
512 |
int code; |
|
513 |
if (statusCode == NO_STATUS_CODE || statusCode == CLOSED_ABNORMALLY) { |
|
514 |
code = NORMAL_CLOSURE; |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
515 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
516 |
"using statusCode %s instead of %s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
517 |
statusCode, code); |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
518 |
|
48083 | 519 |
} else { |
520 |
code = statusCode; |
|
521 |
} |
|
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
522 |
cs.whenComplete((r, e) -> { // TODO log |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
523 |
sendClose0(code, "") // TODO handle errors from here |
48083 | 524 |
.whenComplete((r1, e1) -> { |
56263 | 525 |
if (e1 != null) { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
526 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
527 |
"processClose completed with errors", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
528 |
e1); |
56263 | 529 |
} |
48083 | 530 |
}); |
531 |
}); |
|
532 |
} |
|
533 |
||
534 |
private void processPong() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
535 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
536 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
537 |
id = receiveCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
538 |
debug.log(Level.DEBUG, "enter onPong %s payload=%s", |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
539 |
id, binaryData); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
540 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
541 |
CompletionStage<?> cs = null; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
542 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
543 |
cs = listener.onPong(WebSocketImpl.this, binaryData); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
544 |
} finally { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
545 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
546 |
"exit onPong %s returned %s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
547 |
id, cs); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
548 |
} |
48083 | 549 |
} |
550 |
||
551 |
private void processPing() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
552 |
debug.log(Level.DEBUG, "processPing"); |
48083 | 553 |
ByteBuffer slice = binaryData.slice(); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
554 |
// 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
|
555 |
// replying Pong could be done in parallel with the listener |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
556 |
// handling this Ping. |
48083 | 557 |
ByteBuffer copy = ByteBuffer.allocate(binaryData.remaining()) |
558 |
.put(binaryData) |
|
559 |
.flip(); |
|
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
560 |
if (!trySwapAutomaticPong(copy)) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
561 |
// Non-exclusive send; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
562 |
BiConsumer<WebSocketImpl, Throwable> reporter = (r, e) -> { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
563 |
if (e != null) { // TODO: better error handing. What if already closed? |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
564 |
signalError(Utils.getCompletionCause(e)); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
565 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
566 |
}; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
567 |
transport.sendPong(WebSocketImpl.this::clearAutomaticPong, |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
568 |
WebSocketImpl.this, |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
569 |
reporter); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
570 |
} |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
571 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
572 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
573 |
id = receiveCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
574 |
debug.log(Level.DEBUG, "enter onPing %s payload=%s", |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
575 |
id, slice); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
576 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
577 |
CompletionStage<?> cs = null; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
578 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
579 |
cs = listener.onPing(WebSocketImpl.this, slice); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
580 |
} finally { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
581 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
582 |
"exit onPing %s returned %s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
583 |
id, cs); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
584 |
} |
48083 | 585 |
} |
586 |
||
587 |
private void processBinary() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
588 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
589 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
590 |
id = receiveCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
591 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
592 |
"enter onBinary %s payload=%s part=%s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
593 |
id, binaryData, part); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
594 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
595 |
CompletionStage<?> cs = null; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
596 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
597 |
cs = listener.onBinary(WebSocketImpl.this, binaryData, part); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
598 |
} finally { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
599 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
600 |
"exit onBinary %s returned %s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
601 |
id, cs); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
602 |
} |
48083 | 603 |
} |
604 |
||
605 |
private void processText() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
606 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
607 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
608 |
id = receiveCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
609 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
610 |
"enter onText %s payload.length=%s part=%s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
611 |
id, text.length(), part); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
612 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
613 |
CompletionStage<?> cs = null; |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
614 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
615 |
cs = listener.onText(WebSocketImpl.this, text, part); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
616 |
} finally { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
617 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
618 |
"exit onText %s returned %s", id, cs); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
619 |
} |
48083 | 620 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
621 |
|
48083 | 622 |
private void processOpen() { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
623 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
624 |
if (debug.isLoggable(Level.DEBUG)) { |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
625 |
id = receiveCounter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
626 |
debug.log(Level.DEBUG, "enter onOpen %s", id); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
627 |
} |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
628 |
try { |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
629 |
listener.onOpen(WebSocketImpl.this); |
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
630 |
} finally { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
631 |
debug.log(Level.DEBUG, "exit onOpen %s", id); |
56294
181bc33917e4
http-client-branch: (WebSocket) logging and closure timer
prappo
parents:
56293
diff
changeset
|
632 |
} |
48083 | 633 |
} |
634 |
} |
|
635 |
||
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
636 |
private ByteBuffer clearAutomaticPong() { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
637 |
ByteBuffer data; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
638 |
do { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
639 |
data = lastAutomaticPong.get(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
640 |
if (data == null) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
641 |
// 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
|
642 |
// using it has been added previously |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
643 |
throw new InternalError(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
644 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
645 |
} while (!lastAutomaticPong.compareAndSet(data, null)); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
646 |
return data; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
647 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
648 |
|
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
649 |
private boolean trySwapAutomaticPong(ByteBuffer copy) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
650 |
ByteBuffer message; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
651 |
boolean swapped; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
652 |
while (true) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
653 |
message = lastAutomaticPong.get(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
654 |
if (message == null) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
655 |
if (!lastAutomaticPong.compareAndSet(null, copy)) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
656 |
// 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
|
657 |
// 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
|
658 |
// time. Thus no failure in the atomic operation above is |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
659 |
// expected. |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
660 |
throw new InternalError(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
661 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
662 |
swapped = false; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
663 |
break; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
664 |
} else if (lastAutomaticPong.compareAndSet(message, copy)) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
665 |
swapped = true; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
666 |
break; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
667 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
668 |
} |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
669 |
debug.log(Level.DEBUG, "swapped automatic pong from %s to %s%n", |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
670 |
message, copy); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
671 |
return swapped; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
672 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56297
diff
changeset
|
673 |
|
48083 | 674 |
private void signalOpen() { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
675 |
debug.log(Level.DEBUG, "signalOpen"); |
48083 | 676 |
receiveScheduler.runOrSchedule(); |
677 |
} |
|
678 |
||
679 |
private void signalError(Throwable error) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
680 |
debug.log(Level.DEBUG, "signalError %s", error); |
48083 | 681 |
inputClosed = true; |
682 |
outputClosed = true; |
|
683 |
if (!this.error.compareAndSet(null, error) || !trySetState(ERROR)) { |
|
684 |
Log.logError(error); |
|
685 |
} else { |
|
686 |
close(); |
|
687 |
} |
|
688 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
689 |
|
48083 | 690 |
private void close() { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
691 |
debug.log(Level.DEBUG, "close"); |
56263 | 692 |
Throwable first = null; |
48083 | 693 |
try { |
56263 | 694 |
transport.closeInput(); |
695 |
} catch (Throwable t1) { |
|
696 |
first = t1; |
|
697 |
} finally { |
|
698 |
Throwable second = null; |
|
48083 | 699 |
try { |
56263 | 700 |
transport.closeOutput(); |
701 |
} catch (Throwable t2) { |
|
702 |
second = t2; |
|
48083 | 703 |
} finally { |
56263 | 704 |
Throwable e = null; |
705 |
if (first != null && second != null) { |
|
706 |
first.addSuppressed(second); |
|
707 |
e = first; |
|
708 |
} else if (first != null) { |
|
709 |
e = first; |
|
710 |
} else if (second != null) { |
|
711 |
e = second; |
|
712 |
} |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
713 |
if (e != null) { |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
714 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
715 |
"unexpected exception in close: ", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
716 |
e); |
56263 | 717 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
718 |
} |
48083 | 719 |
} |
720 |
} |
|
721 |
||
722 |
private void signalClose(int statusCode, String reason) { |
|
56263 | 723 |
// FIXME: make sure no race reason & close are not intermixed |
48083 | 724 |
inputClosed = true; |
725 |
this.statusCode = statusCode; |
|
726 |
this.reason = reason; |
|
56263 | 727 |
boolean managed = trySetState(CLOSE); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
728 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
729 |
"signalClose statusCode=%s, reason.length()=%s: %s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
730 |
statusCode, reason.length(), managed); |
56263 | 731 |
if (managed) { |
48083 | 732 |
try { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
733 |
transport.closeInput(); |
48083 | 734 |
} catch (Throwable t) { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
735 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
736 |
"unexpected exception closing input", t); |
48083 | 737 |
} |
738 |
} |
|
739 |
} |
|
740 |
||
741 |
private class SignallingMessageConsumer implements MessageStreamConsumer { |
|
742 |
||
743 |
@Override |
|
744 |
public void onText(CharSequence data, MessagePart part) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
745 |
transport.acknowledgeReception(); |
48083 | 746 |
text = data; |
747 |
WebSocketImpl.this.part = part; |
|
748 |
tryChangeState(WAITING, TEXT); |
|
749 |
} |
|
750 |
||
751 |
@Override |
|
752 |
public void onBinary(ByteBuffer data, MessagePart part) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
753 |
transport.acknowledgeReception(); |
48083 | 754 |
binaryData = data; |
755 |
WebSocketImpl.this.part = part; |
|
756 |
tryChangeState(WAITING, BINARY); |
|
757 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
758 |
|
48083 | 759 |
@Override |
760 |
public void onPing(ByteBuffer data) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
761 |
transport.acknowledgeReception(); |
48083 | 762 |
binaryData = data; |
763 |
tryChangeState(WAITING, PING); |
|
764 |
} |
|
765 |
||
766 |
@Override |
|
767 |
public void onPong(ByteBuffer data) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
768 |
transport.acknowledgeReception(); |
48083 | 769 |
binaryData = data; |
770 |
tryChangeState(WAITING, PONG); |
|
771 |
} |
|
772 |
||
773 |
@Override |
|
774 |
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
|
775 |
transport.acknowledgeReception(); |
48083 | 776 |
signalClose(statusCode, reason.toString()); |
777 |
} |
|
778 |
||
779 |
@Override |
|
780 |
public void onComplete() { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
781 |
transport.acknowledgeReception(); |
48083 | 782 |
signalClose(CLOSED_ABNORMALLY, ""); |
783 |
} |
|
784 |
||
785 |
@Override |
|
786 |
public void onError(Throwable error) { |
|
787 |
signalError(error); |
|
788 |
} |
|
789 |
} |
|
790 |
||
791 |
private boolean trySetState(State newState) { |
|
56263 | 792 |
State currentState; |
793 |
boolean success = false; |
|
48083 | 794 |
while (true) { |
56263 | 795 |
currentState = state.get(); |
48083 | 796 |
if (currentState == ERROR || currentState == CLOSE) { |
56263 | 797 |
break; |
48083 | 798 |
} else if (state.compareAndSet(currentState, newState)) { |
799 |
receiveScheduler.runOrSchedule(); |
|
56263 | 800 |
success = true; |
801 |
break; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
802 |
} |
48083 | 803 |
} |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
804 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
805 |
"set state %s (previous %s) %s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
806 |
newState, currentState, success); |
56263 | 807 |
return success; |
48083 | 808 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
809 |
|
48083 | 810 |
private boolean tryChangeState(State expectedState, State newState) { |
811 |
State witness = state.compareAndExchange(expectedState, newState); |
|
56263 | 812 |
boolean success = false; |
48083 | 813 |
if (witness == expectedState) { |
814 |
receiveScheduler.runOrSchedule(); |
|
56263 | 815 |
success = true; |
816 |
} else if (witness != ERROR && witness != CLOSE) { |
|
817 |
// This should be the only reason for inability to change the state |
|
818 |
// from IDLE to WAITING: the state has changed to terminal |
|
48083 | 819 |
throw new InternalError(); |
820 |
} |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
821 |
debug.log(Level.DEBUG, |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
822 |
"change state from %s to %s %s", |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
823 |
expectedState, newState, success); |
56263 | 824 |
return success; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
825 |
} |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
826 |
|
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
827 |
/* Exposed for testing purposes */ |
56263 | 828 |
protected Transport transport() { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
829 |
return transport; |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
830 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
831 |
} |