-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Downloader.java
156 lines (124 loc) · 4.87 KB
/
Downloader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package com.frostwire.jlibtorrent;
import com.frostwire.jlibtorrent.alerts.Alert;
import com.frostwire.jlibtorrent.alerts.AlertType;
import com.frostwire.jlibtorrent.alerts.MetadataReceivedAlert;
import com.frostwire.jlibtorrent.swig.*;
import java.io.File;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* This class provides a lens only functionality.
*
* @author gubatron
* @author aldenml
*/
public final class Downloader {
private static final Logger LOG = Logger.getLogger(Downloader.class);
private static final int[] LISTENER_TYPES = new int[]{AlertType.METADATA_RECEIVED.getSwig()};
private final Session s;
public Downloader(Session s) {
this.s = s;
}
public TorrentHandle find(Sha1Hash infoHash) {
return s.findTorrent(infoHash);
}
public TorrentHandle find(String infoHash) {
return s.findTorrent(new Sha1Hash(infoHash));
}
public void download(TorrentInfo ti, File saveDir, Priority[] priorities, File resumeFile) {
TorrentHandle th = s.findTorrent(ti.getInfoHash());
if (th != null) {
// found a download with the same hash, just adjust the priorities if needed
if (priorities != null) {
if (ti.getNumFiles() != priorities.length) {
throw new IllegalArgumentException("The priorities length should be equals to the number of files");
}
th.prioritizeFiles(priorities);
} else {
// did they just add the entire torrent (therefore not selecting any priorities)
final Priority[] wholeTorrentPriorities = Priority.array(Priority.NORMAL, ti.getNumFiles());
th.prioritizeFiles(wholeTorrentPriorities);
}
} else { // new download
s.asyncAddTorrent(ti, saveDir, priorities, resumeFile);
}
}
public void download(TorrentInfo ti, File saveDir) {
download(ti, saveDir, null, null);
}
/**
* @param uri
* @param timeout in milliseconds
* @return
*/
public byte[] fetchMagnet(String uri, long timeout) {
add_torrent_params p = add_torrent_params.create_instance_disabled_storage();
error_code ec = new error_code();
libtorrent.parse_magnet_uri(uri, p, ec);
if (ec.value() != 0) {
throw new IllegalArgumentException(ec.message());
}
final sha1_hash info_hash = p.getInfo_hash();
boolean add = true;
torrent_handle th = null;//s.getSwig().find_torrent(info_hash);
/*if (th != null && th.is_valid()) {
// we have a download with the same info-hash, let's see if we have the torrent info
torrent_info ti = th.get_torrent_copy();
if (ti != null && ti.is_valid()) {
// ok. we have it, ready to return the data
return new TorrentInfo(th.get_torrent_copy()).bencode();
} else {
add = false;
}
}*/
final CountDownLatch signal = new CountDownLatch(1);
final byte[][] data = new byte[1][];
AlertListener l = new AlertListener() {
@Override
public int[] types() {
return LISTENER_TYPES;
}
@Override
public void alert(Alert<?> alert) {
try {
if (!(alert instanceof MetadataReceivedAlert)) {
return;
}
MetadataReceivedAlert mr = (MetadataReceivedAlert) alert;
torrent_handle th = mr.getHandle().getSwig();
if (th.info_hash().op_eq(info_hash)) {
try {
data[0] = new TorrentInfo(th.get_torrent_copy()).bencode();
} catch (Throwable e) {
LOG.error("Error encoding torrent info", e);
}
signal.countDown();
}
} catch (Throwable e) {
LOG.error("Error in fetch magnet internal listener", e);
signal.countDown();
}
}
};
s.addListener(l);
if (add) {
p.setName("fetch_magnet:" + uri);
p.setSave_path("fetch_magnet/" + uri);
long flags = p.get_flags();
flags &= ~add_torrent_params.flags_t.flag_auto_managed.swigValue();
p.set_flags(flags);
ec.clear();
th = s.getSwig().add_torrent(p, ec);
th.resume();
}
try {
signal.await(timeout, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
}
s.removeListener(l);
if (add && th != null && th.is_valid()) {
s.getSwig().remove_torrent(th);
}
return data[0];
}
}