Thanks for accepting the task, and for the cleanup, Stefan.  
I'm attaching a patch for this task. 
Adding timeout attributes at the <telnet>, and <read> levels.  
And based on Stuart's suggestions.  A initialCR attribute, required for
some hosts on <telnet>.  An echo attribute on <write>, to suppress echo of
passwords or other commands.

The timeouts are done with a buzz loop.  Not optimal, but it does work.
If anyone has a better way, I be appreciative. 

Hopefully I haven't done anything that isn't 1.1 compatible again.

Scott

On 8 Nov 2000, Stefan Bodewig wrote:

> (1) New package and added license, no big deal.
> (2) Used Vector instead of ArrayList to make it work on JDK 1.1.
> (3) replaced all System.exit calls with throw BuildException
> (4) replaced all System.out.println calls with Ant's logging system
> (5) TelnetSubTask doesn't extend MatchingTask anymore (why did it
> anyway?).
Cut and paste from something else. Whoops.
> 
> Hope this is OK with you Scott.
> 
> Stefan
> 
Index: TelnetTask.java
===================================================================
RCS file: 
/home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/optional/net/TelnetTask.java,v
retrieving revision 1.1
diff -b -u -r1.1 TelnetTask.java
--- TelnetTask.java     2000/11/08 17:01:10     1.1
+++ TelnetTask.java     2000/11/12 14:45:05
@@ -104,6 +104,17 @@
     private Vector telnetTasks = new Vector();
 
     /** 
+     *  If true, adds a CR to beginning of login script
+     */
+    private boolean addCarriageReturn = false;
+
+    /**
+     *  Default time allowed for waiting for a valid response
+     *  for all child reads.  A value of 0 means no limit.
+     */
+    private Integer defaultTimeout = null;
+
+    /** 
      *  Verify that all parameters are included. 
      *  Connect and possibly login
      *  Iterate through the list of Reads and writes 
@@ -136,6 +147,8 @@
        while (tasksToRun!=null && tasksToRun.hasMoreElements())
        {
            TelnetSubTask task = (TelnetSubTask) tasksToRun.nextElement();
+           if (task instanceof TelnetRead && defaultTimeout != null)
+               ((TelnetRead)task).setDefaultTimeout(defaultTimeout);
            task.execute(telnet);
        }
     }
@@ -146,30 +159,52 @@
      */
     private void login()
     {
+       if (addCarriageReturn)
+          telnet.sendString("\n", true);
        telnet.waitForString("ogin:");
-       telnet.sendString(userid);
+       telnet.sendString(userid, true);
        telnet.waitForString("assword:");
-       telnet.sendString(password);
+       telnet.sendString(password, false);
     }
 
     /**
      *  Set the userid attribute 
      */
     public void setUserid(String u) { this.userid = u; }
+
     /**
      *  Set the password attribute 
      */
     public void setPassword(String p) { this.password = p; }
+
     /**
      *  Set the server address attribute 
      */
     public void setServer(String m) { this.server = m; }
+
     /**
      *  Set the tcp port to connect to attribute 
      */
     public void setPort(int p) { this.port = p; }
 
     /**
+     *  Set the tcp port to connect to attribute 
+     */
+    public void setInitialCR(boolean b)
+    {
+       this.addCarriageReturn = b;
+    }
+
+    /**
+     *  Change the default timeout to wait for 
+     *  valid responses
+     */
+    public void setTimeout(Integer i)
+    {
+       this.defaultTimeout = i;
+    }
+
+    /**
      *  A subTask <read> tag was found.  Create the object, 
      *  Save it in our list, and return it.
      */
@@ -215,10 +250,16 @@
      */
     public class TelnetWrite extends TelnetSubTask
     {
+        private boolean echoString = true;
         public void execute(AntTelnetClient telnet) 
                throws BuildException
+        {
+           telnet.sendString(taskString, echoString);
+        }
+        
+        public void setEcho(boolean b)
         {
-            telnet.sendString(taskString);
+           echoString = b;
         }
     }
     /**
@@ -227,12 +268,28 @@
      */
     public class TelnetRead extends TelnetSubTask
     {
+        private Integer timeout = null;
         public void execute(AntTelnetClient telnet) 
                throws BuildException
         {
-            telnet.waitForString(taskString);
+            telnet.waitForString(taskString, timeout);
         }
+        /**
+         *  Override any default timeouts
+         */
+        public void setTimeout(Integer i)
+        {
+           this.timeout = i;
+        }
+        /**
+         *  Sets the default timeout if none has been set already
+         */
+        public void setDefaultTimeout(Integer defaultTimeout)
+        {
+           if (timeout == null)
+              timeout = defaultTimeout;
     }
+    }
     /**
      *  This class handles the abstraction of the telnet protocol.
      *  Currently it is a wrapper around <a href="www.oroinc.com">ORO</a>'s 
@@ -240,30 +297,65 @@
      */
     public class AntTelnetClient extends TelnetClient
     {
+      /**
+       * Read from the telnet session until the string we are 
+       * waiting for is found 
+       * @parm s The string to wait on 
+       */
       public void waitForString(String s)
       {
+           waitForString(s, null);
+      }
+
+      /**
+       * Read from the telnet session until the string we are 
+       * waiting for is found or the timeout has been reached
+       * @parm s The string to wait on 
+       * @parm timeout The maximum number of seconds to wait
+       */
+      public void waitForString(String s, Integer timeout)
+      {
         InputStream is =this.getInputStream();
         try {
           StringBuffer sb = new StringBuffer();
+          if (timeout == null || timeout.intValue() == 0)
+          {
           while (sb.toString().indexOf(s) == -1)
           {
-              while (is.available() == 0);
-              int iC = is.read();
-              Character c = new Character((char)iC);
-              sb.append(c);
+                  sb.append((char) is.read());
           }
+          }
+          else
+          {
+              Calendar endTime = Calendar.getInstance(); 
+              endTime.add(Calendar.SECOND,timeout.intValue());
+              while ( sb.toString().indexOf(s) == -1)
+              {
+                  while (Calendar.getInstance().before(endTime) &&
+                         is.available() == 0);
+                  if (is.available() == 0)
+                      throw new BuildException("Response Timed-Out");
+                  sb.append((char) is.read());
+              }
+          }
           log(sb.toString(), Project.MSG_INFO);
         } catch (Exception e)
         { 
             throw new BuildException(e, getLocation());
         }
       }
+
     
-      public void sendString(String s)
+      /**
+       * Write this string to the telnet session.
+       * @parm echoString  Logs string sent
+       */
+      public void sendString(String s, boolean echoString)
       {
         OutputStream os =this.getOutputStream();
         try {
           os.write((s + "\n").getBytes());
+          if (echoString)
           log(s, Project.MSG_INFO);
           os.flush();
         } catch (Exception e)

Reply via email to