Network programming assignment for University
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 

144 рядки
5.9 KiB

  1. /**
  2. * @file GPS.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.Arrays;
  11. /** @} */
  12. /**
  13. * @class GPS
  14. *
  15. * Class to used for the GPS session
  16. */
  17. class GPS {
  18. static final int GPS_BUFFER_SIZE = 256; /**< GPS trace buffer size */
  19. static final String GPS_BEGIN = "START ITHAKI GPS TRACKING"; /**< starting pattern */
  20. static final String GPS_END = "STOP ITHAKI GPS TRACKING"; /**< ending pattern */
  21. static final int GPS_USE_TRACK = 1; /**< Which track to use (given) */
  22. static final String GPS_TRACK_PREFIX = "R="; /**< GPS command track request prefix */
  23. static final String GPS_IMAGE_PREFIX = "T="; /**< GPS command image request prefix */
  24. static final int GPS_MAX_POINTS = 9; /**< Maximum points (given) */
  25. static final int GPS_COORDINATES_SIZE = 6; /**< Coordinates size */
  26. static final int GPS_LATITUDE_BEGIN = 17; /**< The latitude sequence string position */
  27. static final int GPS_LATITUDE_END = 28; /**< The end of latitude sequence string position */
  28. static final int GPS_LONGITUDE_BEGIN= 29; /**< The longitude sequence string position */
  29. static final int GPS_LONGITUDE_END = 41; /**< The end of longitude sequence string position */
  30. private Com com_; /**< Reference to communication module */
  31. private Log log_; /**< Reference to logging module */
  32. private Transaction transaction_; /**< A transaction object to used as a buffer for all requests */
  33. private byte[] code_; /**< The desired code for the session */
  34. private int start_; /**< Starting point [sec] */
  35. private int duration_; /**< Duration of requested trace */
  36. private int points_; /**< Number of points to fetch from the above trace */
  37. private byte[] coordinates_; /**< Coordinates buffer */
  38. /**
  39. * Basic constructor
  40. */
  41. GPS (Com com, Log log, byte[] code, int start, int duration, int points) {
  42. com_ = com;
  43. log_ = log;
  44. transaction_= new Transaction(null, new byte[GPS_BUFFER_SIZE]);
  45. code_ = code;
  46. start_ = start;
  47. duration_ = duration;
  48. points_ = (points <= GPS_MAX_POINTS) ? points : GPS_MAX_POINTS;
  49. coordinates_= new byte[GPS_COORDINATES_SIZE];
  50. }
  51. /**
  52. * Functionality to drain the response buffer for the welcome message
  53. * from the server
  54. */
  55. void caption () {
  56. String line;
  57. line = "Running GPS with: " + new String(code_)
  58. + " time [" + start_ + " - " +(start_+duration_) + ") sec" + " [" + points_ + " points]";
  59. log_.write(line, true);
  60. transaction_ = com_.request (transaction_, null, null, null, false);
  61. line = new String(transaction_.getResponse());
  62. log_.out(line);
  63. }
  64. /**
  65. * Main transaction loop. It send requests to server, get the response
  66. * and log the procedure while doing it.
  67. * @return the image request code to pass to image module
  68. */
  69. String run () {
  70. String code, image_code;
  71. String line;
  72. log_.out("Get traces");
  73. image_code = new String(code_);
  74. for (int trace =start_ ; trace < start_+duration_ ; trace += duration_/points_) {
  75. code = new String(code_) + GPS_TRACK_PREFIX + GPS_USE_TRACK + String.format("%04d", trace) + "01";
  76. transaction_ = com_.request(transaction_, code.getBytes(), GPS_BEGIN.getBytes(), GPS_END.getBytes(), false);
  77. line = new String(transaction_.code) + ": "
  78. + new String(transaction_.getResponse())
  79. + " Tr= " + (transaction_.arrival - transaction_.departure) + " [msec]";
  80. log_.write(line);
  81. _get_coordinates(transaction_.getResponse());
  82. image_code += GPS_IMAGE_PREFIX +
  83. String.format("%02d%02d%02d%02d%02d%02d", coordinates_[0], coordinates_[1],
  84. coordinates_[2], coordinates_[3],
  85. coordinates_[4], coordinates_[5]);
  86. }
  87. return image_code;
  88. }
  89. /** @name private helper API */
  90. /** @{ */
  91. /**
  92. * Extract coordinates from response
  93. * @param stream The stream to search
  94. * @return The coordinates buffer
  95. */
  96. private byte[] _get_coordinates (byte[] stream) {
  97. int start = Com.detect(stream, "GPGGA".getBytes(), stream.length);
  98. double latitude = _nmea2dec (Double.valueOf(
  99. new String (Arrays.copyOfRange(stream,
  100. start+GPS_LATITUDE_BEGIN,
  101. start+GPS_LATITUDE_END-2))));
  102. double longitude = _nmea2dec (Double.valueOf(
  103. new String(Arrays.copyOfRange(stream,
  104. start+GPS_LONGITUDE_BEGIN,
  105. start+GPS_LONGITUDE_END-2))));
  106. coordinates_[0] = (byte)(longitude); // longitude deg
  107. coordinates_[1] = (byte)((longitude - coordinates_[0]) * 60.0); // longitude '
  108. coordinates_[2] = (byte)((longitude - coordinates_[0]
  109. - coordinates_[1]/60.0)*3600); // longitude "
  110. coordinates_[3] = (byte)(latitude); // latitude deg
  111. coordinates_[4] = (byte)((latitude - coordinates_[3]) * 60.0); // latitude '
  112. coordinates_[5] = (byte)((latitude - coordinates_[3]
  113. - coordinates_[4]/60.0)*3600); // latitude "
  114. return coordinates_;
  115. }
  116. /**
  117. * A helper to convert the NMEA format to canonical
  118. * decimal coordinate format
  119. */
  120. double _nmea2dec (double c) {
  121. int d = (int)c/100;
  122. c -= d*100;
  123. return d + (c/60);
  124. }
  125. /** @} */
  126. }