Skip to content

Commit

Permalink
Improved API
Browse files Browse the repository at this point in the history
Started to implement testAnnouncePeer
  • Loading branch information
aldenml committed Feb 19, 2016
1 parent 0bd3ad3 commit 9057472
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 169 deletions.
17 changes: 8 additions & 9 deletions src/main/java/com/frostwire/jlibtorrent/DhtSettings.java
Expand Up @@ -3,12 +3,7 @@
import com.frostwire.jlibtorrent.swig.dht_settings;

/**
* structure used to hold configuration options for the DHT
* <p/>
* The ``dht_settings`` struct used to contain a ``service_port`` member to
* control which port the DHT would listen on and send messages from. This
* field is deprecated and ignored. libtorrent always tries to open the UDP
* socket on the same port as the TCP socket.
* Structure used to hold configuration options for the DHT.
*
* @author gubatron
* @author aldenml
Expand All @@ -21,7 +16,11 @@ public DhtSettings(dht_settings s) {
this.s = s;
}

public dht_settings getSwig() {
public DhtSettings() {
this(new dht_settings());
}

public dht_settings swig() {
return s;
}

Expand Down Expand Up @@ -118,7 +117,7 @@ public void maxTorrents(int value) {
*
* @return
*/
public int getMaxDHTItems() {
public int getMaxDhtItems() {
return s.getMax_dht_items();
}

Expand All @@ -127,7 +126,7 @@ public int getMaxDHTItems() {
*
* @param value
*/
public void setMaxDHTItems(int value) {
public void setMaxDhtItems(int value) {
s.setMax_dht_items(value);
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/frostwire/jlibtorrent/Session.java
Expand Up @@ -568,7 +568,7 @@ public List<TorrentHandle> getTorrents() {
// otherwise.

void setDHTSettings(DhtSettings settings) {
s.set_dht_settings(settings.getSwig());
s.set_dht_settings(settings.swig());
}

public boolean isDHTRunning() {
Expand Down
34 changes: 16 additions & 18 deletions src/main/java/com/frostwire/jlibtorrent/plugins/DhtStorageBase.java
Expand Up @@ -12,26 +12,25 @@
* @author gubatron
* @author aldenml
*/
public final class DhtStorageBase implements DhtStorage {
public class DhtStorageBase implements DhtStorage {

private final Sha1Hash id;
private final DhtSettings settings;
private final Counters counters;

private final HashMap<String, TorrentEntry> map;
private final HashMap<Sha1Hash, TorrentEntry> map;

public DhtStorageBase(Sha1Hash id, DhtSettings settings) {
this.id = id;
this.settings = settings;
this.counters = new Counters();

this.map = new HashMap<String, TorrentEntry>();
this.map = new HashMap<Sha1Hash, TorrentEntry>();
}

@Override
public boolean getPeers(Sha1Hash infoHash, boolean noseed, boolean scrape, entry peers) {
String hex = infoHash.toHex();
TorrentEntry v = map.get(hex);
TorrentEntry v = map.get(infoHash);

if (v == null) {
return false;
Expand All @@ -47,7 +46,7 @@ public boolean getPeers(Sha1Hash infoHash, boolean noseed, boolean scrape, entry

for (PeerEntry peer : v.peers) {
sha1_hash iphash = new sha1_hash();
libtorrent.sha1_hash_address(peer.addr.address().swig(), iphash);
libtorrent.sha1_hash_address(peer.addr.address(), iphash);
if (peer.seed) {
seeds.set(iphash);
} else {
Expand All @@ -68,7 +67,7 @@ public boolean getPeers(Sha1Hash infoHash, boolean noseed, boolean scrape, entry
if ((Math.random() / (Integer.MAX_VALUE + 1.f)) * (num - t) >= num - m) continue;
if (noseed && e.seed) continue;
endpoint.resize(18);
int n = libtorrent.write_tcp_endpoint(e.addr.swig(), endpoint);
int n = libtorrent.write_tcp_endpoint(e.addr, endpoint);
endpoint.resize(n);
pe.push_back(new entry(endpoint));

Expand All @@ -81,22 +80,21 @@ public boolean getPeers(Sha1Hash infoHash, boolean noseed, boolean scrape, entry

@Override
public void announcePeer(Sha1Hash infoHash, TcpEndpoint endp, String name, boolean seed) {
String hex = infoHash.toHex();
TorrentEntry v = map.get(hex);
TorrentEntry v = map.get(infoHash);
if (v == null) {
// we don't have this torrent, add it
// do we need to remove another one first?
if (!map.isEmpty() && map.size() >= settings.maxTorrents()) {
// we need to remove some. Remove the ones with the
// fewest peers
int num_peers = Integer.MAX_VALUE;
String candidateKey = null;
for (Map.Entry<String, TorrentEntry> kv : map.entrySet()) {
Sha1Hash candidateKey = null;
for (Map.Entry<Sha1Hash, TorrentEntry> kv : map.entrySet()) {

if (kv.getValue().peers.size() > num_peers) {
continue;
}
if (hex.equals(kv.getKey())) {
if (infoHash.equals(kv.getKey())) {
continue;
}
num_peers = kv.getValue().peers.size();
Expand All @@ -108,7 +106,7 @@ public void announcePeer(Sha1Hash infoHash, TcpEndpoint endp, String name, boole
}
counters.torrents += 1;
v = new TorrentEntry();
map.put(hex, v);
map.put(infoHash, v);
}

// the peer announces a torrent name, and we don't have a name
Expand All @@ -122,7 +120,7 @@ public void announcePeer(Sha1Hash infoHash, TcpEndpoint endp, String name, boole
}

PeerEntry peer = new PeerEntry();
peer.addr = endp;
peer.addr = endp.swig();
peer.added = System.currentTimeMillis();
peer.seed = seed;

Expand Down Expand Up @@ -184,16 +182,16 @@ public Counters counters() {
static final class PeerEntry {

public long added;
public TcpEndpoint addr;
public tcp_endpoint addr;
public boolean seed;

public static final Comparator<PeerEntry> COMPARATOR = new Comparator<PeerEntry>() {
@Override
public int compare(PeerEntry o1, PeerEntry o2) {
TcpEndpoint a1 = o1.addr;
TcpEndpoint a2 = o2.addr;
tcp_endpoint a1 = o1.addr;
tcp_endpoint a2 = o2.addr;

int r = Address.compare(a1.address(), a2.address());
int r = address.compare(a1.address(), a2.address());

return r == 0 ? Integer.compare(a1.port(), a2.port()) : r;
}
Expand Down
10 changes: 1 addition & 9 deletions src/main/java/com/frostwire/jlibtorrent/swig/entry.java
Expand Up @@ -104,14 +104,6 @@ public String to_string() {
return libtorrent_jni.entry_to_string(swigCPtr, this);
}

public void setM_type_queried(short value) {
libtorrent_jni.entry_m_type_queried_set(swigCPtr, this, value);
}

public short getM_type_queried() {
return libtorrent_jni.entry_m_type_queried_get(swigCPtr, this);
}

public entry(byte_vector string_bytes) {
this(libtorrent_jni.new_entry__SWIG_7(byte_vector.getCPtr(string_bytes), string_bytes), true);
}
Expand All @@ -128,7 +120,7 @@ public void set(String key, byte_vector value) {
libtorrent_jni.entry_set__SWIG_1(swigCPtr, this, key, byte_vector.getCPtr(value), value);
}

public void set(String key, int value) {
public void set(String key, long value) {
libtorrent_jni.entry_set__SWIG_2(swigCPtr, this, key, value);
}

Expand Down
Expand Up @@ -594,13 +594,11 @@ public class libtorrent_jni {
public final static native void entry_swap(long jarg1, entry jarg1_, long jarg2, entry jarg2_);
public final static native long entry_find_key(long jarg1, entry jarg1_, String jarg2);
public final static native String entry_to_string(long jarg1, entry jarg1_);
public final static native void entry_m_type_queried_set(long jarg1, entry jarg1_, short jarg2);
public final static native short entry_m_type_queried_get(long jarg1, entry jarg1_);
public final static native long new_entry__SWIG_7(long jarg1, byte_vector jarg1_);
public final static native long entry_get(long jarg1, entry jarg1_, String jarg2);
public final static native void entry_set__SWIG_0(long jarg1, entry jarg1_, String jarg2, String jarg3);
public final static native void entry_set__SWIG_1(long jarg1, entry jarg1_, String jarg2, long jarg3, byte_vector jarg3_);
public final static native void entry_set__SWIG_2(long jarg1, entry jarg1_, String jarg2, int jarg3);
public final static native void entry_set__SWIG_2(long jarg1, entry jarg1_, String jarg2, long jarg3);
public final static native long entry_string_bytes(long jarg1, entry jarg1_);
public final static native long entry_bencode(long jarg1, entry jarg1_);
public final static native long entry_bdecode(long jarg1, byte_vector jarg1_);
Expand Down
53 changes: 53 additions & 0 deletions src/test/java/com/frostwire/jlibtorrent/DhtStorageBaseTest.java
@@ -0,0 +1,53 @@
package com.frostwire.jlibtorrent;

import com.frostwire.jlibtorrent.plugins.DhtStorageBase;
import com.frostwire.jlibtorrent.swig.entry;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

/**
* @author gubatron
* @author aldenml
*/
public class DhtStorageBaseTest {

private static final Sha1Hash n1 = new Sha1Hash("5fbfbff10c5d6a4ec8a88e4c6ab4c28b95eee401");

@Test
public void testAnnouncePeer() {
DhtSettings sett = testSettings();
DhtStorageBase s = new DhtStorageBase(new Sha1Hash(), sett);

entry peers = new entry();
s.getPeers(n1, false, false, peers);

assertEquals(peers.get("n").type(), entry.data_type.undefined_t);
assertEquals(peers.get("values").type(), entry.data_type.undefined_t);
/*
tcp::endpoint p1 = ep("124.31.75.21", 1);
tcp::endpoint p2 = ep("124.31.75.22", 1);
tcp::endpoint p3 = ep("124.31.75.23", 1);
tcp::endpoint p4 = ep("124.31.75.24", 1);
s->announce_peer(n1, p1, "torrent_name", false);
s->get_peers(n1, false, false, peers);
TEST_EQUAL(peers["n"].string(), "torrent_name")
TEST_EQUAL(peers["values"].list().size(), 1)
s->announce_peer(n2, p2, "torrent_name1", false);
s->announce_peer(n2, p3, "torrent_name1", false);
s->announce_peer(n3, p4, "torrent_name2", false);
bool r = s->get_peers(n1, false, false, peers);
TEST_CHECK(!r);
*/
}

private static DhtSettings testSettings() {
DhtSettings sett = new DhtSettings();
sett.maxTorrents(2);
sett.setMaxDhtItems(2);
sett.itemLifetime(120 * 60);
return sett;
}
}
7 changes: 3 additions & 4 deletions swig/libtorrent.i
Expand Up @@ -429,9 +429,8 @@ namespace std {
%ignore libtorrent::entry::find_key(std::string const &) const;
%ignore libtorrent::entry::find_key(char const *);
%ignore libtorrent::entry::find_key(char const *) const;
%ignore libtorrent::entry::operator [](char const *);
%ignore libtorrent::entry::operator [](char const *) const;
%ignore libtorrent::entry::operator [](std::string const &) const;
%ignore libtorrent::entry::operator [];
%ignore libtorrent::entry::m_type_queried;
%ignore libtorrent::buffer::data() const;
%ignore libtorrent::buffer::begin() const;
%ignore libtorrent::buffer::end() const;
Expand Down Expand Up @@ -931,7 +930,7 @@ namespace libtorrent {
$self->operator[](key) = std::string(value.begin(), value.end());
}

void set(std::string const& key, long const& value) {
void set(std::string const& key, long long const& value) {
$self->operator[](key) = value;
}

Expand Down
42 changes: 7 additions & 35 deletions swig/libtorrent_jni.cpp
Expand Up @@ -1238,7 +1238,7 @@ SWIGINTERN void libtorrent_entry_set__SWIG_0(libtorrent::entry *self,std::string
SWIGINTERN void libtorrent_entry_set__SWIG_1(libtorrent::entry *self,std::string const &key,std::vector< int8_t > const &value){
self->operator[](key) = std::string(value.begin(), value.end());
}
SWIGINTERN void libtorrent_entry_set__SWIG_2(libtorrent::entry *self,std::string const &key,long const &value){
SWIGINTERN void libtorrent_entry_set__SWIG_2(libtorrent::entry *self,std::string const &key,long long const &value){
self->operator[](key) = value;
}
SWIGINTERN std::vector< int8_t > libtorrent_entry_string_bytes(libtorrent::entry *self){
Expand Down Expand Up @@ -17364,34 +17364,6 @@ SWIGEXPORT jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_e
}


SWIGEXPORT void JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entry_1m_1type_1queried_1set(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jshort jarg2) {
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
boost::uint8_t arg2 ;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(libtorrent::entry **)&jarg1;
arg2 = (boost::uint8_t)jarg2;
if (arg1) (arg1)->m_type_queried = arg2;
}


SWIGEXPORT jshort JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entry_1m_1type_1queried_1get(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jshort jresult = 0 ;
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
boost::uint8_t result;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(libtorrent::entry **)&jarg1;
result = (boost::uint8_t) ((arg1)->m_type_queried);
jresult = (jshort)result;
return jresult;
}


SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_new_1entry_1_1SWIG_17(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jlong jresult = 0 ;
std::vector< int8_t > *arg1 = 0 ;
Expand Down Expand Up @@ -17527,11 +17499,11 @@ SWIGEXPORT void JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entr
}


SWIGEXPORT void JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entry_1set_1_1SWIG_12(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jstring jarg2, jint jarg3) {
SWIGEXPORT void JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entry_1set_1_1SWIG_12(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jstring jarg2, jlong jarg3) {
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
std::string *arg2 = 0 ;
long *arg3 = 0 ;
long temp3 ;
long long *arg3 = 0 ;
long long temp3 ;

(void)jenv;
(void)jcls;
Expand All @@ -17546,11 +17518,11 @@ SWIGEXPORT void JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entr
std::string arg2_str(arg2_pstr);
arg2 = &arg2_str;
jenv->ReleaseStringUTFChars(jarg2, arg2_pstr);
temp3 = (long)jarg3;
temp3 = (long long)jarg3;
arg3 = &temp3;
{
try {
libtorrent_entry_set__SWIG_2(arg1,(std::string const &)*arg2,(long const &)*arg3);
libtorrent_entry_set__SWIG_2(arg1,(std::string const &)*arg2,(long long const &)*arg3);
} catch (std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
} catch (...) {
Expand Down Expand Up @@ -61758,7 +61730,7 @@ SWIGEXPORT jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_J

(void)jenv;
(void)jcls;
result = (char *)("c49a9f5f50156e2859f6797df6c488994077a0b6");
result = (char *)("0bd3ad33ef8676c9dd83235a2af9912d66d73573");
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}
Expand Down

0 comments on commit 9057472

Please sign in to comment.