X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FRemoteLibrary.java;h=0a747bdc11adc3d208f4011c74f4deff6be25f16;hb=2070ced5a252b11ae7e19be1bb2430fa8e9220d9;hp=513849e3ce44a5ede2ec0c1869b909a939daaedf;hpb=a85e807750081c6e77a7916ca4e79506b5a98537;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/library/RemoteLibrary.java b/src/be/nikiroo/fanfix/library/RemoteLibrary.java index 513849e..0a747bd 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibrary.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibrary.java @@ -9,7 +9,6 @@ import java.util.List; import be.nikiroo.fanfix.Instance; import be.nikiroo.fanfix.data.MetaData; import be.nikiroo.fanfix.data.Story; -import be.nikiroo.fanfix.output.BasicOutput.OutputType; import be.nikiroo.utils.Progress; import be.nikiroo.utils.Version; import be.nikiroo.utils.serial.ConnectActionClient; @@ -24,53 +23,40 @@ import be.nikiroo.utils.serial.ConnectActionClient; public class RemoteLibrary extends BasicLibrary { private String host; private int port; - private File baseDir; - - private LocalLibrary lib; - private List metas; + 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; + } - this.baseDir = Instance.getRemoteDir(host); - this.baseDir.mkdirs(); - - this.lib = new LocalLibrary(baseDir, OutputType.INFO_TEXT, - OutputType.CBZ); + @Override + public String getLibraryName() { + return host + ":" + port; } @Override protected List getMetas(Progress pg) { // TODO: progress + final List metas = new ArrayList(); + MetaData[] fromNetwork = this. getRemoteObject( // + new Object[] { key, "GET_METADATA", "*" }); - if (metas == null) { - metas = new ArrayList(); - - try { - new ConnectActionClient(host, port, true, null) { - @Override - public void action(Version serverVersion) throws Exception { - try { - Object rep = send("GET_METADATA *"); - for (MetaData meta : (MetaData[]) rep) { - metas.add(meta); - } - } catch (Exception e) { - Instance.syserr(e); - } - } - }.connect(); - } catch (IOException e) { - Instance.syserr(e); + if (fromNetwork != null) { + for (MetaData meta : fromNetwork) { + metas.add(meta); } } @@ -78,78 +64,57 @@ public class RemoteLibrary extends BasicLibrary { } @Override - public synchronized File getFile(final String luid) { - File file = lib.getFile(luid); - if (file == null) { - final File[] tmp = new File[1]; - try { - new ConnectActionClient(host, port, true, null) { - @Override - public void action(Version serverVersion) throws Exception { - try { - Object rep = send("GET_STORY " + luid); - Story story = (Story) rep; - if (story != null) { - lib.save(story, luid, null); - tmp[0] = lib.getFile(luid); - } - } catch (Exception e) { - Instance.syserr(e); - } - } - }.connect(); - } catch (IOException e) { - Instance.syserr(e); - } - - file = tmp[0]; - } - - if (file != null) { - MetaData meta = getInfo(luid); - metas.add(meta); - } - - return file; + public BufferedImage getCover(final String luid) { + return this. getRemoteObject( // + new Object[] { key, "GET_COVER", luid }); } @Override - public BufferedImage getCover(String luid) { - // Retrieve it from the network if needed: - if (lib.getInfo(luid) == null) { - getFile(luid); - } + public BufferedImage getSourceCover(final String source) { + return this. getRemoteObject( // + new Object[] { key, "GET_SOURCE_COVER", source }); + } - return lib.getCover(luid); + @Override + public synchronized Story getStory(final String luid, Progress pg) { + return this. getRemoteObject( // + new Object[] { key, "GET_STORY", luid }); } @Override protected void clearCache() { - metas = null; - lib.clearCache(); } @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) { + this. getRemoteObject( // + new Object[] { key, "SET_SOURCE_COVER", source, luid }); + } + + @Override + public synchronized File getFile(final String luid, Progress pg) { throw new java.lang.InternalError( - "No write support allowed on remote Libraries"); + "Operation not supportorted on remote Libraries"); } - // All the following methods are only used by Save and Delete in - // BasicLibrary: + // The following methods are only used by Save and Delete in BasicLibrary: @Override protected int getNextId() { @@ -165,4 +130,41 @@ public class RemoteLibrary extends BasicLibrary { protected Story doSave(Story story, Progress pg) throws IOException { throw new java.lang.InternalError("Should not have been called"); } + + /** + * Return an object from the server. + * + * @param + * the expected type of object + * @param command + * the command to send + * + * @return the object or NULL + */ + @SuppressWarnings("unchecked") + private T getRemoteObject(final Object[] command) { + final Object[] result = new Object[1]; + try { + new ConnectActionClient(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); + } + } + }.connect(); + } catch (IOException e) { + Instance.syserr(e); + } + + try { + return (T) result[0]; + } catch (Exception e) { + Instance.syserr(e); + return null; + } + } }