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.
 
 
 
 

149 lines
4.4 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. String logfile;
  31. boolean verbose;
  32. /** @} */
  33. /** @name constructors */
  34. /** @{ */
  35. public VirtualModem () {
  36. parser = new DefaultParser();
  37. options = new Options();
  38. formatter = new HelpFormatter();
  39. com = new Com();
  40. logfile = null;
  41. verbose = false;
  42. // line is initialized in getCmdOptions()
  43. Option verbose = new Option ("v", "verbose", false, "Be more verbose");
  44. Option help = new Option ("h", "help", false, "Print this message");
  45. Option timeout = Option.builder("t")
  46. .longOpt("timeout")
  47. .hasArg()
  48. .valueSeparator('=')
  49. .desc("Select timeout in [sec]")
  50. .build();
  51. Option speed = Option.builder("s")
  52. .longOpt("speed")
  53. .hasArg()
  54. .valueSeparator('=')
  55. .desc("Select speed in [bps]")
  56. .build();
  57. Option log = Option.builder("l")
  58. .longOpt("log")
  59. .hasArg()
  60. .desc("Log file name")
  61. .build();
  62. Option echo = Option.builder("e")
  63. .longOpt("echo")
  64. .numberOfArgs(2)
  65. .desc ("Request echo sequence")
  66. .build();
  67. options.addOption(verbose);
  68. options.addOption(help);
  69. options.addOption(timeout);
  70. options.addOption(speed);
  71. options.addOption(log);
  72. options.addOption(echo);
  73. }
  74. /** @} */
  75. private boolean getCmdOptions (String[] args) {
  76. try {
  77. // parse the command line arguments
  78. line = parser.parse (options, args);
  79. }
  80. catch( ParseException exp ) {
  81. // oops, something went wrong
  82. System.err.println( "Parsing command line failed: " + exp.getMessage() );
  83. return false;
  84. }
  85. return true;
  86. }
  87. private boolean commandDispatcher () {
  88. // Get boolean options first
  89. if (line.hasOption("verbose")) {
  90. verbose = true;
  91. }
  92. // get options
  93. if (line.hasOption("timeout")) {
  94. com.timeout(Integer.parseInt(line.getOptionValue("timeout")));
  95. }
  96. if (line.hasOption("speed")) {
  97. com.speed(Integer.parseInt(line.getOptionValue("speed")));
  98. }
  99. if (line.hasOption("log")) {
  100. logfile = line.getOptionValue("log");
  101. }
  102. // Execution dispatcher
  103. if (line.hasOption("help")) {
  104. formatter.printHelp( "virtualModem", options );
  105. return true;
  106. }
  107. if (line.hasOption("echo")) {
  108. Echo e = new Echo(com,
  109. line.getOptionValues("echo")[0].getBytes(),
  110. Integer.valueOf(line.getOptionValues("echo")[1]),
  111. logfile);
  112. if (com.open() == true) {
  113. e.run(verbose);
  114. com.close();
  115. }
  116. }
  117. else {
  118. System.err.println ("Error: Unrecognized option");
  119. return false;
  120. }
  121. return true;
  122. }
  123. /**
  124. * @brief Main
  125. *
  126. */
  127. public static void main(String[] args) {
  128. // allocate the main object
  129. VirtualModem vmodem = new VirtualModem();
  130. // prepare command line input
  131. if (vmodem.getCmdOptions (args) != true)
  132. return;
  133. if (vmodem.commandDispatcher() != true)
  134. return;
  135. }
  136. }