    1   /*
    2    * Copyright 2003-2004 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.net.telnet;
    17
    18  import java.io.InputStream;
    19  import java.io.OutputStream;
    20
    21
    22  /****
    23   * Simple stream responder.
    24   * Waits for strings on an input stream and answers
    25   * sending corresponfing strings on an output stream.
    26   * The reader runs in a separate thread.
    27   * <p>
    28   * @author Bruno D'Avanzo
    29   ***/
    30  public class TelnetTestResponder implements Runnable
    31  {
    32      InputStream _is;
    33      OutputStream _os;
    34      String _inputs[], _outputs[];
    35      long _timeout;
    36
    37      /****
    38       * Constructor.
    39       * Starts a new thread for the reader.
    40       * <p>
    41       * @param is - InputStream on which to read.
    42       * @param os - OutputStream on which to answer.
    43       * @param inputs - Array of waited for Strings.
    44       * @param inputs - Array of answers.
    45       ***/
    46      public TelnetTestResponder(InputStream is, OutputStream os, String inputs[], String outputs[], long timeout)
    47      {
    48          _is = is;
    49          _os = os;
    50          _timeout = timeout;
    51          _inputs = inputs;
    52          _outputs = outputs;
    53          Thread reader = new Thread (this);
    54
    55          reader.start();
    56      }
    57
    58      /****
    59       * Runs the responder
    60       ***/
    61      public void run()
    62      {
    63          boolean result = false;
    64          byte buffer[] = new byte[32];
    65          long starttime = System.currentTimeMillis();
    66
    67          try
    68          {
    69              String readbytes = new String();
    70              while(!result &&
    71                    ((System.currentTimeMillis() - starttime) < _timeout))
    72              {
    73                  if(_is.available() > 0)
    74                  {
    75                      int ret_read = _is.read(buffer);
    76                      readbytes = readbytes + new String(buffer, 0, ret_read);
    77
    78                      for(int ii=0; ii<_inputs.length; ii++)
    79                      {
    80                          if(readbytes.indexOf(_inputs[ii]) >= 0)
    81                          {
    82                              Thread.sleep(1000 * ii);
    83                              _os.write(_outputs[ii].getBytes());
    84                              result = true;
    85                          }
    86                      }
    87                  }
    88                  else
    89                  {
    90                      Thread.sleep(500);
    91                  }
    92              }
    93
    94          }
    95          catch (Exception e)
    96          {
    97              System.err.println("Error while waiting endstring. " + e.getMessage());
    98          }
    99      }
    100 }