|
- /**
- * @file VirtualModem.java
- * @brief
- * Contain the Main class for the project VirtualModem
- *
- * @author Christos Choutouridis AEM:8997
- * @email cchoutou@ece.auth.gr
- */
- package net.hoo2.auth.vmodem;
-
- /** @name imports */
- /** @{ */
- import org.apache.commons.cli.*;
- /** @} */
-
-
- /**
- * @class VirtualModem
- *
- * @brief This is the main control class of the program.
- *
- * This class includes the main function.Using this class's api
- * the user can ...
- */
- public class VirtualModem {
-
- /** @name Data */
- /** @{ */
- CommandLine line;
- CommandLineParser parser;
- Options options;
- HelpFormatter formatter;
- Com com;
- String logfile;
- boolean verbose;
- /** @} */
-
- /** @name constructors */
- /** @{ */
- public VirtualModem () {
- parser = new DefaultParser();
- options = new Options();
- formatter = new HelpFormatter();
- com = new Com();
- logfile = null;
- verbose = false;
- // line is initialized in getCmdOptions()
-
- Option verbose = new Option ("v", "verbose", false, "Be more verbose");
- Option help = new Option ("h", "help", false, "Print this message");
- Option timeout = Option.builder("t")
- .longOpt("timeout")
- .hasArg()
- .valueSeparator('=')
- .desc("Select timeout in [sec]")
- .build();
- Option speed = Option.builder("s")
- .longOpt("speed")
- .hasArg()
- .valueSeparator('=')
- .desc("Select speed in [bps]")
- .build();
- Option log = Option.builder("l")
- .longOpt("log")
- .hasArg()
- .desc("Log file name")
- .build();
- Option echo = Option.builder("e")
- .longOpt("echo")
- .numberOfArgs(2)
- .desc ("Request echo sequence")
- .build();
-
- options.addOption(verbose);
- options.addOption(help);
- options.addOption(timeout);
- options.addOption(speed);
- options.addOption(log);
- options.addOption(echo);
- }
- /** @} */
-
- private boolean getCmdOptions (String[] args) {
- try {
- // parse the command line arguments
- line = parser.parse (options, args);
- }
- catch( ParseException exp ) {
- // oops, something went wrong
- System.err.println( "Parsing command line failed: " + exp.getMessage() );
- return false;
- }
- return true;
- }
-
- private boolean commandDispatcher () {
- // Get boolean options first
- if (line.hasOption("verbose")) {
- verbose = true;
- }
- // get options
- if (line.hasOption("timeout")) {
- com.timeout(Integer.parseInt(line.getOptionValue("timeout")));
- }
- if (line.hasOption("speed")) {
- com.speed(Integer.parseInt(line.getOptionValue("speed")));
- }
- if (line.hasOption("log")) {
- logfile = line.getOptionValue("log");
- }
- // Execution dispatcher
- if (line.hasOption("help")) {
- formatter.printHelp( "virtualModem", options );
- return true;
- }
- if (line.hasOption("echo")) {
- Echo e = new Echo(com,
- line.getOptionValues("echo")[0].getBytes(),
- Integer.valueOf(line.getOptionValues("echo")[1]),
- logfile);
- if (com.open() == true) {
- e.run(verbose);
- com.close();
- }
- }
- else {
- System.err.println ("Error: Unrecognized option");
- return false;
- }
- return true;
- }
- /**
- * @brief Main
- *
- */
- public static void main(String[] args) {
- // allocate the main object
- VirtualModem vmodem = new VirtualModem();
-
- // prepare command line input
- if (vmodem.getCmdOptions (args) != true)
- return;
- if (vmodem.commandDispatcher() != true)
- return;
-
- }
-
- }
|