http-client-branch: (WebSocket) setting DEBUG to true; refactoring; http-client-branch
authorprappo
Fri, 09 Mar 2018 19:10:01 +0000
branchhttp-client-branch
changeset 56291 c8c4c707ff3a
parent 56290 e178d19ff91c
child 56292 b96b5cbb018d
http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
src/java.net.http/share/classes/jdk/internal/net/http/websocket/MessageDecoder.java
src/java.net.http/share/classes/jdk/internal/net/http/websocket/MessageEncoder.java
src/java.net.http/share/classes/jdk/internal/net/http/websocket/MessageQueue.java
src/java.net.http/share/classes/jdk/internal/net/http/websocket/TransportImpl.java
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/MessageDecoder.java	Fri Mar 09 16:53:44 2018 +0000
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/websocket/MessageDecoder.java	Fri Mar 09 19:10:01 2018 +0000
@@ -49,7 +49,7 @@
 /* Exposed for testing purposes */
 class MessageDecoder implements Frame.Consumer {
 
-    private final static boolean DEBUG = false;
+    private final static boolean DEBUG = true;
     private final MessageStreamConsumer output;
     private final UTF8AccumulatingDecoder decoder = new UTF8AccumulatingDecoder();
     private boolean fin;
--- a/src/java.net.http/share/classes/jdk/internal/net/http/websocket/MessageEncoder.java	Fri Mar 09 16:53:44 2018 +0000
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/websocket/MessageEncoder.java	Fri Mar 09 19:10:01 2018 +0000
@@ -48,7 +48,7 @@
  */
 public class MessageEncoder {
 
-    private final static boolean DEBUG = false;
+    private final static boolean DEBUG = true;
 
     private final SecureRandom maskingKeySource = new SecureRandom();
     private final Frame.HeaderWriter headerWriter = new Frame.HeaderWriter();
@@ -186,25 +186,14 @@
                 }
             }
             if (DEBUG) {
-                System.out.printf("[Output] header #%s%n", headerCount);
+                System.out.printf("[Output] frame #%s%n", headerCount);
             }
             intermediateBuffer.flip();
-            headerBuffer.clear();
-            int mask = maskingKeySource.nextInt();
             Opcode opcode = previousFin && headerCount == 0
                     ? Opcode.TEXT : Opcode.CONTINUATION;
             boolean fin = last && !moreText;
-            if (DEBUG) {
-                System.out.printf("[Output] opcode %s%n", opcode);
-            }
-            headerWriter.fin(fin)
-                    .opcode(opcode)
-                    .payloadLen(intermediateBuffer.remaining())
-                    .mask(mask)
-                    .write(headerBuffer);
-            headerBuffer.flip();
+            setupHeader(opcode, fin, intermediateBuffer.remaining());
             headerCount++;
-            payloadMasker.mask(mask);
         }
     }
 
@@ -238,15 +227,8 @@
                 throw new IllegalStateException("Unexpected binary message");
             }
             expectedLen = src.remaining();
-            int mask = maskingKeySource.nextInt();
-            headerBuffer.clear();
-            headerWriter.fin(last)
-                    .opcode(previousFin ? Opcode.BINARY : Opcode.CONTINUATION)
-                    .payloadLen(expectedLen)
-                    .mask(mask)
-                    .write(headerBuffer);
-            headerBuffer.flip();
-            payloadMasker.mask(mask);
+            Opcode opcode = previousFin ? Opcode.BINARY : Opcode.CONTINUATION;
+            setupHeader(opcode, last, expectedLen);
             previousFin = last;
             previousText = false;
             started = true;
@@ -281,15 +263,7 @@
             if (expectedLen > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) {
                 throw new IllegalArgumentException("Long message: " + expectedLen);
             }
