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/24 15:02:12 UTC
[commons-net] 01/09: Add and use DatagramSocketClient#getSoTimeoutDuration().
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 3502a3b0834ced423894a48c7453f681ef47d4c4
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 24 10:40:17 2023 -0400
Add and use DatagramSocketClient#getSoTimeoutDuration().
Add and use DatagramSocketClient#setSoTimeout(Duration).
---
src/changes/changes.xml | 6 +++++
.../apache/commons/net/DatagramSocketClient.java | 30 ++++++++++++++++++++--
.../apache/commons/net/examples/unix/chargen.java | 3 ++-
.../org/apache/commons/net/examples/unix/echo.java | 3 ++-
.../java/org/apache/commons/net/tftp/TFTP.java | 6 ++---
5 files changed, 41 insertions(+), 7 deletions(-)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 3c828368..6a5fbe00 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -72,6 +72,12 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">
Add and use TFTP.DEFAULT_TIMEOUT_DURATION and deprecate org.apache.commons.net.tftp.TFTP.DEFAULT_TIMEOUT.
</action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">
+ Add and use DatagramSocketClient#getSoTimeoutDuration().
+ </action>
+ <action type="add" dev="ggregory" due-to="Gary Gregory">
+ Add and use DatagramSocketClient#setSoTimeout(Duration).
+ </action>
<!-- FIX -->
<action type="fix" issue="NET-650" dev="ggregory" due-to="Matthew McGillis, exceptionfactory, sebbASF">
Delegate host resolution to Socket.connect() #138.
diff --git a/src/main/java/org/apache/commons/net/DatagramSocketClient.java b/src/main/java/org/apache/commons/net/DatagramSocketClient.java
index 2280f3a7..ff78bea4 100644
--- a/src/main/java/org/apache/commons/net/DatagramSocketClient.java
+++ b/src/main/java/org/apache/commons/net/DatagramSocketClient.java
@@ -140,12 +140,25 @@ public abstract class DatagramSocketClient {
* thrown.
*
* @return The timeout in milliseconds of the currently opened socket.
- * @throws SocketException if an error getting the timeout
+ * @throws SocketException if an error getting the timeout.
+ * @deprecated Use {@link #getSoTimeoutDuration()}.
*/
+ @Deprecated
public int getSoTimeout() throws SocketException {
return _socket_.getSoTimeout();
}
+ /**
+ * Returns the timeout duration of the currently opened socket. If you call this method when the client socket is not open, a NullPointerException is
+ * thrown.
+ *
+ * @return The timeout in milliseconds of the currently opened socket.
+ * @throws SocketException if an error getting the timeout.
+ */
+ public Duration getSoTimeoutDuration() throws SocketException {
+ return Duration.ofMillis(_socket_.getSoTimeout());
+ }
+
/**
* Returns true if the client has a currently open socket.
*
@@ -248,12 +261,25 @@ public abstract class DatagramSocketClient {
_timeout_ = timeout;
}
+ /**
+ * Set the timeout duration of a currently open connection. Only call this method after a connection has been opened by {@link #open open()}.
+ *
+ * @param timeout The timeout in milliseconds to use for the currently open datagram socket connection.
+ * @throws SocketException if an error setting the timeout.
+ * @since 3.10.0
+ */
+ public void setSoTimeout(final Duration timeout) throws SocketException {
+ _socket_.setSoTimeout(Math.toIntExact(timeout.toMillis()));
+ }
+
/**
* Set the timeout in milliseconds of a currently open connection. Only call this method after a connection has been opened by {@link #open open()}.
*
* @param timeout The timeout in milliseconds to use for the currently open datagram socket connection.
- * @throws SocketException if an error setting the timeout
+ * @throws SocketException if an error setting the timeout.
+ * @deprecated Use {@link #setSoTimeout(Duration)}.
*/
+ @Deprecated
public void setSoTimeout(final int timeout) throws SocketException {
_socket_.setSoTimeout(timeout);
}
diff --git a/src/main/java/org/apache/commons/net/examples/unix/chargen.java b/src/main/java/org/apache/commons/net/examples/unix/chargen.java
index 326a7ef0..57482257 100644
--- a/src/main/java/org/apache/commons/net/examples/unix/chargen.java
+++ b/src/main/java/org/apache/commons/net/examples/unix/chargen.java
@@ -23,6 +23,7 @@ import java.io.InputStreamReader;
import java.io.InterruptedIOException;
import java.net.InetAddress;
import java.net.SocketException;
+import java.time.Duration;
import org.apache.commons.net.chargen.CharGenTCPClient;
import org.apache.commons.net.chargen.CharGenUDPClient;
@@ -71,7 +72,7 @@ public final class chargen {
client.open();
// If we don't receive a return packet within 5 seconds, assume
// the packet is lost.
- client.setSoTimeout(5000);
+ client.setSoTimeout(Duration.ofSeconds(5));
while (packets-- > 0) {
client.send(address);
diff --git a/src/main/java/org/apache/commons/net/examples/unix/echo.java b/src/main/java/org/apache/commons/net/examples/unix/echo.java
index b753179d..a61097b4 100644
--- a/src/main/java/org/apache/commons/net/examples/unix/echo.java
+++ b/src/main/java/org/apache/commons/net/examples/unix/echo.java
@@ -25,6 +25,7 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.SocketException;
+import java.time.Duration;
import org.apache.commons.net.echo.EchoTCPClient;
import org.apache.commons.net.echo.EchoUDPClient;
@@ -77,7 +78,7 @@ public final class echo {
client.open();
// If we don't receive an echo within 5 seconds, assume the packet is lost.
- client.setSoTimeout(5000);
+ client.setSoTimeout(Duration.ofSeconds(5));
System.out.println("Ready to echo to " + host + ".");
// Remember, there are no guarantees about the ordering of returned
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 fe187b65..afee0a0d 100644
--- a/src/main/java/org/apache/commons/net/tftp/TFTP.java
+++ b/src/main/java/org/apache/commons/net/tftp/TFTP.java
@@ -187,13 +187,13 @@ public class TFTP extends DatagramSocketClient {
* @throws IOException if an I/O error occurs.
*/
public final void discardPackets() throws IOException {
- final int to;
+ final Duration to;
final DatagramPacket datagram;
datagram = new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE);
- to = getSoTimeout();
- setSoTimeout(1);
+ to = getSoTimeoutDuration();
+ setSoTimeout(Duration.ofMillis(1));
try {
while (true) {