src/java.net.http/share/classes/jdk/internal/net/http/websocket/WebSocketImpl.java
author prappo
Thu, 22 Mar 2018 09:48:27 +0000
branchhttp-client-branch
changeset 56341 10fcbe13cd19
parent 56337 58e16ad3fb98
child 56389 0ba90c4f1e3f
permissions -rw-r--r--
http-client-branch: (WebSocket) from IOE to IAE in sendClose with bad reason
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
56024
de352132c7e8 http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents: 55992
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    26
package jdk.internal.net.http.websocket;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    28
import jdk.internal.net.http.common.Demand;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    29
import jdk.internal.net.http.common.Log;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    30
import jdk.internal.net.http.common.MinimalFuture;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    31
import jdk.internal.net.http.common.SequentialScheduler;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    32
import jdk.internal.net.http.common.Utils;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    33
import jdk.internal.net.http.websocket.OpeningHandshake.Result;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    34
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    35
import java.io.IOException;
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
    36
import java.io.InterruptedIOException;
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
    37
import java.lang.System.Logger.Level;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    38
import java.lang.ref.Reference;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    39
import java.net.ProtocolException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
import java.net.URI;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
    41
import java.net.http.WebSocket;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    42
import java.nio.ByteBuffer;
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
    43
import java.nio.CharBuffer;
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
    44
import java.nio.charset.CharacterCodingException;
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
    45
import java.nio.charset.CharsetEncoder;
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
    46
import java.nio.charset.CodingErrorAction;
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
    47
import java.nio.charset.StandardCharsets;
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    48
import java.util.Objects;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    49
import java.util.concurrent.CompletableFuture;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    50
import java.util.concurrent.CompletionStage;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
import java.util.concurrent.atomic.AtomicBoolean;
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
    52
import java.util.concurrent.atomic.AtomicLong;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    53
import java.util.concurrent.atomic.AtomicReference;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
    54
import java.util.function.BiConsumer;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    55
import java.util.function.Function;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    56
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    57
import static java.util.Objects.requireNonNull;
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
    58
import static jdk.internal.net.http.common.MinimalFuture.completedFuture;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    59
import static jdk.internal.net.http.common.MinimalFuture.failedFuture;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    60
import static jdk.internal.net.http.websocket.StatusCodes.CLOSED_ABNORMALLY;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    61
import static jdk.internal.net.http.websocket.StatusCodes.NO_STATUS_CODE;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    62
import static jdk.internal.net.http.websocket.StatusCodes.isLegalToSendFromClient;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    63
import static jdk.internal.net.http.websocket.WebSocketImpl.State.BINARY;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    64
import static jdk.internal.net.http.websocket.WebSocketImpl.State.CLOSE;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    65
import static jdk.internal.net.http.websocket.WebSocketImpl.State.ERROR;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    66
import static jdk.internal.net.http.websocket.WebSocketImpl.State.IDLE;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    67
import static jdk.internal.net.http.websocket.WebSocketImpl.State.OPEN;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    68
import static jdk.internal.net.http.websocket.WebSocketImpl.State.PING;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    69
import static jdk.internal.net.http.websocket.WebSocketImpl.State.PONG;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    70
import static jdk.internal.net.http.websocket.WebSocketImpl.State.TEXT;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    71
import static jdk.internal.net.http.websocket.WebSocketImpl.State.WAITING;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    72
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    73
/*
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    74
 * A WebSocket client.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    75
 */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    76