-            int mask = maskingKeySource.nextInt();
-            headerBuffer.clear();
-            headerWriter.fin(true)
-                    .opcode(Opcode.PING)
-                    .payloadLen(expectedLen)
-                    .mask(mask)
-                    .write(headerBuffer);
-            headerBuffer.flip();
-            payloadMasker.mask(mask);
+            setupHeader(Opcode.PING, true, expectedLen);
             started = true;
         }
         if (!putAvailable(headerBuffer, dst)) {
@@ -318,15 +292,7 @@
             if (expectedLen > Frame.MAX_CONTROL_FRAME_PAYLOAD_LENGTH) {
                 throw new IllegalArgumentException("Long message: " + expectedLen);
             }
-            int mask = maskingKeySource.nextInt();
-            headerBuffer.clear();
-            headerWriter.fin(true)
-                    .opcode(Opcode.PONG)
-                    .payloadLen(expectedLen)
-                    .mask(mask)
-                    .write(headerBuffer);
-            headerBuffer.flip();
-            payloadMasker.mask(mask);
+            setupHeader(Opcode.PONG, true, expectedLen);
             started = true;
         }
         if (!putAvailable(headerBuffer, dst)) {
@@ -379,15 +345,7 @@
                 throw new InternalError(); // assertion
             }
             intermediateBuffer.flip();
-            headerBuffer.clear();
-            int mask = maskingKeySource.nextInt();
-            headerWriter.fin(true)
-                    .opcode(Opcode.CLOSE)
-                    .payloadLen(intermediateBuffer.remaining())
-                    .mask(mask)
-                    .write(headerBuffer);
-            headerBuffer.flip();
-            payloadMasker.mask(mask);
+            setupHeader(Opcode.CLOSE, true, intermediateBuffer.remaining());
             started = true;
             closed = true;
             if (DEBUG) {
@@ -400,4 +358,20 @@
         }
         return maskAvailable(intermediateBuffer, dst) >= 0;
     }
