|
- /**
- * @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;
- Log log;
- /** @} */
-
- /** @name constructors */
- /** @{ */
- public VirtualModem () {
- parser = new DefaultParser();
- options = new Options();
- formatter = new HelpFormatter();
- com = new Com();
-
-
- Option verb = 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();
- Option aqr = Option.builder("a")
- .longOpt("aqr")
- .numberOfArgs(3)
- .desc ("Request aqr sequence")
- .build();
- Option img = Option.builder("g")
- .longOpt("img")
- .numberOfArgs(3)
- .desc("Request an image sequence")
- .build();
- Option gps = Option.builder("p")
- .longOpt("gps")
- .numberOfArgs(5)
- .desc("Request a GPS sequence")
- .build();
- options.addOption(verb);
- options.addOption(help);
- options.addOption(timeout);
- options.addOption(speed);
- options.addOption(log);
- options.addOption(echo);
- options.addOption(aqr);
- options.addOption(img);
- options.addOption(gps);
- }
- /** @} */
-
- 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 () {
- boolean verbose = false;
-
- // Get log and verbose options first
- if (line.hasOption("verbose"))
- verbose = true;
- if (line.hasOption("log"))
- log = new Log (line.getOptionValue("log"), verbose);
- else
- log = new Log (null, verbose);
- if (log.open() != true)
- return false;
-
- // get other options
- if (line.hasOption("timeout")) {
- com.timeout(Integer.parseInt(line.getOptionValue("timeout")));
- }
- if (line.hasOption("speed")) {
- com.speed(Integer.parseInt(line.getOptionValue("speed")));
- }
-
- // Execution dispatcher
- do {
- if (line.hasOption("help")) {
- formatter.printHelp( "virtualModem", options );
- break;
- }
- if (line.hasOption("echo")) {
- byte[] code = line.getOptionValues("echo")[0].getBytes();
- Echo e = new Echo(com, log, code,
- Integer.valueOf(line.getOptionValues("echo")[1]));
- if (com.open()) {
- e.caption(code);
- e.run();
- com.close();
-
- }
- }
- else if (line.hasOption("aqr")) {
- byte[] ack = line.getOptionValues("aqr")[0].getBytes();
- byte[] nack = line.getOptionValues("aqr")[1].getBytes();
- AQR a = new AQR(com, log, ack, nack,
- Integer.valueOf(line.getOptionValues("aqr")[2]));
- if (com.open()) {
- a.caption(ack, nack);
- a.run();
- com.close();
-
- }
- }
- else if (line.hasOption("img")) {
- byte[] code = line.getOptionValues("img")[0].getBytes();
- Image im = new Image (com, log, code,
- line.getOptionValues("img")[1],
- Integer.valueOf(line.getOptionValues("img")[2]));
- if (com.open()) {
- im.caption(code);
- im.run();
- com.close();
- }
- }
- else if (line.hasOption("gps")) {
- byte[] code = line.getOptionValues("gps")[0].getBytes();
- GPS g = new GPS (com, log, code,
- Integer.valueOf(line.getOptionValues("gps")[1]),
- Integer.valueOf(line.getOptionValues("gps")[2]),
- Integer.valueOf(line.getOptionValues("gps")[3]));
- Image im = new Image (com, log, null,
- line.getOptionValues("gps")[4], 1);
- if (com.open()) {
- g.caption();
- im.code(g.run().getBytes());
- im.run();
- com.close();
- }
- }
- } while (false);
-
- log.close();
- 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;
-
- }
-
- }
|