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 java.net.DatagramSocket;
    19  import java.util.Map;
    20
    21  import org.jboss.netty.channel.socket.DefaultDatagramChannelConfig;
    22  import org.jboss.netty.logging.InternalLogger;
    23  import org.jboss.netty.logging.InternalLoggerFactory;
    24  import org.jboss.netty.util.internal.ConversionUtil;
    25
    26  /**
    27   * The default {@link NioSocketChannelConfig} implementation.
    28   *
    29   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    30   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    31   * @author Daniel Bevenius (dbevenius@jboss.com)
    32   *
    33   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    34   *
    35   */
    36  class DefaultNioDatagramChannelConfig extends DefaultDatagramChannelConfig
    37          implements NioDatagramChannelConfig {
    38
    39      private static final InternalLogger logger =
    40              InternalLoggerFactory
    41                      .getInstance(DefaultNioDatagramChannelConfig.class);
    42
    43      private volatile int writeBufferHighWaterMark = 64 * 1024;
    44      private volatile int writeBufferLowWaterMark = 32 * 1024;
    45      private volatile int writeSpinCount = 16;
    46
    47      DefaultNioDatagramChannelConfig(DatagramSocket socket) {
    48          super(socket);
    49      }
    50
    51      @Override
    52      public void setOptions(Map<String, Object> options) {
    53          super.setOptions(options);
    54          if (getWriteBufferHighWaterMark() < getWriteBufferLowWaterMark()) {
    55              // Recover the integrity of the configuration with a sensible value.
    56              setWriteBufferLowWaterMark0(getWriteBufferHighWaterMark() >>> 1);
    57              // Notify the user about misconfiguration.
    58              logger.warn("writeBufferLowWaterMark cannot be greater than "
    59                      + "writeBufferHighWaterMark; setting to the half of the "
    60                      + "writeBufferHighWaterMark.");
    61          }
    62      }
    63
    64      @Override
    65      public boolean setOption(String key, Object value) {
    66          if (super.setOption(key, value)) {
    67              return true;
    68          }
    69
    70          if (key.equals("writeBufferHighWaterMark")) {
    71              setWriteBufferHighWaterMark0(ConversionUtil.toInt(value));
    72          } else if (key.equals("writeBufferLowWaterMark")) {
    73              setWriteBufferLowWaterMark0(ConversionUtil.toInt(value));
    74          } else if (key.equals("writeSpinCount")) {
    75              setWriteSpinCount(ConversionUtil.toInt(value));
    76          } else {
    77              return false;
    78          }
    79          return true;
    80      }
    81
    82      public int getWriteBufferHighWaterMark() {
    83          return writeBufferHighWaterMark;
    84      }
    85
    86      public void setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
    87          if (writeBufferHighWaterMark < getWriteBufferLowWaterMark()) {
    88              throw new IllegalArgumentException(
    89                      "writeBufferHighWaterMark cannot be less than " +
    90                              "writeBufferLowWaterMark (" +
    91                              getWriteBufferLowWaterMark() + "): " +
    92                              writeBufferHighWaterMark);
    93          }
    94          setWriteBufferHighWaterMark0(writeBufferHighWaterMark);
    95      }
    96
    97      private void setWriteBufferHighWaterMark0(int writeBufferHighWaterMark) {
    98          if (writeBufferHighWaterMark < 0) {
    99              throw new IllegalArgumentException("writeBufferHighWaterMark: " +
    100                     writeBufferHighWaterMark);
    101         }
    102         this.writeBufferHighWaterMark = writeBufferHighWaterMark;
    103     }
    104
    105     public int getWriteBufferLowWaterMark() {
    106         return writeBufferLowWaterMark;
    107     }
    108
    109     public void setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
    110         if (writeBufferLowWaterMark > getWriteBufferHighWaterMark()) {
    111             throw new IllegalArgumentException(
    112                     "writeBufferLowWaterMark cannot be greater than " +
    113                             "writeBufferHighWaterMark (" +
    114                             getWriteBufferHighWaterMark() + "): " +
    115                             writeBufferLowWaterMark);
    116         }
    117         setWriteBufferLowWaterMark0(writeBufferLowWaterMark);
    118     }
    119
    120     private void setWriteBufferLowWaterMark0(int writeBufferLowWaterMark) {
    121         if (writeBufferLowWaterMark < 0) {
    122             throw new IllegalArgumentException("writeBufferLowWaterMark: " +
    123                     writeBufferLowWaterMark);
    124         }
    125         this.writeBufferLowWaterMark = writeBufferLowWaterMark;
    126     }
    127
    128     public int getWriteSpinCount() {
    129         return writeSpinCount;
    130     }
    131
    132     public void setWriteSpinCount(int writeSpinCount) {
    133         if (writeSpinCount <= 0) {
    134             throw new IllegalArgumentException(
    135                     "writeSpinCount must be a positive integer.");
    136         }
    137         this.writeSpinCount = writeSpinCount;
    138     }
    139 }