Network programming assignment for University
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

79 line
2.7 KiB

  1. /**
  2. * @file Echo.java
  3. *
  4. * @author Christos Choutouridis AEM:8997
  5. * @email cchoutou@ece.auth.gr
  6. */
  7. package net.hoo2.auth.vmodem;
  8. /**
  9. * @class Echo
  10. *
  11. * Class to used for the echo sequence
  12. */
  13. class Echo {
  14. static final int ECHO_DURATION_DEFAULT = 240; /**< Default duration for the sequence */
  15. static final int ECHO_BUFFER_SIZE = 256; /**< Echo buffer size */
  16. static final String ECHO_BEGIN = "PSTART"; /**< Begin pattern of the response */
  17. static final String ECHO_END = "PSTOP"; /**< End pattern of the response */
  18. private Com com_; /**< Reference to communication module */
  19. private Log log_; /**< Reference to logging module */
  20. private Transaction transaction_; /**< A transaction object to used as a buffer for all requests */
  21. private int duration_; /**< The desired duration for the session */
  22. private byte[] code_; /**< The desired code for the session */
  23. /**
  24. * Basic constructor
  25. * @param com The Communication module to use
  26. * @param log The logging module to use
  27. * @param code The code to use
  28. * @param duration The duration to use
  29. */
  30. Echo (Com com, Log log, byte[] code, int duration) {
  31. com_ = com;
  32. log_ = log;
  33. duration_ = duration;
  34. // Allocate memory for the response
  35. transaction_= new Transaction(null, new byte[ECHO_BUFFER_SIZE]);
  36. code_ = code;
  37. }
  38. /**
  39. * Functionality to drain the response buffer for the welcome message
  40. * from the server
  41. * @param code The code that we will use (for printing, not sending)
  42. */
  43. void caption (byte[] code) {
  44. String line;
  45. line = "Running ECHO with: " + new String(code);
  46. log_.write(line, true);
  47. transaction_ = com_.request (transaction_, null, null, null, false);
  48. line = new String(transaction_.getResponse());
  49. log_.out(line);
  50. }
  51. /**
  52. * Main transaction loop. It send requests to server, get the response
  53. * and log the procedure while doing it.
  54. */
  55. void run () {
  56. long start;
  57. long now;
  58. String line;
  59. start = System.currentTimeMillis();
  60. do {
  61. transaction_ = com_.request(transaction_, code_, ECHO_BEGIN.getBytes(), ECHO_END.getBytes(), false);
  62. line = new String(transaction_.code) + ": "
  63. + new String(transaction_.getResponse())
  64. + " Tr= " + (transaction_.arrival - transaction_.departure) + " [msec]";
  65. log_.write(line);
  66. now = System.currentTimeMillis();
  67. } while (now - start < duration_*1000);
  68. }
  69. }