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 12, 2016
1 parent 9ab3968 commit 78defdd
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 10 deletions.
Expand Up @@ -4,6 +4,7 @@
import com.frostwire.jlibtorrent.DhtSettings;
import com.frostwire.jlibtorrent.Sha1Hash;
import com.frostwire.jlibtorrent.TcpEndpoint;
import com.frostwire.jlibtorrent.swig.bloom_filter_256;
import com.frostwire.jlibtorrent.swig.entry;

import java.util.HashMap;
Expand Down Expand Up @@ -41,8 +42,46 @@ public boolean getPeers(Sha1Hash infoHash, boolean noseed, boolean scrape, entry
if (!v.name.isEmpty()) {
peers.set("n", v.name);
}
/*
if (scrape)
{
bloom_filter_256 downloaders = new bloom_filter_256();
bloom_filter_256 seeds = new bloom_filter_256();
for (std::set<peer_entry>::const_iterator peer_it = v.peers.begin()
, end(v.peers.end()); peer_it != end; ++peer_it)
{
sha1_hash iphash;
hash_address(peer_it->addr.address(), iphash);
if (peer_it->seed) seeds.set(iphash);
else downloaders.set(iphash);
}
return false;
peers.set["BFpe"] = downloaders.to_string();
peers["BFsd"] = seeds.to_string();
}
else
{
int num = (std::min)(int(v.peers.size()), m_settings.max_peers_reply);
std::set<peer_entry>::const_iterator iter = v.peers.begin();
entry::list_type& pe = peers["values"].list();
std::string endpoint;
for (int t = 0, m = 0; m < num && iter != v.peers.end(); ++iter, ++t)
{
if ((random() / float(UINT_MAX + 1.f)) * (num - t) >= num - m) continue;
if (noseed && iter->seed) continue;
endpoint.resize(18);
std::string::iterator out = endpoint.begin();
write_endpoint(iter->addr, out);
endpoint.resize(out - endpoint.begin());
pe.push_back(entry(endpoint));
++m;
}
}*/

return true;
}

@Override
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/frostwire/jlibtorrent/swig/entry.java
Expand Up @@ -120,8 +120,16 @@ public void set(String key, String value) {
libtorrent_jni.entry_set__SWIG_0(swigCPtr, this, key, value);
}

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) {
libtorrent_jni.entry_set__SWIG_1(swigCPtr, this, key, value);
libtorrent_jni.entry_set__SWIG_2(swigCPtr, this, key, value);
}

public byte_vector string_bytes() {
return new byte_vector(libtorrent_jni.entry_string_bytes(swigCPtr, this), true);
}

public byte_vector bencode() {
Expand Down
Expand Up @@ -605,7 +605,9 @@ public class libtorrent_jni {
public final static native short entry_m_type_queried_get(long jarg1, entry 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, int 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 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_);
public final static native int sha1_hash_size_get();
Expand Down
9 changes: 9 additions & 0 deletions swig/libtorrent.i
Expand Up @@ -887,10 +887,19 @@ namespace libtorrent {
$self->operator[](key) = value;
}

void set(std::string const& key, std::vector<int8_t> const& value) {
$self->operator[](key) = std::string(value.begin(), value.end());
}

void set(std::string const& key, long 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());
}

std::vector<int8_t> bencode() {
std::vector<int8_t> buffer;
libtorrent::bencode(std::back_inserter(buffer), *$self);
Expand Down
74 changes: 70 additions & 4 deletions swig/libtorrent_jni.cpp
Expand Up @@ -1232,9 +1232,16 @@ SWIGINTERN libtorrent::entry &libtorrent_entry_get(libtorrent::entry *self,std::
SWIGINTERN void libtorrent_entry_set__SWIG_0(libtorrent::entry *self,std::string const &key,std::string const &value){
self->operator[](key) = value;
}
SWIGINTERN void libtorrent_entry_set__SWIG_1(libtorrent::entry *self,std::string const &key,long const &value){
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){
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());
}
SWIGINTERN std::vector< int8_t > libtorrent_entry_bencode(libtorrent::entry *self){
std::vector<int8_t> buffer;
libtorrent::bencode(std::back_inserter(buffer), *self);
Expand Down Expand Up @@ -17507,7 +17514,43 @@ 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_11(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_11(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jstring jarg2, jlong jarg3, jobject jarg3_) {
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
std::string *arg2 = 0 ;
std::vector< int8_t > *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 = *(std::vector< int8_t > **)&jarg3;
if (!arg3) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "std::vector< int8_t > const & reference is null");
return ;
}
{
try {
libtorrent_entry_set__SWIG_1(arg1,(std::string const &)*arg2,(std::vector< signed char > const &)*arg3);
} catch (std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
} catch (...) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unknown exception type");
}
}
}


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) {
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
std::string *arg2 = 0 ;
long *arg3 = 0 ;
Expand All @@ -17530,7 +17573,7 @@ SWIGEXPORT void JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entr
arg3 = &temp3;
{
try {
libtorrent_entry_set__SWIG_1(arg1,(std::string const &)*arg2,(long const &)*arg3);
libtorrent_entry_set__SWIG_2(arg1,(std::string const &)*arg2,(long const &)*arg3);
} catch (std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
} catch (...) {
Expand All @@ -17540,6 +17583,29 @@ SWIGEXPORT void JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_entr
}


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 ;
std::vector< int8_t > result;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(libtorrent::entry **)&jarg1;
{
try {
result = libtorrent_entry_string_bytes(arg1);
} catch (std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
} catch (...) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unknown exception type");
}
}
*(std::vector< int8_t > **)&jresult = new std::vector< int8_t >((const std::vector< int8_t > &)result);
return jresult;
}


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

