author | prappo |
Fri, 09 Mar 2018 16:47:00 +0000 | |
branch | http-client-branch |
changeset 56269 | 234813fd33bc |
parent 56263 | 4933a477d628 |
child 56291 | c8c4c707ff3a |
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; |
48083 | 36 |
import java.lang.ref.Reference; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.net.ProtocolException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.net.URI; |
56263 | 39 |
import java.net.http.WebSocket; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
import java.nio.ByteBuffer; |
56045 | 41 |
import java.util.Objects; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
43 |
import java.util.concurrent.CompletionStage; |
48083 | 44 |
import java.util.concurrent.TimeoutException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
import java.util.concurrent.atomic.AtomicBoolean; |
48083 | 46 |
import java.util.concurrent.atomic.AtomicReference; |
56263 | 47 |
import java.util.function.BiConsumer; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
import java.util.function.Function; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
import static java.util.Objects.requireNonNull; |
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
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
|
54 |
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
|
55 |
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
|
56 |
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
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
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
|
62 |
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
|
63 |
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
|
64 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
* A WebSocket client. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
67 |
*/ |
48083 | 68 |
public final class WebSocketImpl implements WebSocket { |
69 |
||
56263 | 70 |
private final static boolean DEBUG = false; |
71 |
||
48083 | 72 |
enum State { |
73 |
OPEN, |
|
74 |
IDLE, |
|
75 |
WAITING, |
|
76 |
TEXT, |
|
77 |
BINARY, |
|
78 |
PING, |
|
79 |
PONG, |
|
80 |
CLOSE, |
|
81 |
ERROR; |
|
82 |
} |
|
83 |
||
56263 | 84 |
private final MinimalFuture<WebSocket> DONE = MinimalFuture.completedFuture(this); |
48083 | 85 |
private volatile boolean inputClosed; |
86 |
private volatile boolean outputClosed; |
|
87 |
||
88 |
private final AtomicReference<State> state = new AtomicReference<>(OPEN); |
|
89 |
||
90 |
/* Components of calls to Listener's methods */ |
|
91 |
private MessagePart part; |
|
92 |
private ByteBuffer binaryData; |
|
93 |
private CharSequence text; |
|
94 |
private int statusCode; |
|
95 |
private String reason; |
|
96 |
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
|
97 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
98 |
private final URI uri; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
99 |
private final String subprotocol; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
100 |
private final Listener listener; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
101 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
102 |
private final AtomicBoolean outstandingSend = new AtomicBoolean(); |
56263 | 103 |
private final Transport transport; |
104 |
private final SequentialScheduler receiveScheduler |
|
105 |
= new SequentialScheduler(new ReceiveTask()); |
|
48083 | 106 |
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
|
107 |
|
48083 | 108 |
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
|
109 |
Function<Result, WebSocket> newWebSocket = r -> { |
48083 | 110 |
WebSocket ws = newInstance(b.getUri(), |
111 |
r.subprotocol, |
|
112 |
b.getListener(), |
|
113 |
r.transport); |
|
114 |
// Make sure we don't release the builder until this lambda |
|
115 |
// has been executed. The builder has a strong reference to |
|
116 |
// the HttpClientFacade, and we want to keep that live until |
|
117 |
// after the raw channel is created and passed to WebSocketImpl. |
|
118 |
Reference.reachabilityFence(b); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
119 |
return ws; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
120 |
}; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
OpeningHandshake h; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
122 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
h = new OpeningHandshake(b); |
48083 | 124 |
} catch (Throwable e) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
125 |
return failedFuture(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
126 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
127 |
return h.send().thenApply(newWebSocket); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
128 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
129 |
|
48083 | 130 |
/* Exposed for testing purposes */ |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
131 |
static WebSocketImpl newInstance(URI uri, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
132 |
String subprotocol, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
133 |
Listener listener, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
134 |
TransportFactory transport) { |
48083 | 135 |
WebSocketImpl ws = new WebSocketImpl(uri, subprotocol, listener, transport); |
136 |
// This initialisation is outside of the constructor for the sake of |
|
137 |
// safe publication of WebSocketImpl.this |
|
138 |
ws.signalOpen(); |
|
139 |
return ws; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
|
48083 | 142 |
private WebSocketImpl(URI uri, |
143 |
String subprotocol, |
|
144 |
Listener listener, |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
145 |
TransportFactory transportFactory) { |
48083 | 146 |
this.uri = requireNonNull(uri); |
147 |
this.subprotocol = requireNonNull(subprotocol); |
|
148 |
this.listener = requireNonNull(listener); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
149 |
this.transport = transportFactory.createTransport( |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
150 |
new SignallingMessageConsumer()); |
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 |
|
56263 | 153 |
// FIXME: add to action handling of errors -> signalError() |
154 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
155 |
@Override |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
156 |
public CompletableFuture<WebSocket> sendText(CharSequence message, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
157 |
boolean isLast) { |
56045 | 158 |
Objects.requireNonNull(message); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
159 |
if (!outstandingSend.compareAndSet(false, true)) { |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
160 |
return failedFuture(new IllegalStateException("Send pending")); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
161 |
} |
56263 | 162 |
CompletableFuture<WebSocket> cf |
163 |
= transport.sendText(message, isLast, this, |
|
164 |
(r, e) -> outstandingSend.set(false)); |
|
165 |
return replaceNull(cf); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
166 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
167 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
168 |
@Override |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
169 |
public CompletableFuture<WebSocket> sendBinary(ByteBuffer message, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
170 |
boolean isLast) { |
56045 | 171 |
Objects.requireNonNull(message); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
172 |
if (!outstandingSend.compareAndSet(false, true)) { |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
173 |
return failedFuture(new IllegalStateException("Send pending")); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
174 |
} |
56263 | 175 |
CompletableFuture<WebSocket> cf |
176 |
= transport.sendBinary(message, isLast, this, |
|
177 |
(r, e) -> outstandingSend.set(false)); |
|
178 |
return replaceNull(cf); |
|
179 |
} |
|
180 |
||
181 |
private CompletableFuture<WebSocket> replaceNull( |
|
182 |
CompletableFuture<WebSocket> cf) |
|
183 |
{ |
|
184 |
if (cf == null) { |
|
185 |
return DONE; |
|
186 |
} else { |
|
187 |
return cf; |
|
188 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
189 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
190 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
191 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
public CompletableFuture<WebSocket> sendPing(ByteBuffer message) { |
56263 | 193 |
Objects.requireNonNull(message); |
194 |
CompletableFuture<WebSocket> cf |
|
195 |
= transport.sendPing(message, this, (r, e) -> { }); |
|
196 |
return replaceNull(cf); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
197 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
198 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
199 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
200 |
public CompletableFuture<WebSocket> sendPong(ByteBuffer message) { |
56263 | 201 |
Objects.requireNonNull(message); |
202 |
CompletableFuture<WebSocket> cf |
|
203 |
= transport.sendPong(message, this, (r, e) -> { }); |
|
204 |
return replaceNull(cf); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
205 |
} |
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 |
@Override |
56263 | 208 |
public CompletableFuture<WebSocket> sendClose(int statusCode, |
209 |
String reason) { |
|
56058 | 210 |
Objects.requireNonNull(reason); |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
211 |
if (!isLegalToSendFromClient(statusCode)) { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
212 |
return failedFuture(new IllegalArgumentException("statusCode")); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
213 |
} |
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
214 |
// check outputClosed |
56263 | 215 |
CompletableFuture<WebSocket> cf = sendClose0(statusCode, reason); |
216 |
return replaceNull(cf); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
218 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
219 |
/* |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
220 |
* Sends a Close message, then shuts down the output since no more |
56263 | 221 |
* messages are expected to be sent at this point. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
222 |
*/ |
56263 | 223 |
private CompletableFuture<WebSocket> sendClose0(int statusCode, |
224 |
String reason) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
225 |
outputClosed = true; |
56263 | 226 |
BiConsumer<WebSocket, Throwable> closer = (r, e) -> { |
227 |
Throwable cause = Utils.getCompletionCause(e); |
|
228 |
if (cause instanceof IllegalArgumentException) { |
|
229 |
// or pre=check it (isLegalToSendFromClient(statusCode)) |
|
230 |
return; |
|
231 |
} |
|
232 |
try { |
|
233 |
transport.closeOutput(); |
|
234 |
} catch (IOException ex) { |
|
235 |
Log.logError(ex); |
|
236 |
} |
|
237 |
if (cause instanceof TimeoutException) { // FIXME: it is not the case anymore |
|
238 |
if (DEBUG) { |
|
239 |
System.out.println("[WebSocket] sendClose0 error: " + e); |
|
240 |
} |
|
241 |
try { |
|
242 |
transport.closeInput(); |
|
243 |
} catch (IOException ex) { |
|
244 |
Log.logError(ex); |
|
245 |
} |
|
246 |
} |
|
247 |
}; |
|
248 |
CompletableFuture<WebSocket> cf |
|
249 |
= transport.sendClose(statusCode, reason, this, closer); |
|
250 |
return cf; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
251 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
252 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
253 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
254 |
public void request(long n) { |
56263 | 255 |
if (DEBUG) { |
256 |
System.out.printf("[WebSocket] request(%s)%n", n); |
|
257 |
} |
|
48083 | 258 |
if (demand.increase(n)) { |
259 |
receiveScheduler.runOrSchedule(); |
|
260 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
261 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
262 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
263 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
264 |
public String getSubprotocol() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
265 |
return subprotocol; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
266 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
267 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
268 |
@Override |
48083 | 269 |
public boolean isOutputClosed() { |
270 |
return outputClosed; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
271 |
} |
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 |
@Override |
48083 | 274 |
public boolean isInputClosed() { |
275 |
return inputClosed; |
|
276 |
} |
|
277 |
||
278 |
@Override |
|
279 |
public void abort() { |
|
56263 | 280 |
if (DEBUG) { |
281 |
System.out.printf("[WebSocket] abort()%n"); |
|
282 |
} |
|
48083 | 283 |
inputClosed = true; |
284 |
outputClosed = true; |
|
285 |
receiveScheduler.stop(); |
|
286 |
close(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
287 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
288 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
289 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
290 |
public String toString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
291 |
return super.toString() |
48083 | 292 |
+ "[uri=" + uri |
293 |
+ (!subprotocol.isEmpty() ? ", subprotocol=" + subprotocol : "") |
|
294 |
+ "]"; |
|
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 |
|
48083 | 297 |
/* |
298 |
* The assumptions about order is as follows: |
|
299 |
* |
|
300 |
* - state is never changed more than twice inside the `run` method: |
|
301 |
* x --(1)--> IDLE --(2)--> y (otherwise we're loosing events, or |
|
302 |
* overwriting parts of messages creating a mess since there's no |
|
303 |
* queueing) |
|
304 |
* - OPEN is always the first state |
|
305 |
* - no messages are requested/delivered before onOpen is called (this |
|
306 |
* is implemented by making WebSocket instance accessible first in |
|
307 |
* onOpen) |
|
308 |
* - after the state has been observed as CLOSE/ERROR, the scheduler |
|
309 |
* is stopped |
|
310 |
*/ |
|
311 |
private class ReceiveTask extends SequentialScheduler.CompleteRestartableTask { |
|
312 |
||
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
313 |
// Transport only asked here and nowhere else because we must make sure |
48083 | 314 |
// onOpen is invoked first and no messages become pending before onOpen |
315 |
// finishes |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
316 |
|
48083 | 317 |
@Override |
318 |
public void run() { |
|
319 |
while (true) { |
|
320 |
State s = state.get(); |
|
321 |
try { |
|
322 |
switch (s) { |
|
323 |
case OPEN: |
|
324 |
processOpen(); |
|
325 |
tryChangeState(OPEN, IDLE); |
|
326 |
break; |
|
327 |
case TEXT: |
|
328 |
processText(); |
|
329 |
tryChangeState(TEXT, IDLE); |
|
330 |
break; |
|
331 |
case BINARY: |
|
332 |
processBinary(); |
|
333 |
tryChangeState(BINARY, IDLE); |
|
334 |
break; |
|
335 |
case PING: |
|
336 |
processPing(); |
|
337 |
tryChangeState(PING, IDLE); |
|
338 |
break; |
|
339 |
case PONG: |
|
340 |
processPong(); |
|
341 |
tryChangeState(PONG, IDLE); |
|
342 |
break; |
|
343 |
case CLOSE: |
|
344 |
processClose(); |
|
345 |
return; |
|
346 |
case ERROR: |
|
347 |
processError(); |
|
348 |
return; |
|
349 |
case IDLE: |
|
350 |
if (demand.tryDecrement() |
|
351 |
&& tryChangeState(IDLE, WAITING)) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
352 |
transport.request(1); |
48083 | 353 |
} |
354 |
return; |
|
355 |
case WAITING: |
|
356 |
// For debugging spurious signalling: when there was a |
|
357 |
// signal, but apparently nothing has changed |
|
358 |
return; |
|
359 |
default: |
|
360 |
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
|
361 |
} |
48083 | 362 |
} catch (Throwable t) { |
363 |
signalError(t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
} |
48083 | 366 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
|
48083 | 368 |
private void processError() throws IOException { |
56263 | 369 |
if (DEBUG) { |
370 |
System.out.println("[WebSocket] processError"); |
|
371 |
} |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
372 |
transport.closeInput(); |
48083 | 373 |
receiveScheduler.stop(); |
374 |
Throwable err = error.get(); |
|
375 |
if (err instanceof FailWebSocketException) { |
|
376 |
int code1 = ((FailWebSocketException) err).getStatusCode(); |
|
377 |
err = new ProtocolException().initCause(err); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
378 |
sendClose0(code1, "") |
48083 | 379 |
.whenComplete( |
380 |
(r, e) -> { |
|
381 |
if (e != null) { |
|
382 |
Log.logError(e); |
|
383 |
} |
|
384 |
}); |
|
385 |
} |
|
386 |
listener.onError(WebSocketImpl.this, err); |
|
387 |
} |
|
388 |
||
389 |
private void processClose() throws IOException { |
|
56263 | 390 |
if (DEBUG) { |
391 |
System.out.println("[WebSocket] processClose"); |
|
392 |
} |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
393 |
transport.closeInput(); |
48083 | 394 |
receiveScheduler.stop(); |
395 |
CompletionStage<?> readyToClose; |
|
396 |
readyToClose = listener.onClose(WebSocketImpl.this, statusCode, reason); |
|
397 |
if (readyToClose == null) { |
|
56263 | 398 |
readyToClose = DONE; |
48083 | 399 |
} |
400 |
int code; |
|
401 |
if (statusCode == NO_STATUS_CODE || statusCode == CLOSED_ABNORMALLY) { |
|
402 |
code = NORMAL_CLOSURE; |
|
56263 | 403 |
if (DEBUG) { |
404 |
System.out.printf("[WebSocket] using statusCode %s instead of %s%n", |
|
405 |
statusCode, code); |
|
406 |
} |
|
48083 | 407 |
} else { |
408 |
code = statusCode; |
|
409 |
} |
|
410 |
readyToClose.whenComplete((r, e) -> { |
|
56263 | 411 |
sendClose0(code, "") // FIXME errors from here? |
48083 | 412 |
.whenComplete((r1, e1) -> { |
56263 | 413 |
if (DEBUG) { |
414 |
if (e1 != null) { |
|
415 |
e1.printStackTrace(System.out); |
|
416 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
417 |
} |
48083 | 418 |
}); |
419 |
}); |
|
420 |
} |
|
421 |
||
422 |
private void processPong() { |
|
423 |
listener.onPong(WebSocketImpl.this, binaryData); |
|
424 |
} |
|
425 |
||
426 |
private void processPing() { |
|
427 |
// Let's make a full copy of this tiny data. What we want here |
|
428 |
// is to rule out a possibility the shared data we send might be |
|
429 |
// corrupted by processing in the listener. |
|
430 |
ByteBuffer slice = binaryData.slice(); |
|
431 |
ByteBuffer copy = ByteBuffer.allocate(binaryData.remaining()) |
|
432 |
.put(binaryData) |
|
433 |
.flip(); |
|
434 |
// Non-exclusive send; |
|
56263 | 435 |
BiConsumer<WebSocketImpl, Throwable> reporter = (r, e) -> { |
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
436 |
if (e != null) { // Better error handing. What if already closed? |
56263 | 437 |
signalError(Utils.getCompletionCause(e)); |
438 |
} |
|
439 |
}; |
|
440 |
transport.sendPong(copy, WebSocketImpl.this, reporter); |
|
48083 | 441 |
listener.onPing(WebSocketImpl.this, slice); |
442 |
} |
|
443 |
||
444 |
private void processBinary() { |
|
445 |
listener.onBinary(WebSocketImpl.this, binaryData, part); |
|
446 |
} |
|
447 |
||
448 |
private void processText() { |
|
449 |
listener.onText(WebSocketImpl.this, text, part); |
|
450 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
451 |
|
48083 | 452 |
private void processOpen() { |
453 |
listener.onOpen(WebSocketImpl.this); |
|
454 |
} |
|
455 |
} |
|
456 |
||
457 |
private void signalOpen() { |
|
56263 | 458 |
if (DEBUG) { |
459 |
System.out.printf("[WebSocket] signalOpen%n"); |
|
460 |
} |
|
48083 | 461 |
receiveScheduler.runOrSchedule(); |
462 |
} |
|
463 |
||
464 |
private void signalError(Throwable error) { |
|
56263 | 465 |
if (DEBUG) { |
466 |
System.out.printf("[WebSocket] signalError %s%n", error); |
|
467 |
} |
|
48083 | 468 |
inputClosed = true; |
469 |
outputClosed = true; |
|
470 |
if (!this.error.compareAndSet(null, error) || !trySetState(ERROR)) { |
|
471 |
Log.logError(error); |
|
472 |
} else { |
|
473 |
close(); |
|
474 |
} |
|
475 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
476 |
|
48083 | 477 |
private void close() { |
56263 | 478 |
if (DEBUG) { |
479 |
System.out.println("[WebSocket] close"); |
|
480 |
} |
|
481 |
Throwable first = null; |
|
48083 | 482 |
try { |
56263 | 483 |
transport.closeInput(); |
484 |
} catch (Throwable t1) { |
|
485 |
first = t1; |
|
486 |
} finally { |
|
487 |
Throwable second = null; |
|
48083 | 488 |
try { |
56263 | 489 |
transport.closeOutput(); |
490 |
} catch (Throwable t2) { |
|
491 |
second = t2; |
|
48083 | 492 |
} finally { |
56263 | 493 |
Throwable e = null; |
494 |
if (first != null && second != null) { |
|
495 |
first.addSuppressed(second); |
|
496 |
e = first; |
|
497 |
} else if (first != null) { |
|
498 |
e = first; |
|
499 |
} else if (second != null) { |
|
500 |
e = second; |
|
501 |
} |
|
502 |
if (DEBUG) { |
|
503 |
if (e != null) { |
|
504 |
e.printStackTrace(System.out); |
|
505 |
} |
|
506 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
507 |
} |
48083 | 508 |
} |
509 |
} |
|
510 |
||
511 |
private void signalClose(int statusCode, String reason) { |
|
56263 | 512 |
// FIXME: make sure no race reason & close are not intermixed |
48083 | 513 |
inputClosed = true; |
514 |
this.statusCode = statusCode; |
|
515 |
this.reason = reason; |
|
56263 | 516 |
boolean managed = trySetState(CLOSE); |
517 |
if (DEBUG) { |
|
518 |
System.out.printf("[WebSocket] signalClose statusCode=%s, reason.length()=%s: %s%n", |
|
519 |
statusCode, reason.length(), managed); |
|
520 |
} |
|
521 |
if (managed) { |
|
48083 | 522 |
try { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
523 |
transport.closeInput(); |
48083 | 524 |
} catch (Throwable t) { |
56263 | 525 |
if (DEBUG) { |
526 |
t.printStackTrace(System.out); |
|
527 |
} |
|
48083 | 528 |
} |
529 |
} |
|
530 |
} |
|
531 |
||
532 |
private class SignallingMessageConsumer implements MessageStreamConsumer { |
|
533 |
||
534 |
@Override |
|
535 |
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
|
536 |
transport.acknowledgeReception(); |
48083 | 537 |
text = data; |
538 |
WebSocketImpl.this.part = part; |
|
539 |
tryChangeState(WAITING, TEXT); |
|
540 |
} |
|
541 |
||
542 |
@Override |
|
543 |
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
|
544 |
transport.acknowledgeReception(); |
48083 | 545 |
binaryData = data; |
546 |
WebSocketImpl.this.part = part; |
|
547 |
tryChangeState(WAITING, BINARY); |
|
548 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
549 |
|
48083 | 550 |
@Override |
551 |
public void onPing(ByteBuffer data) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
552 |
transport.acknowledgeReception(); |
48083 | 553 |
binaryData = data; |
554 |
tryChangeState(WAITING, PING); |
|
555 |
} |
|
556 |
||
557 |
@Override |
|
558 |
public void onPong(ByteBuffer data) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
559 |
transport.acknowledgeReception(); |
48083 | 560 |
binaryData = data; |
561 |
tryChangeState(WAITING, PONG); |
|
562 |
} |
|
563 |
||
564 |
@Override |
|
565 |
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
|
566 |
transport.acknowledgeReception(); |
48083 | 567 |
signalClose(statusCode, reason.toString()); |
568 |
} |
|
569 |
||
570 |
@Override |
|
571 |
public void onComplete() { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
572 |
transport.acknowledgeReception(); |
48083 | 573 |
signalClose(CLOSED_ABNORMALLY, ""); |
574 |
} |
|
575 |
||
576 |
@Override |
|
577 |
public void onError(Throwable error) { |
|
578 |
signalError(error); |
|
579 |
} |
|
580 |
} |
|
581 |
||
582 |
private boolean trySetState(State newState) { |
|
56263 | 583 |
State currentState; |
584 |
boolean success = false; |
|
48083 | 585 |
while (true) { |
56263 | 586 |
currentState = state.get(); |
48083 | 587 |
if (currentState == ERROR || currentState == CLOSE) { |
56263 | 588 |
break; |
48083 | 589 |
} else if (state.compareAndSet(currentState, newState)) { |
590 |
receiveScheduler.runOrSchedule(); |
|
56263 | 591 |
success = true; |
592 |
break; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
593 |
} |
48083 | 594 |
} |
56263 | 595 |
if (DEBUG) { |
596 |
System.out.printf("[WebSocket] set state %s (previous %s) %s%n", |
|
597 |
newState, currentState, success); |
|
598 |
} |
|
599 |
return success; |
|
48083 | 600 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
601 |
|
48083 | 602 |
private boolean tryChangeState(State expectedState, State newState) { |
603 |
State witness = state.compareAndExchange(expectedState, newState); |
|
56263 | 604 |
boolean success = false; |
48083 | 605 |
if (witness == expectedState) { |
606 |
receiveScheduler.runOrSchedule(); |
|
56263 | 607 |
success = true; |
608 |
} else if (witness != ERROR && witness != CLOSE) { |
|
609 |
// This should be the only reason for inability to change the state |
|
610 |
// from IDLE to WAITING: the state has changed to terminal |
|
48083 | 611 |
throw new InternalError(); |
612 |
} |
|
56263 | 613 |
if (DEBUG) { |
614 |
System.out.printf("[WebSocket] change state from %s to %s %s%n", |
|
615 |
expectedState, newState, success); |
|
616 |
} |
|
617 |
return success; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
618 |
} |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
619 |
|
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
620 |
/* Exposed for testing purposes */ |
56263 | 621 |
protected Transport transport() { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
622 |
return transport; |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
623 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
624 |
} |