Network programming assignment for University
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 

139 rindas
5.1 KiB

  1. /**
  2. * @file AQR.java
  3. *
  4. * @author Christos Choutouridis AEM:8997
  5. * @email cchoutou@ece.auth.gr
  6. */
  7. package net.hoo2.auth.vmodem;
  8. /** @name imports */
  9. /** @{ */
  10. import java.util.*;
  11. /** @} */
  12. /**
  13. * @class AQR
  14. *
  15. * Class to used for the ACK-NACK sequence
  16. */
  17. class AQR {
  18. static final int AQ_DURATION_DEFAULT = 240; /**< Default duration for the sequence */
  19. static final int AQR_BUFFER_SIZE = 256; /**< AQR buffer size */
  20. static final String AQR_BEGIN = "PSTART"; /**< Begin pattern of the response */
  21. static final String AQR_END = "PSTOP"; /**< End pattern of the response */
  22. static final int AQR_SEQUENCE_BEGIN = 31; /**< The response sequence string position */
  23. static final int AQR_SEQUENCE_END = 47; /**< The end of response sequence string position */
  24. static final int AQR_CRC_BEGIN = 49; /**< The response crc string position */
  25. static final int AQR_CRC_END = 52; /**< The end of response crc string position */
  26. private Com com_; /**< Reference to communication module */
  27. private Log log_; /**< Reference to logging module */
  28. private Transaction transaction_; /**< A transaction object to used as a buffer for all requests */
  29. private int duration_; /**< The desired duration for the session */
  30. private byte[] ack_; /**< The desired ACK code for the session */
  31. private byte[] nack_; /**< The desired NACK code for the session */
  32. /**
  33. * Basic constructor
  34. * @param com The Com module to use
  35. * @param log The Log module to use
  36. * @param ack The desired ACK code
  37. * @param nack The desired NACK code
  38. * @param duration The desired duration for the session
  39. */
  40. AQR (Com com, Log log, byte[] ack, byte[] nack, int duration) {
  41. com_ = com;
  42. log_ = log;
  43. duration_ = duration;
  44. transaction_= new Transaction(null, new byte[AQR_BUFFER_SIZE]);
  45. ack_ = ack;
  46. nack_ = nack;
  47. }
  48. /**
  49. * Functionality to drain the response buffer for the welcome message
  50. * from the server
  51. * @param ack The ack code that we will use (for printing, not sending)
  52. * @param nack The nack code that we will use (for printing, not sending)
  53. */
  54. void caption (byte[] ack, byte[] nack) {
  55. String line;
  56. line = "Running AQR with: " + new String(ack) + "/" + new String(nack);
  57. log_.write(line, true);
  58. transaction_ = com_.request (transaction_, null, null, null, false);
  59. line = new String(transaction_.getResponse());
  60. log_.out(line);
  61. }
  62. /**
  63. * Main transaction loop. It send requests to server, get the response
  64. * and log the procedure while doing it.
  65. */
  66. void run () {
  67. long start;
  68. long now;
  69. long mark =0, delay =0;
  70. String line;
  71. int bads = 0;
  72. boolean good = true;
  73. boolean prev = true;
  74. start = System.currentTimeMillis();
  75. do {
  76. // Check if the previous transaction was error-free
  77. if (good)
  78. transaction_ = com_.request(transaction_, ack_, AQR_BEGIN.getBytes(), AQR_END.getBytes(), false);
  79. else
  80. transaction_ = com_.request(transaction_, nack_, AQR_BEGIN.getBytes(), AQR_END.getBytes(), false);
  81. good = (_crc_check(transaction_.response)) ? true : false; // crc check the response
  82. bads = (good) ? 0 : bads+1; // count the error strike
  83. // delay calculation
  84. if (prev && !good) mark = transaction_.departure;
  85. if (!prev && good) delay = transaction_.arrival - mark;
  86. else delay = 0;
  87. prev = good;
  88. line = new String(transaction_.code) + ": "
  89. + new String(transaction_.getResponse())
  90. + " Er: " + bads
  91. + " Tr= " + (transaction_.arrival - transaction_.departure) + " [msec]";
  92. if (delay != 0) line += " Tt= " + delay + " [msec]";
  93. log_.write(line);
  94. now = System.currentTimeMillis();
  95. } while (now - start < duration_*1000);
  96. }
  97. /** @name private helper API */
  98. /**@{ */
  99. /**
  100. * A CRC check functionality
  101. * @param data The data to check for validity
  102. * @return The check result
  103. */
  104. private boolean _crc_check (byte[] data) {
  105. byte[] seq = Arrays.copyOfRange(data, AQR_SEQUENCE_BEGIN, AQR_SEQUENCE_END);
  106. int crc = Integer.valueOf(
  107. new String(Arrays.copyOfRange(data, AQR_CRC_BEGIN, AQR_CRC_END)));
  108. return (crc == _crc(seq)) ? true : false;
  109. }
  110. /**
  111. * A CRC calculation function. This primitive CRC calculator
  112. * does not use any known CRC polynomial. It just uses XOR
  113. * @param data Reference to buffer
  114. * @return The CRC result
  115. */
  116. private int _crc (byte[] data) {
  117. int calc =0;
  118. for (int i=0 ; i<data.length ; ++i)
  119. calc ^= data[i];
  120. return calc;
  121. }
  122. /**@} */
  123. }