update nikiroo-utils, better rec/send remote server info
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / RemoteLibraryServer.java
index 4a6ed606453fa1cfbdaf6236143832c55603793a..62548d678f2b04c6e59e8bb3e455a578e9f68f8c 100644 (file)
@@ -4,7 +4,11 @@ import java.io.IOException;
 import java.net.URL;
 import java.util.ArrayList;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+
+import javax.net.ssl.SSLException;
 
 import be.nikiroo.fanfix.Instance;
 import be.nikiroo.fanfix.data.Chapter;
@@ -14,7 +18,6 @@ import be.nikiroo.fanfix.data.Story;
 import be.nikiroo.utils.Progress;
 import be.nikiroo.utils.Progress.ProgressListener;
 import be.nikiroo.utils.StringUtils;
-import be.nikiroo.utils.Version;
 import be.nikiroo.utils.serial.server.ConnectActionServerObject;
 import be.nikiroo.utils.serial.server.ServerObject;
 
@@ -24,14 +27,6 @@ import be.nikiroo.utils.serial.server.ServerObject;
  * The available commands are given as arrays of objects (first item is the
  * command, the rest are the arguments).
  * <p>
- * All commands, including PING, will first return a random value to you that
- * you must hash with your key and return before processing the rest; if the
- * value not correct, the connection will be closed.
- * <p>
- * BTW: this system <b>is by no means secure</b>. It is just slightly
- * obfuscated, and operate on clear text (because Google decided not to support
- * anonymous SSL exchanges on Android, and the main use case for this server is
- * Android).
  * <ul>
  * <li>PING: will return PONG if the key is accepted</li>
  * <li>GET_METADATA *: will return the metadata of all the stories in the
@@ -57,7 +52,8 @@ import be.nikiroo.utils.serial.server.ServerObject;
  * @author niki
  */
 public class RemoteLibraryServer extends ServerObject {
-       private final String key;
+       private Map<Long, String> commands = new HashMap<Long, String>();
+       private Map<Long, Long> times = new HashMap<Long, Long>();
 
        /**
         * Create a new remote server (will not be active until
@@ -73,14 +69,12 @@ public class RemoteLibraryServer extends ServerObject {
         */
        public RemoteLibraryServer(String key, int port) throws IOException {
                super("Fanfix remote library", port, key);
-               this.key = key;
-
                setTraceHandler(Instance.getTraceHandler());
        }
 
        @Override
-       protected Object onRequest(ConnectActionServerObject action,
-                       Version clientVersion, Object data) throws Exception {
+       protected Object onRequest(ConnectActionServerObject action, Object data,
+                       long id) throws Exception {
                long start = new Date().getTime();
 
                String command = "";
@@ -103,28 +97,24 @@ public class RemoteLibraryServer extends ServerObject {
                }
                System.out.println(trace);
 
-               // Authentication:
-               String random = StringUtils.getMd5Hash(Double.toString(Math.random()));
-               action.send(random);
-               String answer = "" + action.rec();
-
-               if (!answer.equals(RemoteLibrary.hashKey(key, random))) {
-                       System.out.println("Key rejected.");
-                       action.close();
-                       return null;
-               }
-               //
-
                Object rep = doRequest(action, command, args);
 
-               String rec = StringUtils.formatNumber(action.getBytesReceived()) + "b";
-               String sent = StringUtils.formatNumber(action.getBytesSent()) + "b";
-               System.out.println(String.format("[>%s]: (%s sent, %s rec) in %d ms",
-                               command, sent, rec, (new Date().getTime() - start)));
+               commands.put(id, command);
+               times.put(id, (new Date().getTime() - start));
 
                return rep;
        }
 
+       @Override
+       protected void onRequestDone(long id, long bytesReceived, long bytesSent) {
+               String rec = StringUtils.formatNumber(bytesReceived) + "b";
+               String sent = StringUtils.formatNumber(bytesSent) + "b";
+               System.out.println(String.format("[>%s]: (%s sent, %s rec) in %d ms",
+                               commands.get(id), sent, rec, times.get(id)));
+               commands.remove(id);
+               times.remove(id);
+       }
+
        private Object doRequest(ConnectActionServerObject action, String command,
                        Object[] args) throws NoSuchFieldException, NoSuchMethodException,
                        ClassNotFoundException, IOException {
@@ -224,7 +214,11 @@ public class RemoteLibraryServer extends ServerObject {
 
        @Override
        protected void onError(Exception e) {
-               getTraceHandler().error(e);
+               if (e instanceof SSLException) {
+                       System.out.println("[Client connection refused (bad key)]");
+               } else {
+                       getTraceHandler().error(e);
+               }
        }
 
        /**