1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.plugin.opennap.net;
21
22 import java.io.File;
23 import java.util.Hashtable;
24 import java.util.*;
25
26 import org.apache.log4j.Logger;
27 import org.xnap.*;
28 import org.xnap.gui.StatusBar;
29 import org.xnap.plugin.opennap.net.msg.MessageHandler;
30 import org.xnap.plugin.opennap.net.msg.client.*;
31 import org.xnap.plugin.opennap.net.msg.client.UploadAckMessage;
32 import org.xnap.plugin.opennap.net.msg.server.DirectBrowseRequestMessage;
33 import org.xnap.plugin.opennap.net.msg.server.UploadRequestMessage;
34 import org.xnap.plugin.opennap.user.OpenNapUser;
35 import org.xnap.plugin.opennap.util.OpenNapFileHelper;
36 import org.xnap.transfer.*;
37 import org.xnap.transfer.UploadManager;
38 import org.xnap.util.Preferences;
39
40 public class OpenNapTransferManager {
41
42
43
44
45
46 private static Logger logger
47 = Logger.getLogger(OpenNapTransferManager.class);
48
49 /***
50 * Stores a list of {@link OpenNapDownloadContainer} objects.
51 */
52 private List downloads = new LinkedList();
53 private Hashtable uploadByRequest = new Hashtable();
54 private OpenNapResumeRepository repository = new OpenNapResumeRepository();
55
56
57
58 public OpenNapTransferManager()
59 {
60 }
61
62
63
64 private synchronized void add(OpenNapDownloadContainer d)
65 {
66 DownloadManager.getInstance().add(d);
67 downloads.add(d);
68 }
69
70 public synchronized void clear()
71 {
72 for (Iterator i = downloads.iterator(); i.hasNext();) {
73 OpenNapDownloadContainer d = (OpenNapDownloadContainer)i.next();
74 d.stop();
75
76 DownloadManager.getInstance().remove(d);
77 i.remove();
78 }
79 }
80
81 public synchronized void done(OpenNapUpload u)
82 {
83 uploadByRequest.remove(u.getHash());
84 }
85
86 /***
87 * Creates a new {@link OpenNapDownloadContainer} object for
88 * results if no download for results is already running.
89 *
90 * @return the created or running download */
91 public synchronized OpenNapDownloadContainer download
92 (OpenNapSearchResult results[])
93 {
94 for (Iterator it = downloads.iterator(); it.hasNext();) {
95 OpenNapDownloadContainer d
96 = (OpenNapDownloadContainer)it.next();
97 if (d.add(results[0], true)) {
98 for (int i = 1; i < results.length; i++) {
99 d.add(results[i], true);
100 }
101 StatusBar.setText(XNap.tr("Results added to already running download"));
102 return d;
103 }
104 }
105
106 OpenNapDownloadContainer d = new OpenNapDownloadContainer(results);
107 add(d);
108 return d;
109 }
110
111 public OpenNapResumeRepository getResumeRepository()
112 {
113 return repository;
114 }
115
116 public synchronized void received(DirectBrowseRequestMessage msg)
117 {
118 OpenNapUser user = msg.getServer().getUser(msg.nick);
119 user.setHost(msg.ip);
120 user.setPort(msg.port);
121
122 OpenNapDirectBrowseUpload u = new OpenNapDirectBrowseUpload(user);
123
124
125 u.start();
126 }
127
128 public synchronized void received(UploadRequestMessage msg)
129 {
130 String hash = msg.nick + ":" + msg.filename;
131 OpenNapUpload u
132 = (OpenNapUpload)uploadByRequest.get(hash);
133 if (u == null) {
134 OpenNapUser user = msg.getServer().getUser(msg.nick);
135 File file = OpenNapFileHelper.getSharedFile(msg.filename);
136 if (user.isUploadDenied() || file == null) {
137
138 MessageHandler.send
139 (msg.getServer(),
140 new AcceptFailedMessage(msg.nick, msg.filename));
141 return;
142 }
143
144
145 u = new OpenNapUpload(msg.getServer(), user, file, msg.filename);
146 u.setHash(hash);
147 uploadByRequest.put(hash, u);
148 u.start();
149
150 UploadManager.getInstance().add(u);
151 int pos = u.getQueuePosition();
152 if (pos > 0) {
153 QueueLimitMessage m
154 = new QueueLimitMessage(msg.nick, msg.filename, pos);
155 MessageHandler.send(msg.getServer(), m);
156 }
157 }
158 else {
159 int pos = u.getQueuePosition();
160 if (pos == 0) {
161
162
163 MessageHandler.send
164 (msg.getServer(),
165 new UploadAckMessage(msg.nick, msg.filename));
166 }
167 else {
168 QueueLimitMessage m
169 = new QueueLimitMessage(msg.nick, msg.filename, pos);
170 MessageHandler.send(msg.getServer(), m);
171
172 u.requested();
173 }
174 }
175 }
176
177 public synchronized void remove(OpenNapDownloadContainer download)
178 {
179 downloads.remove(download);
180 }
181
182 /***
183 * Restores the resumes.
184 */
185 public synchronized void restore()
186 {
187 repository.restore
188 (new File(Preferences.getInstance().getIncompleteDir()));
189
190 for (Iterator it = repository.iterator(); it.hasNext(); ) {
191 OpenNapDownloadContainerData data
192 = (OpenNapDownloadContainerData)it.next();
193 OpenNapDownloadContainer d = new OpenNapDownloadContainer(data);
194 add(d);
195 }
196 }
197
198 public void save()
199 {
200 repository.save
201 (new File(Preferences.getInstance().getIncompleteDir()));
202 }
203
204 public synchronized void startAllDownloads()
205 {
206 for (Iterator i = downloads.iterator(); i.hasNext();) {
207 OpenNapDownloadContainer d = (OpenNapDownloadContainer)i.next();
208 d.startTransfer();
209 }
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273 }