src/java.net.http/share/classes/jdk/internal/net/http/websocket/WebSocketImpl.java
author prappo
Mon, 12 Mar 2018 12:47:29 +0000
branchhttp-client-branch
changeset 56293 7e21161251dc
parent 56291 c8c4c707ff3a
child 56294 181bc33917e4
permissions -rw-r--r--
http-client-branch: (WebSocket) debug logging
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;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    36
import java.lang.ref.Reference;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    37
import java.net.ProtocolException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    38
import java.net.URI;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
    39
import java.net.http.WebSocket;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
import java.nio.ByteBuffer;
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
    41
import java.util.Objects;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    42
import java.util.concurrent.CompletableFuture;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    43
import java.util.concurrent.CompletionStage;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    44
import java.util.concurrent.TimeoutException;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    45
import java.util.concurrent.atomic.AtomicBoolean;
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
    46
import java.util.concurrent.atomic.AtomicLong;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    47
import java.util.concurrent.atomic.AtomicReference;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
    48
import java.util.function.BiConsumer;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    49
import java.util.function.Function;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    50
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
import static java.util.Objects.requireNonNull;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    52
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
    53
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
    54
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
    55
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
    56
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
    57
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
    58
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
    59
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
    60
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
    61
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
    62
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
    63
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
    64
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
    65
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    66
/*
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    67
 * A WebSocket client.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    68
 */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    69
