| 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.server.ConnectActionClientObject; |
| 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 | private final String key; |
| 27 | |
| 28 | /** |
| 29 | * Create a {@link RemoteLibrary} linked to the given server. |
| 30 | * |
| 31 | * @param key |
| 32 | * the key that will allow us to exchange information with the |
| 33 | * server |
| 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 key, String host, int port) { |
| 40 | this.key = key; |
| 41 | this.host = host; |
| 42 | this.port = port; |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public String getLibraryName() { |
| 47 | return host + ":" + port; |
| 48 | } |
| 49 | |
| 50 | @Override |
| 51 | public BufferedImage getCover(final String luid) { |
| 52 | final BufferedImage[] result = new BufferedImage[1]; |
| 53 | |
| 54 | try { |
| 55 | new ConnectActionClientObject(host, port, true) { |
| 56 | @Override |
| 57 | public void action(Version serverVersion) throws Exception { |
| 58 | Object rep = send(new Object[] { key, "GET_COVER", luid }); |
| 59 | result[0] = (BufferedImage) rep; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | protected void onError(Exception e) { |
| 64 | Instance.getTraceHandler().error(e); |
| 65 | } |
| 66 | }.connect(); |
| 67 | } catch (Exception e) { |
| 68 | Instance.getTraceHandler().error(e); |
| 69 | } |
| 70 | |
| 71 | return result[0]; |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public BufferedImage getSourceCover(final String source) { |
| 76 | final BufferedImage[] result = new BufferedImage[1]; |
| 77 | |
| 78 | try { |
| 79 | new ConnectActionClientObject(host, port, true) { |
| 80 | @Override |
| 81 | public void action(Version serverVersion) throws Exception { |
| 82 | Object rep = send(new Object[] { key, "GET_SOURCE_COVER", |
| 83 | source }); |
| 84 | result[0] = (BufferedImage) rep; |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | protected void onError(Exception e) { |
| 89 | Instance.getTraceHandler().error(e); |
| 90 | } |
| 91 | }.connect(); |
| 92 | } catch (Exception e) { |
| 93 | Instance.getTraceHandler().error(e); |
| 94 | } |
| 95 | |
| 96 | return result[0]; |
| 97 | } |
| 98 | |
| 99 | @Override |
| 100 | public synchronized Story getStory(final String luid, Progress pg) { |
| 101 | final Progress pgF = pg; |
| 102 | final Story[] result = new Story[1]; |
| 103 | |
| 104 | try { |
| 105 | new ConnectActionClientObject(host, port, true) { |
| 106 | @Override |
| 107 | public void action(Version serverVersion) throws Exception { |
| 108 | Progress pg = pgF; |
| 109 | if (pg == null) { |
| 110 | pg = new Progress(); |
| 111 | } |
| 112 | |
| 113 | Object rep = send(new Object[] { key, "GET_STORY", luid }); |
| 114 | |
| 115 | MetaData meta = null; |
| 116 | if (rep instanceof MetaData) { |
| 117 | meta = (MetaData) rep; |
| 118 | if (meta.getWords() <= Integer.MAX_VALUE) { |
| 119 | pg.setMinMax(0, (int) meta.getWords()); |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | List<Object> list = new ArrayList<Object>(); |
| 124 | for (Object obj = send(null); obj != null; obj = send(null)) { |
| 125 | list.add(obj); |
| 126 | pg.add(1); |
| 127 | } |
| 128 | |
| 129 | result[0] = RemoteLibraryServer.rebuildStory(list); |
| 130 | pg.done(); |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | protected void onError(Exception e) { |
| 135 | Instance.getTraceHandler().error(e); |
| 136 | } |
| 137 | }.connect(); |
| 138 | } catch (Exception e) { |
| 139 | Instance.getTraceHandler().error(e); |
| 140 | } |
| 141 | |
| 142 | return result[0]; |
| 143 | } |
| 144 | |
| 145 | @Override |
| 146 | public synchronized Story save(final Story story, final String luid, |
| 147 | Progress pg) throws IOException { |
| 148 | final Progress pgF = pg; |
| 149 | |
| 150 | new ConnectActionClientObject(host, port, true) { |
| 151 | @Override |
| 152 | public void action(Version serverVersion) throws Exception { |
| 153 | Progress pg = pgF; |
| 154 | if (pg == null) { |
| 155 | pg = new Progress(); |
| 156 | } |
| 157 | |
| 158 | if (story.getMeta().getWords() <= Integer.MAX_VALUE) { |
| 159 | pg.setMinMax(0, (int) story.getMeta().getWords()); |
| 160 | } |
| 161 | |
| 162 | send(new Object[] { key, "SAVE_STORY", luid }); |
| 163 | |
| 164 | List<Object> list = RemoteLibraryServer.breakStory(story); |
| 165 | for (Object obj : list) { |
| 166 | send(obj); |
| 167 | pg.add(1); |
| 168 | } |
| 169 | |
| 170 | send(null); |
| 171 | pg.done(); |
| 172 | } |
| 173 | |
| 174 | @Override |
| 175 | protected void onError(Exception e) { |
| 176 | Instance.getTraceHandler().error(e); |
| 177 | } |
| 178 | }.connect(); |
| 179 | |
| 180 | // because the meta changed: |
| 181 | clearCache(); |
| 182 | story.setMeta(getInfo(luid)); |
| 183 | |
| 184 | return story; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public synchronized void delete(final String luid) throws IOException { |
| 189 | new ConnectActionClientObject(host, port, true) { |
| 190 | @Override |
| 191 | public void action(Version serverVersion) throws Exception { |
| 192 | send(new Object[] { key, "DELETE_STORY", luid }); |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | protected void onError(Exception e) { |
| 197 | Instance.getTraceHandler().error(e); |
| 198 | } |
| 199 | }.connect(); |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void setSourceCover(final String source, final String luid) { |
| 204 | try { |
| 205 | new ConnectActionClientObject(host, port, true) { |
| 206 | @Override |
| 207 | public void action(Version serverVersion) throws Exception { |
| 208 | send(new Object[] { key, "SET_SOURCE_COVER", source, luid }); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | protected void onError(Exception e) { |
| 213 | Instance.getTraceHandler().error(e); |
| 214 | } |
| 215 | }.connect(); |
| 216 | } catch (IOException e) { |
| 217 | Instance.getTraceHandler().error(e); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | @Override |
| 222 | public synchronized File getFile(final String luid, Progress pg) { |
| 223 | throw new java.lang.InternalError( |
| 224 | "Operation not supportorted on remote Libraries"); |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * Check if this {@link RemoteLibraryServer} is able to connect and identify |
| 229 | * to the remote server. |
| 230 | * |
| 231 | * @return TRUE if it is online |
| 232 | */ |
| 233 | public boolean isOnline() { |
| 234 | final Boolean[] result = new Boolean[1]; |
| 235 | |
| 236 | result[0] = false; |
| 237 | try { |
| 238 | new ConnectActionClientObject(host, port, true) { |
| 239 | @Override |
| 240 | public void action(Version serverVersion) throws Exception { |
| 241 | Object rep = send(new Object[] { key, "PING" }); |
| 242 | result[0] = "PONG".equals(rep); |
| 243 | } |
| 244 | |
| 245 | @Override |
| 246 | protected void onError(Exception e) { |
| 247 | Instance.getTraceHandler().error(e); |
| 248 | } |
| 249 | }.connect(); |
| 250 | } catch (Exception e) { |
| 251 | Instance.getTraceHandler().error(e); |
| 252 | } |
| 253 | |
| 254 | return result[0]; |
| 255 | } |
| 256 | |
| 257 | @Override |
| 258 | protected List<MetaData> getMetas(Progress pg) { |
| 259 | final Progress pgF = pg; |
| 260 | final List<MetaData> metas = new ArrayList<MetaData>(); |
| 261 | |
| 262 | try { |
| 263 | new ConnectActionClientObject(host, port, true) { |
| 264 | @Override |
| 265 | public void action(Version serverVersion) throws Exception { |
| 266 | Progress pg = pgF; |
| 267 | if (pg == null) { |
| 268 | pg = new Progress(); |
| 269 | } |
| 270 | |
| 271 | Object rep = send(new Object[] { key, "GET_METADATA", "*" }); |
| 272 | |
| 273 | while (true) { |
| 274 | if (!RemoteLibraryServer.updateProgress(pg, rep)) { |
| 275 | break; |
| 276 | } |
| 277 | |
| 278 | rep = send(null); |
| 279 | } |
| 280 | |
| 281 | for (MetaData meta : (MetaData[]) rep) { |
| 282 | metas.add(meta); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | @Override |
| 287 | protected void onError(Exception e) { |
| 288 | Instance.getTraceHandler().error(e); |
| 289 | } |
| 290 | }.connect(); |
| 291 | } catch (Exception e) { |
| 292 | Instance.getTraceHandler().error(e); |
| 293 | } |
| 294 | |
| 295 | return metas; |
| 296 | } |
| 297 | |
| 298 | @Override |
| 299 | protected void clearCache() { |
| 300 | } |
| 301 | |
| 302 | // The following methods are only used by Save and Delete in BasicLibrary: |
| 303 | |
| 304 | @Override |
| 305 | protected int getNextId() { |
| 306 | throw new java.lang.InternalError("Should not have been called"); |
| 307 | } |
| 308 | |
| 309 | @Override |
| 310 | protected void doDelete(String luid) throws IOException { |
| 311 | throw new java.lang.InternalError("Should not have been called"); |
| 312 | } |
| 313 | |
| 314 | @Override |
| 315 | protected Story doSave(Story story, Progress pg) throws IOException { |
| 316 | throw new java.lang.InternalError("Should not have been called"); |
| 317 | } |
| 318 | } |