X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FRemoteLibraryServer.java;h=62548d678f2b04c6e59e8bb3e455a578e9f68f8c;hb=9b558341bc348212a2b80170cdc76fa9a3b5a334;hp=b1aebb32e7214f9dcaa6df27e8536d00117dc835;hpb=37abe20c2f39a8e9e4fef5dfe5e14cb732d25b02;p=nikiroo-utils.git
diff --git a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java
index b1aebb3..62548d6 100644
--- a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java
+++ b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java
@@ -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,15 +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).
*
- * 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 is OK, it will return "true", if not, it will return NULL and stop the
- * connection.
- *
- * BTW: this system is by no means secure. 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).
*
* - PING: will return PONG if the key is accepted
* - GET_METADATA *: will return the metadata of all the stories in the
@@ -58,7 +52,8 @@ import be.nikiroo.utils.serial.server.ServerObject;
* @author niki
*/
public class RemoteLibraryServer extends ServerObject {
- private final String key;
+ private Map commands = new HashMap();
+ private Map times = new HashMap();
/**
* Create a new remote server (will not be active until
@@ -73,22 +68,20 @@ public class RemoteLibraryServer extends ServerObject {
* in case of I/O error
*/
public RemoteLibraryServer(String key, int port) throws IOException {
- super("Fanfix remote library", port, false);
- this.key = key;
-
+ super("Fanfix remote library", port, 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 = "";
Object[] args = new Object[0];
if (data instanceof Object[]) {
Object[] dataArray = (Object[]) data;
- if (dataArray.length >= 2) {
+ if (dataArray.length > 0) {
command = "" + dataArray[0];
args = new Object[dataArray.length - 1];
@@ -102,36 +95,26 @@ public class RemoteLibraryServer extends ServerObject {
for (Object arg : args) {
trace += arg + " ";
}
- getTraceHandler().trace(trace);
-
- // Authentication:
- String random = StringUtils.getMd5Hash(Double.toString(Math.random()));
- action.send(random);
- String answer = "";
- try {
- answer += action.rec();
- } catch (NullPointerException e) {
- return null;
- }
-
- if (answer.equals(RemoteLibrary.hashKey(key, random))) {
- action.send(true);
- } else {
- getTraceHandler().trace("Key rejected.");
- return null;
- }
+ System.out.println(trace);
Object rep = doRequest(action, command, args);
- String rec = StringUtils.formatNumber(action.getBytesReceived()) + "b";
- String sent = StringUtils.formatNumber(action.getBytesSent()) + "b";
- getTraceHandler().trace(
- 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 {
@@ -231,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);
+ }
}
/**
@@ -326,8 +313,7 @@ public class RemoteLibraryServer extends ServerObject {
*
* @return the {@link Progress}
*/
- private static Progress createPgForwarder(
- final ConnectActionServerObject action) {
+ private Progress createPgForwarder(final ConnectActionServerObject action) {
final Boolean[] isDoneForwarded = new Boolean[] { false };
final Progress pg = new Progress() {
@Override
@@ -360,7 +346,7 @@ public class RemoteLibraryServer extends ServerObject {
action.send(new Integer[] { min, max, relativeProgress });
action.rec();
} catch (Exception e) {
- Instance.getTraceHandler().error(e);
+ getTraceHandler().error(e);
}
lastTime[0] = new Date().getTime();
@@ -374,14 +360,14 @@ public class RemoteLibraryServer extends ServerObject {
}
// with 30 seconds timeout
- private static void forcePgDoneSent(Progress pg) {
+ private void forcePgDoneSent(Progress pg) {
long start = new Date().getTime();
pg.done();
while (!pg.isDone() && new Date().getTime() - start < 30000) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
- Instance.getTraceHandler().error(e);
+ getTraceHandler().error(e);
}
}
}