Version 3.1.2: Server/ServerBridge bugfix
authorNiki Roo <niki@nikiroo.be>
Wed, 29 Nov 2017 19:58:41 +0000 (20:58 +0100)
committerNiki Roo <niki@nikiroo.be>
Wed, 29 Nov 2017 19:58:41 +0000 (20:58 +0100)
VERSION
changelog.md
src/be/nikiroo/utils/TraceHandler.java
src/be/nikiroo/utils/serial/server/ConnectAction.java
src/be/nikiroo/utils/serial/server/ServerBridge.java

diff --git a/VERSION b/VERSION
index 94ff29cc4de4fe0be04331a1eb51f5c515b728a9..ef538c2810938c03ced86f0380977b308a55b37b 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-3.1.1
+3.1.2
index fa6415792878d181244ec912d58f3c0ea4d0e5c8..f9aeeafa98c3286b289ca4f8907ef2399988d8f6 100644 (file)
@@ -1,5 +1,10 @@
 # nikiroo-utils
 
+## Version 3.1.2
+
+- Fix Server regarding the client version passed to the handler
+- Improve ServerBridge options
+
 ## Version 3.1.1
 
 - Some fixes and trace handling changes in ServerBridge
index daa3743f387832d446f91cea0ba119747f1167aa..be9beeffd780af92c80f7ea16d389723b863419b 100644 (file)
@@ -10,6 +10,7 @@ public class TraceHandler {
        private final boolean showErrors;
        private final boolean showErrorDetails;
        private final int traceLevel;
+       private final int maxPrintSize;
 
        /**
         * Create a default {@link TraceHandler} that will print errors on stderr
@@ -47,9 +48,29 @@ public class TraceHandler {
         */
        public TraceHandler(boolean showErrors, boolean showErrorDetails,
                        int traceLevel) {
+               this(showErrors, showErrorDetails, traceLevel, -1);
+       }
+
+       /**
+        * Create a default {@link TraceHandler}.
+        * 
+        * @param showErrors
+        *            show errors on stderr
+        * @param showErrorDetails
+        *            show more details when printing errors
+        * @param traceLevel
+        *            show traces of this level or lower (0 means "no traces",
+        *            higher means more traces)
+        * @param maxPrintSize
+        *            the maximum size at which to truncate traces data (or -1 for
+        *            "no limit")
+        */
+       public TraceHandler(boolean showErrors, boolean showErrorDetails,
+                       int traceLevel, int maxPrintSize) {
                this.showErrors = showErrors;
                this.showErrorDetails = showErrorDetails;
                this.traceLevel = Math.max(traceLevel, 0);
+               this.maxPrintSize = maxPrintSize;
        }
 
        /**
@@ -92,8 +113,8 @@ public class TraceHandler {
        /**
         * A trace happened, show it.
         * <p>
-        * By default, will only be effective if {@link TraceHandler#showTraces} is
-        * true
+        * By default, will only be effective if {@link TraceHandler#traceLevel} is
+        * not 0.
         * <p>
         * A call to this method is equivalent to a call to
         * {@link TraceHandler#trace(String, int)} with a level of 1.
@@ -108,8 +129,8 @@ public class TraceHandler {
        /**
         * A trace happened, show it.
         * <p>
-        * By default, will only be effective if {@link TraceHandler#showTraces} is
-        * true and the level is lower or equal to {@link TraceHandler#traceLevel}.
+        * By default, will only be effective if {@link TraceHandler#traceLevel} is
+        * not 0 and the level is lower or equal to it.
         * 
         * @param message
         *            the trace message
@@ -118,7 +139,12 @@ public class TraceHandler {
         */
        public void trace(String message, int level) {
                if (traceLevel > 0 && level <= traceLevel) {
-                       System.out.println(message);
+                       if (maxPrintSize > 0 && message.length() > maxPrintSize) {
+                               System.out
+                                               .println(message.substring(0, maxPrintSize) + "[...]");
+                       } else {
+                               System.out.println(message);
+                       }
                }
        }
 }
