Improve remote, fix bugs, update nikiroo-utils
[nikiroo-utils.git] / src / be / nikiroo / fanfix / 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
27 /**
28 * Create a {@link RemoteLibrary} linked to the given server.
29 *
30 * @param host
31 * the host to contact or NULL for localhost
32 * @param port
33 * the port to contact it on
34 */
35 public RemoteLibrary(String host, int port) {
36 this.host = host;
37 this.port = port;
38 }
39
40 @Override
41 public String getLibraryName() {
42 return host + ":" + port;
43 }
44
45 @Override
46 protected List<MetaData> getMetas(Progress pg) {
47 // TODO: progress
48 final List<MetaData> metas = new ArrayList<MetaData>();
49 MetaData[] fromNetwork = this.<MetaData[]> getRemoteObject( //
50 new Object[] { "GET_METADATA", "*" });
51
52 if (fromNetwork != null) {
53 for (MetaData meta : fromNetwork) {
54 metas.add(meta);
55 }
56 }
57
58 return metas;
59 }
60
61 @Override
62 public BufferedImage getCover(final String luid) {
63 return this.<BufferedImage> getRemoteObject( //
64 new Object[] { "GET_COVER", luid });
65 }
66
67 @Override
68 public BufferedImage getSourceCover(final String source) {
69 return this.<BufferedImage> getRemoteObject( //
70 new Object[] { "GET_SOURCE_COVER", source });
71 }
72
73 @Override
74 public synchronized Story getStory(final String luid, Progress pg) {
75 return this.<Story> getRemoteObject(new Object[] { "GET_STORY", luid });
76 }
77
78 @Override
79 protected void clearCache() {
80 }
81
82 @Override
83 public synchronized Story save(Story story, String luid, Progress pg)
84 throws IOException {
85 getRemoteObject(new Object[] { "SAVE_STORY", story, luid });
86
87 // because the meta changed:
88 clearCache();
89 story.setMeta(getInfo(luid));
90
91 return story;
92 }
93
94 @Override
95 public synchronized void delete(String luid) throws IOException {
96 getRemoteObject(new Object[] { "DELETE_STORY", luid });
97 }
98
99 @Override
100 public void setSourceCover(String source, String luid) {
101 this.<BufferedImage> getRemoteObject( //
102 new Object[] { "SET_SOURCE_COVER", source, luid });
103 }
104
105 @Override
106 public synchronized File getFile(final String luid, Progress pg) {
107 throw new java.lang.InternalError(
108 "Operation not supportorted on remote Libraries");
109 }
110
111 // The following methods are only used by Save and Delete in BasicLibrary:
112
113 @Override
114 protected int getNextId() {
115 throw new java.lang.InternalError("Should not have been called");
116 }
117
118 @Override
119 protected void doDelete(String luid) throws IOException {
120 throw new java.lang.InternalError("Should not have been called");
121 }
122
123 @Override
124 protected Story doSave(Story story, Progress pg) throws IOException {
125 throw new java.lang.InternalError("Should not have been called");
126 }
127
128 /**
129 * Return an object from the server.
130 *
131 * @param <T>
132 * the expected type of object
133 * @param command
134 * the command to send
135 *
136 * @return the object or NULL
137 */
138 @SuppressWarnings("unchecked")
139 private <T> T getRemoteObject(final Object[] command) {
140 final Object[] result = new Object[1];
141 try {
142 new ConnectActionClient(host, port, true) {
143 @Override
144 public void action(Version serverVersion) throws Exception {
145 try {
146 Object rep = send(command);
147 result[0] = rep;
148 } catch (Exception e) {
149 Instance.syserr(e);
150 }
151 }
152 }.connect();
153 } catch (IOException e) {
154 Instance.syserr(e);
155 }
156
157 try {
158 return (T) result[0];
159 } catch (Exception e) {
160 Instance.syserr(e);
161 return null;
162 }
163 }
164 }