Improve remote, fix bugs, update nikiroo-utils
[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;
14import 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 */
23public class RemoteLibrary extends BasicLibrary {
b0e88ebd
NR
24 private String host;
25 private int port;
68e2c6d2
NR
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) {
b0e88ebd
NR
36 this.host = host;
37 this.port = port;
b0e88ebd
NR
38 }
39
99ccbdf6
NR
40 @Override
41 public String getLibraryName() {
42 return host + ":" + port;
43 }
44
b0e88ebd 45 @Override
68e2c6d2
NR
46 protected List<MetaData> getMetas(Progress pg) {
47 // TODO: progress
ff05b828 48 final List<MetaData> metas = new ArrayList<MetaData>();
085a2f9a
NR
49 MetaData[] fromNetwork = this.<MetaData[]> getRemoteObject( //
50 new Object[] { "GET_METADATA", "*" });
b0e88ebd 51
ff05b828
NR
52 if (fromNetwork != null) {
53 for (MetaData meta : fromNetwork) {
54 metas.add(meta);
e604986c 55 }
b0e88ebd
NR
56 }
57
68e2c6d2 58 return metas;
b0e88ebd
NR
59 }
60
61 @Override
ff05b828 62 public BufferedImage getCover(final String luid) {
085a2f9a
NR
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 });
b0e88ebd 71 }
68e2c6d2
NR
72
73 @Override
ff05b828 74 public synchronized Story getStory(final String luid, Progress pg) {
085a2f9a 75 return this.<Story> getRemoteObject(new Object[] { "GET_STORY", luid });
68e2c6d2
NR
76 }
77
78 @Override
79 protected void clearCache() {
68e2c6d2
NR
80 }
81
82 @Override
83 public synchronized Story save(Story story, String luid, Progress pg)
84 throws IOException {
085a2f9a
NR
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;
68e2c6d2
NR
92 }
93
94 @Override
a85e8077 95 public synchronized void delete(String luid) throws IOException {
085a2f9a 96 getRemoteObject(new Object[] { "DELETE_STORY", luid });
68e2c6d2
NR
97 }
98
99 @Override
a85e8077 100 public void setSourceCover(String source, String luid) {
085a2f9a
NR
101 this.<BufferedImage> getRemoteObject( //
102 new Object[] { "SET_SOURCE_COVER", source, luid });
68e2c6d2
NR
103 }
104
ff05b828 105 @Override
2249988a 106 public synchronized File getFile(final String luid, Progress pg) {
ff05b828
NR
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:
a85e8077 112
68e2c6d2 113 @Override
a85e8077
NR
114 protected int getNextId() {
115 throw new java.lang.InternalError("Should not have been called");
68e2c6d2 116 }
14b57448
NR
117
118 @Override
a85e8077
NR
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");
14b57448 126 }
e604986c
NR
127
128 /**
ff05b828
NR
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
e604986c 135 *
ff05b828 136 * @return the object or NULL
e604986c 137 */
ff05b828 138 @SuppressWarnings("unchecked")
085a2f9a 139 private <T> T getRemoteObject(final Object[] command) {
ff05b828
NR
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 }
e604986c 163 }
b0e88ebd 164}