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; | |
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 NR |
48 | final List<MetaData> metas = new ArrayList<MetaData>(); |
49 | MetaData[] fromNetwork = this | |
50 | .<MetaData[]> getRemoteObject("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 NR |
62 | public BufferedImage getCover(final String luid) { |
63 | return this.<BufferedImage> getRemoteObject("GET_COVER " + luid); | |
b0e88ebd | 64 | } |
68e2c6d2 NR |
65 | |
66 | @Override | |
ff05b828 NR |
67 | public synchronized Story getStory(final String luid, Progress pg) { |
68 | return this.<Story> getRemoteObject("GET_STORY " + luid); | |
68e2c6d2 NR |
69 | } |
70 | ||
71 | @Override | |
72 | protected void clearCache() { | |
68e2c6d2 NR |
73 | } |
74 | ||
75 | @Override | |
76 | public synchronized Story save(Story story, String luid, Progress pg) | |
77 | throws IOException { | |
78 | throw new java.lang.InternalError( | |
79 | "No write support allowed on remote Libraries"); | |
80 | } | |
81 | ||
82 | @Override | |
a85e8077 | 83 | public synchronized void delete(String luid) throws IOException { |
68e2c6d2 NR |
84 | throw new java.lang.InternalError( |
85 | "No write support allowed on remote Libraries"); | |
86 | } | |
87 | ||
88 | @Override | |
a85e8077 | 89 | public void setSourceCover(String source, String luid) { |
68e2c6d2 NR |
90 | throw new java.lang.InternalError( |
91 | "No write support allowed on remote Libraries"); | |
92 | } | |
93 | ||
ff05b828 NR |
94 | @Override |
95 | public synchronized File getFile(final String luid) { | |
96 | throw new java.lang.InternalError( | |
97 | "Operation not supportorted on remote Libraries"); | |
98 | } | |
99 | ||
100 | // The following methods are only used by Save and Delete in BasicLibrary: | |
a85e8077 | 101 | |
68e2c6d2 | 102 | @Override |
a85e8077 NR |
103 | protected int getNextId() { |
104 | throw new java.lang.InternalError("Should not have been called"); | |
68e2c6d2 | 105 | } |
14b57448 NR |
106 | |
107 | @Override | |
a85e8077 NR |
108 | protected void doDelete(String luid) throws IOException { |
109 | throw new java.lang.InternalError("Should not have been called"); | |
110 | } | |
111 | ||
112 | @Override | |
113 | protected Story doSave(Story story, Progress pg) throws IOException { | |
114 | throw new java.lang.InternalError("Should not have been called"); | |
14b57448 | 115 | } |
e604986c NR |
116 | |
117 | /** | |
ff05b828 NR |
118 | * Return an object from the server. |
119 | * | |
120 | * @param <T> | |
121 | * the expected type of object | |
122 | * @param command | |
123 | * the command to send | |
e604986c | 124 | * |
ff05b828 | 125 | * @return the object or NULL |
e604986c | 126 | */ |
ff05b828 NR |
127 | @SuppressWarnings("unchecked") |
128 | private <T> T getRemoteObject(final String command) { | |
129 | final Object[] result = new Object[1]; | |
130 | try { | |
131 | new ConnectActionClient(host, port, true) { | |
132 | @Override | |
133 | public void action(Version serverVersion) throws Exception { | |
134 | try { | |
135 | Object rep = send(command); | |
136 | result[0] = rep; | |
137 | } catch (Exception e) { | |
138 | Instance.syserr(e); | |
139 | } | |
140 | } | |
141 | }.connect(); | |
142 | } catch (IOException e) { | |
143 | Instance.syserr(e); | |
144 | } | |
145 | ||
146 | try { | |
147 | return (T) result[0]; | |
148 | } catch (Exception e) { | |
149 | Instance.syserr(e); | |
150 | return null; | |
151 | } | |
e604986c | 152 | } |
b0e88ebd | 153 | } |