Skip to content

Commit

Permalink
Improved swig entry API
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Feb 19, 2016
1 parent c6204c2 commit 1a7ea6e
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 11 deletions.
Expand Up @@ -18,19 +18,21 @@ public class DhtStorageBase implements DhtStorage {
private final DhtSettings settings;
private final Counters counters;

private final HashMap<Sha1Hash, TorrentEntry> map;
private final HashMap<Sha1Hash, TorrentEntry> torrents;
private final TreeMap<Sha1Hash, DhtImmutableItem> immutables;

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

this.map = new HashMap<Sha1Hash, TorrentEntry>();
this.torrents = new HashMap<Sha1Hash, TorrentEntry>();
this.immutables = new TreeMap<Sha1Hash, DhtImmutableItem>();
}

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

if (v == null) {
return false;
Expand Down Expand Up @@ -80,16 +82,16 @@ public boolean getPeers(Sha1Hash infoHash, boolean noseed, boolean scrape, entry

@Override
public void announcePeer(Sha1Hash infoHash, TcpEndpoint endp, String name, boolean seed) {
TorrentEntry v = map.get(infoHash);
TorrentEntry v = torrents.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()) {
if (!torrents.isEmpty() && torrents.size() >= settings.maxTorrents()) {
// we need to remove some. Remove the ones with the
// fewest peers
int num_peers = Integer.MAX_VALUE;
Sha1Hash candidateKey = null;
for (Map.Entry<Sha1Hash, TorrentEntry> kv : map.entrySet()) {
for (Map.Entry<Sha1Hash, TorrentEntry> kv : torrents.entrySet()) {

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

// the peer announces a torrent name, and we don't have a name
Expand Down Expand Up @@ -146,7 +148,13 @@ public void announcePeer(Sha1Hash infoHash, TcpEndpoint endp, String name, boole

@Override
public boolean getImmutableItem(Sha1Hash target, entry item) {
return false;
DhtImmutableItem i = immutables.get(target);
if (i == null) {
return false;
}

//item.set("v", = bdecode(i->second.value, i->second.value + i->second.size);
return true;
}

@Override
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/frostwire/jlibtorrent/swig/entry.java
Expand Up @@ -124,6 +124,10 @@ public void set(String key, long value) {
libtorrent_jni.entry_set__SWIG_2(swigCPtr, this, key, value);
}

public void set(String key, entry value) {
libtorrent_jni.entry_set__SWIG_3(swigCPtr, this, key, entry.getCPtr(value), value);
}

public byte_vector string_bytes() {
return new byte_vector(libtorrent_jni.entry_string_bytes(swigCPtr, this), true);
}
Expand Down
Expand Up @@ -599,6 +599,7 @@ public class libtorrent_jni {
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, long jarg3);
public final static native void entry_set__SWIG_3(long jarg1, entry jarg1_, String jarg2, long jarg3, entry 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
4 changes: 4 additions & 0 deletions swig/libtorrent.i
Expand Up @@ -940,6 +940,10 @@ namespace libtorrent {
$self->operator[](key) = value;
}

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

std::vector<int8_t> string_bytes() {
std::string s = $self->string();
return std::vector<int8_t>(s.begin(), s.end());
Expand Down
41 changes: 40 additions & 1 deletion swig/libtorrent_jni.cpp
Expand Up @@ -1241,6 +1241,9 @@ SWIGINTERN void libtorrent_entry_set__SWIG_1(libtorrent::entry *self,std::string
SWIGINTERN void libtorrent_entry_set__SWIG_2(libtorrent::entry *self,std::string const &key,long long const &value){
self->operator[](key) = value;
}
SWIGINTERN void libtorrent_entry_set__SWIG_3(libtorrent::entry *self,std::string const &key,libtorrent::entry const &value){
self->operator[](key) = value;
}
SWIGINTERN std::vector< int8_t > libtorrent_entry_string_bytes(libtorrent::entry *self){
std::string s = self->string();
return std::vector<int8_t>(s.begin(), s.end());
Expand Down Expand Up @@ -17539,6 +17542,42 @@ 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_13(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jstring jarg2, jlong jarg3, jobject jarg3_) {
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
std::string *arg2 = 0 ;
libtorrent::entry *arg3 = 0 ;

(void)jenv;
(void)jcls;
(void)jarg1_;
(void)jarg3_;
arg1 = *(libtorrent::entry **)&jarg1;
if(!jarg2) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null string");
return ;
}
const char *arg2_pstr = (const char *)jenv->GetStringUTFChars(jarg2, 0);
if (!arg2_pstr) return ;
std::string arg2_str(arg2_pstr);
arg2 = &arg2_str;
jenv->ReleaseStringUTFChars(jarg2, arg2_pstr);
arg3 = *(libtorrent::entry **)&jarg3;
if (!arg3) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "libtorrent::entry const & reference is null");
return ;
}
{
try {
libtorrent_entry_set__SWIG_3(arg1,(std::string const &)*arg2,(libtorrent::entry const &)*arg3);
} catch (std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
} catch (...) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unknown exception type");
}
}
}


SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entry_1string_1bytes(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jlong jresult = 0 ;
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
Expand Down Expand Up @@ -61984,7 +62023,7 @@ SWIGEXPORT jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_J

(void)jenv;
(void)jcls;
result = (char *)("25c160b3a20f0e71d58997b18ff7d43ae7054dd5");
result = (char *)("c6204c2b0e8700611ec9b20fc2782c9fb1d9a806");
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}
Expand Down
73 changes: 72 additions & 1 deletion swig/libtorrent_node.cpp
Expand Up @@ -2629,6 +2629,9 @@ SWIGINTERN void libtorrent_entry_set__SWIG_1(libtorrent::entry *self,std::string
SWIGINTERN void libtorrent_entry_set__SWIG_2(libtorrent::entry *self,std::string const &key,long long const &value){
self->operator[](key) = value;
}
SWIGINTERN void libtorrent_entry_set__SWIG_3(libtorrent::entry *self,std::string const &key,libtorrent::entry const &value){
self->operator[](key) = value;
}
SWIGINTERN std::vector< int8_t > libtorrent_entry_string_bytes(libtorrent::entry *self){
std::string s = self->string();
return std::vector<int8_t>(s.begin(), s.end());
Expand Down Expand Up @@ -23741,6 +23744,58 @@ static SwigV8ReturnValue _wrap_entry_set__SWIG_2(const SwigV8Arguments &args, V8
}


static SwigV8ReturnValue _wrap_entry_set__SWIG_3(const SwigV8Arguments &args, V8ErrorHandler &SWIGV8_ErrorHandler)
{
SWIGV8_HANDLESCOPE();

v8::Handle<v8::Value> jsresult;
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
std::string *arg2 = 0 ;
libtorrent::entry *arg3 = 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
int res2 = SWIG_OLDOBJ ;
void *argp3 ;
int res3 = 0 ;

res1 = SWIG_ConvertPtr(args.Holder(), &argp1,SWIGTYPE_p_libtorrent__entry, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "entry_set" "', argument " "1"" of type '" "libtorrent::entry *""'");
}
arg1 = (libtorrent::entry *)(argp1);
{
std::string *ptr = (std::string *)0;
res2 = SWIG_AsPtr_std_string(args[0], &ptr);
if (!SWIG_IsOK(res2)) {
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "entry_set" "', argument " "2"" of type '" "std::string const &""'");
}
if (!ptr) {
SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "entry_set" "', argument " "2"" of type '" "std::string const &""'");
}
arg2 = ptr;
}
res3 = SWIG_ConvertPtr(args[1], &argp3, SWIGTYPE_p_libtorrent__entry, 0 );
if (!SWIG_IsOK(res3)) {
SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "entry_set" "', argument " "3"" of type '" "libtorrent::entry const &""'");
}
if (!argp3) {
SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "entry_set" "', argument " "3"" of type '" "libtorrent::entry const &""'");
}
arg3 = (libtorrent::entry *)(argp3);
libtorrent_entry_set__SWIG_3(arg1,(std::string const &)*arg2,(libtorrent::entry const &)*arg3);
jsresult = SWIGV8_UNDEFINED();

if (SWIG_IsNewObj(res2)) delete arg2;


SWIGV8_RETURN(jsresult);

goto fail;
fail:
SWIGV8_RETURN(SWIGV8_UNDEFINED());
}


static SwigV8ReturnValue _wrap_entry__wrap_entry_set(const SwigV8Arguments &args) {
SWIGV8_HANDLESCOPE();

Expand Down Expand Up @@ -23796,6 +23851,22 @@ static SwigV8ReturnValue _wrap_entry__wrap_entry_set(const SwigV8Arguments &args
}


if(args.Length() == 2) {
errorHandler.err.Clear();
#if (SWIG_V8_VERSION < 0x031903)
jsresult = _wrap_entry_set__SWIG_3(args, errorHandler);
if(errorHandler.err.IsEmpty()) {
SWIGV8_ESCAPE(jsresult);
}
#else
_wrap_entry_set__SWIG_3(args, errorHandler);
if(errorHandler.err.IsEmpty()) {
return;
}
#endif
}


SWIG_exception_fail(SWIG_ERROR, "Illegal arguments for function set.");

goto fail;
Expand Down Expand Up @@ -104346,7 +104417,7 @@ static SwigV8ReturnValue _wrap_JLIBTORRENT_REVISION_SHA1(v8::Local<v8::String> p

v8::Handle<v8::Value> jsresult;

jsresult = SWIG_FromCharPtr((const char *)"25c160b3a20f0e71d58997b18ff7d43ae7054dd5");
jsresult = SWIG_FromCharPtr((const char *)"c6204c2b0e8700611ec9b20fc2782c9fb1d9a806");

SWIGV8_RETURN_INFO(jsresult, info);

Expand Down

0 comments on commit 1a7ea6e

Please sign in to comment.