|
- package net.hoo2.auth.vmodem;
-
- import java.io.IOException;
- import java.io.PrintWriter;
-
- class Echo {
- static final int ECHO_DURATION_DEFAULT = 60;
- static final int ECHO_CODE_SIZE = 5;
- static final int ECHO_BUFFER_SIZE = 256;
- static final int ECHO_RESPONSE_SIZE = 35;
- static final String ECHO_DELIMITER = "PSTOP";
-
- private Com com_;
- private Transaction transaction_;
- private int duration_;
- private String logfile_;
-
- Echo (Com com, byte[] code, int duration, String logfile) {
- com_ = com;
- duration_ = duration;
- transaction_= new Transaction(new byte[ECHO_CODE_SIZE],
- new byte[ECHO_BUFFER_SIZE]);
- transaction_.code = code.clone();
- logfile_ = logfile;
- }
-
- void run (boolean verbose) {
- boolean init;
- long start;
- long now;
- PrintWriter writer = null;
- String line;
-
- line = "Running echo with: " + new String(transaction_.code);
- System.out.println(line);
- if (logfile_ != null) {
- try {
- writer = new PrintWriter(logfile_);
- writer.println(line);
- }
- catch (IOException exp) {
- System.err.println( "Open log file failed: " + exp.getMessage() );
- return;
- }
- }
- init = true;
- start = System.currentTimeMillis();
- do {
- if (init == true) {
- transaction_ = com_.request (transaction_, false, null);
- init = false;
- line = new String(transaction_.response);
- if (verbose) {
- System.out.println(line);
- }
- }
- else {
- transaction_ = com_.request(transaction_, true, ECHO_DELIMITER.getBytes());
- line = new String(transaction_.code)
- + ": "
- + new String(transaction_.response).substring(0, 35)
- + " Resp.time= "
- + (transaction_.arrival - transaction_.departure)
- + " [msec]";
- if (logfile_ != null) writer.println(line);
- if (verbose) System.out.println(line);
-
- }
- now = System.currentTimeMillis();
- } while (now - start < duration_*1000);
-
- try {
- if (writer != null)
- writer.close();
- } catch (Exception ex) {/*ignore*/}
- }
-
- }
|