X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FRemoteLibrary.java;h=6c6c9e92012bb171ff280fa1cc75a0bc5feab677;hb=851dd5388802907146e88c3f6b9e68094e73693e;hp=27a0280459a6d6786b2172e0777888af0c942cdd;hpb=ff05b8284e6e415b13d3543650075d0f7cd27ff5;p=fanfix.git diff --git a/src/be/nikiroo/fanfix/library/RemoteLibrary.java b/src/be/nikiroo/fanfix/library/RemoteLibrary.java index 27a0280..6c6c9e9 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibrary.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibrary.java @@ -11,7 +11,7 @@ import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; import be.nikiroo.utils.Progress; import be.nikiroo.utils.Version; -import be.nikiroo.utils.serial.ConnectActionClient; +import be.nikiroo.utils.serial.server.ConnectActionClientObject; /** * This {@link BasicLibrary} will access a remote server to list the available @@ -23,16 +23,21 @@ import be.nikiroo.utils.serial.ConnectActionClient; public class RemoteLibrary extends BasicLibrary { private String host; private int port; + private final String key; /** * Create a {@link RemoteLibrary} linked to the given server. * + * @param key + * the key that will allow us to exchange information with the + * server * @param host * the host to contact or NULL for localhost * @param port * the port to contact it on */ - public RemoteLibrary(String host, int port) { + public RemoteLibrary(String key, String host, int port) { + this.key = key; this.host = host; this.port = port; } @@ -46,8 +51,8 @@ public class RemoteLibrary extends BasicLibrary { protected List getMetas(Progress pg) { // TODO: progress final List metas = new ArrayList(); - MetaData[] fromNetwork = this - . getRemoteObject("GET_METADATA *"); + MetaData[] fromNetwork = this.getRemoteObject( // + new Object[] { key, "GET_METADATA", "*" }); if (fromNetwork != null) { for (MetaData meta : fromNetwork) { @@ -60,12 +65,20 @@ public class RemoteLibrary extends BasicLibrary { @Override public BufferedImage getCover(final String luid) { - return this. getRemoteObject("GET_COVER " + luid); + return this.getRemoteObject( // + new Object[] { key, "GET_COVER", luid }); + } + + @Override + public BufferedImage getSourceCover(final String source) { + return this.getRemoteObject( // + new Object[] { key, "GET_SOURCE_COVER", source }); } @Override public synchronized Story getStory(final String luid, Progress pg) { - return this. getRemoteObject("GET_STORY " + luid); + return this.getRemoteStory( // + new Object[] { key, "GET_STORY", luid }); } @Override @@ -75,24 +88,28 @@ public class RemoteLibrary extends BasicLibrary { @Override public synchronized Story save(Story story, String luid, Progress pg) throws IOException { - throw new java.lang.InternalError( - "No write support allowed on remote Libraries"); + getRemoteObject(new Object[] { key, "SAVE_STORY", story, luid }); + + // because the meta changed: + clearCache(); + story.setMeta(getInfo(luid)); + + return story; } @Override public synchronized void delete(String luid) throws IOException { - throw new java.lang.InternalError( - "No write support allowed on remote Libraries"); + getRemoteObject(new Object[] { key, "DELETE_STORY", luid }); } @Override public void setSourceCover(String source, String luid) { - throw new java.lang.InternalError( - "No write support allowed on remote Libraries"); + this. getRemoteObject( // + new Object[] { key, "SET_SOURCE_COVER", source, luid }); } @Override - public synchronized File getFile(final String luid) { + public synchronized File getFile(final String luid, Progress pg) { throw new java.lang.InternalError( "Operation not supportorted on remote Libraries"); } @@ -120,33 +137,78 @@ public class RemoteLibrary extends BasicLibrary { * @param * the expected type of object * @param command - * the command to send + * the command to send (can contain at most ONE {@link Story}) + * + * @return the object or NULL + */ + private T getRemoteObject(final Object[] command) { + return getRemoteObjectOrStory(command, false); + } + + /** + * Return an object from the server. + * + * @param command + * the command to send (can contain at most ONE {@link Story}) + * + * @return the object or NULL + */ + private Story getRemoteStory(final Object[] command) { + return getRemoteObjectOrStory(command, true); + } + + /** + * Return an object from the server. + * + * @param + * the expected type of object + * @param command + * the command to send (can contain at most ONE {@link Story}) * * @return the object or NULL */ @SuppressWarnings("unchecked") - private T getRemoteObject(final String command) { + private T getRemoteObjectOrStory(final Object[] command, + final boolean getStory) { final Object[] result = new Object[1]; try { - new ConnectActionClient(host, port, true) { + new ConnectActionClientObject(host, port, true) { @Override public void action(Version serverVersion) throws Exception { - try { - Object rep = send(command); - result[0] = rep; - } catch (Exception e) { - Instance.syserr(e); + Story story = null; + for (int i = 0; i < command.length; i++) { + if (command[i] instanceof Story) { + story = (Story) command[i]; + command[i] = null; + } } + + Object rep = send(command); + + if (story != null) { + RemoteLibraryServer.sendStory(story, this); + } + + if (getStory) { + rep = RemoteLibraryServer.recStory(this); + } + + result[0] = rep; + } + + @Override + protected void onError(Exception e) { + Instance.getTraceHandler().error(e); } }.connect(); } catch (IOException e) { - Instance.syserr(e); + Instance.getTraceHandler().error(e); } try { return (T) result[0]; } catch (Exception e) { - Instance.syserr(e); + Instance.getTraceHandler().error(e); return null; } }