| 1 | package be.nikiroo.fanfix.library; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.security.InvalidParameterException; |
| 5 | import java.util.ArrayList; |
| 6 | import java.util.List; |
| 7 | |
| 8 | import be.nikiroo.fanfix.Instance; |
| 9 | import be.nikiroo.fanfix.data.Chapter; |
| 10 | import be.nikiroo.fanfix.data.MetaData; |
| 11 | import be.nikiroo.fanfix.data.Paragraph; |
| 12 | import be.nikiroo.fanfix.data.Story; |
| 13 | import be.nikiroo.utils.Progress; |
| 14 | import be.nikiroo.utils.Progress.ProgressListener; |
| 15 | import be.nikiroo.utils.Version; |
| 16 | import be.nikiroo.utils.serial.server.ConnectActionServerObject; |
| 17 | import be.nikiroo.utils.serial.server.ServerObject; |
| 18 | |
| 19 | /** |
| 20 | * Create a new remote server that will listen for order on the given port. |
| 21 | * <p> |
| 22 | * The available commands are given as arrays of objects (first item is the key, |
| 23 | * second is the command, the rest are the arguments). |
| 24 | * <p> |
| 25 | * The key is always a String, the commands are also Strings; the parameters |
| 26 | * vary depending upon the command. |
| 27 | * <ul> |
| 28 | * <li>[key] PING: will return PONG if the key is accepted</li> |
| 29 | * <li>[key] GET_METADATA *: will return the metadata of all the stories in the |
| 30 | * library</li> |
| 31 | * <li>[key] GET_STORY [luid]: will return the given story if it exists (or NULL |
| 32 | * if not)</li> |
| 33 | * <li>[key] SAVE_STORY [luid]: save the story (that must be sent just after the |
| 34 | * command) with the given LUID</li> |
| 35 | * <li>[key] DELETE_STORY [luid]: delete the story of LUID luid</li> |
| 36 | * <li>[key] GET_COVER [luid]: return the cover of the story</li> |
| 37 | * <li>[key] GET_SOURCE_COVER [source]: return the cover for this source</li> |
| 38 | * <li>[key] SET_SOURCE_COVER [source], [luid]: set the default cover for the |
| 39 | * given source to the cover of the story denoted by luid</li> |
| 40 | * <li>[key] EXIT: stop the server</li> |
| 41 | * </ul> |
| 42 | * |
| 43 | * @author niki |
| 44 | */ |
| 45 | public class RemoteLibraryServer extends ServerObject { |
| 46 | private final String key; |
| 47 | |
| 48 | /** |
| 49 | * Create a new remote server (will not be active until |
| 50 | * {@link RemoteLibraryServer#start()} is called). |
| 51 | * |
| 52 | * @param key |
| 53 | * the key that will restrict access to this server |
| 54 | * @param port |
| 55 | * the port to listen on |
| 56 | * |
| 57 | * @throws IOException |
| 58 | * in case of I/O error |
| 59 | */ |
| 60 | public RemoteLibraryServer(String key, int port) throws IOException { |
| 61 | super("Fanfix remote library", port, true); |
| 62 | this.key = key; |
| 63 | |
| 64 | setTraceHandler(Instance.getTraceHandler()); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | protected Object onRequest(ConnectActionServerObject action, |
| 69 | Version clientVersion, Object data) throws Exception { |
| 70 | String key = ""; |
| 71 | String command = ""; |
| 72 | Object[] args = new Object[0]; |
| 73 | if (data instanceof Object[]) { |
| 74 | Object[] dataArray = (Object[]) data; |
| 75 | if (dataArray.length >= 2) { |
| 76 | args = new Object[dataArray.length - 2]; |
| 77 | for (int i = 2; i < dataArray.length; i++) { |
| 78 | args[i - 2] = dataArray[i]; |
| 79 | } |
| 80 | |
| 81 | key = "" + dataArray[0]; |
| 82 | command = "" + dataArray[1]; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | String trace = "[" + command + "] "; |
| 87 | for (Object arg : args) { |
| 88 | trace += arg + " "; |
| 89 | } |
| 90 | getTraceHandler().trace(trace); |
| 91 | |
| 92 | if (!key.equals(this.key)) { |
| 93 | getTraceHandler().trace("Key rejected."); |
| 94 | return null; |
| 95 | } |
| 96 | |
| 97 | if ("PING".equals(command)) { |
| 98 | return "PONG"; |
| 99 | } else if ("GET_METADATA".equals(command)) { |
| 100 | if (args[0].equals("*")) { |
| 101 | List<MetaData> metas = Instance.getLibrary().getMetas( |
| 102 | createPgForwarder(action)); |
| 103 | return metas.toArray(new MetaData[] {}); |
| 104 | } |
| 105 | throw new InvalidParameterException( |
| 106 | "only * is valid here, but you passed: " + args[0]); |
| 107 | } else if ("GET_STORY".equals(command)) { |
| 108 | MetaData meta = Instance.getLibrary().getInfo("" + args[0]); |
| 109 | meta = meta.clone(); |
| 110 | meta.setCover(null); |
| 111 | |
| 112 | action.send(meta); |
| 113 | action.rec(); |
| 114 | |
| 115 | Story story = Instance.getLibrary().getStory("" + args[0], null); |
| 116 | for (Object obj : breakStory(story)) { |
| 117 | action.send(obj); |
| 118 | action.rec(); |
| 119 | } |
| 120 | } else if ("SAVE_STORY".equals(command)) { |
| 121 | List<Object> list = new ArrayList<Object>(); |
| 122 | |
| 123 | action.send(null); |
| 124 | Object obj = action.rec(); |
| 125 | while (obj != null) { |
| 126 | list.add(obj); |
| 127 | action.send(null); |
| 128 | obj = action.rec(); |
| 129 | } |
| 130 | |
| 131 | Story story = rebuildStory(list); |
| 132 | Instance.getLibrary().save(story, "" + args[1], null); |
| 133 | } else if ("DELETE_STORY".equals(command)) { |
| 134 | Instance.getLibrary().delete("" + args[0]); |
| 135 | } else if ("GET_COVER".equals(command)) { |
| 136 | return Instance.getLibrary().getCover("" + args[0]); |
| 137 | } else if ("GET_SOURCE_COVER".equals(command)) { |
| 138 | return Instance.getLibrary().getSourceCover("" + args[0]); |
| 139 | } else if ("SET_SOURCE_COVER".equals(command)) { |
| 140 | Instance.getLibrary().setSourceCover("" + args[0], "" + args[1]); |
| 141 | } else if ("EXIT".equals(command)) { |
| 142 | stop(0, false); |
| 143 | } |
| 144 | |
| 145 | return null; |
| 146 | } |
| 147 | |
| 148 | @Override |
| 149 | protected void onError(Exception e) { |
| 150 | getTraceHandler().error(e); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Break a story in multiple {@link Object}s for easier serialisation. |
| 155 | * |
| 156 | * @param story |
| 157 | * the {@link Story} to break |
| 158 | * |
| 159 | * @return the list of {@link Object}s |
| 160 | */ |
| 161 | static List<Object> breakStory(Story story) { |
| 162 | List<Object> list = new ArrayList<Object>(); |
| 163 | |
| 164 | story = story.clone(); |
| 165 | list.add(story); |
| 166 | |
| 167 | if (story.getMeta().isImageDocument()) { |
| 168 | for (Chapter chap : story) { |
| 169 | list.add(chap); |
| 170 | list.addAll(chap.getParagraphs()); |
| 171 | chap.setParagraphs(new ArrayList<Paragraph>()); |
| 172 | } |
| 173 | story.setChapters(new ArrayList<Chapter>()); |
| 174 | } |
| 175 | |
| 176 | return list; |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Rebuild a story from a list of broke up {@link Story} parts. |
| 181 | * |
| 182 | * @param list |
| 183 | * the list of {@link Story} parts |
| 184 | * |
| 185 | * @return the reconstructed {@link Story} |
| 186 | */ |
| 187 | static Story rebuildStory(List<Object> list) { |
| 188 | Story story = null; |
| 189 | Chapter chap = null; |
| 190 | |
| 191 | for (Object obj : list) { |
| 192 | if (obj instanceof Story) { |
| 193 | story = (Story) obj; |
| 194 | } else if (obj instanceof Chapter) { |
| 195 | chap = (Chapter) obj; |
| 196 | story.getChapters().add(chap); |
| 197 | } else if (obj instanceof Paragraph) { |
| 198 | chap.getParagraphs().add((Paragraph) obj); |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | return story; |
| 203 | } |
| 204 | |
| 205 | /** |
| 206 | * Update the {@link Progress} with the adequate {@link Object} received |
| 207 | * from the network via {@link RemoteLibraryServer}. |
| 208 | * |
| 209 | * @param pg |
| 210 | * the {@link Progress} to update |
| 211 | * @param rep |
| 212 | * the object received from the network |
| 213 | * |
| 214 | * @return TRUE if it was a progress event, FALSE if not |
| 215 | */ |
| 216 | static boolean updateProgress(Progress pg, Object rep) { |
| 217 | if (rep instanceof Integer[]) { |
| 218 | Integer[] a = (Integer[]) rep; |
| 219 | if (a.length == 3) { |
| 220 | int min = a[0]; |
| 221 | int max = a[1]; |
| 222 | int progress = a[2]; |
| 223 | |
| 224 | if (min >= 0 && min <= max) { |
| 225 | pg.setMinMax(min, max); |
| 226 | pg.setProgress(progress); |
| 227 | |
| 228 | return true; |
| 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | return false; |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Create a {@link Progress} that will forward its progress over the |
| 238 | * network. |
| 239 | * |
| 240 | * @param action |
| 241 | * the {@link ConnectActionServerObject} to use to forward it |
| 242 | * |
| 243 | * @return the {@link Progress} |
| 244 | */ |
| 245 | private static Progress createPgForwarder( |
| 246 | final ConnectActionServerObject action) { |
| 247 | final Progress pg = new Progress(); |
| 248 | final Integer[] p = new Integer[] { -1, -1, -1 }; |
| 249 | pg.addProgressListener(new ProgressListener() { |
| 250 | @Override |
| 251 | public void progress(Progress progress, String name) { |
| 252 | int min = pg.getMin(); |
| 253 | int max = pg.getMax(); |
| 254 | int relativeProgress = min |
| 255 | + (int) Math.round(pg.getRelativeProgress() |
| 256 | * (max - min)); |
| 257 | |
| 258 | // Do not re-send the same value twice over the wire |
| 259 | if (p[0] != min || p[1] != max || p[2] != relativeProgress) { |
| 260 | p[0] = min; |
| 261 | p[1] = max; |
| 262 | p[2] = relativeProgress; |
| 263 | |
| 264 | try { |
| 265 | action.send(new Integer[] { min, max, relativeProgress }); |
| 266 | action.rec(); |
| 267 | } catch (Exception e) { |
| 268 | Instance.getTraceHandler().error(e); |
| 269 | } |
| 270 | } |
| 271 | } |
| 272 | }); |
| 273 | |
| 274 | return pg; |
| 275 | } |
| 276 | } |