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 io.netty.channel.socket;
17  
18  import io.netty.buffer.ByteBufAllocator;
19  import io.netty.channel.ChannelException;
20  import io.netty.channel.ChannelOption;
21  import io.netty.channel.DefaultChannelConfig;
22  import io.netty.channel.MessageSizeEstimator;
23  import io.netty.channel.RecvByteBufAllocator;
24  import io.netty.util.NetUtil;
25  
26  import java.net.ServerSocket;
27  import java.net.SocketException;
28  import java.util.Map;
29  
30  import static io.netty.channel.ChannelOption.*;
31  
32  /**
33   * The default {@link ServerSocketChannelConfig} implementation.
34   */
35  public class DefaultServerSocketChannelConfig extends DefaultChannelConfig
36                                                implements ServerSocketChannelConfig {
37  
38      protected final ServerSocket javaSocket;
39      private volatile int backlog = NetUtil.SOMAXCONN;
40  
41      /**
42       * Creates a new instance.
43       */
44      public DefaultServerSocketChannelConfig(ServerSocketChannel channel, ServerSocket javaSocket) {
45          super(channel);
46          if (javaSocket == null) {
47              throw new NullPointerException("javaSocket");
48          }
49          this.javaSocket = javaSocket;
50      }
51  
52      @Override
53      public Map<ChannelOption<?>, Object> getOptions() {
54          return getOptions(super.getOptions(), SO_RCVBUF, SO_REUSEADDR, SO_BACKLOG);
55      }
56  
57      @SuppressWarnings("unchecked")
58      @Override
59      public <T> T getOption(ChannelOption<T> option) {
60          if (option == SO_RCVBUF) {
61              return (T) Integer.valueOf(getReceiveBufferSize());
62          }
63          if (option == SO_REUSEADDR) {
64              return (T) Boolean.valueOf(isReuseAddress());
65          }
66          if (option == SO_BACKLOG) {
67              return (T) Integer.valueOf(getBacklog());
68          }
69  
70          return super.getOption(option);
71      }
72  
73      @Override
74      public <T> boolean setOption(ChannelOption<T> option, T value) {
75          validate(option, value);
76  
77          if (option == SO_RCVBUF) {
78              setReceiveBufferSize((Integer) value);
79          } else if (option == SO_REUSEADDR) {
80              setReuseAddress((Boolean) value);
81          } else if (option == SO_BACKLOG) {
82              setBacklog((Integer) value);
83          } else {
84              return super.setOption(option, value);
85          }
86  
87          return true;
88      }
89  
90      @Override
91      public boolean isReuseAddress() {
92          try {
93              return javaSocket.getReuseAddress();
94          } catch (SocketException e) {
95              throw new ChannelException(e);
96          }
97      }
98  
99      @Override
100     public ServerSocketChannelConfig setReuseAddress(boolean reuseAddress) {
101         try {
102             javaSocket.setReuseAddress(reuseAddress);
103         } catch (SocketException e) {
104             throw new ChannelException(e);
105         }
106         return this;
107     }
108 
109     @Override
110     public int getReceiveBufferSize() {
111         try {
112             return javaSocket.getReceiveBufferSize();
113         } catch (SocketException e) {
114             throw new ChannelException(e);
115         }
116     }
117 
118     @Override
119     public ServerSocketChannelConfig setReceiveBufferSize(int receiveBufferSize) {
120         try {
121             javaSocket.setReceiveBufferSize(receiveBufferSize);
122         } catch (SocketException e) {
123             throw new ChannelException(e);
124         }
125         return this;
126     }
127 
128     @Override
129     public ServerSocketChannelConfig setPerformancePreferences(int connectionTime, int latency, int bandwidth) {
130         javaSocket.setPerformancePreferences(connectionTime, latency, bandwidth);
131         return this;
132     }
133 
134     @Override
135     public int getBacklog() {
136         return backlog;
137     }
138 
139     @Override
140     public ServerSocketChannelConfig setBacklog(int backlog) {
141         if (backlog < 0) {
142             throw new IllegalArgumentException("backlog: " + backlog);
143         }
144         this.backlog = backlog;
145         return this;
146     }
147 
148     @Override
149     public ServerSocketChannelConfig setConnectTimeoutMillis(int connectTimeoutMillis) {
150         super.setConnectTimeoutMillis(connectTimeoutMillis);
151         return this;
152     }
153 
154     @Override
155     public ServerSocketChannelConfig setMaxMessagesPerRead(int maxMessagesPerRead) {
156         super.setMaxMessagesPerRead(maxMessagesPerRead);
157         return this;
158     }
159 
160     @Override
161     public ServerSocketChannelConfig setWriteSpinCount(int writeSpinCount) {
162         super.setWriteSpinCount(writeSpinCount);
163         return this;
164     }
165 
166     @Override
167     public ServerSocketChannelConfig setAllocator(ByteBufAllocator allocator) {
168         super.setAllocator(allocator);
169         return this;
170     }
171 
172     @Override
173     public ServerSocketChannelConfig setRecvByteBufAllocator(RecvByteBufAllocator allocator) {
174         super.setRecvByteBufAllocator(allocator);
175         return this;
176     }
177 
178     @Override
179     public ServerSocketChannelConfig setAutoRead(boolean autoRead) {
180         super.setAutoRead(autoRead);
181         return this;
182     }
183 
184     @Override
185     public ServerSocketChannelConfig setWriteBufferHighWaterMark(int writeBufferHighWaterMark) {
186         super.setWriteBufferHighWaterMark(writeBufferHighWaterMark);
187         return this;
188     }
189 
190     @Override
191     public ServerSocketChannelConfig setWriteBufferLowWaterMark(int writeBufferLowWaterMark) {
192         super.setWriteBufferLowWaterMark(writeBufferLowWaterMark);
193         return this;
194     }
195 
196     @Override
197     public ServerSocketChannelConfig setMessageSizeEstimator(MessageSizeEstimator estimator) {
198         super.setMessageSizeEstimator(estimator);
199         return this;
200     }
201 }