src/java.net.http/share/classes/jdk/internal/net/http/websocket/WebSocketImpl.java
author prappo
Wed, 14 Mar 2018 14:42:23 +0000
branchhttp-client-branch
changeset 56306 88c1f551d45e
parent 56304 065641767a75
child 56314 f92e7a8a189f
permissions -rw-r--r--
http-client-branch: (WebSocket) logging typos and leftovers
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;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    37
import java.lang.ref.Reference;
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
    38
import java.lang.System.Logger.Level;
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;
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    43
import java.util.Objects;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    44
import java.util.concurrent.CompletableFuture;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    45
import java.util.concurrent.CompletionStage;
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
    46
import java.util.concurrent.TimeUnit;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    47
import java.util.concurrent.TimeoutException;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    48
import java.util.concurrent.atomic.AtomicBoolean;
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
    49
import java.util.concurrent.atomic.AtomicLong;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    50
import java.util.concurrent.atomic.AtomicReference;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
    51
import java.util.function.BiConsumer;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    52
import java.util.function.Function;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    53
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    54
import static java.util.Objects.requireNonNull;
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
    55
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
    56
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
    57
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
    58
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
    59
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
    60
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
    61
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
    62
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
    63
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
    64
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
    65
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
    66
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
    67
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
    68
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
    69
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    70
/*
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    71
 * A WebSocket client.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    72
 */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    73
