| 1 | package be.nikiroo.fanfix.library; |
| 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.Instance; |
| 10 | import be.nikiroo.fanfix.data.MetaData; |
| 11 | import be.nikiroo.fanfix.data.Story; |
| 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 ones 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 | |
| 27 | /** |
| 28 | * Create a {@link RemoteLibrary} linked to the given server. |
| 29 | * |
| 30 | * @param host |
| 31 | * the host to contact or NULL for localhost |
| 32 | * @param port |
| 33 | * the port to contact it on |
| 34 | */ |
| 35 | public RemoteLibrary(String host, int port) { |
| 36 | this.host = host; |
| 37 | this.port = port; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public String getLibraryName() { |
| 42 | return host + ":" + port; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | protected List<MetaData> getMetas(Progress pg) { |
| 47 | // TODO: progress |
| 48 | final List<MetaData> metas = new ArrayList<MetaData>(); |
| 49 | MetaData[] fromNetwork = this |
| 50 | .<MetaData[]> getRemoteObject("GET_METADATA *"); |
| 51 | |
| 52 | if (fromNetwork != null) { |
| 53 | for (MetaData meta : fromNetwork) { |
| 54 | metas.add(meta); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | return metas; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public BufferedImage getCover(final String luid) { |
| 63 | return this.<BufferedImage> getRemoteObject("GET_COVER " + luid); |
| 64 | } |
| 65 | |
| 66 | @Override |
| 67 | public synchronized Story getStory(final String luid, Progress pg) { |
| 68 | return this.<Story> getRemoteObject("GET_STORY " + luid); |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | protected void clearCache() { |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public synchronized Story save(Story story, String luid, Progress pg) |
| 77 | throws IOException { |
| 78 | throw new java.lang.InternalError( |
| 79 | "No write support allowed on remote Libraries"); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public synchronized void delete(String luid) throws IOException { |
| 84 | throw new java.lang.InternalError( |
| 85 | "No write support allowed on remote Libraries"); |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void setSourceCover(String source, String luid) { |
| 90 | throw new java.lang.InternalError( |
| 91 | "No write support allowed on remote Libraries"); |
| 92 | } |
| 93 | |
| 94 | @Override |
| 95 | public synchronized File getFile(final String luid) { |
| 96 | throw new java.lang.InternalError( |
| 97 | "Operation not supportorted on remote Libraries"); |
| 98 | } |
| 99 | |
| 100 | // The following methods are only used by Save and Delete in BasicLibrary: |
| 101 | |
| 102 | @Override |
| 103 | protected int getNextId() { |
| 104 | throw new java.lang.InternalError("Should not have been called"); |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | protected void doDelete(String luid) throws IOException { |
| 109 | throw new java.lang.InternalError("Should not have been called"); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | protected Story doSave(Story story, Progress pg) throws IOException { |
| 114 | throw new java.lang.InternalError("Should not have been called"); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Return an object from the server. |
| 119 | * |
| 120 | * @param <T> |
| 121 | * the expected type of object |
| 122 | * @param command |
| 123 | * the command to send |
| 124 | * |
| 125 | * @return the object or NULL |
| 126 | */ |
| 127 | @SuppressWarnings("unchecked") |
| 128 | private <T> T getRemoteObject(final String command) { |
| 129 | final Object[] result = new Object[1]; |
| 130 | try { |
| 131 | new ConnectActionClient(host, port, true) { |
| 132 | @Override |
| 133 | public void action(Version serverVersion) throws Exception { |
| 134 | try { |
| 135 | Object rep = send(command); |
| 136 | result[0] = rep; |
| 137 | } catch (Exception e) { |
| 138 | Instance.syserr(e); |
| 139 | } |
| 140 | } |
| 141 | }.connect(); |
| 142 | } catch (IOException e) { |
| 143 | Instance.syserr(e); |
| 144 | } |
| 145 | |
| 146 | try { |
| 147 | return (T) result[0]; |
| 148 | } catch (Exception e) { |
| 149 | Instance.syserr(e); |
| 150 | return null; |
| 151 | } |
| 152 | } |
| 153 | } |