# HG changeset patch # User prappo # Date 1520858849 0 # Node ID 7e21161251dce5250d78880c7e39f51770a781f5 # Parent b96b5cbb018de79232176c0c57f67743de8f28b8 http-client-branch: (WebSocket) debug logging diff -r b96b5cbb018d -r 7e21161251dc src/java.net.http/share/classes/jdk/internal/net/http/websocket/WebSocketImpl.java --- a/src/java.net.http/share/classes/jdk/internal/net/http/websocket/WebSocketImpl.java Mon Mar 12 11:36:51 2018 +0000 +++ b/src/java.net.http/share/classes/jdk/internal/net/http/websocket/WebSocketImpl.java Mon Mar 12 12:47:29 2018 +0000 @@ -43,6 +43,7 @@ import java.util.concurrent.CompletionStage; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; import java.util.function.Function; @@ -68,6 +69,7 @@ public final class WebSocketImpl implements WebSocket { private final static boolean DEBUG = true; + private final AtomicLong counter = new AtomicLong(); enum State { OPEN, @@ -156,26 +158,48 @@ public CompletableFuture sendText(CharSequence message, boolean isLast) { Objects.requireNonNull(message); - if (!outstandingSend.compareAndSet(false, true)) { - return failedFuture(new IllegalStateException("Send pending")); + long id; + if (DEBUG) { + id = counter.incrementAndGet(); + System.out.printf("[WebSocket] %s send text: payload length=%s last=%s%n", + id, message.length(), isLast); } - CompletableFuture cf - = transport.sendText(message, isLast, this, - (r, e) -> outstandingSend.set(false)); - return replaceNull(cf); + CompletableFuture result; + if (!outstandingSend.compareAndSet(false, true)) { + result = failedFuture(new IllegalStateException("Send pending")); + } else { + result = transport.sendText(message, isLast, this, + (r, e) -> outstandingSend.set(false)); + } + if (DEBUG) { + System.out.printf("[WebSocket] %s send text: returned %s%n", + id, result); + } + return replaceNull(result); } @Override public CompletableFuture sendBinary(ByteBuffer message, boolean isLast) { Objects.requireNonNull(message); - if (!outstandingSend.compareAndSet(false, true)) { - return failedFuture(new IllegalStateException("Send pending")); + long id; + if (DEBUG) { + id = counter.incrementAndGet(); + System.out.printf("[WebSocket] %s send binary: payload=%s last=%s%n", + id, message, isLast); } - CompletableFuture cf - = transport.sendBinary(message, isLast, this, - (r, e) -> outstandingSend.set(false)); - return replaceNull(cf); + CompletableFuture result; + if (!outstandingSend.compareAndSet(false, true)) { + result = failedFuture(new IllegalStateException("Send pending")); + } else { + result = transport.sendBinary(message, isLast, this, + (r, e) -> outstandingSend.set(false)); + } + if (DEBUG) { + System.out.printf("[WebSocket] %s send binary: returned %s%n", + id, result); + } + return replaceNull(result); } private CompletableFuture replaceNull( @@ -191,29 +215,61 @@ @Override public CompletableFuture sendPing(ByteBuffer message) { Objects.requireNonNull(message); - CompletableFuture cf - = transport.sendPing(message, this, (r, e) -> { }); - return replaceNull(cf); + long id; + if (DEBUG) { + id = counter.incrementAndGet(); + System.out.printf("[WebSocket] %s send ping: payload=%s%n", + id, message); + } + CompletableFuture result = transport.sendPing(message, this, + (r, e) -> { }); + if (DEBUG) { + System.out.printf("[WebSocket] %s send ping: returned %s%n", + id, result); + } + return replaceNull(result); } @Override public CompletableFuture sendPong(ByteBuffer message) { Objects.requireNonNull(message); - CompletableFuture cf - = transport.sendPong(message, this, (r, e) -> { }); - return replaceNull(cf); + long id; + if (DEBUG) { + id = counter.incrementAndGet(); + System.out.printf("[WebSocket] %s send pong: payload=%s%n", + id, message); + } + CompletableFuture result = transport.sendPong(message, this, + (r, e) -> { }); + if (DEBUG) { + System.out.printf("[WebSocket] %s send pong: returned %s%n", + id, result); + } + return replaceNull(result); } @Override public CompletableFuture sendClose(int statusCode, String reason) { Objects.requireNonNull(reason); - if (!isLegalToSendFromClient(statusCode)) { - return failedFuture(new IllegalArgumentException("statusCode")); + long id; + if (DEBUG) { + id = counter.incrementAndGet(); + System.out.printf("[WebSocket] %s send close: statusCode=%s, reason.length=%s%n", + id, statusCode, reason); } - // check outputClosed - CompletableFuture cf = sendClose0(statusCode, reason); - return replaceNull(cf); + CompletableFuture result; + if (!isLegalToSendFromClient(statusCode)) { + result = failedFuture(new IllegalArgumentException("statusCode")); + } else { + // check outputClosed + result = sendClose0(statusCode, reason); + } + if (DEBUG) { + System.out.printf("[WebSocket] %s send close: returned %s%n", + id, result); + } + return replaceNull(result); } /*