author | prappo |
Wed, 14 Mar 2018 14:42:23 +0000 | |
branch | http-client-branch |
changeset 56306 | 88c1f551d45e |
parent 56304 | 065641767a75 |
child 56314 | f92e7a8a189f |
permissions | -rw-r--r-- |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
1 |
/* |
56024
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
55989
diff
changeset
|
2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
4 |
* |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
10 |
* |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
15 |
* accompanied this code). |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
16 |
* |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
20 |
* |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
23 |
* questions. |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
24 |
*/ |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
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; |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
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.MinimalFuture; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
30 |
import jdk.internal.net.http.common.SequentialScheduler; |
56263 | 31 |
import jdk.internal.net.http.common.SequentialScheduler.CompleteRestartableTask; |
32 |
import jdk.internal.net.http.common.Utils; |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
33 |
|
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
34 |
import java.io.IOException; |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
35 |
import java.lang.System.Logger.Level; |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
36 |
import java.nio.ByteBuffer; |
56263 | 37 |
import java.nio.CharBuffer; |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
38 |
import java.nio.channels.SelectionKey; |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
39 |
import java.util.concurrent.CompletableFuture; |
56263 | 40 |
import java.util.concurrent.atomic.AtomicLong; |
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
41 |
import java.util.concurrent.atomic.AtomicReference; |
56263 | 42 |
import java.util.function.BiConsumer; |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
43 |
import java.util.function.Supplier; |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
44 |
|
56263 | 45 |
import static jdk.internal.net.http.websocket.TransportImpl.ChannelState.AVAILABLE; |
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
46 |
import static jdk.internal.net.http.websocket.TransportImpl.ChannelState.CLOSED; |
56263 | 47 |
import static jdk.internal.net.http.websocket.TransportImpl.ChannelState.UNREGISTERED; |
48 |
import static jdk.internal.net.http.websocket.TransportImpl.ChannelState.WAITING; |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
49 |
|
56263 | 50 |
public class TransportImpl implements Transport { |
51 |
||
52 |
// -- Debugging infrastructure -- |
|
53 |
||
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
54 |
private static final boolean DEBUG = Utils.DEBUG_WS; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
55 |
private static final System.Logger debug = |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
56 |
Utils.getWebSocketLogger("[Transport]"::toString, DEBUG); |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
57 |
|
56263 | 58 |
/* Used for correlating enters to and exists from a method */ |
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
59 |
private final AtomicLong counter = new AtomicLong(); |
56263 | 60 |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
61 |
private final SequentialScheduler sendScheduler = new SequentialScheduler(new SendTask()); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
62 |
|
56263 | 63 |
private final MessageQueue queue = new MessageQueue(); |
64 |
private final MessageEncoder encoder = new MessageEncoder(); |
|
65 |
/* A reusable buffer for writing, initially with no remaining bytes */ |
|
66 |
private final ByteBuffer dst = createWriteBuffer().position(0).limit(0); |
|
67 |
/* This array is created once for gathering writes accepted by RawChannel */ |
|
68 |
private final ByteBuffer[] dstArray = new ByteBuffer[]{dst}; |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
69 |
private final MessageStreamConsumer messageConsumer; |
56263 | 70 |
private final MessageDecoder decoder; |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
71 |
private final Frame.Reader reader = new Frame.Reader(); |
56263 | 72 |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
73 |
private final Demand demand = new Demand(); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
74 |
private final SequentialScheduler receiveScheduler; |
56263 | 75 |
private final RawChannel channel; |
76 |
private final Object closeLock = new Object(); |
|
77 |
private final RawChannel.RawEvent writeEvent = new WriteEvent(); |
|
78 |
private final RawChannel.RawEvent readEvent = new ReadEvent(); |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
79 |
private final AtomicReference<ChannelState> writeState |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
80 |
= new AtomicReference<>(UNREGISTERED); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
81 |
private ByteBuffer data; |
56263 | 82 |
private volatile ChannelState readState = UNREGISTERED; |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
83 |
private boolean inputClosed; |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
84 |
private boolean outputClosed; |
56263 | 85 |
public TransportImpl(MessageStreamConsumer consumer, RawChannel channel) { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
86 |
this.messageConsumer = consumer; |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
87 |
this.channel = channel; |
56263 | 88 |
this.decoder = new MessageDecoder(this.messageConsumer); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
89 |
this.data = channel.initialByteBuffer(); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
90 |
// To ensure the initial non-final `data` will be visible |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
91 |
// (happens-before) when `readEvent.handle()` invokes `receiveScheduler` |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
92 |
// the following assignment is done last: |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
93 |
receiveScheduler = new SequentialScheduler(new ReceiveTask()); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
94 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
95 |
|
56263 | 96 |
private ByteBuffer createWriteBuffer() { |
97 |
String name = "jdk.httpclient.websocket.writeBufferSize"; |
|
98 |
int capacity = Utils.getIntegerNetProperty(name, 16384); |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
99 |
debug.log(Level.DEBUG, "write buffer capacity %s%n", capacity); |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
100 |
|
56263 | 101 |
// TODO (optimization?): allocateDirect if SSL? |
102 |
return ByteBuffer.allocate(capacity); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
103 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
104 |
|
56263 | 105 |
private boolean write() throws IOException { |
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
106 |
debug.log(Level.DEBUG, "writing to the channel"); |
56263 | 107 |
long count = channel.write(dstArray, 0, dstArray.length); |
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
108 |
debug.log(Level.DEBUG, "%s bytes written", count); |
56263 | 109 |
for (ByteBuffer b : dstArray) { |
110 |
if (b.hasRemaining()) { |
|
111 |
return false; |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
112 |
} |
56263 | 113 |
} |
114 |
return true; |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
115 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
116 |
|
56263 | 117 |
@Override |
118 |
public <T> CompletableFuture<T> sendText(CharSequence message, |
|
119 |
boolean isLast, |
|
120 |
T attachment, |
|
121 |
BiConsumer<? super T, ? super Throwable> action) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
122 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
123 |
if (debug.isLoggable(Level.DEBUG)) { |
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
124 |
id = counter.incrementAndGet(); |
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
125 |
debug.log(Level.DEBUG, "enter send text %s message.length()=%s last=%s", |
56263 | 126 |
id, message.length(), isLast); |
127 |
} |
|
128 |
// TODO (optimization?): |
|
129 |
// These sendXXX methods might be a good place to decide whether or not |
|
130 |
// we can write straight ahead, possibly returning null instead of |
|
131 |
// creating a CompletableFuture |
|
132 |
||
133 |
// Even if the text is already CharBuffer, the client will not be happy |
|
134 |
// if they discover the position is changing. So, no instanceof |
|
135 |
// cheating, wrap always. |
|
136 |
CharBuffer text = CharBuffer.wrap(message); |
|
137 |
MinimalFuture<T> f = new MinimalFuture<>(); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
138 |
try { |
56263 | 139 |
queue.addText(text, isLast, attachment, action, f); |
140 |
sendScheduler.runOrSchedule(); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
141 |
} catch (IOException e) { |
56263 | 142 |
f.completeExceptionally(e); |
56024
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
55989
diff
changeset
|
143 |
} |
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
144 |
debug.log(Level.DEBUG, "exit send text %s returned %s", id, f); |
56263 | 145 |
return f; |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
146 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
147 |
|
56263 | 148 |
@Override |
149 |
public <T> CompletableFuture<T> sendBinary(ByteBuffer message, |
|
150 |
boolean isLast, |
|
151 |
T attachment, |
|
152 |
BiConsumer<? super T, ? super Throwable> action) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
153 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
154 |
if (debug.isLoggable(Level.DEBUG)) { |
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
155 |
id = counter.incrementAndGet(); |
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
156 |
debug.log(Level.DEBUG, "enter send binary %s message.remaining()=%s last=%s", |
56263 | 157 |
id, message.remaining(), isLast); |
56024
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
55989
diff
changeset
|
158 |
} |
56263 | 159 |
MinimalFuture<T> f = new MinimalFuture<>(); |
56024
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
55989
diff
changeset
|
160 |
try { |
56263 | 161 |
queue.addBinary(message, isLast, attachment, action, f); |
162 |
sendScheduler.runOrSchedule(); |
|
163 |
} catch (IOException e) { |
|
164 |
f.completeExceptionally(e); |
|
56024
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
55989
diff
changeset
|
165 |
} |
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
166 |
debug.log(Level.DEBUG, "exit send binary %s returned %s", id, f); |
56263 | 167 |
return f; |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
168 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
169 |
|
56263 | 170 |
@Override |
171 |
public <T> CompletableFuture<T> sendPing(ByteBuffer message, |
|
172 |
T attachment, |
|
173 |
BiConsumer<? super T, ? super Throwable> action) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
174 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
175 |
if (debug.isLoggable(Level.DEBUG)) { |
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
176 |
id = counter.incrementAndGet(); |
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
177 |
debug.log(Level.DEBUG, "enter send ping %s message.remaining()=%s", |
56263 | 178 |
id, message.remaining()); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
179 |
} |
56263 | 180 |
MinimalFuture<T> f = new MinimalFuture<>(); |
181 |
try { |
|
182 |
queue.addPing(message, attachment, action, f); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
183 |
sendScheduler.runOrSchedule(); |
56263 | 184 |
} catch (IOException e) { |
185 |
f.completeExceptionally(e); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
186 |
} |
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
187 |
debug.log(Level.DEBUG, "exit send ping %s returned %s", id, f); |
56263 | 188 |
return f; |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
189 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
190 |
|
56263 | 191 |
@Override |
192 |
public <T> CompletableFuture<T> sendPong(ByteBuffer message, |
|
193 |
T attachment, |
|
194 |
BiConsumer<? super T, ? super Throwable> action) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
195 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
196 |
if (debug.isLoggable(Level.DEBUG)) { |
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
197 |
id = counter.incrementAndGet(); |
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
198 |
debug.log(Level.DEBUG, "enter send pong %s message.remaining()=%s", |
56263 | 199 |
id, message.remaining()); |
200 |
} |
|
201 |
MinimalFuture<T> f = new MinimalFuture<>(); |
|
202 |
try { |
|
203 |
queue.addPong(message, attachment, action, f); |
|
204 |
sendScheduler.runOrSchedule(); |
|
205 |
} catch (IOException e) { |
|
206 |
f.completeExceptionally(e); |
|
207 |
} |
|
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
208 |
debug.log(Level.DEBUG, "exit send pong %s returned %s", id, f); |
56263 | 209 |
return f; |
210 |
} |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
211 |
|
56263 | 212 |
@Override |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
213 |
public <T> CompletableFuture<T> sendPong(Supplier<? extends ByteBuffer> message, |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
214 |
T attachment, |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
215 |
BiConsumer<? super T, ? super Throwable> action) { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
216 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
217 |
if (debug.isLoggable(Level.DEBUG)) { |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
218 |
id = counter.incrementAndGet(); |
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
219 |
debug.log(Level.DEBUG, "enter send pong %s supplier=%s", |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
220 |
id, message); |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
221 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
222 |
MinimalFuture<T> f = new MinimalFuture<>(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
223 |
try { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
224 |
queue.addPong(message, attachment, action, f); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
225 |
sendScheduler.runOrSchedule(); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
226 |
} catch (IOException e) { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
227 |
f.completeExceptionally(e); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
228 |
} |
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
229 |
debug.log(Level.DEBUG, "exit send pong %s returned %s", id, f); |
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
230 |
return f; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
231 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
232 |
|
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
233 |
@Override |
56263 | 234 |
public <T> CompletableFuture<T> sendClose(int statusCode, |
235 |
String reason, |
|
236 |
T attachment, |
|
237 |
BiConsumer<? super T, ? super Throwable> action) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
238 |
long id = 0; |
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
239 |
if (debug.isLoggable(Level.DEBUG)) { |
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
240 |
id = counter.incrementAndGet(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
241 |
debug.log(Level.DEBUG, "enter send close %s statusCode=%s, reason.length()=%s", |
56263 | 242 |
id, statusCode, reason.length()); |
243 |
} |
|
244 |
MinimalFuture<T> f = new MinimalFuture<>(); |
|
245 |
try { |
|
246 |
queue.addClose(statusCode, CharBuffer.wrap(reason), attachment, action, f); |
|
247 |
sendScheduler.runOrSchedule(); |
|
248 |
} catch (IOException e) { |
|
249 |
f.completeExceptionally(e); |
|
250 |
} |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
251 |
debug.log(Level.DEBUG, "exit send close %s returned %s", id, f); |
56263 | 252 |
return f; |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
253 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
254 |
|
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
255 |
@Override |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
256 |
public void request(long n) { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
257 |
debug.log(Level.DEBUG, "request %s", n); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
258 |
if (demand.increase(n)) { |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
259 |
receiveScheduler.runOrSchedule(); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
260 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
261 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
262 |
|
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
263 |
@Override |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
264 |
public void acknowledgeReception() { |
55989 | 265 |
boolean decremented = demand.tryDecrement(); |
266 |
if (!decremented) { |
|
267 |
throw new InternalError(); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
268 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
269 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
270 |
|
56263 | 271 |
@Override |
272 |
public void closeOutput() throws IOException { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
273 |
debug.log(Level.DEBUG, "closeOutput"); |
56263 | 274 |
synchronized (closeLock) { |
275 |
if (!outputClosed) { |
|
276 |
outputClosed = true; |
|
277 |
try { |
|
278 |
channel.shutdownOutput(); |
|
279 |
} finally { |
|
280 |
if (inputClosed) { |
|
281 |
channel.close(); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
282 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
283 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
284 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
285 |
} |
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
286 |
writeState.set(CLOSED); |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
287 |
sendScheduler.runOrSchedule(); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
288 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
289 |
|
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
290 |
/* |
55989 | 291 |
* Permanently stops reading from the channel and delivering messages |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
292 |
* regardless of the current demand and data availability. |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
293 |
*/ |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
294 |
@Override |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
295 |
public void closeInput() throws IOException { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
296 |
debug.log(Level.DEBUG, "closeInput"); |
56263 | 297 |
synchronized (closeLock) { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
298 |
if (!inputClosed) { |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
299 |
inputClosed = true; |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
300 |
try { |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
301 |
receiveScheduler.stop(); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
302 |
channel.shutdownInput(); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
303 |
} finally { |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
304 |
if (outputClosed) { |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
305 |
channel.close(); |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
306 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
307 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
308 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
309 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
310 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
311 |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
312 |
/* Common states for send and receive tasks */ |
56263 | 313 |
enum ChannelState { |
314 |
UNREGISTERED, |
|
315 |
AVAILABLE, |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
316 |
WAITING, |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
317 |
CLOSED, |
56263 | 318 |
} |
319 |
||
320 |
@SuppressWarnings({"rawtypes"}) |
|
321 |
private class SendTask extends CompleteRestartableTask { |
|
322 |
||
323 |
private final MessageQueue.QueueCallback<Boolean, IOException> |
|
324 |
encodingCallback = new MessageQueue.QueueCallback<>() { |
|
325 |
||
326 |
@Override |
|
327 |
public <T> Boolean onText(CharBuffer message, |
|
328 |
boolean isLast, |
|
329 |
T attachment, |
|
330 |
BiConsumer<? super T, ? super Throwable> action, |
|
331 |
CompletableFuture<? super T> future) throws IOException |
|
332 |
{ |
|
333 |
return encoder.encodeText(message, isLast, dst); |
|
334 |
} |
|
335 |
||
336 |
@Override |
|
337 |
public <T> Boolean onBinary(ByteBuffer message, |
|
338 |
boolean isLast, |
|
339 |
T attachment, |
|
340 |
BiConsumer<? super T, ? super Throwable> action, |
|
341 |
CompletableFuture<? super T> future) throws IOException |
|
342 |
{ |
|
343 |
return encoder.encodeBinary(message, isLast, dst); |
|
344 |
} |
|
345 |
||
346 |
@Override |
|
347 |
public <T> Boolean onPing(ByteBuffer message, |
|
348 |
T attachment, |
|
349 |
BiConsumer<? super T, ? super Throwable> action, |
|
350 |
CompletableFuture<? super T> future) throws IOException |
|
351 |
{ |
|
352 |
return encoder.encodePing(message, dst); |
|
353 |
} |
|
354 |
||
355 |
@Override |
|
356 |
public <T> Boolean onPong(ByteBuffer message, |
|
357 |
T attachment, |
|
358 |
BiConsumer<? super T, ? super Throwable> action, |
|
359 |
CompletableFuture<? super T> future) throws IOException |
|
360 |
{ |
|
361 |
return encoder.encodePong(message, dst); |
|
362 |
} |
|
363 |
||
364 |
@Override |
|
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
365 |
public <T> Boolean onPong(Supplier<? extends ByteBuffer> message, |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
366 |
T attachment, |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
367 |
BiConsumer<? super T, ? super Throwable> action, |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
368 |
CompletableFuture<? super T> future) throws IOException { |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
369 |
return encoder.encodePong(message.get(), dst); |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
370 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
371 |
|
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
372 |
@Override |
56263 | 373 |
public <T> Boolean onClose(int statusCode, |
374 |
CharBuffer reason, |
|
375 |
T attachment, |
|
376 |
BiConsumer<? super T, ? super Throwable> action, |
|
377 |
CompletableFuture<? super T> future) throws IOException |
|
378 |
{ |
|
379 |
return encoder.encodeClose(statusCode, reason, dst); |
|
380 |
} |
|
381 |
||
382 |
@Override |
|
383 |
public Boolean onEmpty() { |
|
384 |
return false; |
|
385 |
} |
|
386 |
}; |
|
387 |
||
388 |
/* Whether the task sees the current head message for first time */ |
|
389 |
private boolean firstPass = true; |
|
390 |
/* Whether the message has been fully encoded */ |
|
391 |
private boolean encoded; |
|
392 |
||
393 |
// -- Current message completion communication fields -- |
|
394 |
||
395 |
private Object attachment; |
|
396 |
private BiConsumer action; |
|
397 |
private CompletableFuture future; |
|
398 |
private final MessageQueue.QueueCallback<Boolean, RuntimeException> |
|
399 |
/* If there is a message, loads its completion communication fields */ |
|
400 |
loadCallback = new MessageQueue.QueueCallback<Boolean, RuntimeException>() { |
|
401 |
||
402 |
@Override |
|
403 |
public <T> Boolean onText(CharBuffer message, |
|
404 |
boolean isLast, |
|
405 |
T attachment, |
|
406 |
BiConsumer<? super T, ? super Throwable> action, |
|
407 |
CompletableFuture<? super T> future) |
|
408 |
{ |
|
409 |
SendTask.this.attachment = attachment; |
|
410 |
SendTask.this.action = action; |
|
411 |
SendTask.this.future = future; |
|
412 |
return true; |
|
413 |
} |
|
414 |
||
415 |
@Override |
|
416 |
public <T> Boolean onBinary(ByteBuffer message, |
|
417 |
boolean isLast, |
|
418 |
T attachment, |
|
419 |
BiConsumer<? super T, ? super Throwable> action, |
|
420 |
CompletableFuture<? super T> future) |
|
421 |
{ |
|
422 |
SendTask.this.attachment = attachment; |
|
423 |
SendTask.this.action = action; |
|
424 |
SendTask.this.future = future; |
|
425 |
return true; |
|
426 |
} |
|
427 |
||
428 |
@Override |
|
429 |
public <T> Boolean onPing(ByteBuffer message, |
|
430 |
T attachment, |
|
431 |
BiConsumer<? super T, ? super Throwable> action, |
|
432 |
CompletableFuture<? super T> future) |
|
433 |
{ |
|
434 |
SendTask.this.attachment = attachment; |
|
435 |
SendTask.this.action = action; |
|
436 |
SendTask.this.future = future; |
|
437 |
return true; |
|
438 |
} |
|
439 |
||
440 |
@Override |
|
441 |
public <T> Boolean onPong(ByteBuffer message, |
|
442 |
T attachment, |
|
443 |
BiConsumer<? super T, ? super Throwable> action, |
|
444 |
CompletableFuture<? super T> future) |
|
445 |
{ |
|
446 |
SendTask.this.attachment = attachment; |
|
447 |
SendTask.this.action = action; |
|
448 |
SendTask.this.future = future; |
|
449 |
return true; |
|
450 |
} |
|
451 |
||
452 |
@Override |
|
56303
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
453 |
public <T> Boolean onPong(Supplier<? extends ByteBuffer> message, |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
454 |
T attachment, |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
455 |
BiConsumer<? super T, ? super Throwable> action, |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
456 |
CompletableFuture<? super T> future) |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
457 |
{ |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
458 |
SendTask.this.attachment = attachment; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
459 |
SendTask.this.action = action; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
460 |
SendTask.this.future = future; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
461 |
return true; |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
462 |
} |
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
463 |
|
a82058c084ef
http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents:
56295
diff
changeset
|
464 |
@Override |
56263 | 465 |
public <T> Boolean onClose(int statusCode, |
466 |
CharBuffer reason, |
|
467 |
T attachment, |
|
468 |
BiConsumer<? super T, ? super Throwable> action, |
|
469 |
CompletableFuture<? super T> future) |
|
470 |
{ |
|
471 |
SendTask.this.attachment = attachment; |
|
472 |
SendTask.this.action = action; |
|
473 |
SendTask.this.future = future; |
|
474 |
return true; |
|
475 |
} |
|
476 |
||
477 |
@Override |
|
478 |
public Boolean onEmpty() { |
|
479 |
return false; |
|
480 |
} |
|
481 |
}; |
|
482 |
||
483 |
@Override |
|
484 |
public void run() { |
|
485 |
// Could have been only called in one of the following cases: |
|
486 |
// (a) A message has been added to the queue |
|
487 |
// (b) The channel is ready for writing |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
488 |
debug.log(Level.DEBUG, "enter send task"); |
56263 | 489 |
while (!queue.isEmpty()) { |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
490 |
try { |
56263 | 491 |
if (dst.hasRemaining()) { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
492 |
debug.log(Level.DEBUG, "%s bytes in buffer", |
56263 | 493 |
dst.remaining()); |
494 |
// The previous part of the binary representation of the message |
|
495 |
// hasn't been fully written |
|
496 |
if (!tryCompleteWrite()) { |
|
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
497 |
break; |
56263 | 498 |
} |
499 |
} else if (!encoded) { |
|
500 |
if (firstPass) { |
|
501 |
firstPass = false; |
|
502 |
queue.peek(loadCallback); |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
503 |
debug.log(Level.DEBUG, "load message"); |
56263 | 504 |
} |
505 |
dst.clear(); |
|
506 |
encoded = queue.peek(encodingCallback); |
|
507 |
dst.flip(); |
|
508 |
if (!tryCompleteWrite()) { |
|
56291
c8c4c707ff3a
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents:
56269
diff
changeset
|
509 |
break; |
56263 | 510 |
} |
511 |
} else { |
|
512 |
// All done, remove and complete |
|
513 |
encoder.reset(); |
|
514 |
removeAndComplete(null); |
|
515 |
} |
|
516 |
} catch (Throwable t) { |
|
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
517 |
debug.log(Level.DEBUG, "send task exception %s", (Object) t); |
56263 | 518 |
// buffer cleanup: if there is an exception, the buffer |
519 |
// should appear empty for the next write as there is |
|
520 |
// nothing to write |
|
521 |
dst.position(dst.limit()); |
|
522 |
encoder.reset(); |
|
523 |
removeAndComplete(t); |
|
524 |
} |
|
525 |
} |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
526 |
debug.log(Level.DEBUG, "exit send task"); |
56263 | 527 |
} |
528 |
||
529 |
private boolean tryCompleteWrite() throws IOException { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
530 |
debug.log(Level.DEBUG, "enter writing"); |
56263 | 531 |
boolean finished = false; |
532 |
loop: |
|
533 |
while (true) { |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
534 |
final ChannelState ws = writeState.get(); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
535 |
debug.log(Level.DEBUG, "write state: %s", ws); |
56263 | 536 |
switch (ws) { |
537 |
case WAITING: |
|
538 |
break loop; |
|
539 |
case UNREGISTERED: |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
540 |
debug.log(Level.DEBUG, "registering write event"); |
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
541 |
channel.registerEvent(writeEvent); |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
542 |
writeState.compareAndSet(UNREGISTERED, WAITING); |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
543 |
debug.log(Level.DEBUG, "registered write event"); |
56263 | 544 |
break loop; |
545 |
case AVAILABLE: |
|
546 |
boolean written = write(); |
|
547 |
if (written) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
548 |
debug.log(Level.DEBUG, "finished writing to the channel"); |
56263 | 549 |
finished = true; |
550 |
break loop; // All done |
|
551 |
} else { |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
552 |
writeState.compareAndSet(AVAILABLE, UNREGISTERED); |
56263 | 553 |
continue loop; // Effectively "goto UNREGISTERED" |
554 |
} |
|
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
555 |
case CLOSED: |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
556 |
throw new IOException("Output closed"); |
56263 | 557 |
default: |
558 |
throw new InternalError(String.valueOf(ws)); |
|
559 |
} |
|
560 |
} |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
561 |
debug.log(Level.DEBUG, "exit writing"); |
56263 | 562 |
return finished; |
563 |
} |
|
564 |
||
565 |
@SuppressWarnings("unchecked") |
|
566 |
private void removeAndComplete(Throwable error) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
567 |
debug.log(Level.DEBUG, "removeAndComplete error=%s", |
56306
88c1f551d45e
http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents:
56304
diff
changeset
|
568 |
(Object) error); |
56263 | 569 |
queue.remove(); |
570 |
if (error != null) { |
|
571 |
try { |
|
572 |
action.accept(null, error); |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
573 |
} finally { |
56263 | 574 |
future.completeExceptionally(error); |
575 |
} |
|
576 |
} else { |
|
577 |
try { |
|
578 |
action.accept(attachment, null); |
|
579 |
} finally { |
|
580 |
future.complete(attachment); |
|
581 |
} |
|
582 |
} |
|
583 |
encoded = false; |
|
584 |
firstPass = true; |
|
585 |
attachment = null; |
|
586 |
action = null; |
|
587 |
future = null; |
|
588 |
} |
|
589 |
} |
|
590 |
||
591 |
private class ReceiveTask extends CompleteRestartableTask { |
|
592 |
||
593 |
@Override |
|
594 |
public void run() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
595 |
debug.log(Level.DEBUG, "enter receive task"); |
56263 | 596 |
loop: |
597 |
while (!receiveScheduler.isStopped()) { |
|
598 |
if (data.hasRemaining()) { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
599 |
debug.log(Level.DEBUG, "remaining bytes received %s", |
56263 | 600 |
data.remaining()); |
601 |
if (!demand.isFulfilled()) { |
|
602 |
try { |
|
603 |
int oldPos = data.position(); |
|
604 |
reader.readFrame(data, decoder); |
|
605 |
int newPos = data.position(); |
|
606 |
// Reader always consumes bytes: |
|
607 |
assert oldPos != newPos : data; |
|
608 |
} catch (Throwable e) { |
|
609 |
receiveScheduler.stop(); |
|
610 |
messageConsumer.onError(e); |
|
611 |
} |
|
612 |
continue; |
|
613 |
} |
|
614 |
break loop; |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
615 |
} |
56263 | 616 |
final ChannelState rs = readState; |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
617 |
debug.log(Level.DEBUG, "receive state: %s", rs); |
56263 | 618 |
switch (rs) { |
619 |
case WAITING: |
|
620 |
break loop; |
|
621 |
case UNREGISTERED: |
|
622 |
try { |
|
623 |
readState = WAITING; |
|
624 |
channel.registerEvent(readEvent); |
|
625 |
} catch (Throwable e) { |
|
626 |
receiveScheduler.stop(); |
|
627 |
messageConsumer.onError(e); |
|
628 |
} |
|
629 |
break loop; |
|
630 |
case AVAILABLE: |
|
631 |
try { |
|
632 |
data = channel.read(); |
|
633 |
} catch (Throwable e) { |
|
634 |
receiveScheduler.stop(); |
|
635 |
messageConsumer.onError(e); |
|
636 |
break loop; |
|
637 |
} |
|
638 |
if (data == null) { // EOF |
|
639 |
receiveScheduler.stop(); |
|
640 |
messageConsumer.onComplete(); |
|
641 |
break loop; |
|
642 |
} else if (!data.hasRemaining()) { |
|
643 |
// No data at the moment. Pretty much a "goto", |
|
644 |
// reusing the existing code path for registration |
|
645 |
readState = UNREGISTERED; |
|
646 |
} |
|
647 |
continue loop; |
|
648 |
default: |
|
649 |
throw new InternalError(String.valueOf(rs)); |
|
650 |
} |
|
651 |
} |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
652 |
debug.log(Level.DEBUG, "exit receive task"); |
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
653 |
} |
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
654 |
} |
56263 | 655 |
|
656 |
private class WriteEvent implements RawChannel.RawEvent { |
|
657 |
||
658 |
@Override |
|
659 |
public int interestOps() { |
|
660 |
return SelectionKey.OP_WRITE; |
|
661 |
} |
|
662 |
||
663 |
@Override |
|
664 |
public void handle() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
665 |
debug.log(Level.DEBUG, "write event"); |
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
666 |
ChannelState s; |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
667 |
do { |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
668 |
s = writeState.get(); |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
669 |
if (s == CLOSED) { |
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
670 |
debug.log(Level.DEBUG, "write state %s", s); |
56269
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
671 |
break; |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
672 |
} |
234813fd33bc
http-client-branch: (WebSocket) test fix + output closure
prappo
parents:
56263
diff
changeset
|
673 |
} while (!writeState.compareAndSet(s, AVAILABLE)); |
56263 | 674 |
sendScheduler.runOrSchedule(); |
675 |
} |
|
676 |
} |
|
677 |
||
678 |
private class ReadEvent implements RawChannel.RawEvent { |
|
679 |
||
680 |
@Override |
|
681 |
public int interestOps() { |
|
682 |
return SelectionKey.OP_READ; |
|
683 |
} |
|
684 |
||
685 |
@Override |
|
686 |
public void handle() { |
|
56304
065641767a75
http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents:
56303
diff
changeset
|
687 |
debug.log(Level.DEBUG, "read event"); |
56263 | 688 |
readState = AVAILABLE; |
689 |
receiveScheduler.runOrSchedule(); |
|
690 |
} |
|
691 |
} |
|
55988
7f1e0cf933a6
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff
changeset
|
692 |
} |