+
+    private void setupHeader(Opcode opcode, boolean fin, long payloadLen) {
+        if (DEBUG) {
+            System.out.printf("[Output] frame opcode=%s fin=%s len=%s%n",
+                              opcode, fin, payloadLen);
+        }
+        headerBuffer.clear();
+        int mask = maskingKeySource.nextInt();
+        headerWriter.fin(fin)
+                    .opcode(opcode)
+                    .payloadLen(payloadLen)
+                    .mask(mask)
+                    .write(headerBuffer);
+        headerBuffer.flip();
+        payloadMasker.mask(mask);
+    }
 }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/websocket/MessageQueue.java	Fri Mar 09 16:53:44 2018 +0000
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/websocket/MessageQueue.java	Fri Mar 09 19:10:01 2018 +0000
@@ -83,7 +83,7 @@
  */
 public class MessageQueue {
 
-    private final static boolean DEBUG = false;
+    private final static boolean DEBUG = true;
 
     @Stable
     private final Message[] elements;
--- a/src/java.net.http/share/classes/jdk/internal/net/http/websocket/TransportImpl.java	Fri Mar 09 16:53:44 2018 +0000
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/websocket/TransportImpl.java	Fri Mar 09 19:10:01 2018 +0000
@@ -49,10 +49,9 @@
 
     // -- Debugging infrastructure --
 
-    private final static boolean DEBUG = false;
-
+    private final static boolean DEBUG = true;
     /* Used for correlating enters to and exists from a method */
-    private final static AtomicLong COUNTER = new AtomicLong();
+    private final AtomicLong counter = new AtomicLong();
 
     private final SequentialScheduler sendScheduler = new SequentialScheduler(new SendTask());
 
@@ -93,7 +92,7 @@
         String name = "jdk.httpclient.websocket.writeBufferSize";
         int capacity = Utils.getIntegerNetProperty(name, 16384);
         if (DEBUG) {
-            System.out.printf("[Transport] write buffer capacity %s", capacity);
+            System.out.printf("[Transport] write buffer capacity %s%n", capacity);
         }
         // TODO (optimization?): allocateDirect if SSL?
         return ByteBuffer.allocate(capacity);
@@ -122,7 +121,7 @@
                                              BiConsumer<? super T, ? super Throwable> action) {
         long id;
         if (DEBUG) {
-            id = COUNTER.incrementAndGet();
+            id = counter.incrementAndGet();
             System.out.printf("[Transport] %s: sendText message.length()=%s, last=%s%n",
                               id, message.length(), isLast);
         }
@@ -155,7 +154,7 @@
                                                BiConsumer<? super T, ? super Throwable> action) {
         long id;
         if (DEBUG) {
-            id = COUNTER.incrementAndGet();
+            id = counter.incrementAndGet();
             System.out.printf("[Transport] %s: sendBinary message.remaining()=%s, last=%s%n",
                               id, message.remaining(), isLast);
         }
@@ -178,7 +177,7 @@
                                              BiConsumer<? super T, ? super Throwable> action) {
         long id;
         if (DEBUG) {
-            id = COUNTER.incrementAndGet();
+            id = counter.incrementAndGet();
             System.out.printf("[Transport] %s: sendPing message.remaining()=%s%n",
                               id, message.remaining());
         }
@@ -201,7 +200,7 @@
                                              BiConsumer<? super T, ? super Throwable> action) {
         long id;
         if (DEBUG) {
-            id = COUNTER.incrementAndGet();
+            id = counter.incrementAndGet();
             System.out.printf("[Transport] %s: sendPong message.remaining()=%s%n",
                               id, message.remaining());
         }
@@ -225,7 +224,7 @@
                                               BiConsumer<? super T, ? super Throwable> action) {
         long id;
         if (DEBUG) {
-            id = COUNTER.incrementAndGet();
+            id = counter.incrementAndGet();
             System.out.printf("[Transport] %s: sendClose statusCode=%s, reason.length()=%s%n",
                               id, statusCode, reason.length());
         }
@@ -474,21 +473,21 @@
                         // The previous part of the binary representation of the message
                         // hasn't been fully written
                         if (!tryCompleteWrite()) {
-                            return;
+                            break;
                         }
                     } else if (!encoded) {
                         if (firstPass) {
                             firstPass = false;
                             queue.peek(loadCallback);
                             if (DEBUG) {
-                                System.out.printf("[Transport] loaded message%n");
+                                System.out.printf("[Transport] load message%n");
                             }
                         }
                         dst.clear();
                         encoded = queue.peek(encodingCallback);
                         dst.flip();
                         if (!tryCompleteWrite()) {
-                            return;
+                            break;
                         }
                     } else {
                         // All done, remove and complete
@@ -497,7 +496,7 @@
                     }
                 } catch (Throwable t) {
                     if (DEBUG) {
-                        System.out.printf("[Transport] exception %s; cleanup%n", t);
+                        System.out.printf("[Transport] send task exception %s%n", t);
                     }
                     // buffer cleanup: if there is an exception, the buffer
                     // should appear empty for the next write as there is
@@ -563,7 +562,8 @@
         @SuppressWarnings("unchecked")
         private void removeAndComplete(Throwable error) {
             if (DEBUG) {
-                System.out.printf("[Transport] removeAndComplete error=%s%n", error);
+                System.out.printf("[Transport] removeAndComplete error=%s%n",
+                                  error);
             }
             queue.remove();
             if (error != null) {
@@ -677,7 +677,7 @@
                 s = writeState.get();
                 if (s == CLOSED) {
                     if (DEBUG) {
-                        System.out.printf("[Transport] write state %s %n", s);
+                        System.out.printf("[Transport] write state %s%n", s);
                     }
                     break;
                 }
--- a/src/java.net.http/share/classes/jdk/internal/net/http/websocket/WebSocketImpl.java	Fri Mar 09 16:53:44 2018 +0000
+++ b/src/java.net.http/share/classes/jdk/internal/net/http/websocket/WebSocketImpl.java	Fri Mar 09 19:10:01 2018 +0000
@@ -67,7 +67,7 @@
  */
 public final class WebSocketImpl implements WebSocket {
 
-    private final static boolean DEBUG = false;
+    private final static boolean DEBUG = true;
 
     enum State {
         OPEN,
@@ -253,7 +253,7 @@
     @Override
     public void request(long n) {
         if (DEBUG) {
-            System.out.printf("[WebSocket] request(%s)%n", n);
+            System.out.printf("[WebSocket] request %s%n", n);
         }
         if (demand.increase(n)) {
             receiveScheduler.runOrSchedule();
@@ -278,7 +278,7 @@
     @Override
     public void abort() {
         if (DEBUG) {
-            System.out.printf("[WebSocket] abort()%n");
+            System.out.printf("[WebSocket] abort %n");
         }
         inputClosed = true;
         outputClosed = true;