Browse Source

Improved the optional telnet task

1) added an echo attribute to write to suppress output
2) added a timeout mechanism on reads
3) can now send an initial newline before expecting anything from the server

Submitted by:	Scott Carlson <ScottCarlson@email.com>


git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@268179 13f79535-47bb-0310-9956-ffa450edef68
master
Stefan Bodewig 24 years ago
parent
commit
d4be9171f8
1 changed files with 108 additions and 11 deletions
  1. +108
    -11
      src/main/org/apache/tools/ant/taskdefs/optional/net/TelnetTask.java

+ 108
- 11
src/main/org/apache/tools/ant/taskdefs/optional/net/TelnetTask.java View File

@@ -103,6 +103,17 @@ public class TelnetTask extends Task {
*/ */
private Vector telnetTasks = new Vector(); 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. * Verify that all parameters are included.
* Connect and possibly login * Connect and possibly login
@@ -136,6 +147,8 @@ public class TelnetTask extends Task {
while (tasksToRun!=null && tasksToRun.hasMoreElements()) while (tasksToRun!=null && tasksToRun.hasMoreElements())
{ {
TelnetSubTask task = (TelnetSubTask) tasksToRun.nextElement(); TelnetSubTask task = (TelnetSubTask) tasksToRun.nextElement();
if (task instanceof TelnetRead && defaultTimeout != null)
((TelnetRead)task).setDefaultTimeout(defaultTimeout);
task.execute(telnet); task.execute(telnet);
} }
} }
@@ -146,29 +159,51 @@ public class TelnetTask extends Task {
*/ */
private void login() private void login()
{ {
if (addCarriageReturn)
telnet.sendString("\n", true);
telnet.waitForString("ogin:"); telnet.waitForString("ogin:");
telnet.sendString(userid);
telnet.sendString(userid, true);
telnet.waitForString("assword:"); telnet.waitForString("assword:");
telnet.sendString(password);
telnet.sendString(password, false);
} }


/** /**
* Set the userid attribute * Set the userid attribute
*/ */
public void setUserid(String u) { this.userid = u; } public void setUserid(String u) { this.userid = u; }

/** /**
* Set the password attribute * Set the password attribute
*/ */
public void setPassword(String p) { this.password = p; } public void setPassword(String p) { this.password = p; }

/** /**
* Set the server address attribute * Set the server address attribute
*/ */
public void setServer(String m) { this.server = m; } public void setServer(String m) { this.server = m; }

/** /**
* Set the tcp port to connect to attribute * Set the tcp port to connect to attribute
*/ */
public void setPort(int p) { this.port = p; } 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, * A subTask <read> tag was found. Create the object,
* Save it in our list, and return it. * Save it in our list, and return it.
@@ -215,10 +250,16 @@ public class TelnetTask extends Task {
*/ */
public class TelnetWrite extends TelnetSubTask public class TelnetWrite extends TelnetSubTask
{ {
private boolean echoString = true;
public void execute(AntTelnetClient telnet) public void execute(AntTelnetClient telnet)
throws BuildException throws BuildException
{ {
telnet.sendString(taskString);
telnet.sendString(taskString, echoString);
}
public void setEcho(boolean b)
{
echoString = b;
} }
} }
/** /**
@@ -227,11 +268,27 @@ public class TelnetTask extends Task {
*/ */
public class TelnetRead extends TelnetSubTask public class TelnetRead extends TelnetSubTask
{ {
private Integer timeout = null;
public void execute(AntTelnetClient telnet) public void execute(AntTelnetClient telnet)
throws BuildException 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. * This class handles the abstraction of the telnet protocol.
@@ -240,31 +297,71 @@ public class TelnetTask extends Task {
*/ */
public class AntTelnetClient extends TelnetClient 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) 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(); InputStream is =this.getInputStream();
try { try {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
while (sb.toString().indexOf(s) == -1)
if (timeout == null || timeout.intValue() == 0)
{ {
while (is.available() == 0);
int iC = is.read();
Character c = new Character((char)iC);
sb.append(c);
while (sb.toString().indexOf(s) == -1)
{
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) {
Thread.sleep(250);
}
if (is.available() == 0)
throw new BuildException("Response Timed-Out", getLocation());
sb.append((char) is.read());
}
} }
log(sb.toString(), Project.MSG_INFO); log(sb.toString(), Project.MSG_INFO);
} catch (BuildException be)
{
throw be;
} catch (Exception e) } catch (Exception e)
{ {
throw new BuildException(e, getLocation()); 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(); OutputStream os =this.getOutputStream();
try { try {
os.write((s + "\n").getBytes()); os.write((s + "\n").getBytes());
log(s, Project.MSG_INFO);
if (echoString)
log(s, Project.MSG_INFO);
os.flush(); os.flush();
} catch (Exception e) } catch (Exception e)
{ {


Loading…
Cancel
Save