Merge commit '77d3a60869e7a780c6ae069e51530e1eacece5e2'
[fanfix.git] / src / be / nikiroo / utils / serial / server / ServerBridge.java
index 2c5d8bf1250fc559f507995d73d9711b56fd8640..0b734c6bbd6ca105b852406b2a97ec64ee71b589 100644 (file)
@@ -9,6 +9,7 @@ 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;
 
 /**
@@ -127,16 +128,17 @@ public class ServerBridge extends Server {
                // Bad impl, not up to date (should work, but not efficient)
                return new ConnectActionServerString(s, key) {
                        @Override
-                       public void action() throws Exception {
-                               onClientContact();
+                       public void action(Version clientVersion) throws Exception {
+                               onClientContact(clientVersion);
                                final ConnectActionServerString bridge = this;
 
                                try {
                                        new ConnectActionClientString(forwardToHost, forwardToPort,
                                                        forwardToKey) {
                                                @Override
-                                               public void action() throws Exception {
-                                                       onServerContact();
+                                               public void action(Version serverVersion)
+                                                               throws Exception {
+                                                       onServerContact(serverVersion);
 
                                                        for (String fromClient = bridge.rec(); fromClient != null; fromClient = bridge
                                                                        .rec()) {
@@ -165,15 +167,15 @@ public class ServerBridge extends Server {
        /**
         * This is the method that is called each time a client contact us.
         */
-       protected void onClientContact() {
-               getTraceHandler().trace(">>> CLIENT ");
+       protected void onClientContact(Version clientVersion) {
+               getTraceHandler().trace(">>> CLIENT " + clientVersion);
        }
 
        /**
         * This is the method that is called each time a client contact us.
         */
-       protected void onServerContact() {
-               getTraceHandler().trace("<<< SERVER");
+       protected void onServerContact(Version serverVersion) {
+               getTraceHandler().trace("<<< SERVER " + serverVersion);
                getTraceHandler().trace("");
        }
 
@@ -198,6 +200,13 @@ public class ServerBridge extends Server {
                trace("<<< SERVER", data);
        }
 
+       @Override
+       protected ConnectActionClient getConnectionToMe()
+                       throws UnknownHostException, IOException {
+               return new ConnectActionClientString(new Socket((String) null,
+                               getPort()), key);
+       }
+
        @Override
        public void run() {
                getTraceHandler().trace(
@@ -217,7 +226,6 @@ public class ServerBridge extends Server {
         *            the data to trace
         */
        private void trace(String prefix, String data) {
-               // TODO: we convert to string and back
                int size = data == null ? 0 : data.length();
                String ssize = StringUtils.formatNumber(size) + "bytes";
 
@@ -234,7 +242,7 @@ public class ServerBridge extends Server {
                                }
 
                                InputStream stream = new ByteArrayInputStream(
-                                               data.getBytes("UTF-8"));
+                                               StringUtils.getBytes(data));
                                try {
                                        Object obj = new Importer().read(stream).getValue();
                                        if (obj == null) {