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.TypeUDT;
19  import com.barchart.udt.nio.ServerSocketChannelUDT;
20  import com.barchart.udt.nio.SocketChannelUDT;
21  import io.netty.channel.ChannelException;
22  import io.netty.channel.ChannelMetadata;
23  import io.netty.channel.ChannelOutboundBuffer;
24  import io.netty.util.internal.SocketUtils;
25  import io.netty.channel.nio.AbstractNioMessageChannel;
26  import io.netty.channel.udt.DefaultUdtServerChannelConfig;
27  import io.netty.channel.udt.UdtChannel;
28  import io.netty.channel.udt.UdtServerChannel;
29  import io.netty.channel.udt.UdtServerChannelConfig;
30  import io.netty.util.internal.logging.InternalLogger;
31  import io.netty.util.internal.logging.InternalLoggerFactory;
32  
33  import java.net.InetSocketAddress;
34  import java.net.SocketAddress;
35  import java.util.List;
36  
37  import static java.nio.channels.SelectionKey.*;
38  
39  /**
40   * Common base for Netty Byte/Message UDT Stream/Datagram acceptors.
41   *
42   * @deprecated The UDT transport is no longer maintained and will be removed.
43   */
44  @Deprecated
45  public abstract class NioUdtAcceptorChannel extends AbstractNioMessageChannel implements UdtServerChannel {
46  
47      protected static final InternalLogger logger =
48              InternalLoggerFactory.getInstance(NioUdtAcceptorChannel.class);
49  
50      private static final ChannelMetadata METADATA = new ChannelMetadata(false);
51  
52      private final UdtServerChannelConfig config;
53  
54      protected NioUdtAcceptorChannel(final ServerSocketChannelUDT channelUDT) {
55          super(null, channelUDT, OP_ACCEPT);
56          try {
57              channelUDT.configureBlocking(false);
58              config = new DefaultUdtServerChannelConfig(this, channelUDT, true);
59          } catch (final Exception e) {
60              try {
61                  channelUDT.close();
62              } catch (final Exception e2) {
63                  if (logger.isWarnEnabled()) {
64                      logger.warn("Failed to close channel.", e2);
65                  }
66              }
67              throw new ChannelException("Failed to configure channel.", e);
68          }
69      }
70  
71      protected NioUdtAcceptorChannel(final TypeUDT type) {
72          this(NioUdtProvider.newAcceptorChannelUDT(type));
73      }
74  
75      @Override
76      public UdtServerChannelConfig config() {
77          return config;
78      }
79  
80      @Override
81      protected void doBind(final SocketAddress localAddress) throws Exception {
82          javaChannel().socket().bind(localAddress, config.getBacklog());
83      }
84  
85      @Override
86      protected void doClose() throws Exception {
87          javaChannel().close();
88      }
89  
90      @Override
91      protected boolean doConnect(final SocketAddress remoteAddress,
92              final SocketAddress localAddress) throws Exception {
93          throw new UnsupportedOperationException();
94      }
95  
96      @Override
97      protected void doDisconnect() throws Exception {
98          throw new UnsupportedOperationException();
99      }
100 
101     @Override
102     protected void doFinishConnect() throws Exception {
103         throw new UnsupportedOperationException();
104     }
105 
106     @Override
107     protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
108         throw new UnsupportedOperationException();
109     }
110 
111     @Override
112     protected final Object filterOutboundMessage(Object msg) throws Exception {
113         throw new UnsupportedOperationException();
114     }
115 
116     @Override
117     public boolean isActive() {
118         return javaChannel().socket().isBound();
119     }
120 
121     @Override
122     protected ServerSocketChannelUDT javaChannel() {
123         return (ServerSocketChannelUDT) super.javaChannel();
124     }
125 
126     @Override
127     protected SocketAddress localAddress0() {
128         return SocketUtils.localSocketAddress(javaChannel().socket());
129     }
130 
131     @Override
132     public InetSocketAddress localAddress() {
133         return (InetSocketAddress) super.localAddress();
134     }
135 
136     @Override
137     public InetSocketAddress remoteAddress() {
138         return null;
139     }
140 
141     @Override
142     protected SocketAddress remoteAddress0() {
143         return null;
144     }
145 
146     @Override
147     public ChannelMetadata metadata() {
148         return METADATA;
149     }
150 
151     @Override
152     protected int doReadMessages(List<Object> buf) throws Exception {
153         final SocketChannelUDT channelUDT = (SocketChannelUDT) SocketUtils.accept(javaChannel());
154         if (channelUDT == null) {
155             return 0;
156         } else {
157             buf.add(newConnectorChannel(channelUDT));
158             return 1;
159         }
160     }
161 
162     protected abstract UdtChannel newConnectorChannel(SocketChannelUDT channelUDT);
163 }