View Javadoc

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