public final class WebSocketImpl implements WebSocket {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    74
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
    75
    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
    76
    private static final System.Logger debug =
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
    77
            Utils.getWebSocketLogger("[Websocket]"::toString, DEBUG);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
    78
    private final AtomicLong sendCounter = new AtomicLong();
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
    79
    private final AtomicLong receiveCounter = new AtomicLong();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
    80
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    81
    enum State {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    82
        OPEN,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    83
        IDLE,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    84
        WAITING,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    85
        TEXT,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    86
        BINARY,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    87
        PING,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    88
        PONG,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    89
        CLOSE,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    90
        ERROR;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    91
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    92
56303
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
    93
    private final AtomicReference<ByteBuffer> lastAutomaticPong = new AtomicReference<>();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
    94
    private final MinimalFuture<WebSocket> DONE = MinimalFuture.completedFuture(this);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
    95
    private final long closeTimeout;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    96
    private volatile boolean inputClosed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    97
    private volatile boolean outputClosed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    98
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    99
    private final AtomicReference<State> state = new AtomicReference<>(OPEN);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   100
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   101
    /* Components of calls to Listener's methods */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   102
    private MessagePart part;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   103
    private ByteBuffer binaryData;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   104
    private CharSequence text;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   105
    private int statusCode;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   106
    private String reason;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   107
    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
   108
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   109
    private final URI uri;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   110
    private final String subprotocol;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   111
    private final Listener listener;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   113
    private final AtomicBoolean outstandingSend = new AtomicBoolean();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   114
    private final Transport transport;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   115
    private final SequentialScheduler receiveScheduler
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   116
            = new SequentialScheduler(new ReceiveTask());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   117
    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
   118
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   119
    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
   120
        Function<Result, WebSocket> newWebSocket = r -> {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   121
            WebSocket ws = newInstance(b.getUri(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   122
                                       r.subprotocol,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   123
                                       b.getListener(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   124
                                       r.transport);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   125
            // Make sure we don't release the builder until this lambda
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   126
            // has been executed. The builder has a strong reference to
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   127
            // the HttpClientFacade, and we want to keep that live until
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   128
            // after the raw channel is created and passed to WebSocketImpl.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   129
            Reference.reachabilityFence(b);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   130
            return ws;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   131
        };
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   132
        OpeningHandshake h;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   133
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   134
            h = new OpeningHandshake(b);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   135
        } catch (Throwable e) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   136
            return failedFuture(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   137
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   138
        return h.send().thenApply(newWebSocket);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   139
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   140
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   141
    /* Exposed for testing purposes */
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   142
    static WebSocketImpl newInstance(URI uri,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   143
                                     String subprotocol,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   144
                                     Listener listener,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   145
                                     TransportFactory transport) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   146
        WebSocketImpl ws = new WebSocketImpl(uri, subprotocol, listener, transport);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   147
        // This initialisation is outside of the constructor for the sake of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   148
        // safe publication of WebSocketImpl.this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   149
        ws.signalOpen();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   150
        return ws;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   151
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   152
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   153
    private WebSocketImpl(URI uri,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   154
                          String subprotocol,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   155
                          Listener listener,
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   156
                          TransportFactory transportFactory) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   157
        this.uri = requireNonNull(uri);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   158
        this.subprotocol = requireNonNull(subprotocol);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   159
        this.listener = requireNonNull(listener);
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   160
        this.transport = transportFactory.createTransport(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   161
                new SignallingMessageConsumer());
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   162
        closeTimeout = readCloseTimeout();
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   163
    }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   164
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   165
    private static int readCloseTimeout() {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   166
        String property = "jdk.httpclient.websocket.closeTimeout";
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   167
        int defaultValue = 30;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   168
        String value = Utils.getNetProperty(property);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   169
        int v;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   170
        if (value == null) {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   171
            v = defaultValue;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   172
        } else {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   173
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   174
                v = Integer.parseUnsignedInt(value);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   175
            } catch (NumberFormatException ignored) {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   176
                v = defaultValue;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   177
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   178
        }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   179
        debug.log(Level.DEBUG, "%s=%s, using value %s",
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   180
                              property, value, v);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   181
        return v;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   182
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   183
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   184
    // FIXME: add to action handling of errors -> signalError()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   185
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   186
    @Override
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   187
    public CompletableFuture<WebSocket> sendText(CharSequence message,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   188
                                                 boolean isLast) {
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   189
        Objects.requireNonNull(message);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   190
        long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   191
        if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   192
            id = sendCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   193
            debug.log(Level.DEBUG, "%s send text: payload length=%s last=%s",
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   194
                              id, message.length(), isLast);
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   195
        }
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   196
        CompletableFuture<WebSocket> result;
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   197
        if (!outstandingSend.compareAndSet(false, true)) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   198
            result = failedFuture(new IllegalStateException("Send pending"));
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   199
        } else {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   200
            result = transport.sendText(message, isLast, this,
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   201
                                        (r, e) -> outstandingSend.set(false));
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   202
        }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   203
        debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   204
                  "%s send text: returned %s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   205
                  id, result);
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   206
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   207
        return replaceNull(result);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   208
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   209
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   210
    @Override
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   211
    public CompletableFuture<WebSocket> sendBinary(ByteBuffer message,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   212
                                                   boolean isLast) {
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   213
        Objects.requireNonNull(message);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   214
        long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   215
        if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   216
            id = sendCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   217
            debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   218
                    "%s send binary: payload=%s last=%s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   219
                    id, message, isLast);
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   220
        }
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   221
        CompletableFuture<WebSocket> result;
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   222
        if (!outstandingSend.compareAndSet(false, true)) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   223
            result = failedFuture(new IllegalStateException("Send pending"));
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   224
        } else {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   225
            result = transport.sendBinary(message, isLast, this,
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   226
                                          (r, e) -> outstandingSend.set(false));
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   227
        }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   228
        debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   229
                "%s send binary: returned %s", id, result);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   230
        return replaceNull(result);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   231
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   232
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   233
    private CompletableFuture<WebSocket> replaceNull(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   234
            CompletableFuture<WebSocket> cf)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   235
    {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   236
        if (cf == null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   237
            return DONE;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   238
        } else {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   239
            return cf;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   240
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   241
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   242
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   243
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   244
    public CompletableFuture<WebSocket> sendPing(ByteBuffer message) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   245
        Objects.requireNonNull(message);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   246
        long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   247
        if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   248
            id = sendCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   249
            debug.log(Level.DEBUG, "%s send ping: payload=%s",
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   250
                              id, message);
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   251
        }
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   252
        CompletableFuture<WebSocket> result = transport.sendPing(message, this,
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   253
                                                                 (r, e) -> { });
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   254
        debug.log(Level.DEBUG, "%s send ping: returned %s", id, result);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   255
        return replaceNull(result);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   256
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   257
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   258
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   259
    public CompletableFuture<WebSocket> sendPong(ByteBuffer message) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   260
        Objects.requireNonNull(message);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   261
        long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   262
        if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   263
            id = sendCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   264
            debug.log(Level.DEBUG, "%s send pong: payload=%s",
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   265
                              id, message);
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   266
        }
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   267
        CompletableFuture<WebSocket> result = transport.sendPong(message, this,
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   268
                                                                 (r, e) -> { });
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   269
        debug.log(Level.DEBUG, "%s send pong: returned %s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   270
                          id, result);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   271
        return replaceNull(result);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   272
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   273
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   274
    @Override
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   275
    public CompletableFuture<WebSocket> sendClose(int statusCode,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   276
                                                  String reason) {
56058
a02d0098c630 http-client-branch: (WebSocket) example-test
prappo
parents: 56045
diff changeset
   277
        Objects.requireNonNull(reason);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   278
        long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   279
        if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   280
            id = sendCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   281
            debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   282
                      "%s send close: statusCode=%s, reason.length=%s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   283
                      id, statusCode, reason);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   284
        }
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   285
        CompletableFuture<WebSocket> result;
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   286
        if (!isLegalToSendFromClient(statusCode)) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   287
            result = failedFuture(new IllegalArgumentException("statusCode"));
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   288
        } else {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   289
            // check outputClosed
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   290
            result = sendClose0(statusCode, reason);
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   291
        }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   292
        debug.log(Level.DEBUG, "%s send close: returned %s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   293
                          id, result);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   294
        return replaceNull(result);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   295
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   296
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   297
    private CompletableFuture<WebSocket> sendClose0(int statusCode,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   298
                                                    String reason) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   299
        outputClosed = true;
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   300
        CompletableFuture<WebSocket> cf
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   301
                = transport.sendClose(statusCode, reason, this, (r, e) -> { });
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   302
        CompletableFuture<WebSocket> closeOrTimeout
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   303
                = replaceNull(cf).orTimeout(closeTimeout, TimeUnit.SECONDS);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   304
        // The snippet below, whose purpose might not be immediately obvious,
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   305
        // is a trick used to complete a dependant stage with an IOException.
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   306
        // A checked IOException cannot be thrown from inside the BiConsumer
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   307
        // supplied to the handle method. Instead a CompletionStage completed
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   308
        // exceptionally with this IOException is returned.
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   309
        return closeOrTimeout.handle(this::processCloseOutcome)
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   310
                             .thenCompose(Function.identity());
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   311
    }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   312
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   313
    private CompletionStage<WebSocket> processCloseOutcome(WebSocket webSocket,
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   314
                                                           Throwable e) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   315
        if (e == null) {
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   316
            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
   317
        } else {
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   318
            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
   319
        }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   320
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   321
        if (e == null) {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   322
            return completedFuture(webSocket);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   323
        }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   324
        Throwable cause = Utils.getCompletionCause(e);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   325
        if (cause instanceof IllegalArgumentException) {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   326
            return failedFuture(cause);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   327
        }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   328
        try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   329
            transport.closeOutput();
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   330
        } catch (IOException ignored) { }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   331
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   332
        if (cause instanceof TimeoutException) {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   333
            inputClosed = true;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   334
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   335
                transport.closeInput();
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   336
            } catch (IOException ignored) { }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   337
            return failedFuture(new InterruptedIOException(
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   338
                    "Could not send close within a reasonable timeout"));
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   339
        }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   340
        return failedFuture(cause);
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() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   358
        return outputClosed;
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;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   370
        outputClosed = true;
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:
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   407
            while (true) {
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);
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   410
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   411
                try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   412
                    switch (s) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   413
                        case OPEN:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   414
                            processOpen();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   415
                            tryChangeState(OPEN, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   416
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   417
                        case TEXT:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   418
                            processText();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   419
                            tryChangeState(TEXT, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   420
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   421
                        case BINARY:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   422
                            processBinary();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   423
                            tryChangeState(BINARY, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   424
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   425
                        case PING:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   426
                            processPing();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   427
                            tryChangeState(PING, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   428
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   429
                        case PONG:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   430
                            processPong();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   431
                            tryChangeState(PONG, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   432
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   433
                        case CLOSE:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   434
                            processClose();
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   435
                            break loop;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   436
                        case ERROR:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   437
                            processError();
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   438
                            break loop;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   439
                        case IDLE:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   440
                            if (demand.tryDecrement()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   441
                                    && tryChangeState(IDLE, WAITING)) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   442
                                transport.request(1);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   443
                            }
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   444
                            break loop;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   445
                        case WAITING:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   446
                            // For debugging spurious signalling: when there was a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   447
                            // signal, but apparently nothing has changed
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   448
                            break loop;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   449
                        default:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   450
                            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
   451
                    }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   452
                } catch (Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   453
                    signalError(t);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   454
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   455
            }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   456
            debug.log(Level.DEBUG, "exit receive task");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   457
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   458
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   459
        private void processError() throws IOException {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   460
            debug.log(Level.DEBUG, "processError");
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   461
            transport.closeInput();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   462
            receiveScheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   463
            Throwable err = error.get();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   464
            if (err instanceof FailWebSocketException) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   465
                int code1 = ((FailWebSocketException) err).getStatusCode();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   466
                err = new ProtocolException().initCause(err);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   467
                debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   468
                          "failing %s with error=%s statusCode=%s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   469
                          WebSocketImpl.this, err, code1);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   470
                sendClose0(code1, "") // TODO handle errors from here
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   471
                        .whenComplete(
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   472
                                (r, e) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   473
                                    if (e != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   474
                                        Log.logError(e);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   475
                                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   476
                                });
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   477
            }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   478
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   479
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   480
                id = receiveCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   481
                debug.log(Level.DEBUG, "enter onError %s error=%s",
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   482
                                  id, err);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   483
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   484
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   485
                listener.onError(WebSocketImpl.this, err);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   486
            } finally {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   487
                debug.log(Level.DEBUG, "exit onError %s", id);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   488
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   489
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   490
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   491
        private void processClose() throws IOException {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   492
            debug.log(Level.DEBUG, "processClose");
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   493
            transport.closeInput();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   494
            receiveScheduler.stop();
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   495
            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
   496
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   497
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   498
                id = receiveCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   499
                debug.log(Level.DEBUG, "enter onClose %s statusCode=%s reason.length=%s",
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   500
                                  id, statusCode, reason.length());
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   501
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   502
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   503
                cs = listener.onClose(WebSocketImpl.this, statusCode, reason);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   504
            } finally {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   505
                debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   506
                          "exit onClose %s returned %s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   507
                           id, cs);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   508
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   509
            if (cs == null) {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   510
                cs = DONE;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   511
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   512
            int code;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   513
            if (statusCode == NO_STATUS_CODE || statusCode == CLOSED_ABNORMALLY) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   514
                code = NORMAL_CLOSURE;
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   515
                debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   516
                          "using statusCode %s instead of %s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   517
                           statusCode, code);
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   518
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   519
            } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   520
                code = statusCode;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   521
            }
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   522
            cs.whenComplete((r, e) -> { // TODO log
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   523
                sendClose0(code, "") // TODO handle errors from here
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   524
                        .whenComplete((r1, e1) -> {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   525
                                if (e1 != null) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   526
                                    debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   527
                                              "processClose completed with errors",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   528
                                               e1);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   529
                                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   530
                        });
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   531
            });
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 processPong() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   535
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   536
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   537
                id = receiveCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   538
                debug.log(Level.DEBUG, "enter onPong %s payload=%s",
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   539
                                  id, binaryData);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   540
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   541
            CompletionStage<?> cs = null;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   542
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   543
                cs = listener.onPong(WebSocketImpl.this, binaryData);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   544
            } finally {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   545
                debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   546
                          "exit onPong %s returned %s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   547
                          id, cs);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   548
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   549
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   550
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   551
        private void processPing() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   552
            debug.log(Level.DEBUG, "processPing");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   553
            ByteBuffer slice = binaryData.slice();
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   554
            // 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
   555
            // replying Pong could be done in parallel with the listener
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   556
            // handling this Ping.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   557
            ByteBuffer copy = ByteBuffer.allocate(binaryData.remaining())
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   558
                    .put(binaryData)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   559
                    .flip();
