Hi,
could you test whether Socket.setSoTimeout() really sets the send and receive
timeout?


cya
Robert

Sven de Marothy wrote:
> This adds the 1.5 timeout methods in URLConnection and implements them
> for HttUrlConnection (and HttpsUrlConnection). 
> 
> It also fixes a long-standing bug in the native-target-layer aicas
> thingie. SO_TIMEOUT timeout was being used as a socket option, when it's
> the name of the Java socket option. The POSIX constants are SO_SNDTIMEO
> and SO_RCVTIMEO. So timeouts actually work now. 
> (I could say something nasty here about preprocessor macros.. )
> 
> /Sven
> 
> 2006-05-12  Sven de Marothy  <[EMAIL PROTECTED]>
> 
>       * gnu/java/net/protocol/http/HTTPConnection.java (get): Add timeout
> parameter.
>       * gnu/java/net/protocol/http/HTTPURLConnection.java
>       (setConnectTimeout): New method.
>       (getConnection): Add timeout parameter.
>       *  java/net/URLConnection.java  
>       (getConnectTimeout, setConnectTimeout): Implement.
>       * native/target/generic/target_generic_network.h: 
>       Set correct socket parameters SO_SNDTIMEO and SO_RCVTIMEO.
>       
> 
> 
> 
> ------------------------------------------------------------------------
> 
> Index: gnu/java/net/protocol/http/HTTPConnection.java
> ===================================================================
> RCS file: 
> /sources/classpath/classpath/gnu/java/net/protocol/http/HTTPConnection.java,v
> retrieving revision 1.13
> diff -U3 -r1.13 HTTPConnection.java
> --- gnu/java/net/protocol/http/HTTPConnection.java    4 Mar 2006 20:54:01 
> -0000       1.13
> +++ gnu/java/net/protocol/http/HTTPConnection.java    12 May 2006 20:53:22 
> -0000
> @@ -466,7 +466,8 @@
>       */
>      synchronized HTTPConnection get(String host,
>                                      int port,
> -                                    boolean secure)
> +                                    boolean secure, 
> +                                 int connectionTimeout, int timeout)
>      {
>        String ttl =
>          SystemProperties.getProperty("classpath.net.http.keepAliveTTL");
> @@ -494,7 +495,7 @@
>          }
>        if (c == null)
>          {
> -          c = new HTTPConnection(host, port, secure);
> +          c = new HTTPConnection(host, port, secure, connectionTimeout, 
> timeout);
>            c.setPool(this);
>          }
>        return c;
> Index: gnu/java/net/protocol/http/HTTPURLConnection.java
> ===================================================================
> RCS file: 
> /sources/classpath/classpath/gnu/java/net/protocol/http/HTTPURLConnection.java,v
> retrieving revision 1.25
> diff -U3 -r1.25 HTTPURLConnection.java
> --- gnu/java/net/protocol/http/HTTPURLConnection.java 21 Apr 2006 17:28:24 
> -0000      1.25
> +++ gnu/java/net/protocol/http/HTTPURLConnection.java 12 May 2006 20:53:22 
> -0000
> @@ -65,7 +65,7 @@
>   * @author Chris Burdess ([EMAIL PROTECTED])
>   */
>  public class HTTPURLConnection
> -  extends HttpsURLConnection
> + extends HttpsURLConnection
>    implements HandshakeCompletedListener
>  {
>    /*
> @@ -346,11 +346,11 @@
>      HTTPConnection connection;
>      if (keepAlive)
>        {
> -        connection = HTTPConnection.Pool.instance.get(host, port, secure);
> +        connection = HTTPConnection.Pool.instance.get(host, port, secure, 
> getConnectTimeout(), 0);
>        }
>      else
>        {
> -        connection = new HTTPConnection(host, port, secure);
> +        connection = new HTTPConnection(host, port, secure, 0, 
> getConnectTimeout());
>        }
>      return connection;
>    }
> @@ -653,5 +653,27 @@
>      handshakeEvent = event;
>    }
>  
> +  /**
> +   * Set the connection timeout speed, in milliseconds, or zero if the 
> timeout
> +   * is to be considered infinite.
> +   *
> +   * Overloaded.
> +   *
> +   */
> +  public void setConnectTimeout(int timeout)
> +    throws IllegalArgumentException
> +  {
> +    super.setConnectTimeout( timeout );
> +    if( connection == null )
> +      return;
> +    try 
> +      {
> +     connection.getSocket().setSoTimeout( timeout );
> +      } 
> +    catch(IOException se)
> +      {
> +     // Ignore socket exceptions.
> +      }
> +  }
>  }
>  
> Index: java/net/URLConnection.java
> ===================================================================
> RCS file: /sources/classpath/classpath/java/net/URLConnection.java,v
> retrieving revision 1.41
> diff -U3 -r1.41 URLConnection.java
> --- java/net/URLConnection.java       7 Apr 2006 19:45:46 -0000       1.41
> +++ java/net/URLConnection.java       12 May 2006 20:53:24 -0000
> @@ -173,6 +173,11 @@
>  
>    private static SimpleDateFormat[] dateFormats;
>    private static boolean dateformats_initialized;
> +  
> +  /**
> +   * The timeout period.
> +   */
> +  private int timeout;
>  
>    /* Cached ParsePosition, used when parsing dates. */
>    private ParsePosition position;
> @@ -212,6 +217,38 @@
>    }
>  
>    /**
> +   * Returns the connection timeout speed, in milliseconds, or zero if the 
> timeout
> +   * is infinite or not set.
> +   *
> +   * @return The timeout.
> +   *
> +   * @since 1.5
> +   */
> +  public int getConnectTimeout()
> +  {
> +    return timeout;
> +  }
> +
> +  /**
> +   * Set the connection timeout speed, in milliseconds, or zero if the 
> timeout
> +   * is to be considered infinite. Note that in certain socket 
> +   * implementations/platforms this method may not have any effect.
> +   *
> +   * Throws an <code>IllegalArgumentException</code> if timeout < 0.
> +   *
> +   * @param timeout - The timeout, in milliseconds.
> +   *
> +   * @since 1.5
> +   */
> +  public void setConnectTimeout(int timeout)
> +    throws IllegalArgumentException
> +  {
> +    if( timeout < 0 )
> +      throw new IllegalArgumentException("Timeout must be 0 or positive.");
> +    this.timeout = timeout;
> +  }
> +
> +  /**
>     * Returns the value of the content-length header field or -1 if the value
>     * is not known or not present.
>     *
> Index: native/target/generic/target_generic_network.h
> ===================================================================
> RCS file: 
> /sources/classpath/classpath/native/target/generic/target_generic_network.h,v
> retrieving revision 1.21
> diff -U3 -r1.21 target_generic_network.h
> --- native/target/generic/target_generic_network.h    25 Jan 2006 10:40:13 
> -0000      1.21
> +++ native/target/generic/target_generic_network.h    12 May 2006 20:53:25 
> -0000
> @@ -682,7 +682,8 @@
>        \
>        __value.tv_sec = flag / 1000; \
>        __value.tv_usec = (flag % 1000) * 1000; \
> -      
> result=(setsockopt(socketDescriptor,SOL_SOCKET,SO_TIMEOUT,&__value,sizeof(__value))==0)?TARGET_NATIVE_OK:TARGET_NATIVE_ERROR;
>  \
> +      result = ( setsockopt(socketDescriptor, SOL_SOCKET, SO_SNDTIMEO, 
> &__value, sizeof(__value)) | \
> +             setsockopt(socketDescriptor, SOL_SOCKET, SO_RCVTIMEO, &__value, 
> sizeof(__value)) == 0) ? TARGET_NATIVE_OK : TARGET_NATIVE_ERROR; \
>      } while (0)
>  #endif
>  

Attachment: signature.asc
Description: OpenPGP digital signature

Reply via email to