Remote: try to send the stories image-per-image
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / RemoteLibrary.java
CommitLineData
e42573a0 1package be.nikiroo.fanfix.library;
b0e88ebd 2
68e2c6d2 3import java.awt.image.BufferedImage;
b0e88ebd
NR
4import java.io.File;
5import java.io.IOException;
68e2c6d2
NR
6import java.util.ArrayList;
7import java.util.List;
b0e88ebd 8
e42573a0 9import be.nikiroo.fanfix.Instance;
b0e88ebd
NR
10import be.nikiroo.fanfix.data.MetaData;
11import be.nikiroo.fanfix.data.Story;
b0e88ebd
NR
12import be.nikiroo.utils.Progress;
13import be.nikiroo.utils.Version;
62c63b07 14import be.nikiroo.utils.serial.server.ConnectActionClientObject;
b0e88ebd 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 */
23public 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>();
74a40dfb 54 MetaData[] fromNetwork = this.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) {
74a40dfb 68 return this.getRemoteObject( //
2070ced5 69 new Object[] { key, "GET_COVER", luid });
085a2f9a
NR
70 }
71
72 @Override
73 public BufferedImage getSourceCover(final String source) {
74a40dfb 74 return this.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) {
74a40dfb 80 return this.getRemoteStory( //
2070ced5 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
74a40dfb 140 * the command to send (can contain at most ONE {@link Story})
e604986c 141 *
ff05b828 142 * @return the object or NULL
e604986c 143 */
085a2f9a 144 private <T> T getRemoteObject(final Object[] command) {
74a40dfb
NR
145 return getRemoteObjectOrStory(command, false);
146 }
147
148 /**
149 * Return an object from the server.
150 *
151 * @param command
152 * the command to send (can contain at most ONE {@link Story})
153 *
154 * @return the object or NULL
155 */
156 private Story getRemoteStory(final Object[] command) {
157 return getRemoteObjectOrStory(command, true);
158 }
159
160 /**
161 * Return an object from the server.
162 *
163 * @param <T>
164 * the expected type of object
165 * @param command
166 * the command to send (can contain at most ONE {@link Story})
167 *
168 * @return the object or NULL
169 */
170 @SuppressWarnings("unchecked")
171 private <T> T getRemoteObjectOrStory(final Object[] command,
172 final boolean getStory) {
ff05b828
NR
173 final Object[] result = new Object[1];
174 try {
62c63b07 175 new ConnectActionClientObject(host, port, true) {
ff05b828
NR
176 @Override
177 public void action(Version serverVersion) throws Exception {
178 try {
74a40dfb
NR
179 Story story = null;
180 for (int i = 0; i < command.length; i++) {
181 if (command[i] instanceof Story) {
182 story = (Story) command[i];
183 command[i] = null;
184 }
185 }
186
ff05b828 187 Object rep = send(command);
74a40dfb
NR
188
189 if (story != null) {
190 RemoteLibraryServer.sendStory(story, this);
191 }
192
193 if (getStory) {
194 rep = RemoteLibraryServer.recStory(this);
195 }
196
ff05b828
NR
197 result[0] = rep;
198 } catch (Exception e) {
62c63b07 199 Instance.getTraceHandler().error(e);
ff05b828
NR
200 }
201 }
202 }.connect();
203 } catch (IOException e) {
62c63b07 204 Instance.getTraceHandler().error(e);
ff05b828
NR
205 }
206
207 try {
208 return (T) result[0];
209 } catch (Exception e) {
62c63b07 210 Instance.getTraceHandler().error(e);
ff05b828
NR
211 return null;
212 }
e604986c 213 }
b0e88ebd 214}