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