From 217a3310efed6da8b1765738e6386825b3002570 Mon Sep 17 00:00:00 2001 From: Niki Roo Date: Wed, 29 Nov 2017 20:58:41 +0100 Subject: [PATCH] Version 3.1.2: Server/ServerBridge bugfix --- VERSION | 2 +- changelog.md | 5 +++ src/be/nikiroo/utils/TraceHandler.java | 36 ++++++++++++++++--- .../utils/serial/server/ConnectAction.java | 31 ++++++++-------- .../utils/serial/server/ServerBridge.java | 22 +++++++----- 5 files changed, 66 insertions(+), 30 deletions(-) diff --git a/VERSION b/VERSION index 94ff29cc..ef538c28 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.1.1 +3.1.2 diff --git a/changelog.md b/changelog.md index fa641579..f9aeeafa 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/src/be/nikiroo/utils/TraceHandler.java b/src/be/nikiroo/utils/TraceHandler.java index daa3743f..be9beeff 100644 --- a/src/be/nikiroo/utils/TraceHandler.java +++ b/src/be/nikiroo/utils/TraceHandler.java @@ -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. *

- * 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. *

* 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. *

- * 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); + } } } } diff --git a/src/be/nikiroo/utils/serial/server/ConnectAction.java b/src/be/nikiroo/utils/serial/server/ConnectAction.java index 8f57c628..dfdb53ed 100644 --- a/src/be/nikiroo/utils/serial/server/ConnectAction.java +++ b/src/be/nikiroo/utils/serial/server/ConnectAction.java @@ -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; diff --git a/src/be/nikiroo/utils/serial/server/ServerBridge.java b/src/be/nikiroo/utils/serial/server/ServerBridge.java index b72c98dd..251e75a9 100644 --- a/src/be/nikiroo/utils/serial/server/ServerBridge.java +++ b/src/be/nikiroo/utils/serial/server/ServerBridge.java @@ -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 { *

  • The forward server port
  • *
  • TRUE for an SSL forward server, FALSE for plain text
  • *
  • (optional) a trace level
  • + *
  • (optional) a truncate size for data
  • * */ 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); -- 2.27.0