org.sadun.util
Class TelnetInputStreamConsumer

java.lang.Object
  extended byjava.io.InputStream
      extended byjava.io.FilterInputStream
          extended byorg.sadun.util.TelnetInputStreamConsumer

public class TelnetInputStreamConsumer
extends java.io.FilterInputStream

This class wraps over a TelnetInputStreamto provide stream consumption capabilities.

Input from the stream can be consumed until on a certain criterium(i.e., a condition) holds.

A timeoutcan be set on consumption operations to handle situations where the telnet host does not behave as expected.

Common criteria, based on looking for strings in the input, are already implemented as methods.

A general #consumeByCriteria(Criterium)method allows the user to provide customized criteria if necessary.

The class CompositeCriterium allows to combine a set of criteria into one, use it to drive input consumption and then find out which one of the set did indeed hold.

Version:
1.0
Author:
Cristiano Sadun

Nested Class Summary
static class TelnetInputStreamConsumer.AndCriterium
          A criterium which holds if all its composing criteria hold.
static class TelnetInputStreamConsumer.CompositeCriterium
          An abstract criterium based on other criteria.
static class TelnetInputStreamConsumer.ContainsStringCriterium
          A criterium which holds if a certain string is found in the input.
static interface TelnetInputStreamConsumer.Criterium
          A criterium for evaluating input from a TelnetInputStream.
static class TelnetInputStreamConsumer.NoMoreInputAvailableCriterium
          A criterium which holds if no more input is available within a certain amount of time (i.e. we can suppose that the host has nothing more to say).
static class TelnetInputStreamConsumer.OrCriterium
          A criterium which holds if one of its composing criteria hold.
static class TelnetInputStreamConsumer.PatternBasedCriterium
          A criterium based on regular expression matching.
static class TelnetInputStreamConsumer.StringEndBasedCriterium
          A criterium which holds if a certain string is found at the end of the input.
 
Field Summary
static long BASIC_CRITERIA_CHECK_TIME
          The minimum granularity (in milliseconds) with which consumption criteria are checked.
 
Fields inherited from class java.io.FilterInputStream
in
 
