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