author | dfuchs |
Mon, 23 Apr 2018 15:45:40 +0100 | |
branch | http-client-branch |
changeset 56474 | fe2bf7b369b8 |
parent 56451 | 9585061fdb04 |
child 56507 | 2294c51eae30 |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
49765 | 2 |
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved. |
48083 | 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 |
||
49765 | 26 |
package jdk.internal.net.http.common; |
48083 | 27 |
|
28 |
import java.nio.ByteBuffer; |
|
29 |
import java.util.List; |
|
30 |
import java.util.concurrent.Flow; |
|
31 |
||
32 |
/** |
|
33 |
* FlowTube is an I/O abstraction that allows reading from and writing to a |
|
34 |
* destination asynchronously. |
|
35 |
* This is not a {@link Flow.Processor |
|
36 |
* Flow.Processor<List<ByteBuffer>, List<ByteBuffer>>}, |
|
37 |
* but rather models a publisher source and a subscriber sink in a bidirectional flow. |
|
38 |
* <p> |
|
39 |
* The {@code connectFlows} method should be called to connect the bidirectional |
|
40 |
* flow. A FlowTube supports handing over the same read subscription to different |
|
41 |
* sequential read subscribers over time. When {@code connectFlows(writePublisher, |
|
42 |
* readSubscriber} is called, the FlowTube will call {@code dropSubscription} on |
|
43 |
* its former readSubscriber, and {@code onSubscribe} on its new readSubscriber. |
|
44 |
*/ |
|
45 |
public interface FlowTube extends |
|
46 |
Flow.Publisher<List<ByteBuffer>>, |
|
47 |
Flow.Subscriber<List<ByteBuffer>> { |
|
48 |
||
49 |
/** |
|
50 |
* A subscriber for reading from the bidirectional flow. |
|
51 |
* A TubeSubscriber is a {@code Flow.Subscriber} that can be canceled |
|
52 |
* by calling {@code dropSubscription()}. |
|
53 |
* Once {@code dropSubscription()} is called, the {@code TubeSubscriber} |
|
54 |
* should stop calling any method on its subscription. |
|
55 |
*/ |
|
56 |
static interface TubeSubscriber extends Flow.Subscriber<List<ByteBuffer>> { |
|
57 |
||
58 |
/** |
|
59 |
* Called when the flow is connected again, and the subscription |
|
60 |
* is handed over to a new subscriber. |
|
61 |
* Once {@code dropSubscription()} is called, the {@code TubeSubscriber} |
|
62 |
* should stop calling any method on its subscription. |
|
63 |
*/ |
|
64 |
default void dropSubscription() { } |
|
65 |
||
56474
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56451
diff
changeset
|
66 |
default boolean supportsRecycling() { return false; } |
fe2bf7b369b8
http-client-branch: use direct buffer pool for reading off SSL encrypted buffers from the socket + minor test fixes.
dfuchs
parents:
56451
diff
changeset
|
67 |
|
48083 | 68 |
} |
69 |
||
70 |
/** |
|
71 |
* A publisher for writing to the bidirectional flow. |
|
72 |
*/ |
|
73 |
static interface TubePublisher extends Flow.Publisher<List<ByteBuffer>> { |
|
74 |
||
75 |
} |
|
76 |
||
77 |
/** |
|
78 |
* Connects the bidirectional flows to a write {@code Publisher} and a |
|
79 |
* read {@code Subscriber}. This method can be called sequentially |
|
80 |
* several times to switch existing publishers and subscribers to a new |
|
81 |
* pair of write subscriber and read publisher. |
|
82 |
* @param writePublisher A new publisher for writing to the bidirectional flow. |
|
83 |
* @param readSubscriber A new subscriber for reading from the bidirectional |
|
84 |
* flow. |
|
85 |
*/ |
|
86 |
default void connectFlows(TubePublisher writePublisher, |
|
87 |
TubeSubscriber readSubscriber) { |
|
88 |
||
89 |
this.subscribe(readSubscriber); |
|
90 |
writePublisher.subscribe(this); |
|
91 |
} |
|
92 |
||
93 |
/** |
|
94 |
* Returns true if this flow was completed, either exceptionally |
|
95 |
* or normally (EOF reached). |
|
96 |
* @return true if the flow is finished |
|
97 |
*/ |
|
98 |
boolean isFinished(); |
|
99 |
||
100 |
||
101 |
/** |
|
102 |
* Returns {@code s} if {@code s} is a {@code TubeSubscriber}, otherwise |
|
103 |
* wraps it in a {@code TubeSubscriber}. |
|
104 |
* Using the wrapper is only appropriate in the case where |
|
105 |
* {@code dropSubscription} doesn't need to be implemented, and the |
|
106 |
* {@code TubeSubscriber} is subscribed only once. |
|
107 |
* |
|
108 |
* @param s a subscriber for reading. |
|
109 |
* @return a {@code TubeSubscriber}: either {@code s} if {@code s} is a |
|
110 |
* {@code TubeSubscriber}, otherwise a {@code TubeSubscriber} |
|
111 |
* wrapper that delegates to {@code s} |
|
112 |
*/ |
|
113 |
static TubeSubscriber asTubeSubscriber(Flow.Subscriber<? super List<ByteBuffer>> s) { |
|
114 |
if (s instanceof TubeSubscriber) { |
|
115 |
return (TubeSubscriber) s; |
|
116 |
} |
|
117 |
return new AbstractTubeSubscriber.TubeSubscriberWrapper(s); |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Returns {@code s} if {@code s} is a {@code TubePublisher}, otherwise |
|
122 |
* wraps it in a {@code TubePublisher}. |
|
123 |
* |
|
124 |
* @param p a publisher for writing. |
|
125 |
* @return a {@code TubePublisher}: either {@code s} if {@code s} is a |
|
126 |
* {@code TubePublisher}, otherwise a {@code TubePublisher} |
|
127 |
* wrapper that delegates to {@code s} |
|
128 |
*/ |
|
129 |
static TubePublisher asTubePublisher(Flow.Publisher<List<ByteBuffer>> p) { |
|
130 |
if (p instanceof TubePublisher) { |
|
131 |
return (TubePublisher) p; |
|
132 |
} |
|
133 |
return new AbstractTubePublisher.TubePublisherWrapper(p); |
|
134 |
} |
|
135 |
||
136 |
/** |
|
137 |
* Convenience abstract class for {@code TubePublisher} implementations. |
|
138 |
* It is not required that a {@code TubePublisher} implementation extends |
|
139 |
* this class. |
|
140 |
*/ |
|
141 |
static abstract class AbstractTubePublisher implements TubePublisher { |
|
142 |
static final class TubePublisherWrapper extends AbstractTubePublisher { |
|
143 |
final Flow.Publisher<List<ByteBuffer>> delegate; |
|
144 |
public TubePublisherWrapper(Flow.Publisher<List<ByteBuffer>> delegate) { |
|
145 |
this.delegate = delegate; |
|
146 |
} |
|
147 |
@Override |
|
148 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> subscriber) { |
|
149 |
delegate.subscribe(subscriber); |
|
150 |
} |
|
151 |
} |
|
152 |
} |
|
153 |
||
154 |
/** |
|
155 |
* Convenience abstract class for {@code TubeSubscriber} implementations. |
|
156 |
* It is not required that a {@code TubeSubscriber} implementation extends |
|
157 |
* this class. |
|
158 |
*/ |
|
159 |
static abstract class AbstractTubeSubscriber implements TubeSubscriber { |
|
160 |
static final class TubeSubscriberWrapper extends AbstractTubeSubscriber { |
|
161 |
final Flow.Subscriber<? super List<ByteBuffer>> delegate; |
|
162 |
TubeSubscriberWrapper(Flow.Subscriber<? super List<ByteBuffer>> delegate) { |
|
163 |
this.delegate = delegate; |
|
164 |
} |
|
165 |
@Override |
|
166 |
public void dropSubscription() {} |
|
167 |
@Override |
|
168 |
public void onSubscribe(Flow.Subscription subscription) { |
|
169 |
delegate.onSubscribe(subscription); |
|
170 |
} |
|
171 |
@Override |
|
172 |
public void onNext(List<ByteBuffer> item) { |
|
173 |
delegate.onNext(item); |
|
174 |
} |
|
175 |
@Override |
|
176 |
public void onError(Throwable throwable) { |
|
177 |
delegate.onError(throwable); |
|
178 |
} |
|
179 |
@Override |
|
180 |
public void onComplete() { |
|
181 |
delegate.onComplete(); |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
} |
|
186 |
||
187 |
} |