View Javadoc
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.config;
29  
30  import org.apache.http.annotation.ThreadingBehavior;
31  import org.apache.http.annotation.Contract;
32  import org.apache.http.util.Args;
33  
34  /**
35   * Socket configuration.
36   *
37   * @since 4.3
38   */
39  @Contract(threading = ThreadingBehavior.IMMUTABLE)
40  public class SocketConfig implements Cloneable {
41  
42      public static final SocketConfig DEFAULT = new Builder().build();
43  
44      private final int soTimeout;
45      private final boolean soReuseAddress;
46      private final int soLinger;
47      private final boolean soKeepAlive;
48      private final boolean tcpNoDelay;
49      private final int sndBufSize;
50      private final int rcvBufSize;
51      private final int backlogSize;
52  
53      SocketConfig(
54              final int soTimeout,
55              final boolean soReuseAddress,
56              final int soLinger,
57              final boolean soKeepAlive,
58              final boolean tcpNoDelay,
59              final int sndBufSize,
60              final int rcvBufSize,
61              final int backlogSize) {
62          super();
63          this.soTimeout = soTimeout;
64          this.soReuseAddress = soReuseAddress;
65          this.soLinger = soLinger;
66          this.soKeepAlive = soKeepAlive;
67          this.tcpNoDelay = tcpNoDelay;
68          this.sndBufSize = sndBufSize;
69          this.rcvBufSize = rcvBufSize;
70          this.backlogSize = backlogSize;
71      }
72  
73      /**
74       * Determines the default socket timeout value for non-blocking I/O operations.
75       * <p>
76       * Default: {@code 0} (no timeout)
77       * </p>
78       *
79       * @return the default socket timeout value for non-blocking I/O operations.
80       * @see java.net.SocketOptions#SO_TIMEOUT
81       */
82      public int getSoTimeout() {
83          return soTimeout;
84      }
85  
86      /**
87       * Determines the default value of the {@link java.net.SocketOptions#SO_REUSEADDR} parameter
88       * for newly created sockets.
89       * <p>
90       * Default: {@code false}
91       * </p>
92       *
93       * @return the default value of the {@link java.net.SocketOptions#SO_REUSEADDR} parameter.
94       * @see java.net.SocketOptions#SO_REUSEADDR
95       */
96      public boolean isSoReuseAddress() {
97          return soReuseAddress;
98      }
99  
100     /**
101      * Determines the default value of the {@link java.net.SocketOptions#SO_LINGER} parameter
102      * for newly created sockets.
103      * <p>
104      * Default: {@code -1}
105      * </p>
106      *
107      * @return the default value of the {@link java.net.SocketOptions#SO_LINGER} parameter.
108      * @see java.net.SocketOptions#SO_LINGER
109      */
110     public int getSoLinger() {
111         return soLinger;
112     }
113 
114     /**
115      * Determines the default value of the {@link java.net.SocketOptions#SO_KEEPALIVE} parameter
116      * for newly created sockets.
117      * <p>
118      * Default: {@code false}
119      * </p>
120      *
121      * @return the default value of the {@link java.net.SocketOptions#SO_KEEPALIVE} parameter.
122      * @see java.net.SocketOptions#SO_KEEPALIVE
123      */
124     public boolean isSoKeepAlive() {
125         return soKeepAlive;
126     }
127 
128     /**
129      * Determines the default value of the {@link java.net.SocketOptions#TCP_NODELAY} parameter
130      * for newly created sockets.
131      * <p>
132      * Default: {@code true}
133      * </p>
134      *
135      * @return the default value of the {@link java.net.SocketOptions#TCP_NODELAY} parameter.
136      * @see java.net.SocketOptions#TCP_NODELAY
137      */
138     public boolean isTcpNoDelay() {
139         return tcpNoDelay;
140     }
141 
142     /**
143      * Determines the default value of the {@link java.net.SocketOptions#SO_SNDBUF} parameter
144      * for newly created sockets.
145      * <p>
146      * Default: {@code 0} (system default)
147      * </p>
148      *
149      * @return the default value of the {@link java.net.SocketOptions#SO_SNDBUF} parameter.
150      * @see java.net.SocketOptions#SO_SNDBUF
151      * @since 4.4
152      */
153     public int getSndBufSize() {
154         return sndBufSize;
155     }
156 
157     /**
158      * Determines the default value of the {@link java.net.SocketOptions#SO_RCVBUF} parameter
159      * for newly created sockets.
160      * <p>
161      * Default: {@code 0} (system default)
162      * </p>
163      *
164      * @return the default value of the {@link java.net.SocketOptions#SO_RCVBUF} parameter.
165      * @see java.net.SocketOptions#SO_RCVBUF
166      * @since 4.4
167      */
168     public int getRcvBufSize() {
169         return rcvBufSize;
170     }
171 
172     /**
173      * Determines the maximum queue length for incoming connection indications
174      * (a request to connect) also known as server socket backlog.
175      * <p>
176      * Default: {@code 0} (system default)
177      * </p>
178      * @return the maximum queue length for incoming connection indications
179      * @since 4.4
180      */
181     public int getBacklogSize() {
182         return backlogSize;
183     }
184 
185     @Override
186     protected SocketConfig clone() throws CloneNotSupportedException {
187         return (SocketConfig) super.clone();
188     }
189 
190     @Override
191     public String toString() {
192         final StringBuilder builder = new StringBuilder();
193         builder.append("[soTimeout=").append(this.soTimeout)
194                 .append(", soReuseAddress=").append(this.soReuseAddress)
195                 .append(", soLinger=").append(this.soLinger)
196                 .append(", soKeepAlive=").append(this.soKeepAlive)
197                 .append(", tcpNoDelay=").append(this.tcpNoDelay)
198                 .append(", sndBufSize=").append(this.sndBufSize)
199                 .append(", rcvBufSize=").append(this.rcvBufSize)
200                 .append(", backlogSize=").append(this.backlogSize)
201                 .append("]");
202         return builder.toString();
203     }
204 
205     public static SocketConfig.Builder custom() {
206         return new Builder();
207     }
208 
209     public static SocketConfig.Builder copy(final SocketConfig config) {
210         Args.notNull(config, "Socket config");
211         return new Builder()
212             .setSoTimeout(config.getSoTimeout())
213             .setSoReuseAddress(config.isSoReuseAddress())
214             .setSoLinger(config.getSoLinger())
215             .setSoKeepAlive(config.isSoKeepAlive())
216             .setTcpNoDelay(config.isTcpNoDelay())
217             .setSndBufSize(config.getSndBufSize())
218             .setRcvBufSize(config.getRcvBufSize())
219             .setBacklogSize(config.getBacklogSize());
220     }
221 
222     public static class Builder {
223 
224         private int soTimeout;
225         private boolean soReuseAddress;
226         private int soLinger;
227         private boolean soKeepAlive;
228         private boolean tcpNoDelay;
229         private int sndBufSize;
230         private int rcvBufSize;
231         private int backlogSize;
232 
233         Builder() {
234             this.soLinger = -1;
235             this.tcpNoDelay = true;
236         }
237 
238         public Builder setSoTimeout(final int soTimeout) {
239             this.soTimeout = soTimeout;
240             return this;
241         }
242 
243         public Builder setSoReuseAddress(final boolean soReuseAddress) {
244             this.soReuseAddress = soReuseAddress;
245             return this;
246         }
247 
248         public Builder setSoLinger(final int soLinger) {
249             this.soLinger = soLinger;
250             return this;
251         }
252 
253         public Builder setSoKeepAlive(final boolean soKeepAlive) {
254             this.soKeepAlive = soKeepAlive;
255             return this;
256         }
257 
258         public Builder setTcpNoDelay(final boolean tcpNoDelay) {
259             this.tcpNoDelay = tcpNoDelay;
260             return this;
261         }
262 
263         /**
264          * @since 4.4
265          */
266         public Builder setSndBufSize(final int sndBufSize) {
267             this.sndBufSize = sndBufSize;
268             return this;
269         }
270 
271         /**
272          * @since 4.4
273          */
274         public Builder setRcvBufSize(final int rcvBufSize) {
275             this.rcvBufSize = rcvBufSize;
276             return this;
277         }
278 
279         /**
280          * @since 4.4
281          */
282         public Builder setBacklogSize(final int backlogSize) {
283             this.backlogSize = backlogSize;
284             return this;
285         }
286 
287         public SocketConfig build() {
288             return new SocketConfig(soTimeout, soReuseAddress, soLinger, soKeepAlive, tcpNoDelay,
289                     sndBufSize, rcvBufSize, backlogSize);
290         }
291 
292     }
293 
294 }