56303
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   560
            if (!trySwapAutomaticPong(copy)) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   561
                // Non-exclusive send;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   562
                BiConsumer<WebSocketImpl, Throwable> reporter = (r, e) -> {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   563
                    if (e != null) { // TODO: better error handing. What if already closed?
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   564
                        signalError(Utils.getCompletionCause(e));
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   565
                    }
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   566
                };
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   567
                transport.sendPong(WebSocketImpl.this::clearAutomaticPong,
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   568
                                   WebSocketImpl.this,
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   569
                                   reporter);
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   570
            }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   571
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   572
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   573
                id = receiveCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   574
                debug.log(Level.DEBUG, "enter onPing %s payload=%s",
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   575
                                  id, slice);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   576
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   577
            CompletionStage<?> cs = null;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   578
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   579
                cs = listener.onPing(WebSocketImpl.this, slice);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   580
            } finally {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   581
                debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   582
                          "exit onPing %s returned %s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   583
                          id, cs);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   584
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   585
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   586
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   587
        private void processBinary() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   588
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   589
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   590
                id = receiveCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   591
                debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   592
                          "enter onBinary %s payload=%s part=%s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   593
                          id, binaryData, part);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   594
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   595
            CompletionStage<?> cs = null;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   596
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   597
                cs = listener.onBinary(WebSocketImpl.this, binaryData, part);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   598
            } finally {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   599
                debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   600
                          "exit onBinary %s returned %s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   601
                          id, cs);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   602
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   603
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   604
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   605
        private void processText() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   606
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   607
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   608
                id = receiveCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   609
                debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   610
                          "enter onText %s payload.length=%s part=%s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   611
                          id, text.length(), part);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   612
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   613
            CompletionStage<?> cs = null;
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   614
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   615
                cs = listener.onText(WebSocketImpl.this, text, part);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   616
            } finally {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   617
                debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   618
                          "exit onText %s returned %s", id, cs);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   619
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   620
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   621
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   622
        private void processOpen() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   623
            long id = 0;
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   624
            if (debug.isLoggable(Level.DEBUG)) {
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   625
                id = receiveCounter.incrementAndGet();
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   626
                debug.log(Level.DEBUG, "enter onOpen %s", id);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   627
            }
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   628
            try {
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   629
                listener.onOpen(WebSocketImpl.this);
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   630
            } finally {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   631
                debug.log(Level.DEBUG, "exit onOpen %s", id);
56294
181bc33917e4 http-client-branch: (WebSocket) logging and closure timer
prappo
parents: 56293
diff changeset
   632
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   633
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   634
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   635
56303
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   636
    private ByteBuffer clearAutomaticPong() {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   637
        ByteBuffer data;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   638
        do {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   639
            data = lastAutomaticPong.get();
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   640
            if (data == null) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   641
                // 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
   642
                // using it has been added previously
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   643
                throw new InternalError();
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   644
            }
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   645
        } while (!lastAutomaticPong.compareAndSet(data, null));
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   646
        return data;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   647
    }
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   648
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   649
    private boolean trySwapAutomaticPong(ByteBuffer copy) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   650
        ByteBuffer message;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   651
        boolean swapped;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   652
        while (true) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   653
            message = lastAutomaticPong.get();
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   654
            if (message == null) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   655
                if (!lastAutomaticPong.compareAndSet(null, copy)) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   656
                    // 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
   657
                    // 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
   658
                    // time. Thus no failure in the atomic operation above is
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   659
                    // expected.
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   660
                    throw new InternalError();
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   661
                }
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   662
                swapped = false;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   663
                break;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   664
            } else if (lastAutomaticPong.compareAndSet(message, copy)) {
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   665
                swapped = true;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   666
                break;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   667
            }
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   668
        }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   669
        debug.log(Level.DEBUG, "swapped automatic pong from %s to %s%n",
56303
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   670
                              message, copy);
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   671
        return swapped;
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   672
    }
