Version 4.5.0
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ServerBridge.java
index 786d018ae00eaf38ee1ab55160ca05f0628c2003..db50876b32aafb144bc89047e73073c391c569f8 100644 (file)
@@ -1,8 +1,11 @@
 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;
 import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.Importer;
@@ -33,10 +36,21 @@ public class ServerBridge extends Server {
         *            found (which can later on be queried via
         *            {@link ServerBridge#getPort()}
         * @param ssl
-        *            use a SSL connection (or not)
+        *            use an SSL connection (or not)
+        * @param forwardToHost
+        *            the host server to forward the calls to
+        * @param forwardToPort
+        *            the host port to forward the calls to
+        * @param forwardToSsl
+        *            use an SSL connection for the forward server or not
         * 
         * @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 {
@@ -55,10 +69,21 @@ public class ServerBridge extends Server {
         * @param port
         *            the port to listen on
         * @param ssl
-        *            use a SSL connection (or not)
+        *            use an SSL connection (or not)
+        * @param forwardToHost
+        *            the host server to forward the calls to
+        * @param forwardToPort
+        *            the host port to forward the calls to
+        * @param forwardToSsl
+        *            use an SSL connection for the forward server or not
         * 
         * @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)
@@ -72,13 +97,15 @@ public class ServerBridge extends Server {
        /**
         * The traces handler for this {@link Server}.
         * <p>
+        * The trace levels are handled as follow:
         * <ul>
-        * <li>At level 1, it will only print basic IN/OUT messages with length.</li>
-        * <li>At level 2, it will also print the data as a String.</li>
-        * <li>At level 3, it will try to interpret it (SLOW) and print the object
-        * type if possible.</li>
-        * <li>At level 4, it will try to print the {@link Object#toString()} value.
-        * </li>
+        * <li>1: it will only print basic IN/OUT messages with length</li>
+        * <li>2: it will try to interpret it as an object (SLOW) and print the
+        * object class if possible</li>
+        * <li>3: it will try to print the {@link Object#toString()} value, or the
+        * data if it is not an object</li>
+        * <li>4: it will also print the unzipped serialised value if it is an
+        * object</li>
         * </ul>
         * 
         * @param tracer
@@ -97,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);
-
-                                               for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge
-                                                               .rec()) {
-                                                       onRec(clientVersion, fromClient);
-                                                       String fromServer = send(fromClient);
-                                                       onSend(serverVersion, fromServer);
-                                                       bridge.send(fromServer);
+                               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);
+                                                       }
+
+                                                       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);
+                               }
                        }
                };
        }
@@ -122,11 +161,9 @@ public class ServerBridge extends Server {
         * 
         * @param clientVersion
         *            the client version
-        * @param data
-        *            the data sent by the client
         */
        protected void onClientContact(Version clientVersion) {
-               getTraceHandler().trace("CLIENT " + clientVersion);
+               getTraceHandler().trace(">>> CLIENT " + clientVersion);
        }
 
        /**
@@ -134,11 +171,10 @@ public class ServerBridge extends Server {
         * 
         * @param serverVersion
         *            the server version
-        * @param data
-        *            the data sent by the client
         */
        protected void onServerContact(Version serverVersion) {
-               getTraceHandler().trace("SERVER " + serverVersion);
+               getTraceHandler().trace("<<< SERVER " + serverVersion);
+               getTraceHandler().trace("");
        }
 
        /**
@@ -150,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);
        }
 
        /**
@@ -163,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();
        }
 
        /**
@@ -175,78 +220,71 @@ public class ServerBridge extends Server {
         *            the data to trace
         */
        private void trace(String prefix, String data) {
-               getTraceHandler().trace(prefix + ": " + data.length() + " characters",
-                               1);
+               int size = data == null ? 0 : data.length();
+               String ssize = size + " byte";
+               if (size > 1) {
+                       ssize = size + " bytes";
+                       if (size >= 1000) {
+                               size = size / 1000;
+                               ssize = size + " kb";
+                               if (size > 1000) {
+                                       size = size / 1000;
+                                       ssize = size + " MB";
+                               }
+                       }
+               }
+
+               getTraceHandler().trace(prefix + ": " + ssize, 1);
 
                if (getTraceHandler().getTraceLevel() >= 2) {
                        try {
+                               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();
                                if (obj == null) {
+                                       getTraceHandler().trace("NULL", 2);
                                        getTraceHandler().trace("NULL", 3);
                                        getTraceHandler().trace("NULL", 4);
                                } else {
-                                       getTraceHandler().trace("(" + obj.getClass() + ")", 3);
-                                       getTraceHandler().trace("" + obj.toString(), 4);
+                                       if (obj.getClass().isArray()) {
+                                               getTraceHandler().trace(
+                                                               "(" + obj.getClass() + ") with "
+                                                                               + Array.getLength(obj) + "element(s)",
+                                                               3);
+                                       } else {
+                                               getTraceHandler().trace("(" + obj.getClass() + ")", 2);
+                                       }
+                                       getTraceHandler().trace("" + obj.toString(), 3);
+                                       getTraceHandler().trace(data, 4);
                                }
+                       } catch (NoSuchMethodException e) {
+                               getTraceHandler().trace("(not an object)", 2);
+                               getTraceHandler().trace(data, 3);
+                               getTraceHandler().trace("", 4);
+                       } catch (NoSuchFieldException e) {
+                               getTraceHandler().trace(
+                                               "(incompatible: " + e.getMessage() + ")", 2);
+                               getTraceHandler().trace(data, 3);
+                               getTraceHandler().trace("", 4);
+                       } catch (ClassNotFoundException e) {
+                               getTraceHandler().trace(
+                                               "(unknown object: " + e.getMessage() + ")", 2);
+                               getTraceHandler().trace(data, 3);
+                               getTraceHandler().trace("", 4);
                        } catch (Exception e) {
-                               getTraceHandler().trace("(not an object)", 3);
-                               getTraceHandler().trace(data, 4);
-                       }
-
-                       getTraceHandler().trace("", 4);
-               }
-       }
-
-       /**
-        * Start a bridge between 2 servers.
-        * 
-        * @param args
-        *            an array containing:
-        *            <ul>
-        *            <li>The bridge name</li>
-        *            <li>The bridge port</li>
-        *            <li>TRUE for a ssl bridge, FALSE for plain text</li>
-        *            <li>The forward server host</li>
-        *            <li>The forward server port</li>
-        *            <li>TRUE for a ssl forward server, FALSE for plain text</li>
-        *            <li>(optional) a trace level</li>
-        *            </ul>
-        */
-       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])\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");
-                               return;
-                       }
-
-                       int i = 0;
-                       String name = args[i++];
-                       int port = Integer.parseInt(args[i++]);
-                       boolean ssl = Boolean.parseBoolean(args[i++]);
-                       String fhost = args[i++];
-                       int fport = Integer.parseInt(args[i++]);
-                       boolean fssl = Boolean.parseBoolean(args[i++]);
-
-                       int traceLevel = 1;
-                       if (args.length > 6) {
-                               traceLevel = Integer.parseInt(args[i++]);
+                               getTraceHandler().trace(
+                                               "(decode error: " + e.getMessage() + ")", 2);
+                               getTraceHandler().trace(data, 3);
+                               getTraceHandler().trace("", 4);
                        }
 
-                       ServerBridge bridge = new ServerBridge(name, port, ssl, fhost,
-                                       fport, fssl);
-                       bridge.setTraceHandler(new TraceHandler(true, true, traceLevel));
-                       bridge.run();
-               } catch (Exception e) {
-                       tracer.error(e);
+                       getTraceHandler().trace("", 2);
                }
        }
 }