fix bridge util todos
authorNiki Roo <niki@nikiroo.be>
Wed, 29 May 2019 19:47:42 +0000 (21:47 +0200)
committerNiki Roo <niki@nikiroo.be>
Wed, 29 May 2019 19:47:42 +0000 (21:47 +0200)
src/be/nikiroo/utils/Downloader.java
src/be/nikiroo/utils/main/bridge.java
src/be/nikiroo/utils/resources/Bundle.java
src/be/nikiroo/utils/serial/SerialUtils.java

index 68e4dd73e285a941e3193ac6155d846fea2e8316..7ee551ff38f4f0692d11fd35678e283c5cec359d 100644 (file)
@@ -1,6 +1,5 @@
 package be.nikiroo.utils;
 
-import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStreamWriter;
index f2f3df2c6fad170190309e2f2df074be3f10189c..1b7ab851e2773a2218345c0240b0814378908137 100644 (file)
@@ -10,64 +10,118 @@ import be.nikiroo.utils.serial.server.ServerBridge;
  * @author niki
  */
 public class bridge {
+       /**
+        * The optional options that can be passed to the program.
+        * 
+        * @author niki
+        */
+       private enum Option {
+               /**
+                * The encryption key for the input data (optional, but can also be
+                * empty <b>which is different</b> (it will then use an empty encryption
+                * key)).
+                */
+               KEY,
+               /**
+                * The encryption key for the output data (optional, but can also be
+                * empty <b>which is different</b> (it will then use an empty encryption
+                * key)).
+                */
+               FORWARD_KEY,
+               /** The trace level (1, 2, 3.. default is 1). */
+               TRACE_LEVEL,
+               /**
+                * The maximum length after which to truncate data to display (the whole
+                * data will still be sent).
+                */
+               MAX_DISPLAY_SIZE,
+               /** The help message. */
+               HELP,
+       }
+
+       static private String getSyntax() {
+               return "Syntax: (--options) (--) [NAME] [PORT] [FORWARD_HOST] [FORWARD_PORT]\n"//
+                               + "\tNAME         : the bridge name for display/debug purposes\n"//
+                               + "\tPORT         : the port to listen on\n"//
+                               + "\tFORWARD_HOST : the host to connect to\n"//
+                               + "\tFORWARD_PORT : the port to connect to\n"//
+                               + "\n" //
+                               + "\tOptions: \n" //
+                               + "\t--                 : no more options in the rest of the parameters\n" //
+                               + "\t--help             : this help message\n" //
+                               + "\t--key              : the INCOMING encryption key\n" //
+                               + "\t--forward-key      : the OUTGOING encryption key\n" //
+                               + "\t--trace-level      : the trace level (1, 2, 3... default is 1)\n" //
+                               + "\t--max-display-size : the maximum size after which to \n"//
+                               + "\t        truncate the messages to display (the full message will still be sent)\n" //
+               ;
+       }
+
        /**
         * Start a bridge between 2 servers.
         * 
         * @param args
-        *            an array containing:
-        *            <ul>
-        *            <li>The bridge name</li>
-        *            <li>The bridge port</li>
-        *            <li>a key for an encrypted bridge, PLAIN_TEXT for plain text</li>
-        *            <li>The forward server host</li>
-        *            <li>The forward server port</li>
-        *            <li>a key for an encrypted forward server, PLAIN_TEXT for
-        *            plain text</li>
-        *            <li>(optional) a trace level</li>
-        *            <li>(optional) a truncate size for data</li>
-        *            </ul>
+        *            the parameters, which can be seen by passing "--help" or just
+        *            calling the program without parameters
         */
        public static void main(String[] args) {
                final TraceHandler tracer = new TraceHandler(true, false, 0);
                try {
-                       if (args.length < 6) {
-                               tracer.error("Invalid syntax.\n"
-                                               + "Syntax: [name] [port] [ssl] [fhost] [fport] [fssl] ([trace level]) ([max])\n"
-                                               + "\tname: the bridge name\n"
-                                               + "\tport: the bridge port\n"
-                                               + "\tkey: a key for an encrypted bridge, PLAIN_TEXT for plain text\n"
-                                               + "\tfhost: the forward server host\n"
-                                               + "\tfport: the forward server port\n"
-                                               + "\tfkey: a key for an encrypted forward server, PLAIN_TEXT for plain text\n"
-                                               + "\ttrace level: the optional trace level (default is 1)\n"
-                                               + "\tmax: the maximum size after which to truncate data\n");
-                               return;
+                       if (args.length == 0) {
+                               tracer.error(getSyntax());
+                               System.exit(0);
                        }
 
+                       String key = null;
+                       String fkey = null;
+                       int traceLevel = 1;
+                       int maxPrintSize = 0;
+
                        int i = 0;
+                       while (args[i].startsWith("--")) {
+                               String arg = args[i];
+                               i++;
+
+                               if (arg.equals("--")) {
+                                       break;
+                               }
+
+                               arg = arg.substring(2).toUpperCase().replace("-", "_");
+                               try {
+                                       Option opt = Enum.valueOf(Option.class, arg);
+                                       switch (opt) {
+                                       case HELP:
+                                               tracer.trace(getSyntax());
+                                               System.exit(0);
+                                               break;
+                                       case FORWARD_KEY:
+                                               fkey = args[i++];
+                                               break;
+                                       case KEY:
+                                               key = args[i++];
+                                               break;
+                                       case MAX_DISPLAY_SIZE:
+                                               maxPrintSize = Integer.parseInt(args[i++]);
+                                               break;
+                                       case TRACE_LEVEL:
+                                               traceLevel = Integer.parseInt(args[i++]);
+                                               break;
+                                       }
+                               } catch (Exception e) {
+                                       tracer.error(getSyntax());
+                                       System.exit(1);
+                               }
+                       }
+
+                       if ((args.length - i) != 4) {
+                               tracer.error(getSyntax());
+                               System.exit(2);
+                       }
+
                        String name = args[i++];
                        int port = Integer.parseInt(args[i++]);
-                       String key = args[i++];
-                       // TODO: bad
-                       if ("PLAIN_TEXT".equals(key)) {
-                               key = null;
-                       }
                        String fhost = args[i++];
                        int fport = Integer.parseInt(args[i++]);
-                       String fkey = args[i++];
-                       // TODO: bad
-                       if ("PLAIN_TEXT".equals(fkey)) {
-                               fkey = null;
-                       }
-
-                       int traceLevel = 1;
-                       if (args.length > 6) {
-                               traceLevel = Integer.parseInt(args[i++]);
-                       }
-                       int maxPrintSize = 0;
-                       if (args.length > 7) {
-                               maxPrintSize = Integer.parseInt(args[i++]);
-                       }
 
                        ServerBridge bridge = new ServerBridge(name, port, key, fhost,
                                        fport, fkey);
@@ -76,6 +130,7 @@ public class bridge {
                        bridge.run();
                } catch (Exception e) {
                        tracer.error(e);
+                       System.exit(42);
                }
        }
 }
index 68c236fa108e7f54d842775d408607f7e2a7551c..21021e065f05a938f4521bfb5fd399b40847a77d 100644 (file)
@@ -143,7 +143,8 @@ public class Bundle<E extends Enum<E>> {
        /**
         * Return the value associated to the given id as a {@link String}.
         * <p>
-        * If no value is associated, take the default one if any.
+        * If no value is associated (or if it is empty!), take the default one if
+        * any.
         * 
         * @param id
         *            the id of the value to get
@@ -168,7 +169,6 @@ public class Bundle<E extends Enum<E>> {
                        }
                }
 
-               //TODO: is it ok? need to jDoc?
                if (rep == null || rep.isEmpty()) {
                        return def;
                }
index 43aafb2f47d3ecf3edff03f5b4b2347618019b85..c4b7cf9db8539c569d172fd9104a639b10b37823 100644 (file)
@@ -670,7 +670,6 @@ public class SerialUtils {
        static void encodeString(OutputStream out, String raw) throws IOException {
                // TODO: not. efficient.
                out.write('\"');
-               // TODO !! utf-8 required
                for (char car : raw.toCharArray()) {
                        encodeString(out, car);
                }
@@ -691,7 +690,7 @@ public class SerialUtils {
                out.write('\"');
        }
 
-       // for encode string, NOT to encode a char by itself!
+       // for encoding string, NOT to encode a char by itself!
        static void encodeString(OutputStream out, char raw) throws IOException {
                switch (raw) {
                case '\\':