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