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;
    17
    18  import java.net.ServerSocket;
    19  import java.net.SocketException;
    20
    21  import org.jboss.netty.channel.ChannelException;
    22  import org.jboss.netty.channel.DefaultServerChannelConfig;
    23  import org.jboss.netty.util.internal.ConversionUtil;
    24
    25  /**
    26   * The default {@link ServerSocketChannelConfig} implementation.
    27   *
    28   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    29   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    30   *
    31   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    32   */
    33  public class DefaultServerSocketChannelConfig extends DefaultServerChannelConfig
    34                                                implements ServerSocketChannelConfig {
    35
    36      private final ServerSocket socket;
    37      private volatile int backlog;
    38
    39      /**
    40       * Creates a new instance.
    41       */
    42      public DefaultServerSocketChannelConfig(ServerSocket socket) {
    43          if (socket == null) {
    44              throw new NullPointerException("socket");
    45          }
    46          this.socket = socket;
    47      }
    48
    49      @Override
    50      public boolean setOption(String key, Object value) {
    51          if (super.setOption(key, value)) {
    52              return true;
    53          }
    54
    55          if (key.equals("receiveBufferSize")) {
    56              setReceiveBufferSize(ConversionUtil.toInt(value));
    57          } else if (key.equals("reuseAddress")) {
    58              setReuseAddress(ConversionUtil.toBoolean(value));
    59          } else if (key.equals("backlog")) {
    60              setBacklog(ConversionUtil.toInt(value));
    61          } else {
    62              return false;
    63          }
    64          return true;
    65      }
    66
    67      public boolean isReuseAddress() {
    68          try {
    69              return socket.getReuseAddress();
    70          } catch (SocketException e) {
    71              throw new ChannelException(e);
    72          }
    73      }
    74
    75      public void setReuseAddress(boolean reuseAddress) {
    76          try {
    77              socket.setReuseAddress(reuseAddress);
    78          } catch (SocketException e) {
    79              throw new ChannelException(e);
    80          }
    81      }
    82
    83      public int getReceiveBufferSize() {
    84          try {
    85              return socket.getReceiveBufferSize();
    86          } catch (SocketException e) {
    87              throw new ChannelException(e);
    88          }
    89      }
    90
    91      public void setReceiveBufferSize(int receiveBufferSize) {
    92          try {
    93              socket.setReceiveBufferSize(receiveBufferSize);
    94          } catch (SocketException e) {
    95              throw new ChannelException(e);
    96          }
    97      }
    98
    99      public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
    100         socket.setPerformancePreferences(connectionTime, latency, bandwidth);
    101     }
    102
    103     public int getBacklog() {
    104         return backlog;
    105     }
    106
    107     public void setBacklog(int backlog) {
    108         if (backlog < 0) {
    109             throw new IllegalArgumentException("backlog: " + backlog);
    110         }
    111         this.backlog = backlog;
    112     }
    113 }