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.
 
 
 
 

248 lines
9.3 KiB

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