|
- package net.hoo2.auth.vmodem;
-
- import java.util.*;
-
- class AQR {
- static final int AQ_DURATION_DEFAULT = 240;
- static final int AQR_BUFFER_SIZE = 256;
- static final String AQR_BEGIN = "PSTART";
- static final String AQR_END = "PSTOP";
-
- static final int AQR_SEQUENCE_BEGIN = 31;
- static final int AQR_SEQUENCE_END = 47;
- static final int AQR_CRC_BEGIN = 49;
- static final int AQR_CRC_END = 52;
-
- private Com com_;
- private Log log_;
- private Transaction transaction_;
- private int duration_;
- private byte[] ack_;
- private byte[] nack_;
-
- AQR (Com com, Log log, byte[] ack, byte[] nack, int duration) {
- com_ = com;
- log_ = log;
- duration_ = duration;
- transaction_= new Transaction(null, new byte[AQR_BUFFER_SIZE]);
- ack_ = ack;
- nack_ = nack;
- }
-
- void caption (byte[] ack, byte[] nack) {
- String line;
-
- line = "Running AQR with: " + new String(ack) + "/" + new String(nack);
- log_.write(line, true);
-
- transaction_ = com_.request (transaction_, null, null, null, false);
- line = new String(transaction_.getResponse());
- log_.out(line);
- }
-
- void run () {
- long start;
- long now;
- long mark =0, delay =0;
- String line;
- int bads = 0;
- boolean good = true;
- boolean prev = true;
-
- start = System.currentTimeMillis();
- do {
- if (good)
- transaction_ = com_.request(transaction_, ack_, AQR_BEGIN.getBytes(), AQR_END.getBytes(), false);
- else
- transaction_ = com_.request(transaction_, nack_, AQR_BEGIN.getBytes(), AQR_END.getBytes(), false);
- good = (_crc_check(transaction_.response)) ? true : false;
- bads = (good) ? 0 : bads+1;
-
- // delay calculation
- if (prev && !good) mark = transaction_.departure;
- if (!prev && good) delay = transaction_.arrival - mark;
- else delay = 0;
- prev = good;
-
- line = new String(transaction_.code) + ": "
- + new String(transaction_.getResponse())
- + " Er: " + bads
- + " Tr= " + (transaction_.arrival - transaction_.departure) + " [msec]";
- if (delay != 0) line += " Tt= " + delay + " [msec]";
- log_.write(line);
- now = System.currentTimeMillis();
- } while (now - start < duration_*1000);
- }
-
- private boolean _crc_check (byte[] data) {
- byte[] seq = Arrays.copyOfRange(data, AQR_SEQUENCE_BEGIN, AQR_SEQUENCE_END);
- int crc = Integer.valueOf(
- new String(Arrays.copyOfRange(data, AQR_CRC_BEGIN, AQR_CRC_END)));
- return (crc == _crc(seq)) ? true : false;
- }
-
- private int _crc (byte[] data) {
- int calc =0;
- for (int i=0 ; i<data.length ; ++i)
- calc ^= data[i];
- return calc;
- }
- }
|