Network programming assignment for University
No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 

236 líneas
8.8 KiB

  1. /**
  2. * @file VirtualModem.java
  3. * @brief
  4. * Contain the Main class for the project VirtualModem
  5. *
  6. * @author Christos Choutouridis AEM:8997
  7. * @email cchoutou@ece.auth.gr
  8. */
  9. package net.hoo2.auth.vmodem;
  10. /** @name imports */
  11. /** @{ */
  12. import org.apache.commons.cli.*; /**< command line utility */
  13. /** @} */
  14. /**
  15. * @class VirtualModem
  16. *
  17. * @brief This is the main control class of the program.
  18. *
  19. * This class includes the main function which read the user input
  20. * and create the apropriate object for the requested sequence to
  21. * retrieve data.
  22. */
  23. public class VirtualModem {
  24. /** @name Data */
  25. /** @{ */
  26. CommandLine line; /**< Command line for the argument list */
  27. CommandLineParser parser; /**< Parser for the argument list */
  28. Options options; /**< Option configuration container */
  29. HelpFormatter formatter; /**< An extra helper for -h */
  30. Com com; /**< Reference to basic configuration module */
  31. Log log; /**< Reference to basig logging module */
  32. /** @} */
  33. /**
  34. * @brief The main constructor of the class
  35. * We allocate memory for the data needed for the command line
  36. */
  37. public VirtualModem () {
  38. parser = new DefaultParser();
  39. options = new Options();
  40. formatter = new HelpFormatter();
  41. com = new Com();
  42. // Create argument options and a basic help file
  43. // for future use.
  44. Option verb = new Option ("v", "verbose", false, "Be more verbose to the console");
  45. Option help = new Option ("h", "help", false, "Print this message");
  46. Option timeout = Option.builder("t")
  47. .longOpt("timeout")
  48. .hasArg()
  49. .valueSeparator('=')
  50. .desc("Select timeout in [sec]")
  51. .build();
  52. Option speed = Option.builder("s")
  53. .longOpt("speed")
  54. .hasArg()
  55. .valueSeparator('=')
  56. .desc("Select speed in [bps]")
  57. .build();
  58. Option log = Option.builder("l")
  59. .longOpt("log")
  60. .hasArg()
  61. .desc("Log file name to use")
  62. .build();
  63. Option echo = Option.builder("e")
  64. .longOpt("echo")
  65. .numberOfArgs(2)
  66. .desc ("Request echo sequence where <arg> = <1> <2>\n"
  67. + " <1>: The echo requested code from virtual lab\n"
  68. + " <2>: The time duration of the session [sec]")
  69. .build();
  70. Option aqr = Option.builder("a")
  71. .longOpt("aqr")
  72. .numberOfArgs(3)
  73. .desc ("Request aqr sequence where <arg> = <1> <2> <3>\n"
  74. + " <1>: The ACK requested code for virtual lab\n"
  75. + " <2>: The NACK requested code from virtual lab\n"
  76. + " <2>: The time duration for the session [sec]")
  77. .build();
  78. Option img = Option.builder("g")
  79. .longOpt("img")
  80. .numberOfArgs(3)
  81. .desc("Request an image sequence where <arg> = <1> <2> <3>\n"
  82. + " <1>: The image requested code from virtual lab\n"
  83. + " <2>: The filename to use for storing the image (without .jpg)\n"
  84. + " <3>: The requested images to fetch.")
  85. .build();
  86. Option gps = Option.builder("p")
  87. .longOpt("gps")
  88. .numberOfArgs(5) // G8164 10 800 8 gps_10_800
  89. .desc("Request a GPS sequence where <arg> = <1> <2> <3> <4>\n"
  90. + " <1>: The gps requested code from virtual lab\n"
  91. + " <2>: The time from trace to use as starting point [sec]\n"
  92. + " <3>: The time duration for the trace to fetch [sec]\n"
  93. + " <4>: The number of points to fetch from the above trace\n"
  94. + " <5>: The filename to use for storing the image (without .jpg)")
  95. .build();
  96. options.addOption(verb);
  97. options.addOption(help);
  98. options.addOption(timeout);
  99. options.addOption(speed);
  100. options.addOption(log);
  101. options.addOption(echo);
  102. options.addOption(aqr);
  103. options.addOption(img);
  104. options.addOption(gps);
  105. }
  106. /** @name private api */
  107. /**@{*/
  108. /**
  109. * parse the command line arguments
  110. * @param args the arguments to parse
  111. * @return the status of the operation
  112. */
  113. private boolean getCmdOptions (String[] args) {
  114. try {
  115. // parse the command line arguments
  116. line = parser.parse (options, args);
  117. }
  118. catch( ParseException exp ) {
  119. // oops, something went wrong
  120. System.err.println( "Parsing command line failed: " + exp.getMessage() );
  121. return false;
  122. }
  123. return true;
  124. }
  125. /**
  126. * Dispatch the correct functionality based on arguments
  127. * @return the status of the operation (currently true)
  128. */
  129. private boolean commandDispatcher () {
  130. boolean verbose = false;
  131. if (line.hasOption("help")) {
  132. formatter.printHelp( "virtualModem", options );
  133. return true;
  134. }
  135. // Get log and verbose options first
  136. if (line.hasOption("verbose"))
  137. verbose = true;
  138. if (line.hasOption("log"))
  139. log = new Log (line.getOptionValue("log"), verbose);
  140. else
  141. log = new Log (null, verbose);
  142. if (log.open() != true)
  143. return false;
  144. // get other options
  145. if (line.hasOption("timeout")) {
  146. com.timeout(Integer.parseInt(line.getOptionValue("timeout")));
  147. }
  148. if (line.hasOption("speed")) {
  149. com.speed(Integer.parseInt(line.getOptionValue("speed")));
  150. }
  151. // Execution dispatcher
  152. if (line.hasOption("echo")) {
  153. byte[] code = line.getOptionValues("echo")[0].getBytes();
  154. Echo e = new Echo(com, log, code,
  155. Integer.valueOf(line.getOptionValues("echo")[1]));
  156. if (com.open()) {
  157. e.caption(code);
  158. e.run();
  159. com.close();
  160. }
  161. }
  162. else if (line.hasOption("aqr")) {
  163. byte[] ack = line.getOptionValues("aqr")[0].getBytes();
  164. byte[] nack = line.getOptionValues("aqr")[1].getBytes();
  165. AQR a = new AQR(com, log, ack, nack,
  166. Integer.valueOf(line.getOptionValues("aqr")[2]));
  167. if (com.open()) {
  168. a.caption(ack, nack);
  169. a.run();
  170. com.close();
  171. }
  172. }
  173. else if (line.hasOption("img")) {
  174. byte[] code = line.getOptionValues("img")[0].getBytes();
  175. Image im = new Image (com, log, code,
  176. line.getOptionValues("img")[1],
  177. Integer.valueOf(line.getOptionValues("img")[2]));
  178. if (com.open()) {
  179. im.caption(code);
  180. im.run();
  181. com.close();
  182. }
  183. }
  184. else if (line.hasOption("gps")) {
  185. byte[] code = line.getOptionValues("gps")[0].getBytes();
  186. GPS g = new GPS (com, log, code,
  187. Integer.valueOf(line.getOptionValues("gps")[1]),
  188. Integer.valueOf(line.getOptionValues("gps")[2]),
  189. Integer.valueOf(line.getOptionValues("gps")[3]));
  190. Image im = new Image (com, log, null,
  191. line.getOptionValues("gps")[4], 1);
  192. if (com.open()) {
  193. g.caption();
  194. im.code(g.run().getBytes());
  195. im.run();
  196. com.close();
  197. }
  198. }
  199. log.close();
  200. return true;
  201. }
  202. /**@}*/
  203. /**
  204. * @brief Main function
  205. */
  206. public static void main(String[] args) {
  207. // allocate the main object
  208. VirtualModem vmodem = new VirtualModem();
  209. // prepare command line input
  210. if (vmodem.getCmdOptions (args) != true)
  211. return;
  212. // Call the requested functionality
  213. if (vmodem.commandDispatcher() != true)
  214. return;
  215. }
  216. }