jdk/src/java.httpclient/share/classes/java/net/http/AsyncConnection.java
author michaelm
Sat, 30 Apr 2016 00:30:31 +0100
changeset 37720 45cd7cc65382
child 39730 196f4e25d9f5
permissions -rw-r--r--
8087124: HTTP/2 implementation Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     1
/*
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     4
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    10
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    15
 * accompanied this code).
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    16
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    20
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    23
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    24
package java.net.http;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    25
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    26
import java.nio.ByteBuffer;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    27
import java.util.function.Consumer;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    28
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    29
/**
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    30
 * Implemented by classes that offer an asynchronous interface.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    31
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    32
 * PlainHttpConnection, AsyncSSLConnection AsyncSSLDelegate.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    33
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    34
 * setAsyncCallbacks() is called to set the callback for reading
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    35
 * and error notification. Reads all happen on the selector thread, which
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    36
 * must not block.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    37
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    38
 * Writing uses the same write() methods as used in blocking mode.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    39
 * Queues are employed on the writing side to buffer data while it is waiting
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    40
 * to be sent. This strategy relies on HTTP/2 protocol flow control to stop
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    41
 * outgoing queue from continually growing. Writes can be initiated by the
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    42
 * calling thread, but if socket becomes full then the queue is emptied by
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    43
 * the selector thread
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    44
 *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    45
 */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    46
interface AsyncConnection {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    47
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    48
    /**
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    49
     * Enables asynchronous sending and receiving mode. The given async
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    50
     * receiver will receive all incoming data. asyncInput() will be called
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    51
     * to trigger reads. asyncOutput() will be called to drive writes.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    52
     *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    53
     * The errorReceiver callback must be called when any fatal exception
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    54
     * occurs. Connection is assumed to be closed afterwards.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    55
     *
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    56
     * @param asyncReceiver
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    57
     * @param errorReceiver
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    58
     */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    59
    void setAsyncCallbacks(
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    60
            Consumer<ByteBuffer> asyncReceiver,
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    61
            Consumer<Throwable> errorReceiver);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    62
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    63
    /**
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    64
     * Does whatever is required to start reading. Usually registers
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    65
     * an event with the selector thread.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    66
     */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    67
    void startReading();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents:
diff changeset
    68
}