X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Futils%2Fserial%2Fserver%2FServerBridge.java;h=db50876b32aafb144bc89047e73073c391c569f8;hb=b6d4ace81dc228c4dbb5c5fc218e508cd97df5de;hp=b72c98ddd44fe45dcc0b8cb99f97565b4240e6b7;hpb=452f38c8b9e98215f4ed6def6f3f8fd5dfa75daa;p=fanfix.git diff --git a/src/be/nikiroo/utils/serial/server/ServerBridge.java b/src/be/nikiroo/utils/serial/server/ServerBridge.java index b72c98d..db50876 100644 --- a/src/be/nikiroo/utils/serial/server/ServerBridge.java +++ b/src/be/nikiroo/utils/serial/server/ServerBridge.java @@ -3,6 +3,7 @@ package be.nikiroo.utils.serial.server; import java.io.IOException; import java.lang.reflect.Array; import java.net.Socket; +import java.net.UnknownHostException; import be.nikiroo.utils.StringUtils; import be.nikiroo.utils.TraceHandler; @@ -45,6 +46,11 @@ public class ServerBridge extends Server { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ServerBridge(int port, boolean ssl, String forwardToHost, int forwardToPort, boolean forwardToSsl) throws IOException { @@ -73,6 +79,11 @@ public class ServerBridge extends Server { * * @throws IOException * in case of I/O error + * @throws UnknownHostException + * if the IP address of the host could not be determined + * @throws IllegalArgumentException + * if the port parameter is outside the specified range of valid + * port values, which is between 0 and 65535, inclusive */ public ServerBridge(String name, int port, boolean ssl, String forwardToHost, int forwardToPort, boolean forwardToSsl) @@ -113,22 +124,34 @@ public class ServerBridge extends Server { onClientContact(clientVersion); final ConnectActionServerString bridge = this; - new ConnectActionClientString(forwardToHost, forwardToPort, - forwardToSsl, clientVersion) { - @Override - public void action(final Version serverVersion) - throws Exception { - onServerContact(serverVersion); + try { + new ConnectActionClientString(forwardToHost, forwardToPort, + forwardToSsl, clientVersion) { + @Override + public void action(final Version serverVersion) + throws Exception { + onServerContact(serverVersion); - for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge - .rec()) { - onRec(clientVersion, fromClient); - String fromServer = send(fromClient); - onSend(serverVersion, fromServer); - bridge.send(fromServer); + for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge + .rec()) { + onRec(clientVersion, fromClient); + String fromServer = send(fromClient); + onSend(serverVersion, fromServer); + bridge.send(fromServer); + } + + getTraceHandler().trace("=== DONE", 1); + getTraceHandler().trace("", 1); } - } - }.connect(); + + @Override + protected void onError(Exception e) { + ServerBridge.this.onError(e); + } + }.connect(); + } catch (Exception e) { + ServerBridge.this.onError(e); + } } }; } @@ -140,7 +163,7 @@ public class ServerBridge extends Server { * the client version */ protected void onClientContact(Version clientVersion) { - getTraceHandler().trace("<<< CLIENT " + clientVersion); + getTraceHandler().trace(">>> CLIENT " + clientVersion); } /** @@ -150,7 +173,8 @@ public class ServerBridge extends Server { * the server version */ protected void onServerContact(Version serverVersion) { - getTraceHandler().trace(">>> SERVER " + serverVersion); + getTraceHandler().trace("<<< SERVER " + serverVersion); + getTraceHandler().trace(""); } /** @@ -162,7 +186,7 @@ public class ServerBridge extends Server { * the data sent by the client */ protected void onRec(Version clientVersion, String data) { - trace("<<< CLIENT (" + clientVersion + ")", data); + trace(">>> CLIENT (" + clientVersion + ")", data); } /** @@ -175,7 +199,16 @@ public class ServerBridge extends Server { * the data sent by the client */ protected void onSend(Version serverVersion, String data) { - trace(">>> SERVER (" + serverVersion + ")", data); + trace("<<< SERVER (" + serverVersion + ")", data); + } + + @Override + public void run() { + getTraceHandler().trace( + getName() + ": will forward to " + forwardToHost + ":" + + forwardToPort + " (" + + (forwardToSsl ? "SSL" : "plain text") + ")"); + super.run(); } /** @@ -187,7 +220,7 @@ public class ServerBridge extends Server { * the data to trace */ private void trace(String prefix, String data) { - int size = data.length(); + int size = data == null ? 0 : data.length(); String ssize = size + " byte"; if (size > 1) { ssize = size + " bytes"; @@ -205,8 +238,12 @@ public class ServerBridge extends Server { if (getTraceHandler().getTraceLevel() >= 2) { try { - if (data.startsWith("ZIP:")) { - data = StringUtils.unzip64(data.substring(4)); + while (data.startsWith("ZIP:") || data.startsWith("B64:")) { + if (data.startsWith("ZIP:")) { + data = StringUtils.unbase64s(data.substring(4), true); + } else if (data.startsWith("B64:")) { + data = StringUtils.unbase64s(data.substring(4), false); + } } Object obj = new Importer().read(data).getValue(); @@ -232,8 +269,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 +278,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); } @@ -252,57 +287,4 @@ public class ServerBridge extends Server { getTraceHandler().trace("", 2); } } - - /** - * Start a bridge between 2 servers. - * - * @param args - * an array containing: - *