public final class WebSocketImpl implements WebSocket {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    77
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
    78
    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
    79
    private static final System.Logger debug =
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
    80
            Utils.getWebSocketLogger("[WebSocket]"::toString, DEBUG);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
    81
    private final AtomicLong sendCounter = new AtomicLong();
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
    82
    private final AtomicLong receiveCounter = new AtomicLong();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
    83
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    84
    enum State {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    85
        OPEN,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    86
        IDLE,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    87
        WAITING,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    88
        TEXT,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    89
        BINARY,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    90
        PING,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    91
        PONG,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    92
        CLOSE,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    93
        ERROR;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    94
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    95
56303
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
    96
    private final AtomicReference<ByteBuffer> lastAutomaticPong = new AtomicReference<>();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
    97
    private final MinimalFuture<WebSocket> DONE = MinimalFuture.completedFuture(this);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    98
    private volatile boolean inputClosed;
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
    99
    private final AtomicBoolean outputClosed = new AtomicBoolean();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   100
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   101
    private final AtomicReference<State> state = new AtomicReference<>(OPEN);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   102
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   103
    /* Components of calls to Listener's methods */
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   104
    private boolean last;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   105
    private ByteBuffer binaryData;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   106
    private CharSequence text;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   107
    private int statusCode;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   108
    private String reason;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   109
    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
   110
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   111
    private final URI uri;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
    private final String subprotocol;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   113
    private final Listener listener;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   114
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   115
    private final AtomicBoolean pendingTextOrBinary = new AtomicBoolean();
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   116
    private final AtomicBoolean pendingPingOrPong = new AtomicBoolean();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   117
    private final Transport transport;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   118
    private final SequentialScheduler receiveScheduler
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   119
            = new SequentialScheduler(new ReceiveTask());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   120
    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
   121
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   122
    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
   123
        Function<Result, WebSocket> newWebSocket = r -> {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   124
            WebSocket ws = newInstance(b.getUri(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   125
                                       r.subprotocol,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   126
                                       b.getListener(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   127
                                       r.transport);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   128
            // Make sure we don't release the builder until this lambda
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   129
            // has been executed. The builder has a strong reference to
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   130
            // the HttpClientFacade, and we want to keep that live until
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   131
            // after the raw channel is created and passed to WebSocketImpl.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   132
            Reference.reachabilityFence(b);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   133
            return ws;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   134
        };
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   135
        OpeningHandshake h;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   136
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   137
            h = new OpeningHandshake(b);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   138
        } catch (Throwable e) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   139
            return failedFuture(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   140
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   141
        return h.send().thenApply(newWebSocket);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   142
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   143
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   144
    /* Exposed for testing purposes */
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   145
    static WebSocketImpl newInstance(URI uri,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   146
                                     String subprotocol,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   147
                                     Listener listener,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   148
                                     TransportFactory transport) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   149
        WebSocketImpl ws = new WebSocketImpl(uri, subprotocol, listener, transport);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   150
        // This initialisation is outside of the constructor for the sake of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   151
        // safe publication of WebSocketImpl.this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   152
        ws.signalOpen();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   153
        return ws;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   154
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   155
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   156
    private WebSocketImpl(URI uri,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   157
                          String subprotocol,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   158
                          Listener listener,
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   159
                          TransportFactory transportFactory) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   160
        this.uri = requireNonNull(uri);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   161
        this.subprotocol = requireNonNull(subprotocol);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   162
        this.listener = requireNonNull(listener);
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   163
        this.transport = transportFactory.createTransport(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   164
                new SignallingMessageConsumer());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   165
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   166
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   167
    // FIXME: add to action handling of errors -> signalError()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   168
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   169
    @Override
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   170
    public CompletableFuture<WebSocket> sendText(CharSequence message,
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   171
                                                 boolean last) {
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   172
        Objects.requireNonNull(message);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   173
        long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   174
        if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   175
            id = sendCounter.incrementAndGet();
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   176
            debug.log(Level.DEBUG, "enter send text %s payload length=%s last=%s",
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   177
                      id, message.length(), last);
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   178
        }
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   179
        CompletableFuture<WebSocket> result;
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   180
        if (!setPendingTextOrBinary()) {
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   181
            result = failedFuture(new IllegalStateException("Send pending"));
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   182
        } else {
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   183
            result = transport.sendText(message, last, this,
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   184
                                        (r, e) -> clearPendingTextOrBinary());
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   185
        }
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   186
        debug.log(Level.DEBUG, "exit send text %s returned %s", id, result);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   187
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   188
        return replaceNull(result);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   189
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   190
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   191
    @Override
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   192
    public CompletableFuture<WebSocket> sendBinary(ByteBuffer message,
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   193
                                                   boolean last) {
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   194
        Objects.requireNonNull(message);
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)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   197
            id = sendCounter.incrementAndGet();
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   198
            debug.log(Level.DEBUG, "enter send binary %s payload=%s last=%s",
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   199
                      id, message, last);
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   200
        }
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   201
        CompletableFuture<WebSocket> result;
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   202
        if (!setPendingTextOrBinary()) {
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   203
            result = failedFuture(new IllegalStateException("Send pending"));
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   204
        } else {
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   205
            result = transport.sendBinary(message, last, this,
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   206
                                          (r, e) -> clearPendingTextOrBinary());
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   207
        }
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   208
        debug.log(Level.DEBUG, "exit send binary %s returned %s", id, result);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   209
        return replaceNull(result);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   210
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   211
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   212
    private void clearPendingTextOrBinary() {
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   213
        pendingTextOrBinary.set(false);
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   214
    }
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   215
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   216
    private boolean setPendingTextOrBinary() {
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   217
        return pendingTextOrBinary.compareAndSet(false, true);
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   218
    }
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   219
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   220
    private CompletableFuture<WebSocket> replaceNull(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   221
            CompletableFuture<WebSocket> cf)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   222
    {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   223
        if (cf == null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   224
            return DONE;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   225
        } else {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   226
            return cf;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   227
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   228
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   229
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   230
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   231
    public CompletableFuture<WebSocket> sendPing(ByteBuffer message) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   232
        Objects.requireNonNull(message);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   233
        long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   234
        if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   235
            id = sendCounter.incrementAndGet();
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   236
            debug.log(Level.DEBUG, "enter send ping %s payload=%s", id, message);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   237
        }
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   238
        CompletableFuture<WebSocket> result;
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   239
        if (!setPendingPingOrPong()) {
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   240
            result = failedFuture(new IllegalStateException("Send pending"));
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   241
        } else {
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   242
            result = transport.sendPing(message, this,
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   243
                                        (r, e) -> clearPendingPingOrPong());
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   244
        }
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   245
        debug.log(Level.DEBUG, "exit send ping %s returned %s", id, result);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   246
        return replaceNull(result);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   247
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   248
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   249
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   250
    public CompletableFuture<WebSocket> sendPong(ByteBuffer message) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   251
        Objects.requireNonNull(message);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   252
        long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   253
        if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   254
            id = sendCounter.incrementAndGet();
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   255
            debug.log(Level.DEBUG, "enter send pong %s payload=%s", id, message);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   256
        }
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   257
        CompletableFuture<WebSocket> result;
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   258
        if (!setPendingPingOrPong()) {
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   259
            result = failedFuture(new IllegalStateException("Send pending"));
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   260
        } else {
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   261
            result =  transport.sendPong(message, this,
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   262
                                         (r, e) -> clearPendingPingOrPong());
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   263
        }
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   264
        debug.log(Level.DEBUG, "exit send pong %s returned %s", id, result);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   265
        return replaceNull(result);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   266
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   267
56318
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   268
    private boolean setPendingPingOrPong() {
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   269
        return pendingPingOrPong.compareAndSet(false, true);
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   270
    }
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   271
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   272
    private void clearPendingPingOrPong() {
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   273
        pendingPingOrPong.set(false);
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   274
    }
2a96e88888b2 http-client-branch: (WebSocket) API changes + test;
prappo
parents: 56314
diff changeset
   275
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   276
    @Override
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   277
    public CompletableFuture<WebSocket> sendClose(int statusCode,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   278
                                                  String reason) {
56058
a02d0098c630 http-client-branch: (WebSocket) example-test
prappo
parents: 56045
diff changeset
   279
        Objects.requireNonNull(reason);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   280
        long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   281
        if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   282
            id = sendCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   283
            debug.log(Level.DEBUG,
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   284
                      "enter send close %s statusCode=%s reason.length=%s",
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   285
                      id, statusCode, reason.length());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   286
        }
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   287
        CompletableFuture<WebSocket> result;
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   288
        // Close message is the only type of message whose validity is checked
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   289
        // in the corresponding send method. This is made in order to close the
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   290
        // output in place. Otherwise the number of Close messages in queue
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   291
        // would not be bounded.
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   292
        if (!isLegalToSendFromClient(statusCode)) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   293
            result = failedFuture(new IllegalArgumentException("statusCode"));
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   294
        } else if (!isLegalReason(reason)) {
56341
10fcbe13cd19 http-client-branch: (WebSocket) from IOE to IAE in sendClose with bad reason
prappo
parents: 56337
diff changeset
   295
            result = failedFuture(new IllegalArgumentException("reason"));
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   296
        } else if (!outputClosed.compareAndSet(false, true)){
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   297
            result = failedFuture(new IOException("Output closed"));
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   298
        } else {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   299
            result = sendClose0(statusCode, reason);
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   300
        }
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   301
        debug.log(Level.DEBUG, "exit send close %s returned %s", id, result);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   302
        return replaceNull(result);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   303
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   304
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   305
    private static boolean isLegalReason(String reason) {
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   306
        if (reason.length() > 123) { // quick check
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   307
            return false;
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   308
        }
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   309
        CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder()
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   310
                        .onMalformedInput(CodingErrorAction.REPORT)
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   311
                        .onUnmappableCharacter(CodingErrorAction.REPORT);
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   312
        ByteBuffer bytes;
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   313
        try {
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   314
            bytes = encoder.encode(CharBuffer.wrap(reason));
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   315
        } catch (CharacterCodingException ignored) {
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   316
            return false;
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   317
        }
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   318
        return bytes.remaining() <= 123;
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   319
    }
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   320
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   321
    /*
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   322
     * The implementation uses this method internally to send Close messages
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   323
     * with codes that are not allowed to be sent through the API.
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   324
     */
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   325
    private CompletableFuture<WebSocket> sendClose0(int statusCode,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   326
                                                    String reason) {
56327
efd50952acd9 http-client-branch: (WebSocket) remove close timer
prappo
parents: 56326
diff changeset
   327
        return transport.sendClose(statusCode, reason, this,
efd50952acd9 http-client-branch: (WebSocket) remove close timer
prappo
parents: 56326
diff changeset
   328
                                   (r, e) -> processCloseError(e));
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   329
    }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   330
56327
efd50952acd9 http-client-branch: (WebSocket) remove close timer
prappo
parents: 56326
diff changeset
   331
    private void processCloseError(Throwable e) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   332
        if (e == null) {
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   333
            debug.log(Level.DEBUG, "send close completed successfully");
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   334
        } else {
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   335
            debug.log(Level.DEBUG, "send close completed with error", e);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   336
        }
56327
efd50952acd9 http-client-branch: (WebSocket) remove close timer
prappo
parents: 56326
diff changeset
   337
        outputClosed.set(true);
efd50952acd9 http-client-branch: (WebSocket) remove close timer
prappo
parents: 56326
diff changeset
   338
        try {
efd50952acd9 http-client-branch: (WebSocket) remove close timer
prappo
parents: 56326
diff changeset
   339
            transport.closeOutput();
efd50952acd9 http-client-branch: (WebSocket) remove close timer
prappo
parents: 56326
diff changeset
   340
        } catch (IOException ignored) { }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   341
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   342
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   343
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   344
    public void request(long n) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   345
        debug.log(Level.DEBUG, "request %s", n);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   346
        if (demand.increase(n)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   347
            receiveScheduler.runOrSchedule();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   348
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   349
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   350
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   351
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   352
    public String getSubprotocol() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   353
        return subprotocol;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   354
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   355
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   356
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   357
    public boolean isOutputClosed() {
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   358
        return outputClosed.get();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   359
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   360
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   361
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   362
    public boolean isInputClosed() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   363
        return inputClosed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   364
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   365
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   366
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   367
    public void abort() {
56306
88c1f551d45e http-client-branch: (WebSocket) logging typos and leftovers
prappo
parents: 56304
diff changeset
   368
        debug.log(Level.DEBUG, "abort");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   369
        inputClosed = true;
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   370
        outputClosed.set(true);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   371
        receiveScheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   372
        close();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   373
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   374
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   375
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   376
    public String toString() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   377
        return super.toString()
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   378
                + "[uri=" + uri
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   379
                + (!subprotocol.isEmpty() ? ", subprotocol=" + subprotocol : "")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   380
                + "]";
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   381
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   382
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   383
    /*
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   384
     * The assumptions about order is as follows:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   385
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   386
     *     - state is never changed more than twice inside the `run` method:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   387
     *       x --(1)--> IDLE --(2)--> y (otherwise we're loosing events, or
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   388
     *       overwriting parts of messages creating a mess since there's no
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   389
     *       queueing)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   390
     *     - OPEN is always the first state
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   391
     *     - no messages are requested/delivered before onOpen is called (this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   392
     *       is implemented by making WebSocket instance accessible first in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   393
     *       onOpen)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   394
     *     - after the state has been observed as CLOSE/ERROR, the scheduler
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   395
     *       is stopped
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   396
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   397
    private class ReceiveTask extends SequentialScheduler.CompleteRestartableTask {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   398
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   399
        // Transport only asked here and nowhere else because we must make sure
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   400
        // onOpen is invoked first and no messages become pending before onOpen
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   401
        // finishes
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   402
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   403
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   404
        public void run() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   405
            debug.log(Level.DEBUG, "enter receive task");
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   406
            loop:
56322
316be30d078d http-client-branch: (WebSocket) no error after abort
prappo
parents: 56320
diff changeset
   407
            while (!receiveScheduler.isStopped()) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   408
                State s = state.get();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   409
                debug.log(Level.DEBUG, "receive state: %s", s);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   410
                try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   411
                    switch (s) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   412
                        case OPEN:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   413
                            processOpen();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   414
                            tryChangeState(OPEN, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   415
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   416
                        case TEXT:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   417
                            processText();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   418
                            tryChangeState(TEXT, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   419
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   420
                        case BINARY:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   421
                            processBinary();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   422
                            tryChangeState(BINARY, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   423
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   424
                        case PING:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   425
                            processPing();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   426
                            tryChangeState(PING, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   427
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   428
                        case PONG:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   429
                            processPong();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   430
                            tryChangeState(PONG, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   431
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   432
                        case CLOSE:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   433
                            processClose();
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   434
                            break loop;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   435
                        case ERROR:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   436
                            processError();
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   437
                            break loop;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   438
                        case IDLE:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   439
                            if (demand.tryDecrement()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   440
                                    && tryChangeState(IDLE, WAITING)) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   441
                                transport.request(1);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   442
                            }
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   443
                            break loop;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   444
                        case WAITING:
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   445
                            // For debugging spurious signalling: when there was
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   446
                            // a signal, but apparently nothing has changed
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   447
                            break loop;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   448
                        default:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   449
                            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
   450
                    }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   451
                } catch (Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   452
                    signalError(t);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   453
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   454
            }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   455
            debug.log(Level.DEBUG, "exit receive task");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   456
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   457
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   458
        private void processError() throws IOException {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   459
            debug.log(Level.DEBUG, "processError");
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   460
            transport.closeInput();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   461
            receiveScheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   462
            Throwable err = error.get();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   463
            if (err instanceof FailWebSocketException) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   464
                int code1 = ((FailWebSocketException) err).getStatusCode();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   465
                err = new ProtocolException().initCause(err);
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   466
                debug.log(Level.DEBUG, "failing %s with error=%s statusCode=%s",
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   467
                          WebSocketImpl.this, err, code1);
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   468
                sendCloseSilently(code1);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   469
            }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   470
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   471
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   472
                id = receiveCounter.incrementAndGet();
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   473
                debug.log(Level.DEBUG, "enter onError %s error=%s", id, err);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   474
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   475
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   476
                listener.onError(WebSocketImpl.this, err);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   477
            } finally {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   478
                debug.log(Level.DEBUG, "exit onError %s", id);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   479
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   480
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   481
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   482
        private void processClose() throws IOException {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   483
            debug.log(Level.DEBUG, "processClose");
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   484
            transport.closeInput();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   485
            receiveScheduler.stop();
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   486
            CompletionStage<?> cs = null; // when the listener is ready to close
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   487
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   488
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   489
                id = receiveCounter.incrementAndGet();
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   490
                debug.log(Level.DEBUG,
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   491
                          "enter onClose %s statusCode=%s reason.length=%s",
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   492
                          id, statusCode, reason.length());
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   493
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   494
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   495
                cs = listener.onClose(WebSocketImpl.this, statusCode, reason);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   496
            } finally {
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   497
                debug.log(Level.DEBUG, "exit onClose %s returned %s", id, cs);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   498
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   499
            if (cs == null) {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   500
                cs = DONE;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   501
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   502
            int code;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   503
            if (statusCode == NO_STATUS_CODE || statusCode == CLOSED_ABNORMALLY) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   504
                code = NORMAL_CLOSURE;
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   505
                debug.log(Level.DEBUG, "using statusCode %s instead of %s",
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   506
                          statusCode, code);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   507
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   508
            } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   509
                code = statusCode;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   510
            }
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   511
            cs.whenComplete((r, e) -> {
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   512
                debug.log(Level.DEBUG,
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   513
                          "CompletionStage returned by onClose completed result=%s error=%s",
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   514
                          r, e);
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   515
                sendCloseSilently(code);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   516
            });
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   517
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   518
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   519
        private void processPong() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   520
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   521
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   522
                id = receiveCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   523
                debug.log(Level.DEBUG, "enter onPong %s payload=%s",
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   524
                          id, binaryData);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   525
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   526
            CompletionStage<?> cs = null;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   527
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   528
                cs = listener.onPong(WebSocketImpl.this, binaryData);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   529
            } finally {
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   530
                debug.log(Level.DEBUG, "exit onPong %s returned %s", id, cs);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   531
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   532
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   533
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   534
        private void processPing() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   535
            debug.log(Level.DEBUG, "processPing");
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   536
            // A full copy of this (small) data is made. This way sending a
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   537
            // replying Pong could be done in parallel with the listener
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   538
            // handling this Ping.
56323
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   539
            ByteBuffer slice = binaryData.slice();
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   540
            if (!outputClosed.get()) {
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   541
                ByteBuffer copy = ByteBuffer.allocate(binaryData.remaining())
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   542
                        .put(binaryData)
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   543
                        .flip();
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   544
                if (!trySwapAutomaticPong(copy)) {
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   545
                    // Non-exclusive send;
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   546
                    BiConsumer<WebSocketImpl, Throwable> reporter = (r, e) -> {
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   547
                        if (e != null) { // TODO: better error handing. What if already closed?
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   548
                            signalError(Utils.getCompletionCause(e));
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   549
                        }
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   550
                    };
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   551
                    transport.sendPong(WebSocketImpl.this::clearAutomaticPong,
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   552
                                       WebSocketImpl.this,
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   553
                                       reporter);
cf43d0ee8959 http-client-branch: (WebSocket) autopong on a closed channel is not an error + test
prappo
parents: 56322
diff changeset
   554
                }
56303
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   555
            }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   556
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   557
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   558
                id = receiveCounter.incrementAndGet();
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   559
                debug.log(Level.DEBUG, "enter onPing %s payload=%s", id, slice);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   560
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   561
            CompletionStage<?> cs = null;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   562
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   563
                cs = listener.onPing(WebSocketImpl.this, slice);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   564
            } finally {
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   565
                debug.log(Level.DEBUG, "exit onPing %s returned %s", id, cs);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   566
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   567
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   568
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   569
        private void processBinary() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   570
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   571
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   572
                id = receiveCounter.incrementAndGet();
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   573
                debug.log(Level.DEBUG, "enter onBinary %s payload=%s last=%s",
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   574
                          id, binaryData, last);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   575
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   576
            CompletionStage<?> cs = null;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   577
            try {
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   578
                cs = listener.onBinary(WebSocketImpl.this, binaryData, last);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   579
            } finally {
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   580
                debug.log(Level.DEBUG, "exit onBinary %s returned %s", id, cs);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   581
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   582
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   583
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   584
        private void processText() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   585
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   586
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   587
                id = receiveCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   588
                debug.log(Level.DEBUG,
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   589
                          "enter onText %s payload.length=%s last=%s",
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   590
                          id, text.length(), last);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   591
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   592
            CompletionStage<?> cs = null;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   593
            try {
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   594
                cs = listener.onText(WebSocketImpl.this, text, last);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   595
            } finally {
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   596
                debug.log(Level.DEBUG, "exit onText %s returned %s", id, cs);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   597
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   598
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   599
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   600
        private void processOpen() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   601
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   602
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   603
                id = receiveCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   604
                debug.log(Level.DEBUG, "enter onOpen %s", id);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   605
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   606
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   607
                listener.onOpen(WebSocketImpl.this);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   608
            } finally {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   609
                debug.log(Level.DEBUG, "exit onOpen %s", id);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   610
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   611
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   612
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   613
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   614
    private void sendCloseSilently(int statusCode) {
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   615
        sendClose0(statusCode, "").whenComplete((r, e) -> {
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   616
            if (e != null) {
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   617
                debug.log(Level.DEBUG, "automatic closure completed with error",
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   618
                          (Object) e);
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   619
            }
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   620
        });
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   621
    }
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   622
56303
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   623
    private ByteBuffer clearAutomaticPong() {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   624
        ByteBuffer data;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   625
        do {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   626
            data = lastAutomaticPong.get();
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   627
            if (data == null) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   628
                // This method must never be called unless a message that is
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   629
                // using it has been added previously
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   630
                throw new InternalError();
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   631
            }
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   632
        } while (!lastAutomaticPong.compareAndSet(data, null));
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   633
        return data;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   634
    }
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   635
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   636
    // bound pings
56303
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   637
    private boolean trySwapAutomaticPong(ByteBuffer copy) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   638
        ByteBuffer message;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   639
        boolean swapped;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   640
        while (true) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   641
            message = lastAutomaticPong.get();
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   642
            if (message == null) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   643
                if (!lastAutomaticPong.compareAndSet(null, copy)) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   644
                    // It's only this method that can change null to ByteBuffer,
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   645
                    // and this method is invoked at most by one thread at a
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   646
                    // time. Thus no failure in the atomic operation above is
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   647
                    // expected.
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   648
                    throw new InternalError();
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   649
                }
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   650
                swapped = false;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   651
                break;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   652
            } else if (lastAutomaticPong.compareAndSet(message, copy)) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   653
                swapped = true;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   654
                break;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   655
            }
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   656
        }
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   657
        debug.log(Level.DEBUG, "swapped automatic pong from %s to %s",
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   658
                  message, copy);
56303
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   659
        return swapped;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   660
    }
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   661
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   662
    private void signalOpen() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   663
        debug.log(Level.DEBUG, "signalOpen");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   664
        receiveScheduler.runOrSchedule();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   665
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   666
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   667
    private void signalError(Throwable error) {
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   668
        debug.log(Level.DEBUG, "signalError %s", (Object) error);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   669
        inputClosed = true;
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   670
        outputClosed.set(true);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   671
        if (!this.error.compareAndSet(null, error) || !trySetState(ERROR)) {
56326
63422db47911 http-client-branch: RawChannel implementation based on FlowTube
dfuchs
parents: 56323
diff changeset
   672
            debug.log(Level.DEBUG, "signalError", error);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   673
            Log.logError(error);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   674
        } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   675
            close();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   676
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   677
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   678
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   679
    private void close() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   680
        debug.log(Level.DEBUG, "close");
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   681
        Throwable first = null;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   682
        try {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   683
            transport.closeInput();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   684
        } catch (Throwable t1) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   685
            first = t1;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   686
        } finally {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   687
            Throwable second = null;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   688
            try {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   689
                transport.closeOutput();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   690
            } catch (Throwable t2) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   691
                second = t2;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   692
            } finally {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   693
                Throwable e = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   694
                if (first != null && second != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   695
                    first.addSuppressed(second);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   696
                    e = first;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   697
                } else if (first != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   698
                    e = first;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   699
                } else if (second != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   700
                    e = second;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   701
                }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   702
                if (e != null) {
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   703
                    debug.log(Level.DEBUG, "exception in close", e);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   704
                }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   705
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   706
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   707
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   708
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   709
    private void signalClose(int statusCode, String reason) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   710
        // FIXME: make sure no race reason & close are not intermixed
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   711
        inputClosed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   712
        this.statusCode = statusCode;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   713
        this.reason = reason;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   714
        boolean managed = trySetState(CLOSE);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   715
        debug.log(Level.DEBUG,
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   716
                  "signalClose statusCode=%s reason.length=%s: %s",
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   717
                  statusCode, reason.length(), managed);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   718
        if (managed) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   719
            try {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   720
                transport.closeInput();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   721
            } catch (Throwable t) {
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   722
                debug.log(Level.DEBUG, "exception closing input", (Object) t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   723
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   724
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   725
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   726
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   727
    private class SignallingMessageConsumer implements MessageStreamConsumer {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   728
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   729
        @Override
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   730
        public void onText(CharSequence data, boolean last) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   731
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   732
            text = data;
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   733
            WebSocketImpl.this.last = last;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   734
            tryChangeState(WAITING, TEXT);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   735
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   736
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   737
        @Override
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   738
        public void onBinary(ByteBuffer data, boolean last) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   739
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   740
            binaryData = data;
56320
f82729ca8660 http-client-branch: (WebSocket) removed MessagePart
prappo
parents: 56318
diff changeset
   741
            WebSocketImpl.this.last = last;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   742
            tryChangeState(WAITING, BINARY);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   743
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   744
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   745
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   746
        public void onPing(ByteBuffer data) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   747
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   748
            binaryData = data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   749
            tryChangeState(WAITING, PING);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   750
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   751
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   752
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   753
        public void onPong(ByteBuffer data) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   754
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   755
            binaryData = data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   756
            tryChangeState(WAITING, PONG);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   757
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   758
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   759
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   760
        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
   761
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   762
            signalClose(statusCode, reason.toString());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   763
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   764
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   765
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   766
        public void onComplete() {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   767
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   768
            signalClose(CLOSED_ABNORMALLY, "");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   769
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   770
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   771
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   772
        public void onError(Throwable error) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   773
            signalError(error);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   774
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   775
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   776
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   777
    private boolean trySetState(State newState) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   778
        State currentState;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   779
        boolean success = false;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   780
        while (true) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   781
            currentState = state.get();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   782
            if (currentState == ERROR || currentState == CLOSE) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   783
                break;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   784
            } else if (state.compareAndSet(currentState, newState)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   785
                receiveScheduler.runOrSchedule();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   786
                success = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   787
                break;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   788
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   789
        }
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   790
        debug.log(Level.DEBUG, "set state %s (previous %s) %s",
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   791
                  newState, currentState, success);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   792
        return success;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   793
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   794
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   795
    private boolean tryChangeState(State expectedState, State newState) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   796
        State witness = state.compareAndExchange(expectedState, newState);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   797
        boolean success = false;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   798
        if (witness == expectedState) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   799
            receiveScheduler.runOrSchedule();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   800
            success = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   801
        } else if (witness != ERROR && witness != CLOSE) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   802
            // This should be the only reason for inability to change the state
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   803
            // from IDLE to WAITING: the state has changed to terminal
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   804
            throw new InternalError();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   805
        }
56314
f92e7a8a189f http-client-branch: (WebSocket) open tests; internal queue cap (close); logging;
prappo
parents: 56306
diff changeset
   806
        debug.log(Level.DEBUG, "change state from %s to %s %s",
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   807
                  expectedState, newState, success);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   808
        return success;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   809
    }
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   810
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   811
    /* Exposed for testing purposes */
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   812
    protected Transport transport() {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   813
        return transport;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   814
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   815
}