X-Git-Url: http://git.nikiroo.be/?a=blobdiff_plain;f=src%2Fbe%2Fnikiroo%2Ffanfix%2Flibrary%2FRemoteLibrary.java;h=27a0280459a6d6786b2172e0777888af0c942cdd;hb=ff05b8284e6e415b13d3543650075d0f7cd27ff5;hp=83766396499fa783712b0558e51c3ad63def314e;hpb=14b574483b51d3859acef6a269f8841b5a4eb5f8;p=nikiroo-utils.git diff --git a/src/be/nikiroo/fanfix/library/RemoteLibrary.java b/src/be/nikiroo/fanfix/library/RemoteLibrary.java index 8376639..27a0280 100644 --- a/src/be/nikiroo/fanfix/library/RemoteLibrary.java +++ b/src/be/nikiroo/fanfix/library/RemoteLibrary.java @@ -9,14 +9,13 @@ 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; /** * This {@link BasicLibrary} will access a remote server to list the available - * stories, and download the one you try to load to the local directory + * stories, and download the ones you try to load to the local directory * specified in the configuration. * * @author niki @@ -24,10 +23,6 @@ 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; /** * Create a {@link RemoteLibrary} linked to the given server. @@ -40,37 +35,23 @@ public class RemoteLibrary extends BasicLibrary { public RemoteLibrary(String host, int port) { 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("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,55 +59,17 @@ 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("GET_COVER " + luid); } @Override - public BufferedImage getCover(String luid) { - // Retrieve it from the network if needed: - if (lib.getInfo(luid) == null) { - getFile(luid); - } - - return lib.getCover(luid); + public synchronized Story getStory(final String luid, Progress pg) { + return this. getRemoteObject("GET_STORY " + luid); } @Override protected void clearCache() { - metas = null; - lib.clearCache(); } @Override @@ -137,26 +80,74 @@ public class RemoteLibrary extends BasicLibrary { } @Override - protected int getNextId() { + public synchronized void delete(String luid) throws IOException { throw new java.lang.InternalError( "No write support allowed on remote Libraries"); } @Override - protected void doDelete(String luid) throws IOException { + public void setSourceCover(String source, String luid) { throw new java.lang.InternalError( "No write support allowed on remote Libraries"); } @Override - protected Story doSave(Story story, Progress pg) throws IOException { + public synchronized File getFile(final String luid) { throw new java.lang.InternalError( - "No write support allowed on remote Libraries"); + "Operation not supportorted on remote Libraries"); + } + + // The following methods are only used by Save and Delete in BasicLibrary: + + @Override + protected int getNextId() { + throw new java.lang.InternalError("Should not have been called"); } @Override - public void setSourceCover(String source, String luid) { - throw new java.lang.InternalError( - "No write support allowed on remote Libraries"); + protected void doDelete(String luid) throws IOException { + throw new java.lang.InternalError("Should not have been called"); + } + + @Override + 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 String 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; + } } }