X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FRemoteLibraryServer.java;h=f0d5f7aea0722e68c39d0835e36f85c41b0e65e3;hb=49f3dec53ec85b1720dbbfb62a2d495011b66622;hp=d62b41b0283e5b2fe49abe2a47059e98ce8e6bca;hpb=ea734ab478afa830550dd63800e855618a4cab4d;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java index d62b41b..f0d5f7a 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibraryServer.java @@ -26,8 +26,7 @@ import be.nikiroo.utils.serial.server.ServerObject; *

* 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. + * value not correct, the connection will be closed. *

* BTW: this system is by no means secure. It is just slightly * obfuscated, and operate on clear text (because Google decided not to support @@ -37,14 +36,13 @@ import be.nikiroo.utils.serial.server.ServerObject; *

  • PING: will return PONG if the key is accepted
  • *
  • GET_METADATA *: will return the metadata of all the stories in the * library (array)
  • * - *
  • GET_METADATA [luid]: will return the metadata of the story of LUID - * luid
  • + *
  • GET_METADATA [luid]: will return the metadata of the story of LUID luid
  • *
  • GET_STORY [luid]: will return the given story if it exists (or NULL if * not)
  • *
  • SAVE_STORY [luid]: save the story (that must be sent just after the * command) with the given LUID, then return the LUID
  • - *
  • IMPORT [url]: save the story found at the given URL, then return the - * LUID
  • + *
  • IMPORT [url]: save the story found at the given URL, then return the LUID + *
  • *
  • DELETE_STORY [luid]: delete the story of LUID luid
  • *
  • GET_COVER [luid]: return the cover of the story
  • *
  • GET_CUSTOM_COVER ["SOURCE"|"AUTHOR"] [source]: return the cover for this @@ -103,29 +101,26 @@ public class RemoteLibraryServer extends ServerObject { for (Object arg : args) { trace += arg + " "; } - getTraceHandler().trace(trace); + System.out.println(trace); // Authentication: String random = StringUtils.getMd5Hash(Double.toString(Math.random())); action.send(random); - String answer = ""; - try { - answer += action.rec(); - } catch (NullPointerException e) { - return null; - } + String answer = "" + action.rec(); - if (answer.equals(RemoteLibrary.hashKey(key, random))) { - action.send(true); - } else { - getTraceHandler().trace("Key rejected."); + if (!answer.equals(RemoteLibrary.hashKey(key, random))) { + System.out.println("Key rejected."); + action.close(); return null; } + // Object rep = doRequest(action, command, args); - getTraceHandler().trace(String.format("[>%s]: %d ms", command, - (new Date().getTime() - start))); + 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))); return rep; } @@ -157,8 +152,8 @@ public class RemoteLibraryServer extends ServerObject { return metas.toArray(new MetaData[] {}); } - return new MetaData[] { - Instance.getLibrary().getInfo((String) args[0]) }; + return new MetaData[] { Instance.getLibrary().getInfo( + (String) args[0]) }; } else if ("GET_STORY".equals(command)) { MetaData meta = Instance.getLibrary().getInfo((String) args[0]); meta = meta.clone(); @@ -167,8 +162,8 @@ public class RemoteLibraryServer extends ServerObject { action.send(meta); action.rec(); - Story story = Instance.getLibrary().getStory((String) args[0], - null); + Story story = Instance.getLibrary() + .getStory((String) args[0], null); for (Object obj : breakStory(story)) { action.send(obj); action.rec(); @@ -189,8 +184,8 @@ public class RemoteLibraryServer extends ServerObject { return story.getMeta().getLuid(); } else if ("IMPORT".equals(command)) { Progress pg = createPgForwarder(action); - Story story = Instance.getLibrary().imprt(new URL((String) args[0]), - pg); + Story story = Instance.getLibrary().imprt( + new URL((String) args[0]), pg); forcePgDoneSent(pg); return story.getMeta().getLuid(); } else if ("DELETE_STORY".equals(command)) { @@ -199,11 +194,11 @@ public class RemoteLibraryServer extends ServerObject { return Instance.getLibrary().getCover((String) args[0]); } else if ("GET_CUSTOM_COVER".equals(command)) { if ("SOURCE".equals(args[0])) { - return Instance.getLibrary() - .getCustomSourceCover((String) args[1]); + return Instance.getLibrary().getCustomSourceCover( + (String) args[1]); } else if ("AUTHOR".equals(args[0])) { - return Instance.getLibrary() - .getCustomAuthorCover((String) args[1]); + return Instance.getLibrary().getCustomAuthorCover( + (String) args[1]); } else { return null; } @@ -324,8 +319,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 @@ -341,8 +335,9 @@ public class RemoteLibraryServer extends ServerObject { public void progress(Progress progress, String name) { int min = pg.getMin(); int max = pg.getMax(); - int relativeProgress = min + (int) Math - .round(pg.getRelativeProgress() * (max - min)); + int relativeProgress = min + + (int) Math.round(pg.getRelativeProgress() + * (max - min)); // Do not re-send the same value twice over the wire, // unless more than 2 seconds have elapsed (to maintain the @@ -354,11 +349,10 @@ public class RemoteLibraryServer extends ServerObject { p[2] = relativeProgress; try { - action.send( - new Integer[] { min, max, relativeProgress }); + action.send(new Integer[] { min, max, relativeProgress }); action.rec(); } catch (Exception e) { - Instance.getTraceHandler().error(e); + getTraceHandler().error(e); } lastTime[0] = new Date().getTime(); @@ -372,14 +366,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); } } }