20 * or visit www.oracle.com if you need additional information or have any |
20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. |
21 * questions. |
22 */ |
22 */ |
23 |
23 |
24 import java.net.http.WebSocket; |
24 import java.net.http.WebSocket; |
25 import java.net.http.WebSocket.MessagePart; |
|
26 import java.nio.ByteBuffer; |
25 import java.nio.ByteBuffer; |
27 import java.util.ArrayList; |
26 import java.util.ArrayList; |
28 import java.util.List; |
27 import java.util.List; |
29 import java.util.Objects; |
28 import java.util.Objects; |
30 import java.util.concurrent.CompletableFuture; |
29 import java.util.concurrent.CompletableFuture; |
79 } |
78 } |
80 |
79 |
81 @Override |
80 @Override |
82 public CompletionStage<?> onText(WebSocket webSocket, |
81 public CompletionStage<?> onText(WebSocket webSocket, |
83 CharSequence message, |
82 CharSequence message, |
84 MessagePart part) { |
83 boolean last) { |
85 System.out.printf("onText(%s, %s, %s)%n", webSocket, message, part); |
84 System.out.printf("onText(%s, message.length=%s, %s)%n", webSocket, message.length(), last); |
86 OnText inv = new OnText(webSocket, message.toString(), part); |
85 OnText inv = new OnText(webSocket, message.toString(), last); |
87 invocations.add(inv); |
86 invocations.add(inv); |
88 if (collectUntil.test(inv)) { |
87 if (collectUntil.test(inv)) { |
89 lastCall.complete(null); |
88 lastCall.complete(null); |
90 } |
89 } |
91 return onText0(webSocket, message, part); |
90 return onText0(webSocket, message, last); |
92 } |
91 } |
93 |
92 |
94 protected CompletionStage<?> onText0(WebSocket webSocket, |
93 protected CompletionStage<?> onText0(WebSocket webSocket, |
95 CharSequence message, |
94 CharSequence message, |
96 MessagePart part) { |
95 boolean last) { |
97 replenish(webSocket); |
96 replenish(webSocket); |
98 return null; |
97 return null; |
99 } |
98 } |
100 |
99 |
101 @Override |
100 @Override |
102 public CompletionStage<?> onBinary(WebSocket webSocket, |
101 public CompletionStage<?> onBinary(WebSocket webSocket, |
103 ByteBuffer message, |
102 ByteBuffer message, |
104 MessagePart part) { |
103 boolean last) { |
105 System.out.printf("onBinary(%s, %s, %s)%n", webSocket, message, part); |
104 System.out.printf("onBinary(%s, %s, %s)%n", webSocket, message, last); |
106 OnBinary inv = new OnBinary(webSocket, fullCopy(message), part); |
105 OnBinary inv = new OnBinary(webSocket, fullCopy(message), last); |
107 invocations.add(inv); |
106 invocations.add(inv); |
108 if (collectUntil.test(inv)) { |
107 if (collectUntil.test(inv)) { |
109 lastCall.complete(null); |
108 lastCall.complete(null); |
110 } |
109 } |
111 return onBinary0(webSocket, message, part); |
110 return onBinary0(webSocket, message, last); |
112 } |
111 } |
113 |
112 |
114 protected CompletionStage<?> onBinary0(WebSocket webSocket, |
113 protected CompletionStage<?> onBinary0(WebSocket webSocket, |
115 ByteBuffer message, |
114 ByteBuffer message, |
116 MessagePart part) { |
115 boolean last) { |
117 replenish(webSocket); |
116 replenish(webSocket); |
118 return null; |
117 return null; |
119 } |
118 } |
120 |
119 |
121 @Override |
120 @Override |
198 return new OnOpen(webSocket); |
197 return new OnOpen(webSocket); |
199 } |
198 } |
200 |
199 |
201 public static OnText onText(WebSocket webSocket, |
200 public static OnText onText(WebSocket webSocket, |
202 String text, |
201 String text, |
203 MessagePart part) { |
202 boolean last) { |
204 return new OnText(webSocket, text, part); |
203 return new OnText(webSocket, text, last); |
205 } |
204 } |
206 |
205 |
207 public static OnBinary onBinary(WebSocket webSocket, |
206 public static OnBinary onBinary(WebSocket webSocket, |
208 ByteBuffer data, |
207 ByteBuffer data, |
209 MessagePart part) { |
208 boolean last) { |
210 return new OnBinary(webSocket, data, part); |
209 return new OnBinary(webSocket, data, last); |
211 } |
210 } |
212 |
211 |
213 public static OnPing onPing(WebSocket webSocket, |
212 public static OnPing onPing(WebSocket webSocket, |
214 ByteBuffer data) { |
213 ByteBuffer data) { |
215 return new OnPing(webSocket, data); |
214 return new OnPing(webSocket, data); |
264 } |
263 } |
265 |
264 |
266 public static final class OnText extends Invocation { |
265 public static final class OnText extends Invocation { |
267 |
266 |
268 final String text; |
267 final String text; |
269 final MessagePart part; |
268 final boolean last; |
270 |
269 |
271 public OnText(WebSocket webSocket, String text, MessagePart part) { |
270 public OnText(WebSocket webSocket, String text, boolean last) { |
272 super(webSocket); |
271 super(webSocket); |
273 this.text = text; |
272 this.text = text; |
274 this.part = part; |
273 this.last = last; |
275 } |
274 } |
276 |
275 |
277 @Override |
276 @Override |
278 public boolean equals(Object o) { |
277 public boolean equals(Object o) { |
279 if (this == o) return true; |
278 if (this == o) return true; |
280 if (o == null || getClass() != o.getClass()) return false; |
279 if (o == null || getClass() != o.getClass()) return false; |
281 OnText onText = (OnText) o; |
280 OnText onText = (OnText) o; |
282 return Objects.equals(text, onText.text) && |
281 return Objects.equals(text, onText.text) && |
283 part == onText.part && |
282 last == onText.last && |
284 Objects.equals(webSocket, onText.webSocket); |
283 Objects.equals(webSocket, onText.webSocket); |
285 } |
284 } |
286 |
285 |
287 @Override |
286 @Override |
288 public int hashCode() { |
287 public int hashCode() { |
289 return Objects.hash(text, part, webSocket); |
288 return Objects.hash(text, last, webSocket); |
290 } |
289 } |
291 |
290 |
292 @Override |
291 @Override |
293 public String toString() { |
292 public String toString() { |
294 return String.format("onText(%s, %s, %s)", webSocket, text, part); |
293 return String.format("onText(%s, message.length=%s, %s)", webSocket, text.length(), last); |
295 } |
294 } |
296 } |
295 } |
297 |
296 |
298 public static final class OnBinary extends Invocation { |
297 public static final class OnBinary extends Invocation { |
299 |
298 |
300 final ByteBuffer data; |
299 final ByteBuffer data; |
301 final MessagePart part; |
300 final boolean last; |
302 |
301 |
303 public OnBinary(WebSocket webSocket, ByteBuffer data, MessagePart part) { |
302 public OnBinary(WebSocket webSocket, ByteBuffer data, boolean last) { |
304 super(webSocket); |
303 super(webSocket); |
305 this.data = data; |
304 this.data = data; |
306 this.part = part; |
305 this.last = last; |
307 } |
306 } |
308 |
307 |
309 @Override |
308 @Override |
310 public boolean equals(Object o) { |
309 public boolean equals(Object o) { |
311 if (this == o) return true; |
310 if (this == o) return true; |
312 if (o == null || getClass() != o.getClass()) return false; |
311 if (o == null || getClass() != o.getClass()) return false; |
313 OnBinary onBinary = (OnBinary) o; |
312 OnBinary onBinary = (OnBinary) o; |
314 return Objects.equals(data, onBinary.data) && |
313 return Objects.equals(data, onBinary.data) && |
315 part == onBinary.part && |
314 last == onBinary.last && |
316 Objects.equals(webSocket, onBinary.webSocket); |
315 Objects.equals(webSocket, onBinary.webSocket); |
317 } |
316 } |
318 |
317 |
319 @Override |
318 @Override |
320 public int hashCode() { |
319 public int hashCode() { |
321 return Objects.hash(data, part, webSocket); |
320 return Objects.hash(data, last, webSocket); |
322 } |
321 } |
323 |
322 |
324 @Override |
323 @Override |
325 public String toString() { |
324 public String toString() { |
326 return String.format("onBinary(%s, %s, %s)", webSocket, data, part); |
325 return String.format("onBinary(%s, %s, %s)", webSocket, data, last); |
327 } |
326 } |
328 } |
327 } |
329 |
328 |
330 public static final class OnPing extends Invocation { |
329 public static final class OnPing extends Invocation { |
331 |
330 |