View Javadoc

    1   /*
    2    * Copyright 2009 Red Hat, Inc.
    3    *
    4    * Red Hat licenses this file to you under the Apache License, version 2.0
    5    * (the "License"); you may not use this file except in compliance with the
    6    * 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  /**
    38   *
    39   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    40   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    41   *
    42   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    43   *
    44   */
    45  class NioServerSocketChannel extends AbstractServerChannel
    46                               implements org.jboss.netty.channel.socket.ServerSocketChannel {
    47
    48      private static final InternalLogger logger =
    49          InternalLoggerFactory.getInstance(NioServerSocketChannel.class);
    50
    51      final ServerSocketChannel socket;
    52      final Lock shutdownLock = new ReentrantLock();
    53      volatile Selector selector;
    54      private final ServerSocketChannelConfig config;
    55
    56      NioServerSocketChannel(
    57              ChannelFactory factory,
    58              ChannelPipeline pipeline,
    59              ChannelSink sink) {
    60
    61          super(factory, pipeline, sink);
    62
    63          try {
    64              socket = ServerSocketChannel.open();
    65          } catch (IOException e) {
    66              throw new ChannelException(
    67                      "Failed to open a server socket.", e);
    68          }
    69
    70          try {
    71              socket.configureBlocking(false);
    72          } catch (IOException e) {
    73              try {
    74                  socket.close();
    75              } catch (IOException e2) {
    76                  logger.warn(
    77                          "Failed to close a partially initialized socket.", e2);
    78              }
    79
    80              throw new ChannelException("Failed to enter non-blocking mode.", e);
    81          }
    82
    83          config = new DefaultServerSocketChannelConfig(socket.socket());
    84
    85          fireChannelOpen(this);
    86      }
    87
    88      public ServerSocketChannelConfig getConfig() {
    89          return config;
    90      }
    91
    92      public InetSocketAddress getLocalAddress() {
    93          return (InetSocketAddress) socket.socket().getLocalSocketAddress();
    94      }
    95
    96      public InetSocketAddress getRemoteAddress() {
    97          return null;
    98      }
    99
    100     public boolean isBound() {
    101         return isOpen() && socket.socket().isBound();
    102     }
    103
    104     @Override
    105     protected boolean setClosed() {
    106         return super.setClosed();
    107     }
    108 }