doc
[nikiroo-utils.git] / library / RemoteLibrary.java
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 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 protected List<MetaData> getMetas(Progress pg) {
52 // TODO: progress
53 final List<MetaData> metas = new ArrayList<MetaData>();
54 MetaData[] fromNetwork = this.<MetaData[]> getRemoteObject( //
55 new Object[] { key, "GET_METADATA", "*" });
56
57 if (fromNetwork != null) {
58 for (MetaData meta : fromNetwork) {
59 metas.add(meta);
60 }
61 }
62
63 return metas;
64 }
65
66 @Override
67 public BufferedImage getCover(final String luid) {
68 return this.<BufferedImage> getRemoteObject( //
69 new Object[] { key, "GET_COVER", luid });
70 }
71
72 @Override
73 public BufferedImage getSourceCover(final String source) {
74 return this.<BufferedImage> getRemoteObject( //
75 new Object[] { key, "GET_SOURCE_COVER", source });
76 }
77
78 @Override
79 public synchronized Story getStory(final String luid, Progress pg) {
80 return this.<Story> getRemoteObject( //
81 new Object[] { key, "GET_STORY", luid });
82 }
83
84 @Override
85 protected void clearCache() {
86 }
87
88 @Override
89 public synchronized Story save(Story story, String luid, Progress pg)
90 throws IOException {
91 getRemoteObject(new Object[] { key, "SAVE_STORY", story, luid });
92
93 // because the meta changed:
94 clearCache();
95 story.setMeta(getInfo(luid));
96
97 return story;
98 }
99
100 @Override
101 public synchronized void delete(String luid) throws IOException {
102 getRemoteObject(new Object[] { key, "DELETE_STORY", luid });
103 }
104
105 @Override
106 public void setSourceCover(String source, String luid) {
107 this.<BufferedImage> getRemoteObject( //
108 new Object[] { key, "SET_SOURCE_COVER", source, luid });
109 }
110
111 @Override
112 public synchronized File getFile(final String luid, Progress pg) {
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:
118
119 @Override
120 protected int getNextId() {
121 throw new java.lang.InternalError("Should not have been called");
122 }
123
124 @Override
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");
132 }
133
134 /**
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
141 *
142 * @return the object or NULL
143 */
144 @SuppressWarnings("unchecked")
145 private <T> T getRemoteObject(final Object[] command) {
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 }
169 }
170 }