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;
public class RemoteLibrary extends BasicLibrary {
private String host;
private int port;
- private File baseDir;
-
- private LocalLibrary lib;
- private List<MetaData> metas;
/**
* Create a {@link RemoteLibrary} linked to the given server.
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
@Override
protected List<MetaData> getMetas(Progress pg) {
// TODO: progress
+ final List<MetaData> metas = new ArrayList<MetaData>();
+ MetaData[] fromNetwork = this.<MetaData[]> getRemoteObject( //
+ new Object[] { "GET_METADATA", "*" });
- if (metas == null) {
- metas = new ArrayList<MetaData>();
-
- 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);
}
}
}
@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.<BufferedImage> getRemoteObject( //
+ new Object[] { "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.<BufferedImage> getRemoteObject( //
+ new Object[] { "GET_SOURCE_COVER", source });
+ }
- return lib.getCover(luid);
+ @Override
+ public synchronized Story getStory(final String luid, Progress pg) {
+ return this.<Story> getRemoteObject(new Object[] { "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[] { "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[] { "DELETE_STORY", luid });
}
@Override
public void setSourceCover(String source, String luid) {
+ this.<BufferedImage> getRemoteObject( //
+ new Object[] { "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() {
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 <T>
+ * the expected type of object
+ * @param command
+ * the command to send
+ *
+ * @return the object or NULL
+ */
+ @SuppressWarnings("unchecked")
+ private <T> 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;
+ }
+ }
}