You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2023/06/23 12:34:25 UTC

[commons-net] 01/06: Add and use Duration-based timeout method instead of int

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-net.git

commit 4db070999522382fd9939d9dc2ff90bb1d400c2d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Fri Jun 23 08:23:08 2023 -0400

    Add and use Duration-based timeout method instead of int
    
    - Add and use DatagramSocketClient.setDefaultTimeout(Duration) and
    deprecate DatagramSocketClient.setDefaultTimeout(int)
    - Add and use TFTP.DEFAULT_TIMEOUT_DURATION and deprecate
    org.apache.commons.net.tftp.TFTP.DEFAULT_TIMEOUT
---
 .../org/apache/commons/net/DatagramSocketClient.java    | 14 ++++++++++++++
 .../apache/commons/net/examples/ftp/TFTPExample.java    |  3 ++-
 .../org/apache/commons/net/examples/ntp/NTPClient.java  |  3 ++-
 .../org/apache/commons/net/examples/ntp/TimeClient.java |  3 ++-
 .../org/apache/commons/net/examples/unix/daytime.java   |  3 ++-
 .../org/apache/commons/net/examples/unix/rdate.java     |  3 ++-
 src/main/java/org/apache/commons/net/tftp/TFTP.java     | 17 ++++++++++++++---
 .../java/org/apache/commons/net/ntp/TestNtpClient.java  |  3 ++-
 .../java/org/apache/commons/net/tftp/TFTPServer.java    |  3 ++-
 9 files changed, 42 insertions(+), 10 deletions(-)

diff --git a/src/main/java/org/apache/commons/net/DatagramSocketClient.java b/src/main/java/org/apache/commons/net/DatagramSocketClient.java
index 9f906f9b..bbb9db5b 100644
--- a/src/main/java/org/apache/commons/net/DatagramSocketClient.java
+++ b/src/main/java/org/apache/commons/net/DatagramSocketClient.java
@@ -21,6 +21,7 @@ import java.net.DatagramSocket;
 import java.net.InetAddress;
 import java.net.SocketException;
 import java.nio.charset.Charset;
+import java.time.Duration;
 
 /**
  * The DatagramSocketClient provides the basic operations that are required of client objects accessing datagram sockets. It is meant to be subclassed to avoid
@@ -223,13 +224,26 @@ public abstract class DatagramSocketClient {
         }
     }
 
+    /**
+     * Set the default timeout in to use when opening a socket. After a call to open, the timeout for the socket is set using this value. This
+     * method should be used prior to a call to {@link #open open()} and should not be confused with {@link #setSoTimeout setSoTimeout()} which operates on the
+     * currently open socket. _timeout_ contains the new timeout value.
+     *
+     * @param timeout The timeout in milliseconds to use for the datagram socket connection.
+     */
+    public void setDefaultTimeout(final Duration timeout) {
+        _timeout_ = Math.toIntExact(timeout.toMillis());
+    }
+
     /**
      * Set the default timeout in milliseconds to use when opening a socket. After a call to open, the timeout for the socket is set using this value. This
      * method should be used prior to a call to {@link #open open()} and should not be confused with {@link #setSoTimeout setSoTimeout()} which operates on the
      * currently open socket. _timeout_ contains the new timeout value.
      *
      * @param timeout The timeout in milliseconds to use for the datagram socket connection.
+     * @deprecated Use {@link #setDefaultTimeout(Duration)}.
      */
+    @Deprecated
     public void setDefaultTimeout(final int timeout) {
         _timeout_ = timeout;
     }
diff --git a/src/main/java/org/apache/commons/net/examples/ftp/TFTPExample.java b/src/main/java/org/apache/commons/net/examples/ftp/TFTPExample.java
index fefbb5a5..19b3424b 100644
--- a/src/main/java/org/apache/commons/net/examples/ftp/TFTPExample.java
+++ b/src/main/java/org/apache/commons/net/examples/ftp/TFTPExample.java
@@ -24,6 +24,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.net.SocketException;
 import java.net.UnknownHostException;
+import java.time.Duration;
 
 import org.apache.commons.net.tftp.TFTP;
 import org.apache.commons.net.tftp.TFTPClient;
@@ -121,7 +122,7 @@ public final class TFTPExample {
         }
 
         // We want to timeout if a response takes longer than 60 seconds
-        tftp.setDefaultTimeout(timeout);
+        tftp.setDefaultTimeout(Duration.ofSeconds(timeout));
 
         // We haven't closed the local file yet.
         closed = false;
