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.
 
 
 
 

208 line
6.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.*;
  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.Using this class's api
  20. * the user can ...
  21. */
  22. public class VirtualModem {
  23. /** @name Data */
  24. /** @{ */
  25. CommandLine line;
  26. CommandLineParser parser;
  27. Options options;
  28. HelpFormatter formatter;
  29. Com com;
  30. Log log;
  31. /** @} */
  32. /** @name constructors */
  33. /** @{ */
  34. public VirtualModem () {
  35. parser = new DefaultParser();
  36. options = new Options();
  37. formatter = new HelpFormatter();
  38. com = new Com();
  39. Option verb = new Option ("v", "verbose", false, "Be more verbose");
  40. Option help = new Option ("h", "help", false, "Print this message");
  41. Option timeout = Option.builder("t")
  42. .longOpt("timeout")
  43. .hasArg()
  44. .valueSeparator('=')
  45. .desc("Select timeout in [sec]")
  46. .build();
  47. Option speed = Option.builder("s")
  48. .longOpt("speed")
  49. .hasArg()
  50. .valueSeparator('=')
  51. .desc("Select speed in [bps]")
  52. .build();
  53. Option log = Option.builder("l")
  54. .longOpt("log")
  55. .hasArg()
  56. .desc("Log file name")
  57. .build();
  58. Option echo = Option.builder("e")
  59. .longOpt("echo")
  60. .numberOfArgs(2)
  61. .desc ("Request echo sequence")
  62. .build();
  63. Option aqr = Option.builder("a")
  64. .longOpt("aqr")
  65. .numberOfArgs(3)
  66. .desc ("Request aqr sequence")
  67. .build();
  68. Option img = Option.builder("g")
  69. .longOpt("img")
  70. .numberOfArgs(3)
  71. .desc("Request an image sequence")
  72. .build();
  73. Option gps = Option.builder("p")
  74. .longOpt("gps")
  75. .numberOfArgs(5)
  76. .desc("Request a GPS sequence")
  77. .build();
  78. options.addOption(verb);
  79. options.addOption(help);
  80. options.addOption(timeout);
  81. options.addOption(speed);
  82. options.addOption(log);
  83. options.addOption(echo);
  84. options.addOption(aqr);
  85. options.addOption(img);
  86. options.addOption(gps);
  87. }
  88. /** @} */
  89. private boolean getCmdOptions (String[] args) {
  90. try {
  91. // parse the command line arguments
  92. line = parser.parse (options, args);
  93. }
  94. catch( ParseException exp ) {
  95. // oops, something went wrong
  96. System.err.println( "Parsing command line failed: " + exp.getMessage() );
  97. return false;
  98. }
  99. return true;
  100. }
  101. private boolean commandDispatcher () {
  102. boolean verbose = false;
  103. // Get log and verbose options first
  104. if (line.hasOption("verbose"))
  105. verbose = true;
  106. if (line.hasOption("log"))
  107. log = new Log (line.getOptionValue("log"), verbose);
  108. else
  109. log = new Log (null, verbose);
  110. if (log.open() != true)
  111. return false;
  112. // get other options
  113. if (line.hasOption("timeout")) {
  114. com.timeout(Integer.parseInt(line.getOptionValue("timeout")));
  115. }
  116. if (line.hasOption("speed")) {
  117. com.speed(Integer.parseInt(line.getOptionValue("speed")));
  118. }
  119. // Execution dispatcher
  120. do {
  121. if (line.hasOption("help")) {
  122. formatter.printHelp( "virtualModem", options );
  123. break;
  124. }
  125. if (line.hasOption("echo")) {
  126. byte[] code = line.getOptionValues("echo")[0].getBytes();
  127. Echo e = new Echo(com, log, code,
  128. Integer.valueOf(line.getOptionValues("echo")[1]));
  129. if (com.open()) {
  130. e.caption(code);
  131. e.run();
  132. com.close();
  133. }
  134. }
  135. else if (line.hasOption("aqr")) {
  136. byte[] ack = line.getOptionValues("aqr")[0].getBytes();
  137. byte[] nack = line.getOptionValues("aqr")[1].getBytes();
  138. AQR a = new AQR(com, log, ack, nack,
  139. Integer.valueOf(line.getOptionValues("aqr")[2]));
  140. if (com.open()) {
  141. a.caption(ack, nack);
  142. a.run();
  143. com.close();
  144. }
  145. }
  146. else if (line.hasOption("img")) {
  147. byte[] code = line.getOptionValues("img")[0].getBytes();
  148. Image im = new Image (com, log, code,
  149. line.getOptionValues("img")[1],
  150. Integer.valueOf(line.getOptionValues("img")[2]));
  151. if (com.open()) {
  152. im.caption(code);
  153. im.run();
  154. com.close();
  155. }
  156. }
  157. else if (line.hasOption("gps")) {
  158. byte[] code = line.getOptionValues("gps")[0].getBytes();
  159. GPS g = new GPS (com, log, code,
  160. Integer.valueOf(line.getOptionValues("gps")[1]),
  161. Integer.valueOf(line.getOptionValues("gps")[2]),
  162. Integer.valueOf(line.getOptionValues("gps")[3]));
  163. Image im = new Image (com, log, null,
  164. line.getOptionValues("gps")[4], 1);
  165. if (com.open()) {
  166. g.caption();
  167. im.code(g.run().getBytes());
  168. im.run();
  169. com.close();
  170. }
  171. }
  172. } while (false);
  173. log.close();
  174. return true;
  175. }
  176. /**
  177. * @brief Main
  178. *
  179. */
  180. public static void main(String[] args) {
  181. // allocate the main object
  182. VirtualModem vmodem = new VirtualModem();
  183. // prepare command line input
  184. if (vmodem.getCmdOptions (args) != true)
  185. return;
  186. if (vmodem.commandDispatcher() != true)
  187. return;
  188. }
  189. }