src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/websocket/Transport.java
author prappo
Fri, 15 Dec 2017 03:05:16 +0300
branchhttp-client-branch
changeset 55989 76ac25076fdc
parent 55988 7f1e0cf933a6
child 56088 38fac6d0521d
permissions -rw-r--r--
http-client-branch: (WebSocket) refactoring
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     1
/*
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     4
 *
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    10
 *
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    15
 * accompanied this code).
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    16
 *
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    20
 *
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    23
 * questions.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    24
 */
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    25
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    26
package jdk.incubator.http.internal.websocket;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    27
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    28
import java.io.IOException;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    29
import java.nio.ByteBuffer;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    30
import java.util.concurrent.CompletableFuture;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    31
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    32
/*
55989
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    33
 * Transport needs some way to asynchronously notify the send operation has been
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    34
 * completed. It can have several different designs each of which has its own
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    35
 * pros and cons:
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    36
 *
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    37
 *     (1) void sendMessage(..., Callback)
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    38
 *     (2) CompletableFuture<T> sendMessage(...)
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    39
 *     (3) CompletableFuture<T> sendMessage(..., Callback)
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    40
 *     (4) boolean sendMessage(..., Callback) throws IOException
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    41
 *     ...
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    42
 *
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    43
 * If Transport's users use CFs, (1) forces these users to create CFs and pass
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    44
 * them to the callback. If any additional (dependant) action needs to be
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    45
 * attached to the returned CF, this means an extra object (CF) must be created
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    46
 * in (2). (3) and (4) solves both issues, however (4) does not abstract out
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    47
 * when exactly the operation has been performed. So the handling code needs to
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    48
 * be repeated twice. And that leads to 2 different code paths (more bugs).
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    49
 * Unless designed for this, the user should not assume any specific order of
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    50
 * completion in (3) (e.g. callback first and then the returned CF).
76ac25076fdc http-client-branch: (WebSocket) refactoring
prappo
parents: 55988
diff changeset
    51
 *
55988
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    52
 * The only parametrization of Transport<T> used is Transport<WebSocket>. The
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    53
 * type parameter T was introduced solely to avoid circular dependency between
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    54
 * Transport and WebSocket. After all, instances of T are used solely to
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    55
 * complete CompletableFutures. Transport doesn't care about the exact type of
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    56
 * T.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    57
 *
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    58
 * This way the Transport is fully in charge of creating CompletableFutures.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    59
 * On the one hand, Transport may use it to cache/reuse CompletableFutures. On
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    60
 * the other hand, the class that uses Transport, may benefit by not converting
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    61
 * from CompletableFuture<K> returned from Transport, to CompletableFuture<V>
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    62
 * needed by the said class.
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    63
 */
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    64
public interface Transport<T> {
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    65
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    66
    CompletableFuture<T> sendText(CharSequence message, boolean isLast);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    67
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    68
    CompletableFuture<T> sendBinary(ByteBuffer message, boolean isLast);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    69
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    70
    CompletableFuture<T> sendPing(ByteBuffer message);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    71
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    72
    CompletableFuture<T> sendPong(ByteBuffer message);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    73
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    74
    CompletableFuture<T> sendClose(int statusCode, String reason);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    75
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    76
    void request(long n);
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    77
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    78
    /*
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    79
     * Why is this method needed? Since Receiver operates through callbacks
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    80
     * this method allows to abstract out what constitutes as a message being
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    81
     * received (i.e. to decide outside this type when exactly one should
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    82
     * decrement the demand).
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    83
     */
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    84
    void acknowledgeReception();
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    85
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    86
    void closeOutput() throws IOException;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    87
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    88
    void closeInput() throws IOException;
7f1e0cf933a6 http-client-branch: (WebSocket) refactoring for the sake of extra test coverage
prappo
parents:
diff changeset
    89
}