1 /* |
|
2 * Copyright (c) 2017, 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.incubator.http.internal.websocket; |
|
27 |
|
28 import java.io.IOException; |
|
29 |
|
30 /* |
|
31 * Abstracts out I/O channel for the WebSocket implementation. The latter then |
|
32 * deals with input and output streams of messages and does not have to |
|
33 * understand the state machine of channels (e.g. how exactly they are closed). |
|
34 * Mocking this type will allow testing WebSocket message exchange in isolation. |
|
35 */ |
|
36 public class TransportSupplier { |
|
37 |
|
38 protected final RawChannel channel; /* Exposed for testing purposes */ |
|
39 private final Object lock = new Object(); |
|
40 private Transmitter transmitter; |
|
41 private Receiver receiver; |
|
42 private boolean receiverShutdown; |
|
43 private boolean transmitterShutdown; |
|
44 private boolean closed; |
|
45 |
|
46 public TransportSupplier(RawChannel channel) { |
|
47 this.channel = channel; |
|
48 } |
|
49 |
|
50 public Receiver receiver(MessageStreamConsumer consumer) { |
|
51 synchronized (lock) { |
|
52 if (receiver == null) { |
|
53 receiver = newReceiver(consumer); |
|
54 } |
|
55 return receiver; |
|
56 } |
|
57 } |
|
58 |
|
59 public Transmitter transmitter() { |
|
60 synchronized (lock) { |
|
61 if (transmitter == null) { |
|
62 transmitter = newTransmitter(); |
|
63 } |
|
64 return transmitter; |
|
65 } |
|
66 } |
|
67 |
|
68 protected Receiver newReceiver(MessageStreamConsumer consumer) { |
|
69 return new Receiver(consumer, channel) { |
|
70 @Override |
|
71 public void close() throws IOException { |
|
72 synchronized (lock) { |
|
73 if (!closed) { |
|
74 try { |
|
75 super.close(); |
|
76 } finally { |
|
77 receiverShutdown = true; |
|
78 if (transmitterShutdown) { |
|
79 closed = true; |
|
80 channel.close(); |
|
81 } |
|
82 } |
|
83 } |
|
84 } |
|
85 } |
|
86 }; |
|
87 } |
|
88 |
|
89 protected Transmitter newTransmitter() { |
|
90 return new Transmitter(channel) { |
|
91 @Override |
|
92 public void close() throws IOException { |
|
93 synchronized (lock) { |
|
94 if (!closed) { |
|
95 try { |
|
96 super.close(); |
|
97 } finally { |
|
98 transmitterShutdown = true; |
|
99 if (receiverShutdown) { |
|
100 closed = true; |
|
101 channel.close(); |
|
102 } |
|
103 } |
|
104 } |
|
105 } |
|
106 } |
|
107 }; |
|
108 } |
|
109 } |
|