-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
GetMagnet2.java
97 lines (76 loc) · 3.02 KB
/
GetMagnet2.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
package com.frostwire.jlibtorrent.demo;
import com.frostwire.jlibtorrent.AlertListener;
import com.frostwire.jlibtorrent.Downloader;
import com.frostwire.jlibtorrent.Entry;
import com.frostwire.jlibtorrent.Session;
import com.frostwire.jlibtorrent.alerts.Alert;
import com.frostwire.jlibtorrent.alerts.AlertType;
import com.frostwire.jlibtorrent.alerts.DhtStatsAlert;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* @author gubatron
* @author aldenml
*/
public final class GetMagnet2 {
public static void main(String[] args) throws Throwable {
final String uri = "magnet:?xt=urn:btih:a83cc13bf4a07e85b938dcf06aa707955687ca7c";
final Session s = new Session();
final CountDownLatch signal = new CountDownLatch(1);
// the session stats are posted about once per second.
AlertListener l = new AlertListener() {
@Override
public int[] types() {
return new int[]{AlertType.SESSION_STATS.getSwig(), AlertType.DHT_STATS.getSwig()};
}
@Override
public void alert(Alert<?> alert) {
if (alert.getType().equals(AlertType.SESSION_STATS)) {
s.postDHTStats();
}
if (alert.getType().equals(AlertType.DHT_STATS)) {
long nodes = ((DhtStatsAlert) alert).totalNodes();
// wait for at least 10 nodes in the DHT.
if (nodes >= 10) {
System.out.println("DHT contains " + nodes + " nodes");
signal.countDown();
}
}
}
};
s.addListener(l);
s.postDHTStats();
final Downloader d = new Downloader(s);
System.out.println("Waiting for nodes in DHT (10 seconds)...");
boolean r = signal.await(10, TimeUnit.SECONDS);
if (!r) {
System.out.println("DHT bootstrap timeout");
System.exit(0);
}
// no more trigger of DHT stats
s.removeListener(l);
System.out.println("Fetching the magnet uri (multi thread), please wait...");
final AtomicInteger counter = new AtomicInteger(0);
for (int i = 0; i < 3; i++) {
final int index = i;
Thread t = new Thread() {
@Override
public void run() {
byte[] data = d.fetchMagnet(uri, 30000);
int count = counter.incrementAndGet();
if (data != null) {
System.out.println("Success fetching magnet: " + index + "/" + count);
} else {
System.out.println("Failed to retrieve the magnet: " + index + "/" + count);
}
}
};
t.start();
//t.join();
}
System.out.println("Press ENTER to exit");
System.in.read();
s.abort();
}
}