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