Constructor Summary
TelnetInputStreamConsumer(TelnetInputStream is)
           
 
Method Summary
 java.lang.String consumeByCriteria(TelnetInputStreamConsumer.Criterium criteria)
          Consumes the input based on some criterium.
 java.lang.String consumeInput(long timeout)
          Consumes all input that appears within a certain timeout.
 java.lang.String consumeInput(java.util.regex.Pattern toMatch)
          Consume all the input until a pattern is matched at the end of the input sequence
 java.lang.String consumeInput(java.util.regex.Pattern toMatch, boolean onlyAtEnd)
          Consume all the input until a pattern is matched - possibly only at the end of the input sequence
 java.lang.String consumeInputUntilStringFound(java.lang.String s)
          Consume all the input until a string is found in it (may be faster than {@link #consumeInput(Pattern) for constant patterns).
 java.lang.String consumeInputUntilStringFound(java.lang.String[] s)
          Consume all the input until one of a set strings is found in it (may be faster than {@link #consumeInput(Pattern) for constant patterns).
 java.lang.String consumeInputUntilStringFoundAtEnd(java.lang.String s)
          Consume all the input until a given string is found at the end of it (faster than {@link #consumeInput(Pattern) for constant patterns).
 java.lang.String consumeInputUntilStringFoundAtEnd(java.lang.String[] s)
          Consume all the input until one of a set of given strings is found at the end of it (faster than {@link #consumeInput(Pattern) for constant patterns).
 boolean consumeUntilResultOrFailure(java.lang.String successString, java.lang.String failureString, java.lang.String[] result)
          This method implements a typical consumption pattern when a command is sent programmaticly over a telnet channel, and one of two results may incur as a result in the host output (that is, this inputstream).
 long getConsumptionOperationsTimeout()
          Get the timeout on all consumption operations.
 void registerTelnetCommandListener(TelnetInputStream.TelnetCommandListener tcl)
          Register a listener to Telnet commands.
 void removeTelnetCommandListener(TelnetInputStream.TelnetCommandListener tcl)
          Remove a listener to Telnet commands.
 void setConsumptionOperationsTimeout(long consumptionOperationsTimeout)
          Set the timeout on all consumption operations.
 
Methods inherited from class java.io.FilterInputStream
available, close, mark, markSupported, read, read, read, reset, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BASIC_CRITERIA_CHECK_TIME

public static final long BASIC_CRITERIA_CHECK_TIME
The minimum granularity (in milliseconds) with which consumption criteria are checked. Its value is 1000L.

See Also:
Constant Field Values
Constructor Detail

TelnetInputStreamConsumer

public TelnetInputStreamConsumer(TelnetInputStream is)
Method Detail

registerTelnetCommandListener

public void registerTelnetCommandListener(TelnetInputStream.TelnetCommandListener tcl)
Register a listener to Telnet commands.

Parameters:
tcl - the listener to register

removeTelnetCommandListener

public void removeTelnetCommandListener(TelnetInputStream.TelnetCommandListener tcl)
Remove a listener to Telnet commands.

Parameters:
tcl - the listener to remove

consumeInput

public java.lang.String consumeInput(long timeout)
                              throws java.io.IOException
Consumes all input that appears within a certain timeout. This is useful to get ride of post-login babble.

Throws:
java.io.IOException

consumeInput

public java.lang.String consumeInput(java.util.regex.Pattern toMatch,
                                     boolean onlyAtEnd)
                              throws java.io.IOException,
                                     OperationTimedoutException
Consume all the input until a pattern is matched - possibly only at the end of the input sequence

Parameters:
toMatch -
onlyAtEnd -
Returns:
Throws:
java.io.IOException
OperationTimedoutException

consumeInput

public java.lang.String consumeInput(java.util.regex.Pattern toMatch)
                              throws java.io.IOException,
                                     OperationTimedoutException
Consume all the input until a pattern is matched at the end of the input sequence

Parameters:
toMatch -
Returns:
Throws:
java.io.IOException
OperationTimedoutException

consumeInputUntilStringFoundAtEnd

public java.lang.String consumeInputUntilStringFoundAtEnd(java.lang.String s)
                                                   throws java.io.IOException,
                                                          OperationTimedoutException
Consume all the input until a given string is found at the end of it (faster than {@link #consumeInput(Pattern) for constant patterns).

Parameters:
s -
Returns:
Throws:
java.io.IOException
OperationTimedoutException

consumeInputUntilStringFoundAtEnd

public java.lang.String consumeInputUntilStringFoundAtEnd(java.lang.String[] s)
                                                   throws java.io.IOException,
                                                          OperationTimedoutException
Consume all the input until one of a set of given strings is found at the end of it (faster than {@link #consumeInput(Pattern) for constant patterns).

Parameters:
s -
Returns:
Throws:
java.io.IOException
OperationTimedoutException

consumeInputUntilStringFound

public java.lang.String consumeInputUntilStringFound(java.lang.String s)
                                              throws java.io.IOException,
                                                     OperationTimedoutException
Consume all the input until a string is found in it (may be faster than {@link #consumeInput(Pattern) for constant patterns).

Parameters:
s -
Returns:
Throws:
java.io.IOException
OperationTimedoutException

consumeInputUntilStringFound

public java.lang.String consumeInputUntilStringFound(java.lang.String[] s)
                                              throws java.io.IOException,
                                                     OperationTimedoutException
Consume all the input until one of a set strings is found in it (may be faster than {@link #consumeInput(Pattern) for constant patterns).

Parameters:
s -
Returns:
Throws:
java.io.IOException
OperationTimedoutException

consumeByCriteria

public java.lang.String consumeByCriteria(TelnetInputStreamConsumer.Criterium criteria)
                                   throws java.io.IOException,
                                          OperationTimedoutException
Consumes the input based on some criterium.

The consumption operations timeout is used for checking timeouts.

Parameters:
criteria -
Returns:
Throws:
java.io.IOException
OperationTimedoutException

consumeUntilResultOrFailure

public boolean consumeUntilResultOrFailure(java.lang.String successString,
                                           java.lang.String failureString,
                                           java.lang.String[] result)
                                    throws java.io.IOException,
                                           OperationTimedoutException
This method implements a typical consumption pattern when a command is sent programmaticly over a telnet channel, and one of two results may incur as a result in the host output (that is, this inputstream).

setConsumptionOperationsTimeout(long)can be used to impose a timeout on the result.

Parameters:
result -
Returns:
Throws:
OperationTimedoutException
java.io.IOException

getConsumptionOperationsTimeout

public long getConsumptionOperationsTimeout()
Get the timeout on all consumption operations. A zero timeout indicates indefinite wait.

Returns:
the timeout on all consumption operations.

setConsumptionOperationsTimeout

public void setConsumptionOperationsTimeout(long consumptionOperationsTimeout)
Set the timeout on all consumption operations. A zero timeout indicates indefinite wait.

Parameters:
consumptionOperationsTimeout - the timeout on all consumption operations.