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 23:10:33 UTC

[commons-net] 01/02: Add TelnetClient.sendAYT(Duration).

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 8198c661273df6dbaf17fd2b9243be0b850f79e9
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Jun 24 19:09:51 2023 -0400

    Add TelnetClient.sendAYT(Duration).
---
 src/changes/changes.xml                            |  3 +++
 .../net/examples/telnet/TelnetClientExample.java   |  3 ++-
 .../java/org/apache/commons/net/telnet/Telnet.java |  7 ++++---
 .../apache/commons/net/telnet/TelnetClient.java    | 22 +++++++++++++++++++++-
 .../commons/net/telnet/TelnetClientTest.java       |  5 +++--
 5 files changed, 33 insertions(+), 7 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index ae0b31c5..38cd32b1 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -81,6 +81,9 @@ The <action> type attribute can be add,update,fix,remove.
       <action type="add" dev="ggregory" due-to="Gary Gregory">
         Add and use DatagramSocketClient.checkOpen().
       </action>
+      <action type="add" dev="ggregory" due-to="Gary Gregory">
+        Add TelnetClient.sendAYT(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/examples/telnet/TelnetClientExample.java b/src/main/java/org/apache/commons/net/examples/telnet/TelnetClientExample.java
index 05dcfc4a..1bfda6ea 100644
--- a/src/main/java/org/apache/commons/net/examples/telnet/TelnetClientExample.java
+++ b/src/main/java/org/apache/commons/net/examples/telnet/TelnetClientExample.java
@@ -21,6 +21,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.time.Duration;
 import java.util.StringTokenizer;
 
 import org.apache.commons.net.telnet.EchoOptionHandler;
@@ -116,7 +117,7 @@ public class TelnetClientExample implements Runnable, TelnetNotificationHandler
                                 try {
                                     System.out.println("Sending AYT");
 
-                                    System.out.println("AYT response:" + tc.sendAYT(5000));
+                                    System.out.println("AYT response:" + tc.sendAYT(Duration.ofSeconds(5)));
                                 } catch (final IOException e) {
                                     System.err.println("Exception waiting AYT response: " + e.getMessage());
                                 }
diff --git a/src/main/java/org/apache/commons/net/telnet/Telnet.java b/src/main/java/org/apache/commons/net/telnet/Telnet.java
index 0d926def..c198ff4c 100644
--- a/src/main/java/org/apache/commons/net/telnet/Telnet.java
+++ b/src/main/java/org/apache/commons/net/telnet/Telnet.java
@@ -21,6 +21,7 @@ import java.io.BufferedInputStream;
 import java.io.BufferedOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
+import java.time.Duration;
 import java.util.Arrays;
 
 import org.apache.commons.net.SocketClient;
@@ -195,13 +196,13 @@ class Telnet extends SocketClient {
     /**
      * Sends an {@code Are You There (AYT)} sequence and waits for the result.
      *
-     * @param timeout - Time to wait for a response (millis.)
+     * @param timeout - Time to wait for a response.
      * @throws IOException              - Exception in I/O.
      * @throws IllegalArgumentException - Illegal argument
      * @throws InterruptedException     - Interrupted during wait.
      * @return true if AYT received a response, false otherwise
      **/
-    final boolean _sendAYT(final long timeout) throws IOException, IllegalArgumentException, InterruptedException {
+    final boolean _sendAYT(final Duration timeout) throws IOException, IllegalArgumentException, InterruptedException {
         boolean retValue = false;
         synchronized (aytMonitor) {
             synchronized (this) {
@@ -209,7 +210,7 @@ class Telnet extends SocketClient {
                 _output_.write(COMMAND_AYT);
                 _output_.flush();
             }
-            aytMonitor.wait(timeout);
+            aytMonitor.wait(timeout.toMillis());
             if (!aytFlag) {
                 aytFlag = true;
             } else {
diff --git a/src/main/java/org/apache/commons/net/telnet/TelnetClient.java b/src/main/java/org/apache/commons/net/telnet/TelnetClient.java
index d8366010..b402fbab 100644
--- a/src/main/java/org/apache/commons/net/telnet/TelnetClient.java
+++ b/src/main/java/org/apache/commons/net/telnet/TelnetClient.java
@@ -21,6 +21,7 @@ import java.io.BufferedInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
+import java.time.Duration;
 
 /**
  * The TelnetClient class implements the simple network virtual terminal (NVT) for the Telnet protocol according to RFC 854. It does not implement any of the
@@ -280,6 +281,22 @@ public class TelnetClient extends Telnet {
         super._registerSpyStream(spystream);
     }
 
+    /**
+     * Sends an {@code Are You There (AYT)} sequence and waits for the result.
+     *
+     * @param timeout - Time to wait for a response.
+     *
+     * @return true if AYT received a response, false otherwise.
+     *
+     * @throws InterruptedException     on error
+     * @throws IllegalArgumentException on error
+     * @throws IOException              on error
+     * @since 3.10.0
+     */
+    public boolean sendAYT(final Duration timeout) throws IOException, IllegalArgumentException, InterruptedException {
+        return _sendAYT(timeout);
+    }
+
     /**
      * Sends an {@code Are You There (AYT)} sequence and waits for the result.
      *
@@ -290,10 +307,13 @@ public class TelnetClient extends Telnet {
      * @throws InterruptedException     on error
      * @throws IllegalArgumentException on error
      * @throws IOException              on error
+     * @deprecated Use {@link #sendAYT(Duration)}.
      */
+    @Deprecated
     public boolean sendAYT(final long timeout) throws IOException, IllegalArgumentException, InterruptedException {
-        return _sendAYT(timeout);
+        return _sendAYT(Duration.ofMillis(timeout));
     }
+
     /* Code Section added for supporting AYT (start) */
 
     /**
diff --git a/src/test/java/org/apache/commons/net/telnet/TelnetClientTest.java b/src/test/java/org/apache/commons/net/telnet/TelnetClientTest.java
index 298076e8..f7ff71a2 100644
--- a/src/test/java/org/apache/commons/net/telnet/TelnetClientTest.java
+++ b/src/test/java/org/apache/commons/net/telnet/TelnetClientTest.java
@@ -21,6 +21,7 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.io.PipedInputStream;
 import java.io.PipedOutputStream;
+import java.time.Duration;
 
 import junit.framework.TestCase;
 
@@ -233,7 +234,7 @@ public class TelnetClientTest extends TestCase implements TelnetNotificationHand
         final InputStream is = ANSI.server.getInputStream();
         final TelnetTestResponder tr = new TelnetTestResponder(is, os, inputs, outputs, 30000);
         assertNotNull(tr);
-        final boolean res1 = ANSI.client.sendAYT(2000);
+        final boolean res1 = ANSI.client.sendAYT(Duration.ofSeconds(2));
 
         if (res1 == true) {
             ayt_true_ok = true;
@@ -242,7 +243,7 @@ public class TelnetClientTest extends TestCase implements TelnetNotificationHand
         Thread.sleep(1000);
         is.skip(is.available());
 
-        final boolean res2 = ANSI.client.sendAYT(2000);
+        final boolean res2 = ANSI.client.sendAYT(Duration.ofSeconds(2));
 
         if (res2 == false) {
             ayt_false_ok = true;