Skip to content

Commit

Permalink
Improved Sha1Hash API
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Feb 19, 2016
1 parent c49a9f5 commit 6c91f25
Show file tree
Hide file tree
Showing 8 changed files with 306 additions and 17 deletions.
Expand Up @@ -278,7 +278,7 @@ public Sha1Hash infoHash() {
* @param value
*/
public void infoHash(Sha1Hash value) {
p.setInfo_hash(value.getSwig());
p.setInfo_hash(value.swig());
}

/*
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/frostwire/jlibtorrent/Session.java
Expand Up @@ -504,7 +504,7 @@ public void postDHTStats() {
* @return
*/
public TorrentHandle findTorrent(Sha1Hash infoHash) {
torrent_handle th = s.find_torrent(infoHash.getSwig());
torrent_handle th = s.find_torrent(infoHash.swig());

return th != null && th.is_valid() ? new TorrentHandle(th) : null;
}
Expand Down Expand Up @@ -609,7 +609,7 @@ public void addDHTRouter(Pair<String, Integer> node) {
* @param target
*/
public void dhtGetItem(Sha1Hash target) {
s.dht_get_item(target.getSwig());
s.dht_get_item(target.swig());
}

/**
Expand Down Expand Up @@ -687,15 +687,15 @@ public void dhtPutItem(byte[] publicKey, byte[] privateKey, Entry entry, byte[]
}

public void dhtGetPeers(Sha1Hash infoHash) {
s.dht_get_peers(infoHash.getSwig());
s.dht_get_peers(infoHash.swig());
}

public void dhtAnnounce(Sha1Hash infoHash, int port, int flags) {
s.dht_announce(infoHash.getSwig(), port, flags);
s.dht_announce(infoHash.swig(), port, flags);
}

public void dhtAnnounce(Sha1Hash infoHash) {
s.dht_announce(infoHash.getSwig());
s.dht_announce(infoHash.swig());
}

public void dhtDirectRequest(UdpEndpoint endp, Entry entry) {
Expand Down
21 changes: 19 additions & 2 deletions src/main/java/com/frostwire/jlibtorrent/Sha1Hash.java
Expand Up @@ -21,6 +21,14 @@ public Sha1Hash(sha1_hash h) {
this.h = h;
}

public Sha1Hash(byte[] bytes) {
if (bytes.length != 20) {
throw new IllegalArgumentException("bytes array must be of length 20");
}

this.h = new sha1_hash(Vectors.bytes2byte_vector(bytes));
}

public Sha1Hash(String hex) {
this();
if (!sha1_hash.from_hex(hex, h)) {
Expand All @@ -29,13 +37,13 @@ public Sha1Hash(String hex) {
}

/**
* Constructs an all-sero sha1-hash
* Constructs an all-zero sha1-hash
*/
public Sha1Hash() {
this(new sha1_hash());
}

public sha1_hash getSwig() {
public sha1_hash swig() {
return h;
}

Expand All @@ -55,6 +63,10 @@ public boolean isAllZeros() {
return h.is_all_zeros();
}

public byte[] toBytes() {
return Vectors.byte_vector2bytes(h.to_bytes());
}

/**
* Returns the hex representation of this has.
* <p/>
Expand Down Expand Up @@ -86,6 +98,11 @@ public boolean equals(Object obj) {
return h.op_eq(((Sha1Hash) obj).h);
}

@Override
public int hashCode() {
return h.hash_code();
}

/**
* returns an all-F sha1-hash. i.e. the maximum value
* representable by a 160 bit number (20 bytes). This is
Expand Down
Expand Up @@ -605,14 +605,17 @@ public class libtorrent_jni {
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();
public final static native long new_sha1_hash();
public final static native long new_sha1_hash__SWIG_0();
public final static native long sha1_hash_max();
public final static native long sha1_hash_min();
public final static native void sha1_hash_clear(long jarg1, sha1_hash jarg1_);
public final static native boolean sha1_hash_is_all_zeros(long jarg1, sha1_hash jarg1_);
public final static native boolean sha1_hash_op_eq(long jarg1, sha1_hash jarg1_, long jarg2, sha1_hash jarg2_);
public final static native boolean sha1_hash_op_neq(long jarg1, sha1_hash jarg1_, long jarg2, sha1_hash jarg2_);
public final static native boolean sha1_hash_op_lt(long jarg1, sha1_hash jarg1_, long jarg2, sha1_hash jarg2_);
public final static native long new_sha1_hash__SWIG_1(long jarg1, byte_vector jarg1_);
public final static native int sha1_hash_hash_code(long jarg1, sha1_hash jarg1_);
public final static native long sha1_hash_to_bytes(long jarg1, sha1_hash jarg1_);
public final static native String sha1_hash_to_hex(long jarg1, sha1_hash jarg1_);
public final static native boolean sha1_hash_from_hex(String jarg1, long jarg2, sha1_hash jarg2_);
public final static native int sha1_hash_compare(long jarg1, sha1_hash jarg1_, long jarg2, sha1_hash jarg2_);
Expand Down
14 changes: 13 additions & 1 deletion src/main/java/com/frostwire/jlibtorrent/swig/sha1_hash.java
Expand Up @@ -36,7 +36,7 @@ public synchronized void delete() {
}

public sha1_hash() {
this(libtorrent_jni.new_sha1_hash(), true);
this(libtorrent_jni.new_sha1_hash__SWIG_0(), true);
}

public static sha1_hash max() {
Expand Down Expand Up @@ -67,6 +67,18 @@ public boolean op_lt(sha1_hash n) {
return libtorrent_jni.sha1_hash_op_lt(swigCPtr, this, sha1_hash.getCPtr(n), n);
}

public sha1_hash(byte_vector s) {
this(libtorrent_jni.new_sha1_hash__SWIG_1(byte_vector.getCPtr(s), s), true);
}

public int hash_code() {
return libtorrent_jni.sha1_hash_hash_code(swigCPtr, this);
}

public byte_vector to_bytes() {
return new byte_vector(libtorrent_jni.sha1_hash_to_bytes(swigCPtr, this), true);
}

public String to_hex() {
return libtorrent_jni.sha1_hash_to_hex(swigCPtr, this);
}
Expand Down
18 changes: 18 additions & 0 deletions swig/libtorrent.i
Expand Up @@ -1013,6 +1013,24 @@ namespace libtorrent {
};

%extend sha1_hash {
sha1_hash(std::vector<int8_t> const& s) {
return new sha1_hash(std::string(s.begin(), s.end()));
}

int hash_code() {
char const* data = $self->data();
int result = 1;
for (int i = 0; i < 20; i++) {
result = 31 * result + data[i];
}
return result;
}

std::vector<int8_t> to_bytes() {
std::string s = $self->to_string();
return std::vector<int8_t>(s.begin(), s.end());
}

std::string to_hex() {
return libtorrent::to_hex($self->to_string());
}
Expand Down
94 changes: 91 additions & 3 deletions swig/libtorrent_jni.cpp
Expand Up @@ -1253,6 +1253,21 @@ SWIGINTERN std::vector< int8_t > libtorrent_entry_bencode(libtorrent::entry *sel
SWIGINTERN libtorrent::entry libtorrent_entry_bdecode(std::vector< int8_t > &buffer){
return libtorrent::bdecode(buffer.begin(), buffer.end());
}
SWIGINTERN libtorrent::sha1_hash *new_libtorrent_sha1_hash__SWIG_1(std::vector< int8_t > const &s){
return new sha1_hash(std::string(s.begin(), s.end()));
}
SWIGINTERN int libtorrent_sha1_hash_hash_code(libtorrent::sha1_hash *self){
char const* data = self->data();
int result = 1;
for (int i = 0; i < 20; i++) {
result = 31 * result + data[i];
}
return result;
}
SWIGINTERN std::vector< int8_t > libtorrent_sha1_hash_to_bytes(libtorrent::sha1_hash *self){
std::string s = self->to_string();
return std::vector<int8_t>(s.begin(), s.end());
}
SWIGINTERN std::string libtorrent_sha1_hash_to_hex(libtorrent::sha1_hash *self){
return libtorrent::to_hex(self->to_string());
}
Expand Down Expand Up @@ -17630,7 +17645,7 @@ SWIGEXPORT jint JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_sha1
}


SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_new_1sha1_1hash(JNIEnv *jenv, jclass jcls) {
SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_new_1sha1_1hash_1_1SWIG_10(JNIEnv *jenv, jclass jcls) {
jlong jresult = 0 ;
libtorrent::sha1_hash *result = 0 ;

Expand Down Expand Up @@ -17822,6 +17837,79 @@ SWIGEXPORT jboolean JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_
}


SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_new_1sha1_1hash_1_1SWIG_11(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jlong jresult = 0 ;
std::vector< int8_t > *arg1 = 0 ;
libtorrent::sha1_hash *result = 0 ;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(std::vector< int8_t > **)&jarg1;
if (!arg1) {
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "std::vector< int8_t > const & reference is null");
return 0;
}
{
try {
result = (libtorrent::sha1_hash *)new_libtorrent_sha1_hash__SWIG_1((std::vector< signed char > const &)*arg1);
} catch (std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
} catch (...) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unknown exception type");
}
}
*(libtorrent::sha1_hash **)&jresult = result;
return jresult;
}


SWIGEXPORT jint JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_sha1_1hash_1hash_1code(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jint jresult = 0 ;
libtorrent::sha1_hash *arg1 = (libtorrent::sha1_hash *) 0 ;
int result;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(libtorrent::sha1_hash **)&jarg1;
{
try {
result = (int)libtorrent_sha1_hash_hash_code(arg1);
} catch (std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
} catch (...) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unknown exception type");
}
}
jresult = (jint)result;
return jresult;
}


SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_sha1_1hash_1to_1bytes(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jlong jresult = 0 ;
libtorrent::sha1_hash *arg1 = (libtorrent::sha1_hash *) 0 ;
std::vector< int8_t > result;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(libtorrent::sha1_hash **)&jarg1;
{
try {
result = libtorrent_sha1_hash_to_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 jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_sha1_1hash_1to_1hex(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jstring jresult = 0 ;
libtorrent::sha1_hash *arg1 = (libtorrent::sha1_hash *) 0 ;
Expand Down Expand Up @@ -61658,7 +61746,7 @@ SWIGEXPORT jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_L

(void)jenv;
(void)jcls;
result = (char *)("d6d0480313985ab311cf7db2a0a9adc1d3f7314c");
result = (char *)("a675a749e00ef9636d73f334a96e0b3ec34c8fd6");
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}
Expand All @@ -61670,7 +61758,7 @@ SWIGEXPORT jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_J

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

0 comments on commit 6c91f25

Please sign in to comment.