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