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.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  public class DefaultServerSocketChannelConfig extends DefaultServerChannelConfig
29                                                implements ServerSocketChannelConfig {
30  
31      private final ServerSocket socket;
32      private volatile int backlog;
33  
34      /**
35       * Creates a new instance.
36       */
37      public DefaultServerSocketChannelConfig(ServerSocket socket) {
38          if (socket == null) {
39              throw new NullPointerException("socket");
40          }
41          this.socket = socket;
42      }
43  
44      @Override
45      public boolean setOption(String key, Object value) {
46          if (super.setOption(key, value)) {
47              return true;
48          }
49  
50          if ("receiveBufferSize".equals(key)) {
51              setReceiveBufferSize(ConversionUtil.toInt(value));
52          } else if ("reuseAddress".equals(key)) {
53              setReuseAddress(ConversionUtil.toBoolean(value));
54          } else if ("backlog".equals(key)) {
55              setBacklog(ConversionUtil.toInt(value));
56          } else {
57              return false;
58          }
59          return true;
60      }
61  
62      public boolean isReuseAddress() {
63          try {
64              return socket.getReuseAddress();
65          } catch (SocketException e) {
66              throw new ChannelException(e);
67          }
68      }
69  
70      public void setReuseAddress(boolean reuseAddress) {
71          try {
72              socket.setReuseAddress(reuseAddress);
73          } catch (SocketException e) {
74              throw new ChannelException(e);
75          }
76      }
77  
78      public int getReceiveBufferSize() {
79          try {
80              return socket.getReceiveBufferSize();
81          } catch (SocketException e) {
82              throw new ChannelException(e);
83          }
84      }
85  
86      public void setReceiveBufferSize(int receiveBufferSize) {
87          try {
88              socket.setReceiveBufferSize(receiveBufferSize);
89          } catch (SocketException e) {
90              throw new ChannelException(e);
91          }
92      }
93  
94      public void setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
95          socket.setPerformancePreferences(connectionTime, latency, bandwidth);
96      }
97  
98      public int getBacklog() {
99          return backlog;
100     }
101 
102     public void setBacklog(int backlog) {
103         if (backlog < 0) {
104             throw new IllegalArgumentException("backlog: " + backlog);
105         }
106         this.backlog = backlog;
107     }
108 }