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 io.netty.channel.ChannelException;
21  import io.netty.channel.ChannelOutboundBuffer;
22  import io.netty.channel.nio.AbstractNioMessageChannel;
23  import io.netty.channel.udt.DefaultUdtServerChannelConfig;
24  import io.netty.channel.udt.UdtServerChannel;
25  import io.netty.channel.udt.UdtServerChannelConfig;
26  import io.netty.util.internal.logging.InternalLogger;
27  import io.netty.util.internal.logging.InternalLoggerFactory;
28  
29  import java.net.InetSocketAddress;
30  import java.net.SocketAddress;
31  
32  import static java.nio.channels.SelectionKey.*;
33  
34  /**
35   * Common base for Netty Byte/Message UDT Stream/Datagram acceptors.
36   */
37  public abstract class NioUdtAcceptorChannel extends AbstractNioMessageChannel implements UdtServerChannel {
38  
39      protected static final InternalLogger logger =
40              InternalLoggerFactory.getInstance(NioUdtAcceptorChannel.class);
41  
42      private final UdtServerChannelConfig config;
43  
44      protected NioUdtAcceptorChannel(final ServerSocketChannelUDT channelUDT) {
45          super(null, channelUDT, OP_ACCEPT);
46          try {
47              channelUDT.configureBlocking(false);
48              config = new DefaultUdtServerChannelConfig(this, channelUDT, true);
49          } catch (final Exception e) {
50              try {
51                  channelUDT.close();
52              } catch (final Exception e2) {
53                  if (logger.isWarnEnabled()) {
54                      logger.warn("Failed to close channel.", e2);
55                  }
56              }
57              throw new ChannelException("Failed to configure channel.", e);
58          }
59      }
60  
61      protected NioUdtAcceptorChannel(final TypeUDT type) {
62          this(NioUdtProvider.newAcceptorChannelUDT(type));
63      }
64  
65      @Override
66      public UdtServerChannelConfig config() {
67          return config;
68      }
69  
70      @Override
71      protected void doBind(final SocketAddress localAddress) throws Exception {
72          javaChannel().socket().bind(localAddress, config.getBacklog());
73      }
74  
75      @Override
76      protected void doClose() throws Exception {
77          javaChannel().close();
78      }
79  
80      @Override
81      protected boolean doConnect(final SocketAddress remoteAddress,
82              final SocketAddress localAddress) throws Exception {
83          throw new UnsupportedOperationException();
84      }
85  
86      @Override
87      protected void doDisconnect() throws Exception {
88          throw new UnsupportedOperationException();
89      }
90  
91      @Override
92      protected void doFinishConnect() throws Exception {
93          throw new UnsupportedOperationException();
94      }
95  
96      @Override
97      protected boolean doWriteMessage(Object msg, ChannelOutboundBuffer in) throws Exception {
98          throw new UnsupportedOperationException();
99      }
100 
101     @Override
102     protected final Object filterOutboundMessage(Object msg) throws Exception {
103         throw new UnsupportedOperationException();
104     }
105 
106     @Override
107     public boolean isActive() {
108         return javaChannel().socket().isBound();
109     }
110 
111     @Override
112     protected ServerSocketChannelUDT javaChannel() {
113         return (ServerSocketChannelUDT) super.javaChannel();
114     }
115 
116     @Override
117     protected SocketAddress localAddress0() {
118         return javaChannel().socket().getLocalSocketAddress();
119     }
120     @Override
121     public InetSocketAddress localAddress() {
122         return (InetSocketAddress) super.localAddress();
123     }
124 
125     @Override
126     public InetSocketAddress remoteAddress() {
127         return null;
128     }
129 
130     @Override
131     protected SocketAddress remoteAddress0() {
132         return null;
133     }
134 
135 }