src/java.net.http/share/classes/jdk/internal/net/http/websocket/Transport.java
author chegar
Wed, 02 May 2018 02:36:17 -0700
changeset 49944 4690a2871b44
parent 49765 ee6f7a61f3a5
child 56451 9585061fdb04
permissions -rw-r--r--
8202423: Small HTTP Client refresh Reviewed-by: chegar, dfuchs, michaelm, prappo Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     2
 * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    26
package jdk.internal.net.http.websocket;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
import java.io.IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
import java.nio.ByteBuffer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
import java.util.concurrent.CompletableFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
import java.util.function.BiConsumer;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
import java.util.function.Supplier;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
/*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
 * A WebSocket view of the underlying communication channel. This view provides
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
 * an asynchronous exchange of WebSocket messages rather than asynchronous
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
 * exchange of bytes.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
 * Methods sendText, sendBinary, sendPing, sendPong and sendClose initiate a
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
 * corresponding operation and return a CompletableFuture (CF) which will
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
 * complete once the operation has completed (succeeded or failed).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
 * These methods are designed such that their clients may take an advantage on
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
 * possible implementation optimizations. Namely, these methods:
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
 * 1. May return null which is considered the same as a CF completed normally
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
 * 2. Accept an arbitrary attachment to complete a CF with
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
 * 3. Accept an action to take once the operation has completed
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    50
 * All of the above allows not to create unnecessary instances of CF.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    51
 * For example, if a message has been sent straight away, there's no need to
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    52
 * create a CF (given the parties agree on the meaning of null and are prepared
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    53
 * to handle it).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    54
 * If the result of a returned CF is useless to the client, they may specify the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
 * exact instance (attachment) they want the CF to complete with. Thus, no need
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
 * to create transforming stages (e.g. thenApply(useless -> myResult)).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
 * If there is the same action that needs to be done each time the CF completes,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
 * the client may pass it directly to the method instead of creating a dependant
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
 * stage (e.g. whenComplete(action)).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
public interface Transport {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    62
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    63
    <T> CompletableFuture<T> sendText(CharSequence message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    64
                                      boolean isLast,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    65
                                      T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    66
                                      BiConsumer<? super T, ? super Throwable> action);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    67
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    68
    <T> CompletableFuture<T> sendBinary(ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    69
                                        boolean isLast,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
                                        T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
                                        BiConsumer<? super T, ? super Throwable> action);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
    <T> CompletableFuture<T> sendPing(ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
                                      T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
                                      BiConsumer<? super T, ? super Throwable> action);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
    <T> CompletableFuture<T> sendPong(ByteBuffer message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
                                      T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
                                      BiConsumer<? super T, ? super Throwable> action);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
    /*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
     * Sends a Pong message with initially unknown data. Used for sending the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
     * most recent automatic Pong reply.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
    <T> CompletableFuture<T> sendPong(Supplier<? extends ByteBuffer> message,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
                                      T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
                                      BiConsumer<? super T, ? super Throwable> action);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
    <T> CompletableFuture<T> sendClose(int statusCode,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
                                       String reason,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
                                       T attachment,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
                                       BiConsumer<? super T, ? super Throwable> action);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
    void request(long n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
    /*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
     * Why is this method needed? Since receiving of messages operates through
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
     * callbacks this method allows to abstract out what constitutes as a
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
     * message being received (i.e. to decide outside this type when exactly one
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
     * should decrement the demand).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
    void acknowledgeReception(); // TODO: hide
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
    /*
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
     * If this method is invoked, then all pending and subsequent send
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
     * operations will fail with IOException.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
    void closeOutput() throws IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
    void closeInput() throws IOException;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
}