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 org.jboss.netty.channel.socket.nio;
17  
18  import static org.jboss.netty.channel.Channels.*;
19  
20  import java.io.IOException;
21  import java.net.InetSocketAddress;
22  import java.nio.channels.Selector;
23  import java.nio.channels.ServerSocketChannel;
24  import java.util.concurrent.locks.Lock;
25  import java.util.concurrent.locks.ReentrantLock;
26  
27  import org.jboss.netty.channel.AbstractServerChannel;
28  import org.jboss.netty.channel.ChannelException;
29  import org.jboss.netty.channel.ChannelFactory;
30  import org.jboss.netty.channel.ChannelPipeline;
31  import org.jboss.netty.channel.ChannelSink;
32  import org.jboss.netty.channel.socket.DefaultServerSocketChannelConfig;
33  import org.jboss.netty.channel.socket.ServerSocketChannelConfig;
34  import org.jboss.netty.logging.InternalLogger;
35  import org.jboss.netty.logging.InternalLoggerFactory;
36  
37  class NioServerSocketChannel extends AbstractServerChannel
38                               implements org.jboss.netty.channel.socket.ServerSocketChannel {
39  
40      private static final InternalLogger logger =
41          InternalLoggerFactory.getInstance(NioServerSocketChannel.class);
42  
43      final ServerSocketChannel socket;
44      final Lock shutdownLock = new ReentrantLock();
45      volatile Selector selector;
46      private final ServerSocketChannelConfig config;
47  
48      NioServerSocketChannel(
49              ChannelFactory factory,
50              ChannelPipeline pipeline,
51              ChannelSink sink) {
52  
53          super(factory, pipeline, sink);
54  
55          try {
56              socket = ServerSocketChannel.open();
57          } catch (IOException e) {
58              throw new ChannelException(
59                      "Failed to open a server socket.", e);
60          }
61  
62          try {
63              socket.configureBlocking(false);
64          } catch (IOException e) {
65              try {
66                  socket.close();
67              } catch (IOException e2) {
68                  if (logger.isWarnEnabled()) {
69                      logger.warn(
70                              "Failed to close a partially initialized socket.", e2);
71                  }
72  
73              }
74  
75              throw new ChannelException("Failed to enter non-blocking mode.", e);
76          }
77  
78          config = new DefaultServerSocketChannelConfig(socket.socket());
79  
80          fireChannelOpen(this);
81      }
82  
83      public ServerSocketChannelConfig getConfig() {
84          return config;
85      }
86  
87      public InetSocketAddress getLocalAddress() {
88          return (InetSocketAddress) socket.socket().getLocalSocketAddress();
89      }
90  
91      public InetSocketAddress getRemoteAddress() {
92          return null;
93      }
94  
95      public boolean isBound() {
96          return isOpen() && socket.socket().isBound();
97      }
98  
99      @Override
100     protected boolean setClosed() {
101         return super.setClosed();
102     }
103 }