index 8f57c628fb18e861c9805174468f5ccb0ce0fba1..dfdb53ed017da95df65d80f5b8111fc5e0fdc153 100644 (file)
@@ -108,6 +108,20 @@ abstract class ConnectAction {
                                out = new OutputStreamWriter(s.getOutputStream(), "UTF-8");
                                try {
                                        if (server) {
+                                               String line = in.readLine();
+                                               if (line != null && line.startsWith("VERSION ")) {
+                                                       // "VERSION client-version" (VERSION 1.0.0)
+                                                       Version clientVersion = new Version(
+                                                                       line.substring("VERSION ".length()));
+                                                       this.clientVersion = clientVersion;
+                                                       Version v = negotiateVersion(clientVersion);
+                                                       if (v == null) {
+                                                               v = new Version();
+                                                       }
+
+                                                       sendString("VERSION " + v.toString());
+                                               }
+
                                                action(clientVersion);
                                        } else {
                                                String v = sendString("VERSION " + version.toString());
@@ -253,22 +267,7 @@ abstract class ConnectAction {
                                        contentToSend = false;
                                }
 
-                               String line = in.readLine();
-                               if (server && line != null && line.startsWith("VERSION ")) {
-                                       // "VERSION client-version" (VERSION 1.0.0)
-                                       Version clientVersion = new Version(
-                                                       line.substring("VERSION ".length()));
-                                       this.clientVersion = clientVersion;
-                                       Version v = negotiateVersion(clientVersion);
-                                       if (v == null) {
-                                               v = new Version();
-                                       }
-                                       sendString("VERSION " + v.toString());
-
-                                       line = in.readLine();
-                               }
-
-                               return line;
+                               return in.readLine();
                        }
 
                        return null;
index b72c98ddd44fe45dcc0b8cb99f97565b4240e6b7..251e75a99a9cdb10d1b282a90cd4c9940a326aef 100644 (file)
@@ -151,6 +151,7 @@ public class ServerBridge extends Server {
         */
        protected void onServerContact(Version serverVersion) {
                getTraceHandler().trace(">>> SERVER " + serverVersion);
+               getTraceHandler().trace("");
        }
 
        /**
@@ -232,8 +233,7 @@ public class ServerBridge extends Server {
                                getTraceHandler().trace("", 4);
                        } catch (NoSuchFieldException e) {
                                getTraceHandler().trace(
-                                               "(object known but incompatible: " + e.getMessage()
-                                                               + ")", 2);
+                                               "(incompatible: " + e.getMessage() + ")", 2);
                                getTraceHandler().trace(data, 3);
                                getTraceHandler().trace("", 4);
                        } catch (ClassNotFoundException e) {
@@ -242,9 +242,8 @@ public class ServerBridge extends Server {
                                getTraceHandler().trace(data, 3);
                                getTraceHandler().trace("", 4);
                        } catch (Exception e) {
-                               getTraceHandler()
-                                               .trace("(error when trying to decode: "
-                                                               + e.getMessage() + ")", 2);
+                               getTraceHandler().trace(
+                                               "(decode error: " + e.getMessage() + ")", 2);
                                getTraceHandler().trace(data, 3);
                                getTraceHandler().trace("", 4);
                        }
@@ -266,6 +265,7 @@ public class ServerBridge extends Server {
         *            <li>The forward server port</li>
         *            <li>TRUE for an SSL forward server, FALSE for plain text</li>
         *            <li>(optional) a trace level</li>
+        *            <li>(optional) a truncate size for data</li>
         *            </ul>
         */
        public static void main(String[] args) {
@@ -273,14 +273,15 @@ public class ServerBridge extends Server {
                try {
                        if (args.length < 6) {
                                tracer.error("Invalid syntax.\n"
-                                               + "Syntax: [name] [port] [ssl] [fhost] [fport] [fssl] ([trace level])\n"
+                                               + "Syntax: [name] [port] [ssl] [fhost] [fport] [fssl] ([trace level]) ([max])\n"
                                                + "\tname: the bridge name\n"
                                                + "\tport: the bridge port\n"
                                                + "\tssl: TRUE for an SSL bridge, FALSE for plain text\n"
                                                + "\tfhost: the forward server host\n"
                                                + "\tfport: the forward server port\n"
                                                + "\tfssl: TRUE for an SSL forward server, FALSE for plain text\n"
-                                               + "\ttrace level: the optional trace level (default is 1)\n");
+                                               + "\ttrace level: the optional trace level (default is 1)\n"
+                                               + "\tmax: the maximum size after which to truncate data\n");
                                return;
                        }
 
@@ -296,10 +297,15 @@ public class ServerBridge extends Server {
                        if (args.length > 6) {
                                traceLevel = Integer.parseInt(args[i++]);
                        }
+                       int maxPrintSize = 1;
+                       if (args.length > 7) {
+                               maxPrintSize = Integer.parseInt(args[i++]);
+                       }
 
                        ServerBridge bridge = new ServerBridge(name, port, ssl, fhost,
                                        fport, fssl);
-                       bridge.setTraceHandler(new TraceHandler(true, true, traceLevel));
+                       bridge.setTraceHandler(new TraceHandler(true, true, traceLevel,
+                                       maxPrintSize));
                        bridge.run();
                } catch (Exception e) {
                        tracer.error(e);