# 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
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
*/
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;
}
/**
/**
* 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.
/**
* 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
*/
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);
+ }
}
}
}
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());
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;
*/
protected void onServerContact(Version serverVersion) {
getTraceHandler().trace(">>> SERVER " + serverVersion);
+ getTraceHandler().trace("");
}
/**
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) {
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);
}
* <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) {
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;
}
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);