src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/websocket/Transport.java
author prappo
Fri, 15 Dec 2017 00:47:16 +0300
branchhttp-client-branch
changeset 55988 7f1e0cf933a6
child 55989 76ac25076fdc
permissions -rw-r--r--
http-client-branch: (WebSocket) refactoring for the sake of extra test coverage

/*
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.  Oracle designates this
 * particular file as subject to the "Classpath" exception as provided
 * by Oracle in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

package jdk.incubator.http.internal.websocket;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.concurrent.CompletableFuture;

/*
 * The only parametrization of Transport<T> used is Transport<WebSocket>. The
 * type parameter T was introduced solely to avoid circular dependency between
 * Transport and WebSocket. After all, instances of T are used solely to
 * complete CompletableFutures. Transport doesn't care about the exact type of
 * T.
 *
 * This way the Transport is fully in charge of creating CompletableFutures.
 * On the one hand, Transport may use it to cache/reuse CompletableFutures. On
 * the other hand, the class that uses Transport, may benefit by not converting
 * from CompletableFuture<K> returned from Transport, to CompletableFuture<V>
 * needed by the said class.
 */
public interface Transport<T> {

    CompletableFuture<T> sendText(CharSequence message, boolean isLast);

    CompletableFuture<T> sendBinary(ByteBuffer message, boolean isLast);

    CompletableFuture<T> sendPing(ByteBuffer message);

    CompletableFuture<T> sendPong(ByteBuffer message);

    CompletableFuture<T> sendClose(int statusCode, String reason);

    void request(long n);

    /*
     * Why is this method needed? Since Receiver operates through callbacks
     * this method allows to abstract out what constitutes as a message being
     * received (i.e. to decide outside this type when exactly one should
     * decrement the demand).
     */
    void acknowledgeReception();

    void closeOutput() throws IOException;

    void closeInput() throws IOException;
}