1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.channel.udt.nio;
17
18 import com.barchart.udt.SocketUDT;
19 import com.barchart.udt.TypeUDT;
20 import com.barchart.udt.nio.ChannelUDT;
21 import com.barchart.udt.nio.KindUDT;
22 import com.barchart.udt.nio.RendezvousChannelUDT;
23 import com.barchart.udt.nio.SelectorProviderUDT;
24 import com.barchart.udt.nio.ServerSocketChannelUDT;
25 import com.barchart.udt.nio.SocketChannelUDT;
26 import io.netty.bootstrap.ChannelFactory;
27 import io.netty.channel.Channel;
28 import io.netty.channel.ChannelException;
29 import io.netty.channel.udt.UdtServerChannel;
30 import io.netty.channel.udt.UdtChannel;
31
32 import java.io.IOException;
33 import java.nio.channels.spi.SelectorProvider;
34
35
36
37
38
39
40
41
42
43
44 @Deprecated
45 public final class NioUdtProvider<T extends UdtChannel> implements ChannelFactory<T> {
46
47
48
49
50
51 public static final ChannelFactory<UdtServerChannel> BYTE_ACCEPTOR = new NioUdtProvider<UdtServerChannel>(
52 TypeUDT.STREAM, KindUDT.ACCEPTOR);
53
54
55
56
57
58 public static final ChannelFactory<UdtChannel> BYTE_CONNECTOR = new NioUdtProvider<UdtChannel>(
59 TypeUDT.STREAM, KindUDT.CONNECTOR);
60
61
62
63
64
65 public static final SelectorProvider BYTE_PROVIDER = SelectorProviderUDT.STREAM;
66
67
68
69
70
71 public static final ChannelFactory<UdtChannel> BYTE_RENDEZVOUS = new NioUdtProvider<UdtChannel>(
72 TypeUDT.STREAM, KindUDT.RENDEZVOUS);
73
74
75
76
77
78 public static final ChannelFactory<UdtServerChannel> MESSAGE_ACCEPTOR = new NioUdtProvider<UdtServerChannel>(
79 TypeUDT.DATAGRAM, KindUDT.ACCEPTOR);
80
81
82
83
84
85 public static final ChannelFactory<UdtChannel> MESSAGE_CONNECTOR = new NioUdtProvider<UdtChannel>(
86 TypeUDT.DATAGRAM, KindUDT.CONNECTOR);
87
88
89
90
91
92 public static final SelectorProvider MESSAGE_PROVIDER = SelectorProviderUDT.DATAGRAM;
93
94
95
96
97
98 public static final ChannelFactory<UdtChannel> MESSAGE_RENDEZVOUS = new NioUdtProvider<UdtChannel>(
99 TypeUDT.DATAGRAM, KindUDT.RENDEZVOUS);
100
101
102
103
104
105
106
107 public static ChannelUDT channelUDT(final Channel channel) {
108
109 if (channel instanceof NioUdtByteAcceptorChannel) {
110 return ((NioUdtByteAcceptorChannel) channel).javaChannel();
111 }
112 if (channel instanceof NioUdtByteRendezvousChannel) {
113 return ((NioUdtByteRendezvousChannel) channel).javaChannel();
114 }
115 if (channel instanceof NioUdtByteConnectorChannel) {
116 return ((NioUdtByteConnectorChannel) channel).javaChannel();
117 }
118
119
120 if (channel instanceof NioUdtMessageAcceptorChannel) {
121 return ((NioUdtMessageAcceptorChannel) channel).javaChannel();
122 }
123 if (channel instanceof NioUdtMessageRendezvousChannel) {
124 return ((NioUdtMessageRendezvousChannel) channel).javaChannel();
125 }
126 if (channel instanceof NioUdtMessageConnectorChannel) {
127 return ((NioUdtMessageConnectorChannel) channel).javaChannel();
128 }
129
130 return null;
131 }
132
133
134
135
136 static ServerSocketChannelUDT newAcceptorChannelUDT(
137 final TypeUDT type) {
138 try {
139 return SelectorProviderUDT.from(type).openServerSocketChannel();
140 } catch (final IOException e) {
141 throw new ChannelException("failed to open a server socket channel", e);
142 }
143 }
144
145
146
147
148 static SocketChannelUDT newConnectorChannelUDT(final TypeUDT type) {
149 try {
150 return SelectorProviderUDT.from(type).openSocketChannel();
151 } catch (final IOException e) {
152 throw new ChannelException("failed to open a socket channel", e);
153 }
154 }
155
156
157
158
159 static RendezvousChannelUDT newRendezvousChannelUDT(
160 final TypeUDT type) {
161 try {
162 return SelectorProviderUDT.from(type).openRendezvousChannel();
163 } catch (final IOException e) {
164 throw new ChannelException("failed to open a rendezvous channel", e);
165 }
166 }
167
168
169
170
171
172
173
174 public static SocketUDT socketUDT(final Channel channel) {
175 final ChannelUDT channelUDT = channelUDT(channel);
176 if (channelUDT == null) {
177 return null;
178 } else {
179 return channelUDT.socketUDT();
180 }
181 }
182
183 private final KindUDT kind;
184 private final TypeUDT type;
185
186
187
188
189 private NioUdtProvider(final TypeUDT type, final KindUDT kind) {
190 this.type = type;
191 this.kind = kind;
192 }
193
194
195
196
197 public KindUDT kind() {
198 return kind;
199 }
200
201
202
203
204
205 @SuppressWarnings("unchecked")
206 @Override
207 public T newChannel() {
208 switch (kind) {
209 case ACCEPTOR:
210 switch (type) {
211 case DATAGRAM:
212 return (T) new NioUdtMessageAcceptorChannel();
213 case STREAM:
214 return (T) new NioUdtByteAcceptorChannel();
215 default:
216 throw new IllegalStateException("wrong type=" + type);
217 }
218 case CONNECTOR:
219 switch (type) {
220 case DATAGRAM:
221 return (T) new NioUdtMessageConnectorChannel();
222 case STREAM:
223 return (T) new NioUdtByteConnectorChannel();
224 default:
225 throw new IllegalStateException("wrong type=" + type);
226 }
227 case RENDEZVOUS:
228 switch (type) {
229 case DATAGRAM:
230 return (T) new NioUdtMessageRendezvousChannel();
231 case STREAM:
232 return (T) new NioUdtByteRendezvousChannel();
233 default:
234 throw new IllegalStateException("wrong type=" + type);
235 }
236 default:
237 throw new IllegalStateException("wrong kind=" + kind);
238 }
239 }
240
241
242
243
244 public TypeUDT type() {
245 return type;
246 }
247
248 }