Network programming assignment for University
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

91 lines
3.0 KiB

  1. package net.hoo2.auth.vmodem;
  2. import java.util.*;
  3. class AQR {
  4. static final int AQ_DURATION_DEFAULT = 240;
  5. static final int AQR_BUFFER_SIZE = 256;
  6. static final String AQR_BEGIN = "PSTART";
  7. static final String AQR_END = "PSTOP";
  8. static final int AQR_SEQUENCE_BEGIN = 31;
  9. static final int AQR_SEQUENCE_END = 47;
  10. static final int AQR_CRC_BEGIN = 49;
  11. static final int AQR_CRC_END = 52;
  12. private Com com_;
  13. private Log log_;
  14. private Transaction transaction_;
  15. private int duration_;
  16. private byte[] ack_;
  17. private byte[] nack_;
  18. AQR (Com com, Log log, byte[] ack, byte[] nack, int duration) {
  19. com_ = com;
  20. log_ = log;
  21. duration_ = duration;
  22. transaction_= new Transaction(null, new byte[AQR_BUFFER_SIZE]);
  23. ack_ = ack;
  24. nack_ = nack;
  25. }
  26. void caption (byte[] ack, byte[] nack) {
  27. String line;
  28. line = "Running AQR with: " + new String(ack) + "/" + new String(nack);
  29. log_.write(line, true);
  30. transaction_ = com_.request (transaction_, null, null, null, false);
  31. line = new String(transaction_.getResponse());
  32. log_.out(line);
  33. }
  34. void run () {
  35. long start;
  36. long now;
  37. long mark =0, delay =0;
  38. String line;
  39. int bads = 0;
  40. boolean good = true;
  41. boolean prev = true;
  42. start = System.currentTimeMillis();
  43. do {
  44. if (good)
  45. transaction_ = com_.request(transaction_, ack_, AQR_BEGIN.getBytes(), AQR_END.getBytes(), false);
  46. else
  47. transaction_ = com_.request(transaction_, nack_, AQR_BEGIN.getBytes(), AQR_END.getBytes(), false);
  48. good = (_crc_check(transaction_.response)) ? true : false;
  49. bads = (good) ? 0 : bads+1;
  50. // delay calculation
  51. if (prev && !good) mark = transaction_.departure;
  52. if (!prev && good) delay = transaction_.arrival - mark;
  53. else delay = 0;
  54. prev = good;
  55. line = new String(transaction_.code) + ": "
  56. + new String(transaction_.getResponse())
  57. + " Er: " + bads
  58. + " Tr= " + (transaction_.arrival - transaction_.departure) + " [msec]";
  59. if (delay != 0) line += " Tt= " + delay + " [msec]";
  60. log_.write(line);
  61. now = System.currentTimeMillis();
  62. } while (now - start < duration_*1000);
  63. }
  64. private boolean _crc_check (byte[] data) {
  65. byte[] seq = Arrays.copyOfRange(data, AQR_SEQUENCE_BEGIN, AQR_SEQUENCE_END);
  66. int crc = Integer.valueOf(
  67. new String(Arrays.copyOfRange(data, AQR_CRC_BEGIN, AQR_CRC_END)));
  68. return (crc == _crc(seq)) ? true : false;
  69. }
  70. private int _crc (byte[] data) {
  71. int calc =0;
  72. for (int i=0 ; i<data.length ; ++i)
  73. calc ^= data[i];
  74. return calc;
  75. }
  76. }