View Javadoc

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