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