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.io.IOException;
    19  import java.net.DatagramSocket;
    20  import java.net.InetAddress;
    21  import java.net.MulticastSocket;
    22  import java.net.NetworkInterface;
    23  import java.net.SocketException;
    24
    25  import org.jboss.netty.channel.ChannelException;
    26  import org.jboss.netty.channel.DefaultChannelConfig;
    27  import org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory;
    28  import org.jboss.netty.channel.ReceiveBufferSizePredictor;
    29  import org.jboss.netty.channel.ReceiveBufferSizePredictorFactory;
    30  import org.jboss.netty.util.internal.ConversionUtil;
    31
    32  /**
    33   * The default {@link DatagramChannelConfig} implementation.
    34   *
    35   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
    36   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
    37   *
    38   * @version $Rev: 2080 $, $Date: 2010-01-26 18:04:19 +0900 (Tue, 26 Jan 2010) $
    39   *
    40   */
    41  public class DefaultDatagramChannelConfig extends DefaultChannelConfig
    42                                          implements DatagramChannelConfig {
    43
    44      private static final ReceiveBufferSizePredictorFactory DEFAULT_PREDICTOR_FACTORY =
    45          new FixedReceiveBufferSizePredictorFactory(768);
    46
    47      private final DatagramSocket socket;
    48      private volatile ReceiveBufferSizePredictor predictor;
    49      private volatile ReceiveBufferSizePredictorFactory predictorFactory = DEFAULT_PREDICTOR_FACTORY;
    50
    51      /**
    52       * Creates a new instance.
    53       */
    54      public DefaultDatagramChannelConfig(DatagramSocket socket) {
    55          if (socket == null) {
    56              throw new NullPointerException("socket");
    57          }
    58          this.socket = socket;
    59      }
    60
    61      @Override
    62      public boolean setOption(String key, Object value) {
    63          if (super.setOption(key, value)) {
    64              return true;
    65          }
    66
    67          if (key.equals("broadcast")) {
    68              setBroadcast(ConversionUtil.toBoolean(value));
    69          } else if (key.equals("receiveBufferSize")) {
    70              setReceiveBufferSize(ConversionUtil.toInt(value));
    71          } else if (key.equals("sendBufferSize")) {
    72              setSendBufferSize(ConversionUtil.toInt(value));
    73          } else if (key.equals("receiveBufferSizePredictorFactory")) {
    74              setReceiveBufferSizePredictorFactory((ReceiveBufferSizePredictorFactory) value);
    75          } else if (key.equals("receiveBufferSizePredictor")) {
    76              setReceiveBufferSizePredictor((ReceiveBufferSizePredictor) value);
    77          } else if (key.equals("reuseAddress")) {
    78              setReuseAddress(ConversionUtil.toBoolean(value));
    79          } else if (key.equals("loopbackModeDisabled")) {
    80              setLoopbackModeDisabled(ConversionUtil.toBoolean(value));
    81          } else if (key.equals("interface")) {
    82              setInterface((InetAddress) value);
    83          } else if (key.equals("networkInterface")) {
    84              setNetworkInterface((NetworkInterface) value);
    85          } else if (key.equals("timeToLive")) {
    86              setTimeToLive(ConversionUtil.toInt(value));
    87          } else if (key.equals("trafficClass")) {
    88              setTrafficClass(ConversionUtil.toInt(value));
    89          } else {
    90              return false;
    91          }
    92          return true;
    93      }
    94
    95      public boolean isBroadcast() {
    96          try {
    97              return socket.getBroadcast();
    98          } catch (SocketException e) {
    99              throw new ChannelException(e);
    100         }
    101     }
    102
    103     public void setBroadcast(boolean broadcast) {
    104         try {
    105             socket.setBroadcast(broadcast);
    106         } catch (SocketException e) {
    107             throw new ChannelException(e);
    108         }
    109     }
    110
    111     public InetAddress getInterface() {
    112         if (socket instanceof MulticastSocket) {
    113             try {
    114                 return ((MulticastSocket) socket).getInterface();
    115             } catch (SocketException e) {
    116                 throw new ChannelException(e);
    117             }
    118         } else {
    119             throw new UnsupportedOperationException();
    120         }
    121     }
    122
    123     public void setInterface(InetAddress interfaceAddress) {
    124         if (socket instanceof MulticastSocket) {
    125             try {
    126                 ((MulticastSocket) socket).setInterface(interfaceAddress);
    127             } catch (SocketException e) {
    128                 throw new ChannelException(e);
    129             }
    130         } else {
    131             throw new UnsupportedOperationException();
    132         }
    133     }
    134
    135     public boolean isLoopbackModeDisabled() {
    136         if (socket instanceof MulticastSocket) {
    137             try {
    138                 return ((MulticastSocket) socket).getLoopbackMode();
    139             } catch (SocketException e) {
    140                 throw new ChannelException(e);
    141             }
    142         } else {
    143             throw new UnsupportedOperationException();
    144         }
    145     }
    146
    147     public void setLoopbackModeDisabled(boolean loopbackModeDisabled) {
    148         if (socket instanceof MulticastSocket) {
    149             try {
    150                 ((MulticastSocket) socket).setLoopbackMode(loopbackModeDisabled);
    151             } catch (SocketException e) {
    152                 throw new ChannelException(e);
    153             }
    154         } else {
    155             throw new UnsupportedOperationException();
    156         }
    157     }
    158
    159     public NetworkInterface getNetworkInterface() {
    160         if (socket instanceof MulticastSocket) {
    161             try {
    162                 return ((MulticastSocket) socket).getNetworkInterface();
    163             } catch (SocketException e) {
    164                 throw new ChannelException(e);
    165             }
    166         } else {
    167             throw new UnsupportedOperationException();
    168         }
    169     }
    170
    171     public void setNetworkInterface(NetworkInterface networkInterface) {
    172         if (socket instanceof MulticastSocket) {
    173             try {
    174                 ((MulticastSocket) socket).setNetworkInterface(networkInterface);
    175             } catch (SocketException e) {
    176                 throw new ChannelException(e);
    177             }
    178         } else {
    179             throw new UnsupportedOperationException();
    180         }
    181     }
    182
    183     public boolean isReuseAddress() {
    184         try {
    185             return socket.getReuseAddress();
    186         } catch (SocketException e) {
    187             throw new ChannelException(e);
    188         }
    189     }
    190
    191     public void setReuseAddress(boolean reuseAddress) {
    192         try {
    193             socket.setReuseAddress(reuseAddress);
    194         } catch (SocketException e) {
    195             throw new ChannelException(e);
    196         }
    197     }
    198
    199     public int getReceiveBufferSize() {
    200         try {
    201             return socket.getReceiveBufferSize();
    202         } catch (SocketException e) {
    203             throw new ChannelException(e);
    204         }
    205     }
    206
    207     public void setReceiveBufferSize(int receiveBufferSize) {
    208         try {
    209             socket.setReceiveBufferSize(receiveBufferSize);
    210         } catch (SocketException e) {
    211             throw new ChannelException(e);
    212         }
    213     }
    214
    215     public int getSendBufferSize() {
    216         try {
    217             return socket.getSendBufferSize();
    218         } catch (SocketException e) {
    219             throw new ChannelException(e);
    220         }
    221     }
    222
    223     public void setSendBufferSize(int sendBufferSize) {
    224         try {
    225             socket.setSendBufferSize(sendBufferSize);
    226         } catch (SocketException e) {
    227             throw new ChannelException(e);
    228         }
    229     }
    230
    231     public int getTimeToLive() {
    232         if (socket instanceof MulticastSocket) {
    233             try {
    234                 return ((MulticastSocket) socket).getTimeToLive();
    235             } catch (IOException e) {
    236                 throw new ChannelException(e);
    237             }
    238         } else {
    239             throw new UnsupportedOperationException();
    240         }
    241     }
    242
    243     public void setTimeToLive(int ttl) {
    244         if (socket instanceof MulticastSocket) {
    245             try {
    246                 ((MulticastSocket) socket).setTimeToLive(ttl);
    247             } catch (IOException e) {
    248                 throw new ChannelException(e);
    249             }
    250         } else {
    251             throw new UnsupportedOperationException();
    252         }
    253     }
    254
    255     public int getTrafficClass() {
    256         try {
    257             return socket.getTrafficClass();
    258         } catch (SocketException e) {
    259             throw new ChannelException(e);
    260         }
    261     }
    262
    263     public void setTrafficClass(int trafficClass) {
    264         try {
    265             socket.setTrafficClass(trafficClass);
    266         } catch (SocketException e) {
    267             throw new ChannelException(e);
    268         }
    269     }
    270
    271     public ReceiveBufferSizePredictor getReceiveBufferSizePredictor() {
    272         ReceiveBufferSizePredictor predictor = this.predictor;
    273         if (predictor == null) {
    274             try {
    275                 this.predictor = predictor = getReceiveBufferSizePredictorFactory().getPredictor();
    276             } catch (Exception e) {
    277                 throw new ChannelException(
    278                         "Failed to create a new " +
    279                         ReceiveBufferSizePredictor.class.getSimpleName() + '.',
    280                         e);
    281             }
    282         }
    283         return predictor;
    284     }
    285
    286     public void setReceiveBufferSizePredictor(
    287             ReceiveBufferSizePredictor predictor) {
    288         if (predictor == null) {
    289             throw new NullPointerException("predictor");
    290         }
    291         this.predictor = predictor;
    292     }
    293
    294     public ReceiveBufferSizePredictorFactory getReceiveBufferSizePredictorFactory() {
    295         return predictorFactory;
    296     }
    297
    298     public void setReceiveBufferSizePredictorFactory(ReceiveBufferSizePredictorFactory predictorFactory) {
    299         if (predictorFactory == null) {
    300             throw new NullPointerException("predictorFactory");
    301         }
    302         this.predictorFactory = predictorFactory;
    303     }
    304 }