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