public final class WebSocketImpl implements WebSocket {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    70
56291
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
    71
    private final static boolean DEBUG = true;
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
    72
    private final AtomicLong counter = new AtomicLong();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
    73
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    74
    enum State {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    75
        OPEN,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    76
        IDLE,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    77
        WAITING,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    78
        TEXT,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    79
        BINARY,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    80
        PING,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    81
        PONG,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    82
        CLOSE,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    83
        ERROR;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    84
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    85
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
    86
    private final MinimalFuture<WebSocket> DONE = MinimalFuture.completedFuture(this);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    87
    private volatile boolean inputClosed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    88
    private volatile boolean outputClosed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    89
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    90
    private final AtomicReference<State> state = new AtomicReference<>(OPEN);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    91
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    92
    /* Components of calls to Listener's methods */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    93
    private MessagePart part;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    94
    private ByteBuffer binaryData;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    95
    private CharSequence text;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    96
    private int statusCode;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    97
    private String reason;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    98
    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
    99
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   100
    private final URI uri;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   101
    private final String subprotocol;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   102
    private final Listener listener;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   103
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   104
    private final AtomicBoolean outstandingSend = new AtomicBoolean();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   105
    private final Transport transport;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   106
    private final SequentialScheduler receiveScheduler
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   107
            = new SequentialScheduler(new ReceiveTask());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   108
    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
   109
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   110
    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
   111
        Function<Result, WebSocket> newWebSocket = r -> {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   112
            WebSocket ws = newInstance(b.getUri(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   113
                                       r.subprotocol,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   114
                                       b.getListener(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   115
                                       r.transport);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
            // Make sure we don't release the builder until this lambda
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   117
            // has been executed. The builder has a strong reference to
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   118
            // the HttpClientFacade, and we want to keep that live until
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   119
            // after the raw channel is created and passed to WebSocketImpl.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   120
            Reference.reachabilityFence(b);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   121
            return ws;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   122
        };
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   123
        OpeningHandshake h;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   124
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   125
            h = new OpeningHandshake(b);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   126
        } catch (Throwable e) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   127
            return failedFuture(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   128
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   129
        return h.send().thenApply(newWebSocket);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   130
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   131
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   132
    /* Exposed for testing purposes */
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   133
    static WebSocketImpl newInstance(URI uri,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   134
                                     String subprotocol,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   135
                                     Listener listener,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   136
                                     TransportFactory transport) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   137
        WebSocketImpl ws = new WebSocketImpl(uri, subprotocol, listener, transport);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   138
        // This initialisation is outside of the constructor for the sake of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   139
        // safe publication of WebSocketImpl.this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   140
        ws.signalOpen();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   141
        return ws;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   142
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   143
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   144
    private WebSocketImpl(URI uri,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   145
                          String subprotocol,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   146
                          Listener listener,
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   147
                          TransportFactory transportFactory) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   148
        this.uri = requireNonNull(uri);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   149
        this.subprotocol = requireNonNull(subprotocol);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   150
        this.listener = requireNonNull(listener);
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   151
        this.transport = transportFactory.createTransport(
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   152
                new SignallingMessageConsumer());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   153
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   154
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   155
    // FIXME: add to action handling of errors -> signalError()
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   156
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   157
    @Override
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   158
    public CompletableFuture<WebSocket> sendText(CharSequence message,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   159
                                                 boolean isLast) {
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   160
        Objects.requireNonNull(message);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   161
        long id;
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   162
        if (DEBUG) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   163
            id = counter.incrementAndGet();
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   164
            System.out.printf("[WebSocket] %s send text: payload length=%s last=%s%n",
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   165
                              id, message.length(), isLast);
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   166
        }
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   167
        CompletableFuture<WebSocket> result;
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   168
        if (!outstandingSend.compareAndSet(false, true)) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   169
            result = failedFuture(new IllegalStateException("Send pending"));
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   170
        } else {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   171
            result = transport.sendText(message, isLast, this,
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   172
                                        (r, e) -> outstandingSend.set(false));
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   173
        }
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   174
        if (DEBUG) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   175
            System.out.printf("[WebSocket] %s send text: returned %s%n",
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   176
                              id, result);
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   177
        }
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   178
        return replaceNull(result);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   179
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   180
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   181
    @Override
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   182
    public CompletableFuture<WebSocket> sendBinary(ByteBuffer message,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   183
                                                   boolean isLast) {
56045
5c6e3b76d2ad http-client-branch: (WebSocket) more tests
prappo
parents: 56024
diff changeset
   184
        Objects.requireNonNull(message);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   185
        long id;
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   186
        if (DEBUG) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   187
            id = counter.incrementAndGet();
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   188
            System.out.printf("[WebSocket] %s send binary: payload=%s last=%s%n",
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   189
                              id, message, isLast);
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   190
        }
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   191
        CompletableFuture<WebSocket> result;
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   192
        if (!outstandingSend.compareAndSet(false, true)) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   193
            result = failedFuture(new IllegalStateException("Send pending"));
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   194
        } else {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   195
            result = transport.sendBinary(message, isLast, this,
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   196
                                          (r, e) -> outstandingSend.set(false));
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   197
        }
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   198
        if (DEBUG) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   199
            System.out.printf("[WebSocket] %s send binary: returned %s%n",
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   200
                              id, result);
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   201
        }
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   202
        return replaceNull(result);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   203
    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   204
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   205
    private CompletableFuture<WebSocket> replaceNull(
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   206
            CompletableFuture<WebSocket> cf)
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   207
    {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   208
        if (cf == null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   209
            return DONE;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   210
        } else {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   211
            return cf;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   212
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   213
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   214
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   215
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   216
    public CompletableFuture<WebSocket> sendPing(ByteBuffer message) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   217
        Objects.requireNonNull(message);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   218
        long id;
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   219
        if (DEBUG) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   220
            id = counter.incrementAndGet();
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   221
            System.out.printf("[WebSocket] %s send ping: payload=%s%n",
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   222
                              id, message);
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   223
        }
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   224
        CompletableFuture<WebSocket> result = transport.sendPing(message, this,
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   225
                                                                 (r, e) -> { });
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   226
        if (DEBUG) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   227
            System.out.printf("[WebSocket] %s send ping: returned %s%n",
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   228
                              id, result);
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   229
        }
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   230
        return replaceNull(result);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   231
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   232
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   233
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   234
    public CompletableFuture<WebSocket> sendPong(ByteBuffer message) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   235
        Objects.requireNonNull(message);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   236
        long id;
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   237
        if (DEBUG) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   238
            id = counter.incrementAndGet();
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   239
            System.out.printf("[WebSocket] %s send pong: payload=%s%n",
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   240
                              id, message);
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   241
        }
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   242
        CompletableFuture<WebSocket> result = transport.sendPong(message, this,
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   243
                                                                 (r, e) -> { });
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   244
        if (DEBUG) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   245
            System.out.printf("[WebSocket] %s send pong: returned %s%n",
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   246
                              id, result);
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   247
        }
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   248
        return replaceNull(result);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   249
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   250
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   251
    @Override
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   252
    public CompletableFuture<WebSocket> sendClose(int statusCode,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   253
                                                  String reason) {
56058
a02d0098c630 http-client-branch: (WebSocket) example-test
prappo
parents: 56045
diff changeset
   254
        Objects.requireNonNull(reason);
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   255
        long id;
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   256
        if (DEBUG) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   257
            id = counter.incrementAndGet();
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   258
            System.out.printf("[WebSocket] %s send close: statusCode=%s, reason.length=%s%n",
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   259
                              id, statusCode, reason);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   260
        }
