View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * UDT NIO components provider:
37   * <p>
38   * Provides {@link ChannelFactory} for UDT channels.
39   * <p>
40   * Provides {@link SelectorProvider} for UDT channels.
41   *
42   * @deprecated The UDT transport is no longer maintained and will be removed.
43   */
44  @Deprecated
45  public final class NioUdtProvider<T extends UdtChannel> implements ChannelFactory<T> {
46  
47      /**
48       * {@link ChannelFactory} for UDT Byte Acceptor. See {@link TypeUDT#STREAM}
49       * and {@link KindUDT#ACCEPTOR}.
50       */
51      public static final ChannelFactory<UdtServerChannel> BYTE_ACCEPTOR = new NioUdtProvider<UdtServerChannel>(
52              TypeUDT.STREAM, KindUDT.ACCEPTOR);
53  
54      /**
55       * {@link ChannelFactory} for UDT Byte Connector. See {@link TypeUDT#STREAM}
56       * and {@link KindUDT#CONNECTOR}.
57       */
58      public static final ChannelFactory<UdtChannel> BYTE_CONNECTOR = new NioUdtProvider<UdtChannel>(
59              TypeUDT.STREAM, KindUDT.CONNECTOR);
60  
61      /**
62       * {@link SelectorProvider} for UDT Byte channels. See
63       * {@link TypeUDT#STREAM}.
64       */
65      public static final SelectorProvider BYTE_PROVIDER = SelectorProviderUDT.STREAM;
66  
67      /**
68       * {@link ChannelFactory} for UDT Byte Rendezvous. See
69       * {@link TypeUDT#STREAM} and {@link KindUDT#RENDEZVOUS}.
70       */
71      public static final ChannelFactory<UdtChannel> BYTE_RENDEZVOUS = new NioUdtProvider<UdtChannel>(
72              TypeUDT.STREAM, KindUDT.RENDEZVOUS);
73  
74      /**
75       * {@link ChannelFactory} for UDT Message Acceptor. See
76       * {@link TypeUDT#DATAGRAM} and {@link KindUDT#ACCEPTOR}.
77       */
78      public static final ChannelFactory<UdtServerChannel> MESSAGE_ACCEPTOR = new NioUdtProvider<UdtServerChannel>(
79              TypeUDT.DATAGRAM, KindUDT.ACCEPTOR);
80  
81      /**
82       * {@link ChannelFactory} for UDT Message Connector. See
83       * {@link TypeUDT#DATAGRAM} and {@link KindUDT#CONNECTOR}.
84       */
85      public static final ChannelFactory<UdtChannel> MESSAGE_CONNECTOR = new NioUdtProvider<UdtChannel>(
86              TypeUDT.DATAGRAM, KindUDT.CONNECTOR);
87  
88      /**
89       * {@link SelectorProvider} for UDT Message channels. See
90       * {@link TypeUDT#DATAGRAM}.
91       */
92      public static final SelectorProvider MESSAGE_PROVIDER = SelectorProviderUDT.DATAGRAM;
93  
94      /**
95       * {@link ChannelFactory} for UDT Message Rendezvous. See
96       * {@link TypeUDT#DATAGRAM} and {@link KindUDT#RENDEZVOUS}.
97       */
98      public static final ChannelFactory<UdtChannel> MESSAGE_RENDEZVOUS = new NioUdtProvider<UdtChannel>(
99              TypeUDT.DATAGRAM, KindUDT.RENDEZVOUS);
100 
101     /**
102      * Expose underlying {@link ChannelUDT} for debugging and monitoring.
103      * <p>
104      * @return underlying {@link ChannelUDT} or null, if parameter is not
105      *         {@link UdtChannel}
106      */
107     public static ChannelUDT channelUDT(final Channel channel) {
108         // bytes
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         // message
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      * Convenience factory for {@link KindUDT#ACCEPTOR} channels.
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      * Convenience factory for {@link KindUDT#CONNECTOR} channels.
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      * Convenience factory for {@link KindUDT#RENDEZVOUS} channels.
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      * Expose underlying {@link SocketUDT} for debugging and monitoring.
170      * <p>
171      * @return underlying {@link SocketUDT} or null, if parameter is not
172      *         {@link UdtChannel}
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      * {@link ChannelFactory} for given {@link TypeUDT} and {@link KindUDT}
188      */
189     private NioUdtProvider(final TypeUDT type, final KindUDT kind) {
190         this.type = type;
191         this.kind = kind;
192     }
193 
194     /**
195      * UDT Channel Kind. See {@link KindUDT}
196      */
197     public KindUDT kind() {
198         return kind;
199     }
200 
201     /**
202      * Produce new {@link UdtChannel} based on factory {@link #kind()} and
203      * {@link #type()}
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      * UDT Socket Type. See {@link TypeUDT}
243      */
244     public TypeUDT type() {
245         return type;
246     }
247 
248 }