Commit | Line | Data |
---|---|---|
e42573a0 | 1 | package be.nikiroo.fanfix.library; |
b0e88ebd | 2 | |
68e2c6d2 | 3 | import java.awt.image.BufferedImage; |
b0e88ebd NR |
4 | import java.io.File; |
5 | import java.io.IOException; | |
68e2c6d2 NR |
6 | import java.util.ArrayList; |
7 | import java.util.List; | |
b0e88ebd | 8 | |
e42573a0 | 9 | import be.nikiroo.fanfix.Instance; |
b0e88ebd NR |
10 | import be.nikiroo.fanfix.data.MetaData; |
11 | import be.nikiroo.fanfix.data.Story; | |
b0e88ebd NR |
12 | import be.nikiroo.utils.Progress; |
13 | import be.nikiroo.utils.Version; | |
14 | import be.nikiroo.utils.serial.ConnectActionClient; | |
15 | ||
68e2c6d2 NR |
16 | /** |
17 | * This {@link BasicLibrary} will access a remote server to list the available | |
a85e8077 | 18 | * stories, and download the ones you try to load to the local directory |
68e2c6d2 NR |
19 | * specified in the configuration. |
20 | * | |
21 | * @author niki | |
22 | */ | |
23 | public class RemoteLibrary extends BasicLibrary { | |
b0e88ebd NR |
24 | private String host; |
25 | private int port; | |
2070ced5 | 26 | private final String key; |
68e2c6d2 NR |
27 | |
28 | /** | |
29 | * Create a {@link RemoteLibrary} linked to the given server. | |
30 | * | |
2070ced5 NR |
31 | * @param key |
32 | * the key that will allow us to exchange information with the | |
33 | * server | |
68e2c6d2 NR |
34 | * @param host |
35 | * the host to contact or NULL for localhost | |
36 | * @param port | |
37 | * the port to contact it on | |
38 | */ | |
2070ced5 NR |
39 | public RemoteLibrary(String key, String host, int port) { |
40 | this.key = key; | |
b0e88ebd NR |
41 | this.host = host; |
42 | this.port = port; | |
b0e88ebd NR |
43 | } |
44 | ||
99ccbdf6 NR |
45 | @Override |
46 | public String getLibraryName() { | |
47 | return host + ":" + port; | |
48 | } | |
49 | ||
b0e88ebd | 50 | @Override |
68e2c6d2 NR |
51 | protected List<MetaData> getMetas(Progress pg) { |
52 | // TODO: progress | |
ff05b828 | 53 | final List<MetaData> metas = new ArrayList<MetaData>(); |
085a2f9a | 54 | MetaData[] fromNetwork = this.<MetaData[]> getRemoteObject( // |
2070ced5 | 55 | new Object[] { key, "GET_METADATA", "*" }); |
b0e88ebd | 56 | |
ff05b828 NR |
57 | if (fromNetwork != null) { |
58 | for (MetaData meta : fromNetwork) { | |
59 | metas.add(meta); | |
e604986c | 60 | } |
b0e88ebd NR |
61 | } |
62 | ||
68e2c6d2 | 63 | return metas; |
b0e88ebd NR |
64 | } |
65 | ||
66 | @Override | |
ff05b828 | 67 | public BufferedImage getCover(final String luid) { |
085a2f9a | 68 | return this.<BufferedImage> getRemoteObject( // |
2070ced5 | 69 | new Object[] { key, "GET_COVER", luid }); |
085a2f9a NR |
70 | } |
71 | ||
72 | @Override | |
73 | public BufferedImage getSourceCover(final String source) { | |
74 | return this.<BufferedImage> getRemoteObject( // | |
2070ced5 | 75 | new Object[] { key, "GET_SOURCE_COVER", source }); |
b0e88ebd | 76 | } |
68e2c6d2 NR |
77 | |
78 | @Override | |
ff05b828 | 79 | public synchronized Story getStory(final String luid, Progress pg) { |
2070ced5 NR |
80 | return this.<Story> getRemoteObject( // |
81 | new Object[] { key, "GET_STORY", luid }); | |
68e2c6d2 NR |
82 | } |
83 | ||
84 | @Override | |
85 | protected void clearCache() { | |
68e2c6d2 NR |
86 | } |
87 | ||
88 | @Override | |
89 | public synchronized Story save(Story story, String luid, Progress pg) | |
90 | throws IOException { | |
2070ced5 | 91 | getRemoteObject(new Object[] { key, "SAVE_STORY", story, luid }); |
085a2f9a NR |
92 | |
93 | // because the meta changed: | |
94 | clearCache(); | |
95 | story.setMeta(getInfo(luid)); | |
96 | ||
97 | return story; | |
68e2c6d2 NR |
98 | } |
99 | ||
100 | @Override | |
a85e8077 | 101 | public synchronized void delete(String luid) throws IOException { |
2070ced5 | 102 | getRemoteObject(new Object[] { key, "DELETE_STORY", luid }); |
68e2c6d2 NR |
103 | } |
104 | ||
105 | @Override | |
a85e8077 | 106 | public void setSourceCover(String source, String luid) { |
085a2f9a | 107 | this.<BufferedImage> getRemoteObject( // |
2070ced5 | 108 | new Object[] { key, "SET_SOURCE_COVER", source, luid }); |
68e2c6d2 NR |
109 | } |
110 | ||
ff05b828 | 111 | @Override |
2249988a | 112 | public synchronized File getFile(final String luid, Progress pg) { |
ff05b828 NR |
113 | throw new java.lang.InternalError( |
114 | "Operation not supportorted on remote Libraries"); | |
115 | } | |
116 | ||
117 | // The following methods are only used by Save and Delete in BasicLibrary: | |
a85e8077 | 118 | |
68e2c6d2 | 119 | @Override |
a85e8077 NR |
120 | protected int getNextId() { |
121 | throw new java.lang.InternalError("Should not have been called"); | |
68e2c6d2 | 122 | } |
14b57448 NR |
123 | |
124 | @Override | |
a85e8077 NR |
125 | protected void doDelete(String luid) throws IOException { |
126 | throw new java.lang.InternalError("Should not have been called"); | |
127 | } | |
128 | ||
129 | @Override | |
130 | protected Story doSave(Story story, Progress pg) throws IOException { | |
131 | throw new java.lang.InternalError("Should not have been called"); | |
14b57448 | 132 | } |
e604986c NR |
133 | |
134 | /** | |
ff05b828 NR |
135 | * Return an object from the server. |
136 | * | |
137 | * @param <T> | |
138 | * the expected type of object | |
139 | * @param command | |
140 | * the command to send | |
e604986c | 141 | * |
ff05b828 | 142 | * @return the object or NULL |
e604986c | 143 | */ |
ff05b828 | 144 | @SuppressWarnings("unchecked") |
085a2f9a | 145 | private <T> T getRemoteObject(final Object[] command) { |
ff05b828 NR |
146 | final Object[] result = new Object[1]; |
147 | try { | |
148 | new ConnectActionClient(host, port, true) { | |
149 | @Override | |
150 | public void action(Version serverVersion) throws Exception { | |
151 | try { | |
152 | Object rep = send(command); | |
153 | result[0] = rep; | |
154 | } catch (Exception e) { | |
155 | Instance.syserr(e); | |
156 | } | |
157 | } | |
158 | }.connect(); | |
159 | } catch (IOException e) { | |
160 | Instance.syserr(e); | |
161 | } | |
162 | ||
163 | try { | |
164 | return (T) result[0]; | |
165 | } catch (Exception e) { | |
166 | Instance.syserr(e); | |
167 | return null; | |
168 | } | |
e604986c | 169 | } |
b0e88ebd | 170 | } |