Skip to content

Commit

Permalink
Improved DHT API with synchronous getPeers functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Jan 12, 2015
1 parent 5cdf54c commit 50147b3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
43 changes: 43 additions & 0 deletions src/com/frostwire/jlibtorrent/DHT.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.frostwire.jlibtorrent;

import com.frostwire.jlibtorrent.alerts.Alert;
import com.frostwire.jlibtorrent.alerts.DhtGetPeersReplyAlert;
import com.frostwire.jlibtorrent.alerts.DhtImmutableItemAlert;
import com.frostwire.jlibtorrent.swig.char_vector;
import com.frostwire.jlibtorrent.swig.dht_item;
import com.frostwire.jlibtorrent.swig.sha1_hash;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static com.frostwire.jlibtorrent.alerts.AlertType.DHT_GET_PEERS_REPLY_ALERT;
import static com.frostwire.jlibtorrent.alerts.AlertType.DHT_IMMUTABLE_ITEM;

/**
Expand All @@ -20,6 +23,7 @@
public final class DHT {

private static final int[] DHT_IMMUTABLE_ITEM_TYPES = {DHT_IMMUTABLE_ITEM.getSwig()};
private static final int[] DHT_GET_PEERS_REPLY_ALERT_TYPES = {DHT_GET_PEERS_REPLY_ALERT.getSwig()};

private final Session s;

Expand Down Expand Up @@ -107,6 +111,45 @@ public void getPeers(String sha1) {
s.dhtGetPeers(new Sha1Hash(sha1));
}

public ArrayList<TcpEndpoint> getPeers(String sha1, long timeout, TimeUnit unit) {
final Sha1Hash target = new Sha1Hash(sha1);
final Object[] result = {new ArrayList<TcpEndpoint>()};
final CountDownLatch signal = new CountDownLatch(1);

AlertListener l = new AlertListener() {

@Override
public int[] types() {
return DHT_GET_PEERS_REPLY_ALERT_TYPES;
}

@Override
public void alert(Alert<?> alert) {
if (alert instanceof DhtGetPeersReplyAlert) {
DhtGetPeersReplyAlert replyAlert = (DhtGetPeersReplyAlert) alert;
if (target.equals(replyAlert.getInfoHash())) {
result[0] = replyAlert.getPeers();
signal.countDown();
}
}
}
};

s.addListener(l);

s.dhtGetPeers(target);

try {
signal.await(timeout, unit);
} catch (InterruptedException e) {
// ignore
}

s.removeListener(l);

return (ArrayList<TcpEndpoint>) result[0];
}

public void announce(String sha1, int port, int flags) {
s.dhtAnnounce(new Sha1Hash(sha1), port, flags);
}
Expand Down
21 changes: 10 additions & 11 deletions src/com/frostwire/jlibtorrent/demo/DhtTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.frostwire.jlibtorrent.demo;

import com.frostwire.jlibtorrent.AlertListener;
import com.frostwire.jlibtorrent.DHT;
import com.frostwire.jlibtorrent.Session;
import com.frostwire.jlibtorrent.TcpEndpoint;
import com.frostwire.jlibtorrent.*;
import com.frostwire.jlibtorrent.alerts.Alert;
import com.frostwire.jlibtorrent.alerts.DhtBootstrapAlert;
import com.frostwire.jlibtorrent.alerts.DhtGetPeersReplyAlert;
Expand Down Expand Up @@ -36,7 +33,7 @@ public int[] types() {

@Override
public void alert(Alert<?> alert) {
System.out.println(alert);
//System.out.println(alert);

if (alert instanceof DhtBootstrapAlert) {
signal.countDown();
Expand All @@ -62,18 +59,20 @@ public void alert(Alert<?> alert) {

signal.await();

System.out.println("Calling dht_get_peers");
//System.out.println("Calling dht_get_peers");

dht.getPeers("86d0502ead28e495c9e67665340f72aa72fe304");
//dht.getPeers("86d0502ead28e495c9e67665340f72aa72fe304");

Thread.sleep(4000);
dht.waitNodes(1);

dht.announce("47d0502ead28e495c9e67665340f72aa72fe304", 9999, 0);
//dht.announce("47d0502ead28e495c9e67665340f72aa72fe304", 9999, 0);

System.out.println("Waiting 15 seconds");
Thread.sleep(15000);
//System.out.println("Waiting 15 seconds");
//Thread.sleep(15000);

dht.getPeers("47d0502ead28e495c9e67665340f72aa72fe304");
dht.getPeers("5472d2fe734c16f28912e1e756b57e2470148b93");
//dht.getPeers("47d0502ead28e495c9e67665340f72aa72fe304");

System.out.println("Press ENTER to exit");
System.in.read();
Expand Down

0 comments on commit 50147b3

Please sign in to comment.