Network programming assignment for University
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 

79 wiersze
2.4 KiB

  1. package net.hoo2.auth.vmodem;
  2. import java.io.IOException;
  3. import java.io.PrintWriter;
  4. class Echo {
  5. static final int ECHO_DURATION_DEFAULT = 60;
  6. static final int ECHO_CODE_SIZE = 5;
  7. static final int ECHO_BUFFER_SIZE = 256;
  8. static final int ECHO_RESPONSE_SIZE = 35;
  9. static final String ECHO_DELIMITER = "PSTOP";
  10. private Com com_;
  11. private Transaction transaction_;
  12. private int duration_;
  13. private String logfile_;
  14. Echo (Com com, byte[] code, int duration, String logfile) {
  15. com_ = com;
  16. duration_ = duration;
  17. transaction_= new Transaction(new byte[ECHO_CODE_SIZE],
  18. new byte[ECHO_BUFFER_SIZE]);
  19. transaction_.code = code.clone();
  20. logfile_ = logfile;
  21. }
  22. void run (boolean verbose) {
  23. boolean init;
  24. long start;
  25. long now;
  26. PrintWriter writer = null;
  27. String line;
  28. line = "Running echo with: " + new String(transaction_.code);
  29. System.out.println(line);
  30. if (logfile_ != null) {
  31. try {
  32. writer = new PrintWriter(logfile_);
  33. writer.println(line);
  34. }
  35. catch (IOException exp) {
  36. System.err.println( "Open log file failed: " + exp.getMessage() );
  37. return;
  38. }
  39. }
  40. init = true;
  41. start = System.currentTimeMillis();
  42. do {
  43. if (init == true) {
  44. transaction_ = com_.request (transaction_, false, null);
  45. init = false;
  46. line = new String(transaction_.response);
  47. if (verbose) {
  48. System.out.println(line);
  49. }
  50. }
  51. else {
  52. transaction_ = com_.request(transaction_, true, ECHO_DELIMITER.getBytes());
  53. line = new String(transaction_.code)
  54. + ": "
  55. + new String(transaction_.response).substring(0, 35)
  56. + " Resp.time= "
  57. + (transaction_.arrival - transaction_.departure)
  58. + " [msec]";
  59. if (logfile_ != null) writer.println(line);
  60. if (verbose) System.out.println(line);
  61. }
  62. now = System.currentTimeMillis();
  63. } while (now - start < duration_*1000);
  64. try {
  65. if (writer != null)
  66. writer.close();
  67. } catch (Exception ex) {/*ignore*/}
  68. }
  69. }