| 1 | package be.nikiroo.fanfix; |
| 2 | |
| 3 | import java.awt.image.BufferedImage; |
| 4 | import java.io.File; |
| 5 | import java.io.IOException; |
| 6 | import java.util.ArrayList; |
| 7 | import java.util.List; |
| 8 | |
| 9 | import be.nikiroo.fanfix.data.MetaData; |
| 10 | import be.nikiroo.fanfix.data.Story; |
| 11 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; |
| 12 | import be.nikiroo.utils.Progress; |
| 13 | import be.nikiroo.utils.Version; |
| 14 | import be.nikiroo.utils.serial.ConnectActionClient; |
| 15 | |
| 16 | /** |
| 17 | * This {@link BasicLibrary} will access a remote server to list the available |
| 18 | * stories, and download the one you try to load to the local directory |
| 19 | * specified in the configuration. |
| 20 | * |
| 21 | * @author niki |
| 22 | */ |
| 23 | public class RemoteLibrary extends BasicLibrary { |
| 24 | private String host; |
| 25 | private int port; |
| 26 | private File baseDir; |
| 27 | |
| 28 | private LocalLibrary lib; |
| 29 | private List<MetaData> metas; |
| 30 | |
| 31 | /** |
| 32 | * Create a {@link RemoteLibrary} linked to the given server. |
| 33 | * |
| 34 | * @param host |
| 35 | * the host to contact or NULL for localhost |
| 36 | * @param port |
| 37 | * the port to contact it on |
| 38 | */ |
| 39 | public RemoteLibrary(String host, int port) { |
| 40 | this.host = host; |
| 41 | this.port = port; |
| 42 | |
| 43 | this.baseDir = Instance.getRemoteDir(host); |
| 44 | this.baseDir.mkdirs(); |
| 45 | |
| 46 | this.lib = new LocalLibrary(baseDir, OutputType.INFO_TEXT, |
| 47 | OutputType.CBZ); |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | protected List<MetaData> getMetas(Progress pg) { |
| 52 | // TODO: progress |
| 53 | |
| 54 | if (metas == null) { |
| 55 | metas = new ArrayList<MetaData>(); |
| 56 | |
| 57 | try { |
| 58 | new ConnectActionClient(host, port, true, null) { |
| 59 | public void action(Version serverVersion) throws Exception { |
| 60 | try { |
| 61 | Object rep = send("GET_METADATA *"); |
| 62 | for (MetaData meta : (MetaData[]) rep) { |
| 63 | metas.add(meta); |
| 64 | } |
| 65 | } catch (Exception e) { |
| 66 | Instance.syserr(e); |
| 67 | } |
| 68 | } |
| 69 | }.connect(); |
| 70 | } catch (IOException e) { |
| 71 | Instance.syserr(e); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return metas; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public synchronized File getFile(final String luid) { |
| 80 | File file = lib.getFile(luid); |
| 81 | if (file == null) { |
| 82 | final File[] tmp = new File[1]; |
| 83 | try { |
| 84 | new ConnectActionClient(host, port, true, null) { |
| 85 | public void action(Version serverVersion) throws Exception { |
| 86 | try { |
| 87 | Object rep = send("GET_STORY " + luid); |
| 88 | Story story = (Story) rep; |
| 89 | if (story != null) { |
| 90 | lib.save(story, luid, null); |
| 91 | tmp[0] = lib.getFile(luid); |
| 92 | } |
| 93 | } catch (Exception e) { |
| 94 | Instance.syserr(e); |
| 95 | } |
| 96 | } |
| 97 | }.connect(); |
| 98 | } catch (IOException e) { |
| 99 | Instance.syserr(e); |
| 100 | } |
| 101 | |
| 102 | file = tmp[0]; |
| 103 | } |
| 104 | |
| 105 | if (file != null) { |
| 106 | MetaData meta = getInfo(luid); |
| 107 | metas.add(meta); |
| 108 | } |
| 109 | |
| 110 | return file; |
| 111 | } |
| 112 | |
| 113 | @Override |
| 114 | public BufferedImage getCover(String luid) { |
| 115 | // Retrieve it from the network if needed: |
| 116 | if (lib.getInfo(luid) == null) { |
| 117 | getFile(luid); |
| 118 | } |
| 119 | |
| 120 | return lib.getCover(luid); |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | protected void clearCache() { |
| 125 | metas = null; |
| 126 | lib.clearCache(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public synchronized Story save(Story story, String luid, Progress pg) |
| 131 | throws IOException { |
| 132 | throw new java.lang.InternalError( |
| 133 | "No write support allowed on remote Libraries"); |
| 134 | } |
| 135 | |
| 136 | @Override |
| 137 | protected int getNextId() { |
| 138 | throw new java.lang.InternalError( |
| 139 | "No write support allowed on remote Libraries"); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | protected void doDelete(String luid) throws IOException { |
| 144 | throw new java.lang.InternalError( |
| 145 | "No write support allowed on remote Libraries"); |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | protected Story doSave(Story story, Progress pg) throws IOException { |
| 150 | throw new java.lang.InternalError( |
| 151 | "No write support allowed on remote Libraries"); |
| 152 | } |
| 153 | } |