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