diff --git a/src/main/java/org/apache/commons/net/examples/ntp/NTPClient.java b/src/main/java/org/apache/commons/net/examples/ntp/NTPClient.java
index 562e05d1..9ba0c853 100644
--- a/src/main/java/org/apache/commons/net/examples/ntp/NTPClient.java
+++ b/src/main/java/org/apache/commons/net/examples/ntp/NTPClient.java
@@ -22,6 +22,7 @@ import java.net.InetAddress;
 import java.net.SocketException;
 import java.net.UnknownHostException;
 import java.text.NumberFormat;
+import java.time.Duration;
 
 import org.apache.commons.net.ntp.NTPUDPClient;
 import org.apache.commons.net.ntp.NtpUtils;
@@ -52,7 +53,7 @@ public final class NTPClient {
 
         final NTPUDPClient client = new NTPUDPClient();
         // We want to timeout if a response takes longer than 10 seconds
-        client.setDefaultTimeout(10000);
+        client.setDefaultTimeout(Duration.ofSeconds(10));
         try {
             client.open();
             for (final String arg : args) {
diff --git a/src/main/java/org/apache/commons/net/examples/ntp/TimeClient.java b/src/main/java/org/apache/commons/net/examples/ntp/TimeClient.java
index 3d5619d4..2dbc8a16 100644
--- a/src/main/java/org/apache/commons/net/examples/ntp/TimeClient.java
+++ b/src/main/java/org/apache/commons/net/examples/ntp/TimeClient.java
@@ -19,6 +19,7 @@ package org.apache.commons.net.examples.ntp;
 
 import java.io.IOException;
 import java.net.InetAddress;
+import java.time.Duration;
 
 import org.apache.commons.net.time.TimeTCPClient;
 import org.apache.commons.net.time.TimeUDPClient;
@@ -72,7 +73,7 @@ public final class TimeClient {
         final TimeUDPClient client = new TimeUDPClient();
 
         // We want to timeout if a response takes longer than 60 seconds
-        client.setDefaultTimeout(60000);
+        client.setDefaultTimeout(Duration.ofSeconds(60));
         client.open();
         System.out.println(client.getDate(InetAddress.getByName(host)));
         client.close();
diff --git a/src/main/java/org/apache/commons/net/examples/unix/daytime.java b/src/main/java/org/apache/commons/net/examples/unix/daytime.java
index ecb11ea0..a3c460bf 100644
--- a/src/main/java/org/apache/commons/net/examples/unix/daytime.java
+++ b/src/main/java/org/apache/commons/net/examples/unix/daytime.java
@@ -19,6 +19,7 @@ package org.apache.commons.net.examples.unix;
 
 import java.io.IOException;
 import java.net.InetAddress;
+import java.time.Duration;
 
 import org.apache.commons.net.daytime.DaytimeTCPClient;
 import org.apache.commons.net.daytime.DaytimeUDPClient;
@@ -45,7 +46,7 @@ public final class daytime {
         final DaytimeUDPClient client = new DaytimeUDPClient();
 
         // We want to timeout if a response takes longer than 60 seconds
-        client.setDefaultTimeout(60000);
+        client.setDefaultTimeout(Duration.ofSeconds(60));
         client.open();
         System.out.println(client.getTime(InetAddress.getByName(host)).trim());
         client.close();
diff --git a/src/main/java/org/apache/commons/net/examples/unix/rdate.java b/src/main/java/org/apache/commons/net/examples/unix/rdate.java
index ed39c54e..a1ebd409 100644
--- a/src/main/java/org/apache/commons/net/examples/unix/rdate.java
+++ b/src/main/java/org/apache/commons/net/examples/unix/rdate.java
@@ -19,6 +19,7 @@ package org.apache.commons.net.examples.unix;
 
 import java.io.IOException;
 import java.net.InetAddress;
+import java.time.Duration;
 
 import org.apache.commons.net.time.TimeTCPClient;
 import org.apache.commons.net.time.TimeUDPClient;
@@ -69,7 +70,7 @@ public final class rdate {
         final TimeUDPClient client = new TimeUDPClient();
 
         // We want to timeout if a response takes longer than 60 seconds
-        client.setDefaultTimeout(60000);
+        client.setDefaultTimeout(Duration.ofSeconds(60));
         client.open();
         System.out.println(client.getDate(InetAddress.getByName(host)).toString());
         client.close();
diff --git a/src/main/java/org/apache/commons/net/tftp/TFTP.java b/src/main/java/org/apache/commons/net/tftp/TFTP.java
index 8330c334..fe187b65 100644
--- a/src/main/java/org/apache/commons/net/tftp/TFTP.java
+++ b/src/main/java/org/apache/commons/net/tftp/TFTP.java
@@ -21,6 +21,7 @@ import java.io.IOException;
 import java.io.InterruptedIOException;
 import java.net.DatagramPacket;
 import java.net.SocketException;
+import java.time.Duration;
 
 import org.apache.commons.net.DatagramSocketClient;
 
@@ -67,10 +68,20 @@ public class TFTP extends DatagramSocketClient {
     public static final int OCTET_MODE = 1;
 
     /**
-     * The default number of milliseconds to wait to receive a datagram before timing out. The default is 5000 milliseconds (5 seconds).
+     * The default number of milliseconds to wait to receive a datagram before timing out. The default is 5,000 milliseconds (5 seconds).
+     *
+     * @deprecated Use {@link #DEFAULT_TIMEOUT_DURATION}.
      */
+    @Deprecated
     public static final int DEFAULT_TIMEOUT = 5000;
 
+    /**
+     * The default duration to wait to receive a datagram before timing out. The default is 5 seconds.
+     *
+     * @since 3.10.0
+     */
+    public static final Duration DEFAULT_TIMEOUT_DURATION = Duration.ofSeconds(5);
+
     /**
      * The default TFTP port according to RFC 783 is 69.
      */
@@ -107,10 +118,10 @@ public class TFTP extends DatagramSocketClient {
     byte[] sendBuffer;
 
     /**
-     * Creates a TFTP instance with a default timeout of DEFAULT_TIMEOUT, a null socket, and buffered operations disabled.
+     * Creates a TFTP instance with a default timeout of {@link #DEFAULT_TIMEOUT_DURATION}, a null socket, and buffered operations disabled.
      */
     public TFTP() {
-        setDefaultTimeout(DEFAULT_TIMEOUT);
+        setDefaultTimeout(DEFAULT_TIMEOUT_DURATION);
         receiveBuffer = null;
         receiveDatagram = null;
     }
diff --git a/src/test/java/org/apache/commons/net/ntp/TestNtpClient.java b/src/test/java/org/apache/commons/net/ntp/TestNtpClient.java
index af894ca4..d7cc2371 100644
--- a/src/test/java/org/apache/commons/net/ntp/TestNtpClient.java
+++ b/src/test/java/org/apache/commons/net/ntp/TestNtpClient.java
@@ -18,6 +18,7 @@ package org.apache.commons.net.ntp;
 
 import java.io.IOException;
 import java.net.InetAddress;
+import java.time.Duration;
 
 import org.apache.commons.net.examples.ntp.SimpleNTPServer;
 import org.junit.AfterClass;
@@ -75,7 +76,7 @@ public class TestNtpClient {
         final long currentTimeMillis = System.currentTimeMillis();
         final NTPUDPClient client = new NTPUDPClient();
         // timeout if response takes longer than 2 seconds
-        client.setDefaultTimeout(2000);
+        client.setDefaultTimeout(Duration.ofSeconds(2));
         try {
             // Java 1.7: use InetAddress.getLoopbackAddress() instead
             final InetAddress addr = InetAddress.getByAddress("loopback", new byte[] { 127, 0, 0, 1 });
diff --git a/src/test/java/org/apache/commons/net/tftp/TFTPServer.java b/src/test/java/org/apache/commons/net/tftp/TFTPServer.java
index d45b54f7..a4baf855 100644
--- a/src/test/java/org/apache/commons/net/tftp/TFTPServer.java
+++ b/src/test/java/org/apache/commons/net/tftp/TFTPServer.java
@@ -30,6 +30,7 @@ import java.io.PrintStream;
 import java.net.InetAddress;
 import java.net.NetworkInterface;
 import java.net.SocketTimeoutException;
+import java.time.Duration;
 import java.util.Enumeration;
 import java.util.HashSet;
 
@@ -643,7 +644,7 @@ public class TFTPServer implements Runnable {
         socketTimeout = serverTftp.getDefaultTimeout();
 
         // we want the server thread to listen forever.
-        serverTftp.setDefaultTimeout(0);
+        serverTftp.setDefaultTimeout(Duration.ZERO);
 
         if (laddr != null) {
             serverTftp.open(port, laddr);