a82058c084ef http-client-branch: (WebSocket) swapping automatic pong replies
prappo
parents: 56297
diff changeset
   673
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   674
    private void signalOpen() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   675
        debug.log(Level.DEBUG, "signalOpen");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   676
        receiveScheduler.runOrSchedule();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   677
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   678
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   679
    private void signalError(Throwable error) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   680
        debug.log(Level.DEBUG, "signalError %s", error);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   681
        inputClosed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   682
        outputClosed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   683
        if (!this.error.compareAndSet(null, error) || !trySetState(ERROR)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   684
            Log.logError(error);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   685
        } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   686
            close();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   687
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   688
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   689
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   690
    private void close() {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   691
        debug.log(Level.DEBUG, "close");
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   692
        Throwable first = null;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   693
        try {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   694
            transport.closeInput();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   695
        } catch (Throwable t1) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   696
            first = t1;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   697
        } finally {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   698
            Throwable second = null;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   699
            try {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   700
                transport.closeOutput();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   701
            } catch (Throwable t2) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   702
                second = t2;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   703
            } finally {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   704
                Throwable e = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   705
                if (first != null && second != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   706
                    first.addSuppressed(second);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   707
                    e = first;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   708
                } else if (first != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   709
                    e = first;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   710
                } else if (second != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   711
                    e = second;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   712
                }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   713
                if (e != null) {
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   714
                    debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   715
                              "unexpected exception in close: ",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   716
                              e);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   717
                }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   718
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   719
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   720
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   721
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   722
    private void signalClose(int statusCode, String reason) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   723
        // FIXME: make sure no race reason & close are not intermixed
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   724
        inputClosed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   725
        this.statusCode = statusCode;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   726
        this.reason = reason;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   727
        boolean managed = trySetState(CLOSE);
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   728
        debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   729
                  "signalClose statusCode=%s, reason.length()=%s: %s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   730
                  statusCode, reason.length(), managed);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   731
        if (managed) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   732
            try {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   733
                transport.closeInput();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   734
            } catch (Throwable t) {
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   735
                debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   736
                          "unexpected exception closing input", t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   737
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   738
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   739
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   740
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   741
    private class SignallingMessageConsumer implements MessageStreamConsumer {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   742
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   743
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   744
        public void onText(CharSequence data, MessagePart part) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   745
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   746
            text = data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   747
            WebSocketImpl.this.part = part;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   748
            tryChangeState(WAITING, TEXT);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   749
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   750
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   751
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   752
        public void onBinary(ByteBuffer data, MessagePart part) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   753
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   754
            binaryData = data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   755
            WebSocketImpl.this.part = part;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   756
            tryChangeState(WAITING, BINARY);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   757
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   758
48083
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 onPing(ByteBuffer data) {
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
            binaryData = data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   763
            tryChangeState(WAITING, PING);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   764
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   765
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   766
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   767
        public void onPong(ByteBuffer data) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   768
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   769
            binaryData = data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   770
            tryChangeState(WAITING, PONG);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   771
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   772
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   773
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   774
        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
   775
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   776
            signalClose(statusCode, reason.toString());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   777
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   778
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   779
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   780
        public void onComplete() {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   781
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   782
            signalClose(CLOSED_ABNORMALLY, "");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   783
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   784
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   785
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   786
        public void onError(Throwable error) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   787
            signalError(error);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   788
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   789
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   790
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   791
    private boolean trySetState(State newState) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   792
        State currentState;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   793
        boolean success = false;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   794
        while (true) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   795
            currentState = state.get();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   796
            if (currentState == ERROR || currentState == CLOSE) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   797
                break;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   798
            } else if (state.compareAndSet(currentState, newState)) {
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
                break;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   802
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   803
        }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   804
        debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   805
                  "set state %s (previous %s) %s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   806
                  newState, currentState, success);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   807
        return success;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   808
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   809
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   810
    private boolean tryChangeState(State expectedState, State newState) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   811
        State witness = state.compareAndExchange(expectedState, newState);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   812
        boolean success = false;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   813
        if (witness == expectedState) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   814
            receiveScheduler.runOrSchedule();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   815
            success = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   816
        } else if (witness != ERROR && witness != CLOSE) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   817
            // This should be the only reason for inability to change the state
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   818
            // from IDLE to WAITING: the state has changed to terminal
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   819
            throw new InternalError();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   820
        }
56304
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   821
        debug.log(Level.DEBUG,
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   822
                  "change state from %s to %s %s",
065641767a75 http-client-branch: change websocket to use System.Logger for debug logging
dfuchs
parents: 56303
diff changeset
   823
                  expectedState, newState, success);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   824
        return success;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   825
    }
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   826
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   827
    /* Exposed for testing purposes */
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   828
    protected Transport transport() {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   829
        return transport;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   830
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   831
}