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