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; | |
12 | import be.nikiroo.fanfix.output.BasicOutput.OutputType; | |
13 | import be.nikiroo.utils.Progress; | |
14 | import be.nikiroo.utils.Version; | |
15 | import be.nikiroo.utils.serial.ConnectActionClient; | |
16 | ||
68e2c6d2 NR |
17 | /** |
18 | * This {@link BasicLibrary} will access a remote server to list the available | |
19 | * stories, and download the one you try to load to the local directory | |
20 | * specified in the configuration. | |
21 | * | |
22 | * @author niki | |
23 | */ | |
24 | public class RemoteLibrary extends BasicLibrary { | |
b0e88ebd NR |
25 | private String host; |
26 | private int port; | |
68e2c6d2 NR |
27 | private File baseDir; |
28 | ||
29 | private LocalLibrary lib; | |
30 | private List<MetaData> metas; | |
31 | ||
32 | /** | |
33 | * Create a {@link RemoteLibrary} linked to the given server. | |
34 | * | |
35 | * @param host | |
36 | * the host to contact or NULL for localhost | |
37 | * @param port | |
38 | * the port to contact it on | |
39 | */ | |
40 | public RemoteLibrary(String host, int port) { | |
b0e88ebd NR |
41 | this.host = host; |
42 | this.port = port; | |
43 | ||
b0e88ebd NR |
44 | this.baseDir = Instance.getRemoteDir(host); |
45 | this.baseDir.mkdirs(); | |
46 | ||
68e2c6d2 NR |
47 | this.lib = new LocalLibrary(baseDir, OutputType.INFO_TEXT, |
48 | OutputType.CBZ); | |
b0e88ebd NR |
49 | } |
50 | ||
51 | @Override | |
68e2c6d2 NR |
52 | protected List<MetaData> getMetas(Progress pg) { |
53 | // TODO: progress | |
b0e88ebd | 54 | |
68e2c6d2 NR |
55 | if (metas == null) { |
56 | metas = new ArrayList<MetaData>(); | |
b0e88ebd | 57 | |
b0e88ebd NR |
58 | try { |
59 | new ConnectActionClient(host, port, true, null) { | |
60 | public void action(Version serverVersion) throws Exception { | |
61 | try { | |
62 | Object rep = send("GET_METADATA *"); | |
63 | for (MetaData meta : (MetaData[]) rep) { | |
68e2c6d2 | 64 | metas.add(meta); |
b0e88ebd NR |
65 | } |
66 | } catch (Exception e) { | |
68e2c6d2 | 67 | Instance.syserr(e); |
b0e88ebd NR |
68 | } |
69 | } | |
70 | }.connect(); | |
71 | } catch (IOException e) { | |
72 | Instance.syserr(e); | |
73 | } | |
74 | } | |
75 | ||
68e2c6d2 | 76 | return metas; |
b0e88ebd NR |
77 | } |
78 | ||
79 | @Override | |
80 | public synchronized File getFile(final String luid) { | |
81 | File file = lib.getFile(luid); | |
82 | if (file == null) { | |
83 | final File[] tmp = new File[1]; | |
84 | try { | |
85 | new ConnectActionClient(host, port, true, null) { | |
86 | public void action(Version serverVersion) throws Exception { | |
87 | try { | |
88 | Object rep = send("GET_STORY " + luid); | |
89 | Story story = (Story) rep; | |
90 | if (story != null) { | |
91 | lib.save(story, luid, null); | |
92 | tmp[0] = lib.getFile(luid); | |
93 | } | |
94 | } catch (Exception e) { | |
95 | Instance.syserr(e); | |
96 | } | |
97 | } | |
98 | }.connect(); | |
99 | } catch (IOException e) { | |
100 | Instance.syserr(e); | |
101 | } | |
102 | ||
103 | file = tmp[0]; | |
104 | } | |
105 | ||
106 | if (file != null) { | |
107 | MetaData meta = getInfo(luid); | |
68e2c6d2 | 108 | metas.add(meta); |
b0e88ebd NR |
109 | } |
110 | ||
111 | return file; | |
112 | } | |
68e2c6d2 NR |
113 | |
114 | @Override | |
115 | public BufferedImage getCover(String luid) { | |
116 | // Retrieve it from the network if needed: | |
117 | if (lib.getInfo(luid) == null) { | |
118 | getFile(luid); | |
119 | } | |
120 | ||
121 | return lib.getCover(luid); | |
122 | } | |
123 | ||
124 | @Override | |
125 | protected void clearCache() { | |
126 | metas = null; | |
127 | lib.clearCache(); | |
128 | } | |
129 | ||
130 | @Override | |
131 | public synchronized Story save(Story story, String luid, Progress pg) | |
132 | throws IOException { | |
133 | throw new java.lang.InternalError( | |
134 | "No write support allowed on remote Libraries"); | |
135 | } | |
136 | ||
137 | @Override | |
138 | protected int getNextId() { | |
139 | throw new java.lang.InternalError( | |
140 | "No write support allowed on remote Libraries"); | |
141 | } | |
142 | ||
143 | @Override | |
144 | protected void doDelete(String luid) throws IOException { | |
145 | throw new java.lang.InternalError( | |
146 | "No write support allowed on remote Libraries"); | |
147 | } | |
148 | ||
149 | @Override | |
150 | protected Story doSave(Story story, Progress pg) throws IOException { | |
151 | throw new java.lang.InternalError( | |
152 | "No write support allowed on remote Libraries"); | |
153 | } | |
b0e88ebd | 154 | } |