author | prappo |
Fri, 15 Dec 2017 13:35:56 +0300 | |
branch | http-client-branch |
changeset 55992 | 10cefe168d6d |
parent 55989 | 76ac25076fdc |
child 56024 | de352132c7e8 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, 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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
package jdk.incubator.http.internal.websocket; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import jdk.incubator.http.WebSocket; |
48083 | 29 |
import jdk.incubator.http.internal.common.Demand; |
43730 | 30 |
import jdk.incubator.http.internal.common.Log; |
48083 | 31 |
import jdk.incubator.http.internal.common.MinimalFuture; |
32 |
import jdk.incubator.http.internal.common.SequentialScheduler; |
|
33 |
import jdk.incubator.http.internal.common.Utils; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
34 |
import jdk.incubator.http.internal.websocket.OpeningHandshake.Result; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.io.IOException; |
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; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
import java.nio.ByteBuffer; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
41 |
import java.util.concurrent.CompletableFuture; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
import java.util.concurrent.CompletionStage; |
48083 | 43 |
import java.util.concurrent.TimeUnit; |
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; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
import java.util.function.Function; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
import static java.util.Objects.requireNonNull; |
48083 | 50 |
import static jdk.incubator.http.internal.common.MinimalFuture.failedFuture; |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
51 |
import static jdk.incubator.http.internal.websocket.StatusCodes.CLOSED_ABNORMALLY; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
import static jdk.incubator.http.internal.websocket.StatusCodes.NO_STATUS_CODE; |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
53 |
import static jdk.incubator.http.internal.websocket.StatusCodes.isLegalToSendFromClient; |
48083 | 54 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.BINARY; |
55 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.CLOSE; |
|
56 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.ERROR; |
|
57 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.IDLE; |
|
58 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.OPEN; |
|
59 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.PING; |
|
60 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.PONG; |
|
61 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.TEXT; |
|
62 |
import static jdk.incubator.http.internal.websocket.WebSocketImpl.State.WAITING; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
|
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 |
* A WebSocket client. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
*/ |
48083 | 67 |
public final class WebSocketImpl implements WebSocket { |
68 |
||
69 |
enum State { |
|
70 |
OPEN, |
|
71 |
IDLE, |
|
72 |
WAITING, |
|
73 |
TEXT, |
|
74 |
BINARY, |
|
75 |
PING, |
|
76 |
PONG, |
|
77 |
CLOSE, |
|
78 |
ERROR; |
|
79 |
} |
|
80 |
||
81 |
private volatile boolean inputClosed; |
|
82 |
private volatile boolean outputClosed; |
|
83 |
||
84 |
private final AtomicReference<State> state = new AtomicReference<>(OPEN); |
|
85 |
||
86 |
/* Components of calls to Listener's methods */ |
|
87 |
private MessagePart part; |
|
88 |
private ByteBuffer binaryData; |
|
89 |
private CharSequence text; |
|
90 |
private int statusCode; |
|
91 |
private String reason; |
|
92 |
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
|
93 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
94 |
private final URI uri; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
private final String subprotocol; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
96 |
private final Listener listener; |
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 AtomicBoolean outstandingSend = new AtomicBoolean(); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
99 |
private final Transport<WebSocket> transport; |
48083 | 100 |
private final SequentialScheduler receiveScheduler = new SequentialScheduler(new ReceiveTask()); |
101 |
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
|
102 |
|
48083 | 103 |
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
|
104 |
Function<Result, WebSocket> newWebSocket = r -> { |
48083 | 105 |
WebSocket ws = newInstance(b.getUri(), |
106 |
r.subprotocol, |
|
107 |
b.getListener(), |
|
108 |
r.transport); |
|
109 |
// Make sure we don't release the builder until this lambda |
|
110 |
// has been executed. The builder has a strong reference to |
|
111 |
// the HttpClientFacade, and we want to keep that live until |
|
112 |
// after the raw channel is created and passed to WebSocketImpl. |
|
113 |
Reference.reachabilityFence(b); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
114 |
return ws; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
115 |
}; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
116 |
OpeningHandshake h; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
117 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
118 |
h = new OpeningHandshake(b); |
48083 | 119 |
} catch (Throwable e) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
120 |
return failedFuture(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
122 |
return h.send().thenApply(newWebSocket); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
|
48083 | 125 |
/* Exposed for testing purposes */ |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
126 |
static WebSocketImpl newInstance(URI uri, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
127 |
String subprotocol, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
128 |
Listener listener, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
129 |
TransportFactory transport) { |
48083 | 130 |
WebSocketImpl ws = new WebSocketImpl(uri, subprotocol, listener, transport); |
131 |
// This initialisation is outside of the constructor for the sake of |
|
132 |
// safe publication of WebSocketImpl.this |
|
133 |
ws.signalOpen(); |
|
134 |
return ws; |
|
42460
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 |
private WebSocketImpl(URI uri, |
138 |
String subprotocol, |
|
139 |
Listener listener, |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
140 |
TransportFactory transportFactory) { |
48083 | 141 |
this.uri = requireNonNull(uri); |
142 |
this.subprotocol = requireNonNull(subprotocol); |
|
143 |
this.listener = requireNonNull(listener); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
144 |
this.transport = transportFactory.createTransport( |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
145 |
() -> WebSocketImpl.this, // What about escape of WebSocketImpl.this? |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
146 |
new SignallingMessageConsumer()); |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
149 |
@Override |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
150 |
public CompletableFuture<WebSocket> sendText(CharSequence message, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
151 |
boolean isLast) { |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
152 |
if (!outstandingSend.compareAndSet(false, true)) { |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
153 |
return failedFuture(new IllegalStateException("Send pending")); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
154 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
155 |
CompletableFuture<WebSocket> cf = transport.sendText(message, isLast); |
55989 | 156 |
return cf.whenComplete((r, e) -> outstandingSend.set(false)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
157 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
158 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
159 |
@Override |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
160 |
public CompletableFuture<WebSocket> sendBinary(ByteBuffer message, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
161 |
boolean isLast) { |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
162 |
if (!outstandingSend.compareAndSet(false, true)) { |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
163 |
return failedFuture(new IllegalStateException("Send pending")); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
164 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
165 |
CompletableFuture<WebSocket> cf = transport.sendBinary(message, isLast); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
166 |
// Optimize? |
55992 | 167 |
// if (cf.isDone()) { |
168 |
// outstandingSend.set(false); |
|
169 |
// } else { |
|
170 |
// cf.whenComplete((r, e) -> outstandingSend.set(false)); |
|
171 |
// } |
|
55989 | 172 |
return cf.whenComplete((r, e) -> outstandingSend.set(false)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
173 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
174 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
175 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
176 |
public CompletableFuture<WebSocket> sendPing(ByteBuffer message) { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
177 |
return transport.sendPing(message); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
178 |
} |
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 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
public CompletableFuture<WebSocket> sendPong(ByteBuffer message) { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
182 |
return transport.sendPong(message); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
184 |
|
55992 | 185 |
// TODO: this is the only method that works unconditionally: e.g. even if CF |
186 |
// completes with an exception, the output will be closed |
|
187 |
// Even if arguments are illegal the closure will happen (e.g. a default message) |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
@Override |
48083 | 189 |
public CompletableFuture<WebSocket> sendClose(int statusCode, String reason) { |
45119
decbbff9fdb4
8179021: Latest bugfixes to WebSocket/HPACK from the sandbox repo
prappo
parents:
43730
diff
changeset
|
190 |
if (!isLegalToSendFromClient(statusCode)) { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
191 |
return failedFuture(new IllegalArgumentException("statusCode")); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
} |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
193 |
return sendClose0(statusCode, reason); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
195 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
196 |
/* |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
197 |
* Sends a Close message, then shuts down the output since no more |
48083 | 198 |
* messages are expected to be sent after this. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
199 |
*/ |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
200 |
private CompletableFuture<WebSocket> sendClose0(int statusCode, String reason ) { |
48083 | 201 |
// TODO: MUST be a CF created once and shared across sendClose, otherwise |
202 |
// a second sendClose may prematurely close the channel |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
203 |
outputClosed = true; |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
204 |
return transport.sendClose(statusCode, reason) |
48083 | 205 |
.whenComplete((r, error) -> { |
206 |
try { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
207 |
transport.closeOutput(); |
48083 | 208 |
} catch (IOException e) { |
209 |
Log.logError(e); |
|
210 |
} |
|
211 |
if (error instanceof TimeoutException) { |
|
212 |
try { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
213 |
transport.closeInput(); |
48083 | 214 |
} catch (IOException e) { |
215 |
Log.logError(e); |
|
216 |
} |
|
217 |
} |
|
218 |
}); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
219 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
220 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
221 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
222 |
public void request(long n) { |
48083 | 223 |
if (demand.increase(n)) { |
224 |
receiveScheduler.runOrSchedule(); |
|
225 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
public String getSubprotocol() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
return subprotocol; |
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 |
48083 | 234 |
public boolean isOutputClosed() { |
235 |
return outputClosed; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
236 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
@Override |
48083 | 239 |
public boolean isInputClosed() { |
240 |
return inputClosed; |
|
241 |
} |
|
242 |
||
243 |
@Override |
|
244 |
public void abort() { |
|
245 |
inputClosed = true; |
|
246 |
outputClosed = true; |
|
247 |
receiveScheduler.stop(); |
|
248 |
close(); |
|
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 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
252 |
public String toString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
253 |
return super.toString() |
48083 | 254 |
+ "[uri=" + uri |
255 |
+ (!subprotocol.isEmpty() ? ", subprotocol=" + subprotocol : "") |
|
256 |
+ "]"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
257 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
258 |
|
48083 | 259 |
/* |
260 |
* The assumptions about order is as follows: |
|
261 |
* |
|
262 |
* - state is never changed more than twice inside the `run` method: |
|
263 |
* x --(1)--> IDLE --(2)--> y (otherwise we're loosing events, or |
|
264 |
* overwriting parts of messages creating a mess since there's no |
|
265 |
* queueing) |
|
266 |
* - OPEN is always the first state |
|
267 |
* - no messages are requested/delivered before onOpen is called (this |
|
268 |
* is implemented by making WebSocket instance accessible first in |
|
269 |
* onOpen) |
|
270 |
* - after the state has been observed as CLOSE/ERROR, the scheduler |
|
271 |
* is stopped |
|
272 |
*/ |
|
273 |
private class ReceiveTask extends SequentialScheduler.CompleteRestartableTask { |
|
274 |
||
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
275 |
// Transport only asked here and nowhere else because we must make sure |
48083 | 276 |
// onOpen is invoked first and no messages become pending before onOpen |
277 |
// finishes |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
278 |
|
48083 | 279 |
@Override |
280 |
public void run() { |
|
281 |
while (true) { |
|
282 |
State s = state.get(); |
|
283 |
try { |
|
284 |
switch (s) { |
|
285 |
case OPEN: |
|
286 |
processOpen(); |
|
287 |
tryChangeState(OPEN, IDLE); |
|
288 |
break; |
|
289 |
case TEXT: |
|
290 |
processText(); |
|
291 |
tryChangeState(TEXT, IDLE); |
|
292 |
break; |
|
293 |
case BINARY: |
|
294 |
processBinary(); |
|
295 |
tryChangeState(BINARY, IDLE); |
|
296 |
break; |
|
297 |
case PING: |
|
298 |
processPing(); |
|
299 |
tryChangeState(PING, IDLE); |
|
300 |
break; |
|
301 |
case PONG: |
|
302 |
processPong(); |
|
303 |
tryChangeState(PONG, IDLE); |
|
304 |
break; |
|
305 |
case CLOSE: |
|
306 |
processClose(); |
|
307 |
return; |
|
308 |
case ERROR: |
|
309 |
processError(); |
|
310 |
return; |
|
311 |
case IDLE: |
|
312 |
if (demand.tryDecrement() |
|
313 |
&& tryChangeState(IDLE, WAITING)) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
314 |
transport.request(1); |
48083 | 315 |
} |
316 |
return; |
|
317 |
case WAITING: |
|
318 |
// For debugging spurious signalling: when there was a |
|
319 |
// signal, but apparently nothing has changed |
|
320 |
return; |
|
321 |
default: |
|
322 |
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
|
323 |
} |
48083 | 324 |
} catch (Throwable t) { |
325 |
signalError(t); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
326 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
327 |
} |
48083 | 328 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
|
48083 | 330 |
private void processError() throws IOException { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
331 |
transport.closeInput(); |
48083 | 332 |
receiveScheduler.stop(); |
333 |
Throwable err = error.get(); |
|
334 |
if (err instanceof FailWebSocketException) { |
|
335 |
int code1 = ((FailWebSocketException) err).getStatusCode(); |
|
336 |
err = new ProtocolException().initCause(err); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
337 |
sendClose0(code1, "") |
48083 | 338 |
.whenComplete( |
339 |
(r, e) -> { |
|
340 |
if (e != null) { |
|
341 |
Log.logError(e); |
|
342 |
} |
|
343 |
}); |
|
344 |
} |
|
345 |
listener.onError(WebSocketImpl.this, err); |
|
346 |
} |
|
347 |
||
348 |
private void processClose() throws IOException { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
349 |
transport.closeInput(); |
48083 | 350 |
receiveScheduler.stop(); |
351 |
CompletionStage<?> readyToClose; |
|
352 |
readyToClose = listener.onClose(WebSocketImpl.this, statusCode, reason); |
|
353 |
if (readyToClose == null) { |
|
354 |
readyToClose = MinimalFuture.completedFuture(null); |
|
355 |
} |
|
356 |
int code; |
|
357 |
if (statusCode == NO_STATUS_CODE || statusCode == CLOSED_ABNORMALLY) { |
|
358 |
code = NORMAL_CLOSURE; |
|
359 |
} else { |
|
360 |
code = statusCode; |
|
361 |
} |
|
362 |
readyToClose.whenComplete((r, e) -> { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
363 |
sendClose0(code, "") |
48083 | 364 |
.whenComplete((r1, e1) -> { |
365 |
if (e1 != null) { |
|
366 |
Log.logError(e1); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
} |
48083 | 368 |
}); |
369 |
}); |
|
370 |
} |
|
371 |
||
372 |
private void processPong() { |
|
373 |
listener.onPong(WebSocketImpl.this, binaryData); |
|
374 |
} |
|
375 |
||
376 |
private void processPing() { |
|
377 |
// Let's make a full copy of this tiny data. What we want here |
|
378 |
// is to rule out a possibility the shared data we send might be |
|
379 |
// corrupted by processing in the listener. |
|
380 |
ByteBuffer slice = binaryData.slice(); |
|
381 |
ByteBuffer copy = ByteBuffer.allocate(binaryData.remaining()) |
|
382 |
.put(binaryData) |
|
383 |
.flip(); |
|
384 |
// Non-exclusive send; |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
385 |
CompletableFuture<WebSocket> pongSent = transport.sendPong(copy); |
48083 | 386 |
pongSent.whenComplete( |
387 |
(r, e) -> { |
|
388 |
if (e != null) { |
|
389 |
signalError(Utils.getCompletionCause(e)); |
|
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 |
listener.onPing(WebSocketImpl.this, slice); |
|
394 |
} |
|
395 |
||
396 |
private void processBinary() { |
|
397 |
listener.onBinary(WebSocketImpl.this, binaryData, part); |
|
398 |
} |
|
399 |
||
400 |
private void processText() { |
|
401 |
listener.onText(WebSocketImpl.this, text, part); |
|
402 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
|
48083 | 404 |
private void processOpen() { |
405 |
listener.onOpen(WebSocketImpl.this); |
|
406 |
} |
|
407 |
} |
|
408 |
||
409 |
private void signalOpen() { |
|
410 |
receiveScheduler.runOrSchedule(); |
|
411 |
} |
|
412 |
||
413 |
private void signalError(Throwable error) { |
|
414 |
inputClosed = true; |
|
415 |
outputClosed = true; |
|
416 |
if (!this.error.compareAndSet(null, error) || !trySetState(ERROR)) { |
|
417 |
Log.logError(error); |
|
418 |
} else { |
|
419 |
close(); |
|
420 |
} |
|
421 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
422 |
|
48083 | 423 |
private void close() { |
424 |
try { |
|
425 |
try { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
426 |
transport.closeInput(); |
48083 | 427 |
} finally { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
428 |
transport.closeOutput(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
429 |
} |
48083 | 430 |
} catch (Throwable t) { |
431 |
Log.logError(t); |
|
432 |
} |
|
433 |
} |
|
434 |
||
435 |
/* |
|
436 |
* Signals a Close event (might not correspond to anything happened on the |
|
437 |
* channel, i.e. might be synthetic). |
|
438 |
*/ |
|
439 |
private void signalClose(int statusCode, String reason) { |
|
440 |
inputClosed = true; |
|
441 |
this.statusCode = statusCode; |
|
442 |
this.reason = reason; |
|
443 |
if (!trySetState(CLOSE)) { |
|
444 |
Log.logTrace("Close: {0}, ''{1}''", statusCode, reason); |
|
445 |
} else { |
|
446 |
try { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
447 |
transport.closeInput(); |
48083 | 448 |
} catch (Throwable t) { |
449 |
Log.logError(t); |
|
450 |
} |
|
451 |
} |
|
452 |
} |
|
453 |
||
454 |
private class SignallingMessageConsumer implements MessageStreamConsumer { |
|
455 |
||
456 |
@Override |
|
457 |
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
|
458 |
transport.acknowledgeReception(); |
48083 | 459 |
text = data; |
460 |
WebSocketImpl.this.part = part; |
|
461 |
tryChangeState(WAITING, TEXT); |
|
462 |
} |
|
463 |
||
464 |
@Override |
|
465 |
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
|
466 |
transport.acknowledgeReception(); |
48083 | 467 |
binaryData = data; |
468 |
WebSocketImpl.this.part = part; |
|
469 |
tryChangeState(WAITING, BINARY); |
|
470 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
471 |
|
48083 | 472 |
@Override |
473 |
public void onPing(ByteBuffer data) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
474 |
transport.acknowledgeReception(); |
48083 | 475 |
binaryData = data; |
476 |
tryChangeState(WAITING, PING); |
|
477 |
} |
|
478 |
||
479 |
@Override |
|
480 |
public void onPong(ByteBuffer data) { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
481 |
transport.acknowledgeReception(); |
48083 | 482 |
binaryData = data; |
483 |
tryChangeState(WAITING, PONG); |
|
484 |
} |
|
485 |
||
486 |
@Override |
|
487 |
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
|
488 |
transport.acknowledgeReception(); |
48083 | 489 |
signalClose(statusCode, reason.toString()); |
490 |
} |
|
491 |
||
492 |
@Override |
|
493 |
public void onComplete() { |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
494 |
transport.acknowledgeReception(); |
48083 | 495 |
signalClose(CLOSED_ABNORMALLY, ""); |
496 |
} |
|
497 |
||
498 |
@Override |
|
499 |
public void onError(Throwable error) { |
|
500 |
signalError(error); |
|
501 |
} |
|
502 |
} |
|
503 |
||
504 |
private boolean trySetState(State newState) { |
|
505 |
while (true) { |
|
506 |
State currentState = state.get(); |
|
507 |
if (currentState == ERROR || currentState == CLOSE) { |
|
508 |
return false; |
|
509 |
} else if (state.compareAndSet(currentState, newState)) { |
|
510 |
receiveScheduler.runOrSchedule(); |
|
511 |
return true; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
512 |
} |
48083 | 513 |
} |
514 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
515 |
|
48083 | 516 |
private boolean tryChangeState(State expectedState, State newState) { |
517 |
State witness = state.compareAndExchange(expectedState, newState); |
|
518 |
if (witness == expectedState) { |
|
519 |
receiveScheduler.runOrSchedule(); |
|
520 |
return true; |
|
521 |
} |
|
522 |
// This should be the only reason for inability to change the state from |
|
523 |
// IDLE to WAITING: the state has changed to terminal |
|
524 |
if (witness != ERROR && witness != CLOSE) { |
|
525 |
throw new InternalError(); |
|
526 |
} |
|
527 |
return false; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
528 |
} |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
529 |
|
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
530 |
/* Exposed for testing purposes */ |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
531 |
protected final Transport<WebSocket> transport() { |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
532 |
return transport; |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
55973
diff
changeset
|
533 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
534 |
} |