(void)jenv;
(void)jcls;
result = (char *)("a7a42b52a80f307872f73aeb4ad26cb6414df465");
result = (char *)("9ab396824df8b6cbe0c40027d9ef43dbe3289420");
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}
Expand Down
110 changes: 107 additions & 3 deletions swig/libtorrent_node.cpp
Expand Up @@ -2640,9 +2640,16 @@ SWIGINTERN libtorrent::entry &libtorrent_entry_get(libtorrent::entry *self,std::
SWIGINTERN void libtorrent_entry_set__SWIG_0(libtorrent::entry *self,std::string const &key,std::string const &value){
self->operator[](key) = value;
}
SWIGINTERN void libtorrent_entry_set__SWIG_1(libtorrent::entry *self,std::string const &key,long const &value){
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){
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());
}
SWIGINTERN std::vector< int8_t > libtorrent_entry_bencode(libtorrent::entry *self){
std::vector<int8_t> buffer;
libtorrent::bencode(std::back_inserter(buffer), *self);
Expand Down Expand Up @@ -23714,6 +23721,58 @@ static SwigV8ReturnValue _wrap_entry_set__SWIG_1(const SwigV8Arguments &args, V8
{
SWIGV8_HANDLESCOPE();

v8::Handle<v8::Value> jsresult;
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
std::string *arg2 = 0 ;
std::vector< int8_t > *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_std__vectorT_signed_char_t, 0 );
if (!SWIG_IsOK(res3)) {
SWIG_exception_fail(SWIG_ArgError(res3), "in method '" "entry_set" "', argument " "3"" of type '" "std::vector< int8_t > const &""'");
}
if (!argp3) {
SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "entry_set" "', argument " "3"" of type '" "std::vector< int8_t > const &""'");
}
arg3 = (std::vector< int8_t > *)(argp3);
libtorrent_entry_set__SWIG_1(arg1,(std::string const &)*arg2,(std::vector< signed char > 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_set__SWIG_2(const SwigV8Arguments &args, V8ErrorHandler &SWIGV8_ErrorHandler)
{
SWIGV8_HANDLESCOPE();

v8::Handle<v8::Value> jsresult;
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
std::string *arg2 = 0 ;
Expand Down Expand Up @@ -23747,7 +23806,7 @@ static SwigV8ReturnValue _wrap_entry_set__SWIG_1(const SwigV8Arguments &args, V8
}
temp3 = (long)(val3);
arg3 = &temp3;
libtorrent_entry_set__SWIG_1(arg1,(std::string const &)*arg2,(long const &)*arg3);
libtorrent_entry_set__SWIG_2(arg1,(std::string const &)*arg2,(long const &)*arg3);
jsresult = SWIGV8_UNDEFINED();

if (SWIG_IsNewObj(res2)) delete arg2;
Expand Down Expand Up @@ -23800,6 +23859,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_2(args, errorHandler);
if(errorHandler.err.IsEmpty()) {
SWIGV8_ESCAPE(jsresult);
}
#else
_wrap_entry_set__SWIG_2(args, errorHandler);
if(errorHandler.err.IsEmpty()) {
return;
}
#endif
}


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

goto fail;
Expand All @@ -23808,6 +23883,34 @@ static SwigV8ReturnValue _wrap_entry__wrap_entry_set(const SwigV8Arguments &args
}


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

v8::Handle<v8::Value> jsresult;
libtorrent::entry *arg1 = (libtorrent::entry *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
std::vector< int8_t > result;

if(args.Length() != 0) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for _wrap_entry_string_bytes.");

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_string_bytes" "', argument " "1"" of type '" "libtorrent::entry *""'");
}
arg1 = (libtorrent::entry *)(argp1);
result = libtorrent_entry_string_bytes(arg1);
jsresult = SWIG_NewPointerObj((new std::vector< int8_t >((const std::vector< int8_t >&)(result))), SWIGTYPE_p_std__vectorT_signed_char_t, SWIG_POINTER_OWN | 0 );


SWIGV8_RETURN(jsresult);

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


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

Expand Down Expand Up @@ -103288,7 +103391,7 @@ static SwigV8ReturnValue _wrap_JLIBTORRENT_REVISION_SHA1(v8::Local<v8::String> p

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

jsresult = SWIG_FromCharPtr((const char *)"a7a42b52a80f307872f73aeb4ad26cb6414df465");
jsresult = SWIG_FromCharPtr((const char *)"9ab396824df8b6cbe0c40027d9ef43dbe3289420");

SWIGV8_RETURN_INFO(jsresult, info);

Expand Down Expand Up @@ -112621,6 +112724,7 @@ SWIGV8_AddMemberFunction(_exports_entry_class, "to_string", _wrap_entry_to_strin
SWIGV8_AddMemberVariable(_exports_entry_class, "m_type_queried", _wrap_entry_m_type_queried_get, _wrap_entry_m_type_queried_set);
SWIGV8_AddMemberFunction(_exports_entry_class, "get", _wrap_entry_get);
SWIGV8_AddMemberFunction(_exports_entry_class, "set", _wrap_entry__wrap_entry_set);
SWIGV8_AddMemberFunction(_exports_entry_class, "string_bytes", _wrap_entry_string_bytes);
SWIGV8_AddMemberFunction(_exports_entry_class, "bencode", _wrap_entry_bencode);
SWIGV8_AddMemberFunction(_exports_sha1_hash_class, "clear", _wrap_sha1_hash_clear);
SWIGV8_AddMemberFunction(_exports_sha1_hash_class, "is_all_zeros", _wrap_sha1_hash_is_all_zeros);
Expand Down

0 comments on commit 78defdd

Please sign in to comment.