56293
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   261
        CompletableFuture<WebSocket> result;
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   262
        if (!isLegalToSendFromClient(statusCode)) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   263
            result = failedFuture(new IllegalArgumentException("statusCode"));
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   264
        } else {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   265
            // check outputClosed
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   266
            result = sendClose0(statusCode, reason);
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   267
        }
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   268
        if (DEBUG) {
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   269
            System.out.printf("[WebSocket] %s send close: returned %s%n",
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   270
                              id, result);
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   271
        }
7e21161251dc http-client-branch: (WebSocket) debug logging
prappo
parents: 56291
diff changeset
   272
        return replaceNull(result);
42460
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
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   275
    /*
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   276
     * Sends a Close message, then shuts down the output since no more
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   277
     * messages are expected to be sent at this point.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   278
     */
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   279
    private CompletableFuture<WebSocket> sendClose0(int statusCode,
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   280
                                                    String reason) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   281
        outputClosed = true;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   282
        BiConsumer<WebSocket, Throwable> closer = (r, e) -> {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   283
            Throwable cause = Utils.getCompletionCause(e);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   284
            if (cause instanceof IllegalArgumentException) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   285
                // or pre=check it (isLegalToSendFromClient(statusCode))
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   286
                return;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   287
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   288
            try {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   289
                transport.closeOutput();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   290
            } catch (IOException ex) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   291
                Log.logError(ex);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   292
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   293
            if (cause instanceof TimeoutException) { // FIXME: it is not the case anymore
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   294
                if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   295
                    System.out.println("[WebSocket] sendClose0 error: " + e);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   296
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   297
                try {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   298
                    transport.closeInput();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   299
                } catch (IOException ex) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   300
                    Log.logError(ex);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   301
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   302
            }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   303
        };
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   304
        CompletableFuture<WebSocket> cf
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   305
                = transport.sendClose(statusCode, reason, this, closer);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   306
        return cf;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   307
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   308
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   309
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   310
    public void request(long n) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   311
        if (DEBUG) {
56291
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   312
            System.out.printf("[WebSocket] request %s%n", n);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   313
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   314
        if (demand.increase(n)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   315
            receiveScheduler.runOrSchedule();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   316
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   317
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   318
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   319
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   320
    public String getSubprotocol() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   321
        return subprotocol;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   322
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   323
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   324
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   325
    public boolean isOutputClosed() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   326
        return outputClosed;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   327
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   328
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   329
    @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   330
    public boolean isInputClosed() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   331
        return inputClosed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   332
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   333
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   334
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   335
    public void abort() {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   336
        if (DEBUG) {
56291
c8c4c707ff3a http-client-branch: (WebSocket) setting DEBUG to true; refactoring;
prappo
parents: 56269
diff changeset
   337
            System.out.printf("[WebSocket] abort %n");
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   338
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   339
        inputClosed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   340
        outputClosed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   341
        receiveScheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   342
        close();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   343
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   344
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   345
    @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   346
    public String toString() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   347
        return super.toString()
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   348
                + "[uri=" + uri
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   349
                + (!subprotocol.isEmpty() ? ", subprotocol=" + subprotocol : "")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   350
                + "]";
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   351
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   352
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   353
    /*
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   354
     * The assumptions about order is as follows:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   355
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   356
     *     - state is never changed more than twice inside the `run` method:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   357
     *       x --(1)--> IDLE --(2)--> y (otherwise we're loosing events, or
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   358
     *       overwriting parts of messages creating a mess since there's no
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   359
     *       queueing)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   360
     *     - OPEN is always the first state
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   361
     *     - no messages are requested/delivered before onOpen is called (this
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   362
     *       is implemented by making WebSocket instance accessible first in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   363
     *       onOpen)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   364
     *     - after the state has been observed as CLOSE/ERROR, the scheduler
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   365
     *       is stopped
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   366
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   367
    private class ReceiveTask extends SequentialScheduler.CompleteRestartableTask {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   368
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   369
        // Transport only asked here and nowhere else because we must make sure
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   370
        // onOpen is invoked first and no messages become pending before onOpen
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   371
        // finishes
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   372
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   373
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   374
        public void run() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   375
            while (true) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   376
                State s = state.get();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   377
                try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   378
                    switch (s) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   379
                        case OPEN:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   380
                            processOpen();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   381
                            tryChangeState(OPEN, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   382
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   383
                        case TEXT:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   384
                            processText();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   385
                            tryChangeState(TEXT, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   386
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   387
                        case BINARY:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   388
                            processBinary();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   389
                            tryChangeState(BINARY, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   390
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   391
                        case PING:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   392
                            processPing();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   393
                            tryChangeState(PING, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   394
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   395
                        case PONG:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   396
                            processPong();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   397
                            tryChangeState(PONG, IDLE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   398
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   399
                        case CLOSE:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   400
                            processClose();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   401
                            return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   402
                        case ERROR:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   403
                            processError();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   404
                            return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   405
                        case IDLE:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   406
                            if (demand.tryDecrement()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   407
                                    && tryChangeState(IDLE, WAITING)) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   408
                                transport.request(1);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   409
                            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   410
                            return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   411
                        case WAITING:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   412
                            // For debugging spurious signalling: when there was a
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   413
                            // signal, but apparently nothing has changed
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   414
                            return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   415
                        default:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   416
                            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
   417
                    }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   418
                } catch (Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   419
                    signalError(t);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   420
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   421
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   422
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   423
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   424
        private void processError() throws IOException {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   425
            if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   426
                System.out.println("[WebSocket] processError");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   427
            }
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   428
            transport.closeInput();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   429
            receiveScheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   430
            Throwable err = error.get();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   431
            if (err instanceof FailWebSocketException) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   432
                int code1 = ((FailWebSocketException) err).getStatusCode();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   433
                err = new ProtocolException().initCause(err);
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   434
                sendClose0(code1, "")
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   435
                        .whenComplete(
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   436
                                (r, e) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   437
                                    if (e != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   438
                                        Log.logError(e);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   439
                                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   440
                                });
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   441
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   442
            listener.onError(WebSocketImpl.this, err);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   443
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   444
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   445
        private void processClose() throws IOException {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   446
            if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   447
                System.out.println("[WebSocket] processClose");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   448
            }
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   449
            transport.closeInput();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   450
            receiveScheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   451
            CompletionStage<?> readyToClose;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   452
            readyToClose = listener.onClose(WebSocketImpl.this, statusCode, reason);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   453
            if (readyToClose == null) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   454
                readyToClose = DONE;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   455
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   456
            int code;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   457
            if (statusCode == NO_STATUS_CODE || statusCode == CLOSED_ABNORMALLY) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   458
                code = NORMAL_CLOSURE;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   459
                if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   460
                    System.out.printf("[WebSocket] using statusCode %s instead of %s%n",
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   461
                                      statusCode, code);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   462
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   463
            } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   464
                code = statusCode;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   465
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   466
            readyToClose.whenComplete((r, e) -> {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   467
                sendClose0(code, "") // FIXME errors from here?
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   468
                        .whenComplete((r1, e1) -> {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   469
                            if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   470
                                if (e1 != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   471
                                    e1.printStackTrace(System.out);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   472
                                }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   473
                            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   474
                        });
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
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   478
        private void processPong() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   479
            listener.onPong(WebSocketImpl.this, binaryData);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   480
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   481
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   482
        private void processPing() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   483
            // Let's make a full copy of this tiny data. What we want here
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   484
            // is to rule out a possibility the shared data we send might be
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   485
            // corrupted by processing in the listener.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   486
            ByteBuffer slice = binaryData.slice();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   487
            ByteBuffer copy = ByteBuffer.allocate(binaryData.remaining())
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   488
                    .put(binaryData)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   489
                    .flip();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   490
            // Non-exclusive send;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   491
            BiConsumer<WebSocketImpl, Throwable> reporter = (r, e) -> {
56269
234813fd33bc http-client-branch: (WebSocket) test fix + output closure
prappo
parents: 56263
diff changeset
   492
                if (e != null) { // Better error handing. What if already closed?
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   493
                    signalError(Utils.getCompletionCause(e));
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   494
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   495
            };
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   496
            transport.sendPong(copy, WebSocketImpl.this, reporter);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   497
            listener.onPing(WebSocketImpl.this, slice);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   498
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   499
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   500
        private void processBinary() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   501
            listener.onBinary(WebSocketImpl.this, binaryData, part);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   502
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   503
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   504
        private void processText() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   505
            listener.onText(WebSocketImpl.this, text, part);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   506
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   507
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   508
        private void processOpen() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   509
            listener.onOpen(WebSocketImpl.this);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   510
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   511
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   512
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   513
    private void signalOpen() {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   514
        if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   515
            System.out.printf("[WebSocket] signalOpen%n");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   516
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   517
        receiveScheduler.runOrSchedule();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   518
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   519
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   520
    private void signalError(Throwable error) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   521
        if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   522
            System.out.printf("[WebSocket] signalError %s%n", error);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   523
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   524
        inputClosed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   525
        outputClosed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   526
        if (!this.error.compareAndSet(null, error) || !trySetState(ERROR)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   527
            Log.logError(error);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   528
        } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   529
            close();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   530
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   531
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   532
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   533
    private void close() {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   534
        if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   535
            System.out.println("[WebSocket] close");
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   536
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   537
        Throwable first = null;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   538
        try {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   539
            transport.closeInput();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   540
        } catch (Throwable t1) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   541
            first = t1;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   542
        } finally {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   543
            Throwable second = null;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   544
            try {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   545
                transport.closeOutput();
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   546
            } catch (Throwable t2) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   547
                second = t2;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   548
            } finally {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   549
                Throwable e = null;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   550
                if (first != null && second != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   551
                    first.addSuppressed(second);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   552
                    e = first;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   553
                } else if (first != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   554
                    e = first;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   555
                } else if (second != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   556
                    e = second;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   557
                }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   558
                if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   559
                    if (e != null) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   560
                        e.printStackTrace(System.out);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   561
                    }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   562
                }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   563
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   564
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   565
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   566
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   567
    private void signalClose(int statusCode, String reason) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   568
        // FIXME: make sure no race reason & close are not intermixed
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   569
        inputClosed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   570
        this.statusCode = statusCode;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   571
        this.reason = reason;
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   572
        boolean managed = trySetState(CLOSE);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   573
        if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   574
            System.out.printf("[WebSocket] signalClose statusCode=%s, reason.length()=%s: %s%n",
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   575
                              statusCode, reason.length(), managed);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   576
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   577
        if (managed) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   578
            try {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   579
                transport.closeInput();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   580
            } catch (Throwable t) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   581
                if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   582
                    t.printStackTrace(System.out);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   583
                }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   584
            }
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
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   588
    private class SignallingMessageConsumer implements MessageStreamConsumer {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   589
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   590
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   591
        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
   592
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   593
            text = data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   594
            WebSocketImpl.this.part = part;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   595
            tryChangeState(WAITING, TEXT);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   596
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   597
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   598
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   599
        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
   600
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   601
            binaryData = data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   602
            WebSocketImpl.this.part = part;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   603
            tryChangeState(WAITING, BINARY);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   604
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   605
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   606
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   607
        public void onPing(ByteBuffer data) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   608
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   609
            binaryData = data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   610
            tryChangeState(WAITING, PING);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   611
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   612
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   613
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   614
        public void onPong(ByteBuffer data) {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   615
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   616
            binaryData = data;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   617
            tryChangeState(WAITING, PONG);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   618
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   619
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   620
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   621
        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
   622
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   623
            signalClose(statusCode, reason.toString());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   624
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   625
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   626
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   627
        public void onComplete() {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   628
            transport.acknowledgeReception();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   629
            signalClose(CLOSED_ABNORMALLY, "");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   630
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   631
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   632
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   633
        public void onError(Throwable error) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   634
            signalError(error);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   635
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   636
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   637
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   638
    private boolean trySetState(State newState) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   639
        State currentState;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   640
        boolean success = false;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   641
        while (true) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   642
            currentState = state.get();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   643
            if (currentState == ERROR || currentState == CLOSE) {
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   644
                break;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   645
            } else if (state.compareAndSet(currentState, newState)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   646
                receiveScheduler.runOrSchedule();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   647
                success = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   648
                break;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   649
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   650
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   651
        if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   652
            System.out.printf("[WebSocket] set state %s (previous %s) %s%n",
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   653
                              newState, currentState, success);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   654
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   655
        return success;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   656
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   657
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   658
    private boolean tryChangeState(State expectedState, State newState) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   659
        State witness = state.compareAndExchange(expectedState, newState);
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   660
        boolean success = false;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   661
        if (witness == expectedState) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   662
            receiveScheduler.runOrSchedule();
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   663
            success = true;
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   664
        } else if (witness != ERROR && witness != CLOSE) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   665
            // This should be the only reason for inability to change the state
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   666
            // from IDLE to WAITING: the state has changed to terminal
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   667
            throw new InternalError();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   668
        }
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   669
        if (DEBUG) {
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   670
            System.out.printf("[WebSocket] change state from %s to %s %s%n",
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   671
                              expectedState, newState, success);
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   672
        }
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   673
        return success;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   674
    }
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   675
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   676
    /* Exposed for testing purposes */
56263
4933a477d628 http-client-branch: (WebSocket) impl change
prappo
parents: 56092
diff changeset
   677
    protected Transport transport() {
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   678
        return transport;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents: 55973
diff changeset
   679
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   680
}