Add 'src/be/nikiroo/utils/' from commit '46add0670fdee4bd936a13fe2448c5e20a7ffd0a'
[nikiroo-utils.git] / src / be / nikiroo / utils / serial / server / ServerBridge.java
index 6c2ed019e830226ce93ed95a95c316d4b6f220eb..0b734c6bbd6ca105b852406b2a97ec64ee71b589 100644 (file)
@@ -1,6 +1,8 @@
 package be.nikiroo.utils.serial.server;
 
+import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.lang.reflect.Array;
 import java.net.Socket;
 import java.net.UnknownHostException;
@@ -123,25 +125,26 @@ public class ServerBridge extends Server {
 
        @Override
        protected ConnectActionServer createConnectActionServer(Socket s) {
+               // Bad impl, not up to date (should work, but not efficient)
                return new ConnectActionServerString(s, key) {
                        @Override
-                       public void action(final Version clientVersion) throws Exception {
+                       public void action(Version clientVersion) throws Exception {
                                onClientContact(clientVersion);
                                final ConnectActionServerString bridge = this;
 
                                try {
                                        new ConnectActionClientString(forwardToHost, forwardToPort,
-                                                       forwardToKey, clientVersion) {
+                                                       forwardToKey) {
                                                @Override
-                                               public void action(final Version serverVersion)
+                                               public void action(Version serverVersion)
                                                                throws Exception {
                                                        onServerContact(serverVersion);
 
                                                        for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge
                                                                        .rec()) {
-                                                               onRec(clientVersion, fromClient);
+                                                               onRec(fromClient);
                                                                String fromServer = send(fromClient);
-                                                               onSend(serverVersion, fromServer);
+                                                               onSend(fromServer);
                                                                bridge.send(fromServer);
                                                        }
 
@@ -163,9 +166,6 @@ public class ServerBridge extends Server {
 
        /**
         * This is the method that is called each time a client contact us.
-        * 
-        * @param clientVersion
-        *            the client version
         */
        protected void onClientContact(Version clientVersion) {
                getTraceHandler().trace(">>> CLIENT " + clientVersion);
@@ -173,9 +173,6 @@ public class ServerBridge extends Server {
 
        /**
         * This is the method that is called each time a client contact us.
-        * 
-        * @param serverVersion
-        *            the server version
         */
        protected void onServerContact(Version serverVersion) {
                getTraceHandler().trace("<<< SERVER " + serverVersion);
@@ -185,26 +182,29 @@ public class ServerBridge extends Server {
        /**
         * This is the method that is called each time a client contact us.
         * 
-        * @param clientVersion
-        *            the client version
         * @param data
         *            the data sent by the client
         */
-       protected void onRec(Version clientVersion, String data) {
-               trace(">>> CLIENT (" + clientVersion + ")", data);
+       protected void onRec(String data) {
+               trace(">>> CLIENT", data);
        }
 
        /**
         * This is the method that is called each time the forwarded server contact
         * us.
         * 
-        * @param serverVersion
-        *            the client version
         * @param data
         *            the data sent by the client
         */
-       protected void onSend(Version serverVersion, String data) {
-               trace("<<< SERVER (" + serverVersion + ")", data);
+       protected void onSend(String data) {
+               trace("<<< SERVER", data);
+       }
+
+       @Override
+       protected ConnectActionClient getConnectionToMe()
+                       throws UnknownHostException, IOException {
+               return new ConnectActionClientString(new Socket((String) null,
+                               getPort()), key);
        }
 
        @Override
@@ -235,28 +235,35 @@ public class ServerBridge extends Server {
                        try {
                                while (data.startsWith("ZIP:") || data.startsWith("B64:")) {
                                        if (data.startsWith("ZIP:")) {
-                                               data = StringUtils.unbase64s(data.substring(4), true);
+                                               data = StringUtils.unzip64s(data.substring(4));
                                        } else if (data.startsWith("B64:")) {
-                                               data = StringUtils.unbase64s(data.substring(4), false);
+                                               data = StringUtils.unzip64s(data.substring(4));
                                        }
                                }
 
-                               Object obj = new Importer().read(data).getValue();
-                               if (obj == null) {
-                                       getTraceHandler().trace("NULL", 2);
-                                       getTraceHandler().trace("NULL", 3);
-                                       getTraceHandler().trace("NULL", 4);
-                               } else {
-                                       if (obj.getClass().isArray()) {
-                                               getTraceHandler().trace(
-                                                               "(" + obj.getClass() + ") with "
-                                                                               + Array.getLength(obj) + "element(s)",
-                                                               3);
+                               InputStream stream = new ByteArrayInputStream(
+                                               StringUtils.getBytes(data));
+                               try {
+                                       Object obj = new Importer().read(stream).getValue();
+                                       if (obj == null) {
+                                               getTraceHandler().trace("NULL", 2);
+                                               getTraceHandler().trace("NULL", 3);
+                                               getTraceHandler().trace("NULL", 4);
                                        } else {
-                                               getTraceHandler().trace("(" + obj.getClass() + ")", 2);
+                                               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);
                                        }
-                                       getTraceHandler().trace("" + obj.toString(), 3);
-                                       getTraceHandler().trace(data, 4);
+                               } finally {
+                                       stream.close();
                                }
                        } catch (NoSuchMethodException e) {
                                getTraceHandler().trace("(not an object)", 2);