1 /*
2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 * ====================================================================
20 *
21 * This software consists of voluntary contributions made by many
22 * individuals on behalf of the Apache Software Foundation. For more
23 * information on the Apache Software Foundation, please see
24 * <http://www.apache.org/>.
25 *
26 */
27
28 package org.apache.http.impl.nio.reactor;
29
30 import java.net.SocketOptions;
31 import java.nio.channels.SelectionKey;
32
33 import org.apache.http.annotation.NotThreadSafe;
34
35 /**
36 * I/O reactor configuration parameters.
37 *
38 * @since 4.2
39 */
40 @NotThreadSafe
41 public final class IOReactorConfig implements Cloneable {
42
43 private static final int AVAIL_PROCS = Runtime.getRuntime().availableProcessors();
44
45 private long selectInterval;
46 private long shutdownGracePeriod;
47 private boolean interestOpQueued;
48 private int ioThreadCount;
49 private int soTimeout;
50 private boolean soReuseAddress;
51 private int soLinger;
52 private boolean soKeepAlive;
53 private boolean tcpNoDelay;
54 private int connectTimeout;
55 private int sndBufSize;
56 private int rcvBufSize;
57
58 public IOReactorConfig() {
59 super();
60 this.selectInterval = 1000;
61 this.shutdownGracePeriod = 500;
62 this.interestOpQueued = false;
63 this.ioThreadCount = AVAIL_PROCS;
64 this.soTimeout = 0;
65 this.soReuseAddress = false;
66 this.soLinger = -1;
67 this.soKeepAlive = false;
68 this.tcpNoDelay = true;
69 this.connectTimeout = 0;
70 }
71
72 /**
73 * Determines time interval in milliseconds at which the I/O reactor wakes up to check for
74 * timed out sessions and session requests.
75 * <p/>
76 * Default: <code>1000</code> milliseconds.
77 */
78 public long getSelectInterval() {
79 return this.selectInterval;
80 }
81
82 /**
83 * Defines time interval in milliseconds at which the I/O reactor wakes up to check for
84 * timed out sessions and session requests. May not be negative or zero.
85 */
86 public void setSelectInterval(long selectInterval) {
87 if (selectInterval <= 0) {
88 throw new IllegalArgumentException("Select internal may not be negative or zero");
89 }
90 this.selectInterval = selectInterval;
91 }
92
93 /**
94 * Determines grace period in milliseconds the I/O reactors are expected to block waiting
95 * for individual worker threads to terminate cleanly.
96 * <p/>
97 * Default: <code>500</code> milliseconds.
98 */
99 public long getShutdownGracePeriod() {
100 return this.shutdownGracePeriod;
101 }
102
103 /**
104 * Defines grace period in milliseconds the I/O reactors are expected to block waiting
105 * for individual worker threads to terminate cleanly. May not be negative or zero.
106 */
107 public void setShutdownGracePeriod(long gracePeriod) {
108 if (gracePeriod <= 0) {
109 throw new IllegalArgumentException("Shutdown grace period may not be negative or zero");
110 }
111 this.shutdownGracePeriod = gracePeriod;
112 }
113
114 /**
115 * Determines whether or not I/O interest operations are to be queued and executed
116 * asynchronously by the I/O reactor thread or to be applied to the underlying
117 * {@link SelectionKey} immediately.
118 * <p/>
119 * Default: <code>false</code>
120 *
121 * @see SelectionKey
122 * @see SelectionKey#interestOps()
123 * @see SelectionKey#interestOps(int)
124 */
125 public boolean isInterestOpQueued() {
126 return this.interestOpQueued;
127 }
128
129 /**
130 * Defines whether or not I/O interest operations are to be queued and executed
131 * asynchronously by the I/O reactor thread or to be applied to the underlying
132 * {@link SelectionKey} immediately.
133 *
134 * @see SelectionKey
135 * @see SelectionKey#interestOps()
136 * @see SelectionKey#interestOps(int)
137 */
138 public void setInterestOpQueued(boolean interestOpQueued) {
139 this.interestOpQueued = interestOpQueued;
140 }
141
142 /**
143 * Determines the number of I/O dispatch threads to be used by the I/O reactor.
144 * <p/>
145 * Default: <code>2</code>
146 */
147 public int getIoThreadCount() {
148 return this.ioThreadCount;
149 }
150
151 /**
152 * Defines the number of I/O dispatch threads to be used by the I/O reactor.
153 * May not be negative or zero.
154 */
155 public void setIoThreadCount(int ioThreadCount) {
156 if (ioThreadCount <= 0) {
157 throw new IllegalArgumentException("I/O thread count may not be negative or zero");
158 }
159 this.ioThreadCount = ioThreadCount;
160 }
161
162 /**
163 * Determines the default socket timeout value for non-blocking I/O operations.
164 * <p/>
165 * Default: <code>0</code> (no timeout)
166 *
167 * @see SocketOptions#SO_TIMEOUT
168 */
169 public int getSoTimeout() {
170 return soTimeout;
171 }
172
173 /**
174 * Defines the default socket timeout value for non-blocking I/O operations.
175 * <p/>
176 * Default: <code>0</code> (no timeout)
177 *
178 * @see SocketOptions#SO_TIMEOUT
179 */
180 public void setSoTimeout(int soTimeout) {
181 this.soTimeout = soTimeout;
182 }
183
184 /**
185 * Determines the default value of the {@link SocketOptions#SO_REUSEADDR} parameter
186 * for newly created sockets.
187 * <p/>
188 * Default: <code>false</code>
189 *
190 * @see SocketOptions#SO_REUSEADDR
191 */
192 public boolean isSoReuseAddress() {
193 return soReuseAddress;
194 }
195
196 /**
197 * Defines the default value of the {@link SocketOptions#SO_REUSEADDR} parameter
198 * for newly created sockets.
199 *
200 * @see SocketOptions#SO_REUSEADDR
201 */
202 public void setSoReuseAddress(boolean soReuseAddress) {
203 this.soReuseAddress = soReuseAddress;
204 }
205
206 /**
207 * Determines the default value of the {@link SocketOptions#SO_LINGER} parameter
208 * for newly created sockets.
209 * <p/>
210 * Default: <code>-1</code>
211 *
212 * @see SocketOptions#SO_LINGER
213 */
214 public int getSoLinger() {
215 return soLinger;
216 }
217
218 /**
219 * Defines the default value of the {@link SocketOptions#SO_LINGER} parameter
220 * for newly created sockets.
221 *
222 * @see SocketOptions#SO_LINGER
223 */
224 public void setSoLinger(int soLinger) {
225 this.soLinger = soLinger;
226 }
227
228 /**
229 * Determines the default value of the {@link SocketOptions#SO_KEEPALIVE} parameter
230 * for newly created sockets.
231 * <p/>
232 * Default: <code>-1</code>
233 *
234 * @see SocketOptions#SO_KEEPALIVE
235 */
236 public boolean isSoKeepalive() {
237 return this.soKeepAlive;
238 }
239
240 /**
241 * Defines the default value of the {@link SocketOptions#SO_KEEPALIVE} parameter
242 * for newly created sockets.
243 * <p/>
244 * Default: <code>-1</code>
245 *
246 * @see SocketOptions#SO_KEEPALIVE
247 */
248 public void setSoKeepalive(boolean soKeepAlive) {
249 this.soKeepAlive = soKeepAlive;
250 }
251
252 /**
253 * Determines the default value of the {@link SocketOptions#TCP_NODELAY} parameter
254 * for newly created sockets.
255 * <p/>
256 * Default: <code>false</code>
257 *
258 * @see SocketOptions#TCP_NODELAY
259 */
260 public boolean isTcpNoDelay() {
261 return tcpNoDelay;
262 }
263
264 /**
265 * Defines the default value of the {@link SocketOptions#TCP_NODELAY} parameter
266 * for newly created sockets.
267 *
268 * @see SocketOptions#TCP_NODELAY
269 */
270 public void setTcpNoDelay(boolean tcpNoDelay) {
271 this.tcpNoDelay = tcpNoDelay;
272 }
273
274 /**
275 * Determines the default connect timeout value for non-blocking connection requests.
276 * <p/>
277 * Default: <code>0</code> (no timeout)
278 */
279 public int getConnectTimeout() {
280 return connectTimeout;
281 }
282
283 /**
284 * Defines the default connect timeout value for non-blocking connection requests.
285 */
286 public void setConnectTimeout(int connectTimeout) {
287 this.connectTimeout = connectTimeout;
288 }
289
290 /**
291 * Determines the default value of the {@link SocketOptions#SO_SNDBUF} parameter
292 * for newly created sockets.
293 * <p/>
294 * Default: <code>0</code> (system default)
295 *
296 * @see SocketOptions#SO_SNDBUF
297 */
298 public int getSndBufSize() {
299 return sndBufSize;
300 }
301
302 /**
303 * Defines the default value of the {@link SocketOptions#SO_SNDBUF} parameter
304 * for newly created sockets.
305 *
306 * @see SocketOptions#SO_SNDBUF
307 */
308 public void setSndBufSize(int sndBufSize) {
309 this.sndBufSize = sndBufSize;
310 }
311
312 /**
313 * Determines the default value of the {@link SocketOptions#SO_RCVBUF} parameter
314 * for newly created sockets.
315 * <p/>
316 * Default: <code>0</code> (system default)
317 *
318 * @see SocketOptions#SO_RCVBUF
319 */
320 public int getRcvBufSize() {
321 return rcvBufSize;
322 }
323
324 /**
325 * Defines the default value of the {@link SocketOptions#SO_RCVBUF} parameter
326 * for newly created sockets.
327 *
328 * @see SocketOptions#SO_RCVBUF
329 */
330 public void setRcvBufSize(int rcvBufSize) {
331 this.rcvBufSize = rcvBufSize;
332 }
333
334 @Override
335 protected IOReactorConfig clone() throws CloneNotSupportedException {
336 return (IOReactorConfig) super.clone();
337 }
338
339 @Override
340 public String toString() {
341 StringBuilder builder = new StringBuilder();
342 builder.append("[selectInterval=").append(this.selectInterval)
343 .append(", shutdownGracePeriod=").append(this.shutdownGracePeriod)
344 .append(", interestOpQueued=").append(this.interestOpQueued)
345 .append(", ioThreadCount=").append(this.ioThreadCount)
346 .append(", soTimeout=").append(this.soTimeout)
347 .append(", soReuseAddress=").append(this.soReuseAddress)
348 .append(", soLinger=").append(this.soLinger)
349 .append(", soKeepAlive=").append(this.soKeepAlive)
350 .append(", tcpNoDelay=").append(this.tcpNoDelay)
351 .append(", connectTimeout=").append(this.connectTimeout)
352 .append(", sndBufSize=").append(this.sndBufSize)
353 .append(", rcvBufSize=").append(this.rcvBufSize)
354 .append("]");
355 return builder.toString();
356 }
357
358 }