Skip to content

Commit

Permalink
Added swig hasher type
Browse files Browse the repository at this point in the history
More swig cleanup and refactor
Added native address comparator and hasher for better performance
  • Loading branch information
aldenml committed Feb 12, 2016
1 parent ab024a9 commit 8bc38bf
Show file tree
Hide file tree
Showing 10 changed files with 1,196 additions and 504 deletions.
26 changes: 5 additions & 21 deletions src/main/java/com/frostwire/jlibtorrent/Address.java
Expand Up @@ -2,14 +2,14 @@

import com.frostwire.jlibtorrent.swig.address;

import java.util.Comparator;

/**
* @author gubatron
* @author aldenml
*/
public final class Address {

public static final Comparator COMPARATOR = new Comparator();

private final address addr;

public Address(address addr) {
Expand All @@ -29,26 +29,10 @@ public static int compare(Address a1, Address a2) {
return COMPARATOR.compare(a1, a2);
}

/**
*
*/
public static final class Comparator implements java.util.Comparator<Address> {

private Comparator() {
}

public static final Comparator<Address> COMPARATOR = new Comparator<Address>() {
@Override
public int compare(Address o1, Address o2) {
address a1 = o1.addr;
address a2 = o2.addr;
if (a1.op_lt(a2)) {
return -1;
}
if (a2.op_lt(a1)) {
return 1;
}

return 0;
return address.compare(o1.addr, o2.addr);
}
}
};
}
4 changes: 2 additions & 2 deletions src/main/java/com/frostwire/jlibtorrent/DHTSettings.java
Expand Up @@ -30,7 +30,7 @@ public dht_settings getSwig() {
*
* @return
*/
public int getMaxPeersReply() {
public int maxPeersReply() {
return s.getMax_peers_reply();
}

Expand All @@ -39,7 +39,7 @@ public int getMaxPeersReply() {
*
* @param value
*/
public void setMaxPeersReply(int value) {
public void maxPeersReply(int value) {
s.setMax_peers_reply(value);
}

Expand Down
Expand Up @@ -9,6 +9,7 @@
import com.frostwire.jlibtorrent.swig.entry;
import com.frostwire.jlibtorrent.swig.sha1_hash;

import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;

Expand Down Expand Up @@ -125,8 +126,7 @@ public Counters counters() {
return counters;
}

private static void hash_address(address ip, sha1_hash h)
{/*
private static void hash_address(address ip, sha1_hash h) {/*
if (ip.is_v6())
{
address_v6::bytes_type b = ip.to_v6().to_bytes();
Expand All @@ -141,17 +141,11 @@ private static void hash_address(address ip, sha1_hash h)

static final class PeerEntry {

public static final Comparator COMPARATOR = new Comparator();

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

public static final class Comparator implements java.util.Comparator<PeerEntry> {

private Comparator() {
}

public static final Comparator<PeerEntry> COMPARATOR = new Comparator<PeerEntry>() {
@Override
public int compare(PeerEntry o1, PeerEntry o2) {
TcpEndpoint a1 = o1.addr;
Expand All @@ -161,7 +155,7 @@ public int compare(PeerEntry o1, PeerEntry o2) {

return r == 0 ? Integer.compare(a1.port(), a2.port()) : r;
}
}
};
}

static final class TorrentEntry {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/frostwire/jlibtorrent/swig/address.java
Expand Up @@ -99,4 +99,12 @@ public boolean op_lt(address a2) {
return libtorrent_jni.address_op_lt(swigCPtr, this, address.getCPtr(a2), a2);
}

public void hash(sha1_hash h) {
libtorrent_jni.address_hash(swigCPtr, this, sha1_hash.getCPtr(h), h);
}

public static int compare(address a1, address a2) {
return libtorrent_jni.address_compare(address.getCPtr(a1), a1, address.getCPtr(a2), a2);
}

}
58 changes: 58 additions & 0 deletions src/main/java/com/frostwire/jlibtorrent/swig/hasher.java
@@ -0,0 +1,58 @@
/* ----------------------------------------------------------------------------
* This file was automatically generated by SWIG (http://www.swig.org).
* Version 3.0.8
*
* Do not make changes to this file unless you know what you are doing--modify
* the SWIG interface file instead.
* ----------------------------------------------------------------------------- */

package com.frostwire.jlibtorrent.swig;

public class hasher {
private transient long swigCPtr;
protected transient boolean swigCMemOwn;

protected hasher(long cPtr, boolean cMemoryOwn) {
swigCMemOwn = cMemoryOwn;
swigCPtr = cPtr;
}

protected static long getCPtr(hasher obj) {
return (obj == null) ? 0 : obj.swigCPtr;
}

protected void finalize() {
delete();
}

public synchronized void delete() {
if (swigCPtr != 0) {
if (swigCMemOwn) {
swigCMemOwn = false;
libtorrent_jni.delete_hasher(swigCPtr);
}
swigCPtr = 0;
}
}

public hasher() {
this(libtorrent_jni.new_hasher__SWIG_0(), true);
}

public sha1_hash final_hash() {
return new sha1_hash(libtorrent_jni.hasher_final_hash(swigCPtr, this), true);
}

public void reset() {
libtorrent_jni.hasher_reset(swigCPtr, this);
}

public hasher(byte_vector data) {
this(libtorrent_jni.new_hasher__SWIG_1(byte_vector.getCPtr(data), data), true);
}

public hasher update(byte_vector data) {
return new hasher(libtorrent_jni.hasher_update(swigCPtr, this, byte_vector.getCPtr(data), data), false);
}

}
25 changes: 17 additions & 8 deletions src/main/java/com/frostwire/jlibtorrent/swig/libtorrent_jni.java
Expand Up @@ -564,14 +564,6 @@ public class libtorrent_jni {
public final static native void bitfield_set_all(long jarg1, bitfield jarg1_);
public final static native void bitfield_clear_all(long jarg1, bitfield jarg1_);
public final static native void bitfield_clear(long jarg1, bitfield jarg1_);
public final static native int stat_upload_payload_get();
public final static native int stat_upload_protocol_get();
public final static native int stat_download_payload_get();
public final static native int stat_download_protocol_get();
public final static native int stat_upload_ip_protocol_get();
public final static native int stat_download_ip_protocol_get();
public final static native int stat_num_channels_get();
public final static native void delete_stat(long jarg1);
public final static native void peer_request_piece_set(long jarg1, peer_request jarg1_, int jarg2);
public final static native int peer_request_piece_get(long jarg1, peer_request jarg1_);
public final static native void peer_request_start_set(long jarg1, peer_request jarg1_, int jarg2);
Expand Down Expand Up @@ -2856,6 +2848,12 @@ public class libtorrent_jni {
public final static native boolean torrent_status_stop_when_ready_get(long jarg1, torrent_status jarg1_);
public final static native void torrent_status_info_hash_set(long jarg1, torrent_status jarg1_, long jarg2, sha1_hash jarg2_);
public final static native long torrent_status_info_hash_get(long jarg1, torrent_status jarg1_);
public final static native long new_hasher__SWIG_0();
public final static native long hasher_final_hash(long jarg1, hasher jarg1_);
public final static native void hasher_reset(long jarg1, hasher jarg1_);
public final static native void delete_hasher(long jarg1);
public final static native long new_hasher__SWIG_1(long jarg1, byte_vector jarg1_);
public final static native long hasher_update(long jarg1, hasher jarg1_, long jarg2, byte_vector jarg2_);
public final static native int ed25519_seed_size_get();
public final static native int ed25519_private_key_size_get();
public final static native int ed25519_public_key_size_get();
Expand All @@ -2878,6 +2876,8 @@ public class libtorrent_jni {
public final static native boolean address_is_unspecified(long jarg1, address jarg1_);
public final static native boolean address_is_multicast(long jarg1, address jarg1_);
public final static native boolean address_op_lt(long jarg1, address jarg1_, long jarg2, address jarg2_);
public final static native void address_hash(long jarg1, address jarg1_, long jarg2, sha1_hash jarg2_);
public final static native int address_compare(long jarg1, address jarg1_, long jarg2, address jarg2_);
public final static native void delete_address(long jarg1);
public final static native long new_address_v4__SWIG_0();
public final static native long new_address_v4__SWIG_1(long jarg1);
Expand Down Expand Up @@ -2939,6 +2939,15 @@ public class libtorrent_jni {
public final static native long udp_endpoint_address(long jarg1, udp_endpoint jarg1_);
public final static native long new_udp_endpoint();
public final static native void delete_udp_endpoint(long jarg1);
public final static native int stat_upload_payload_get();
public final static native int stat_upload_protocol_get();
public final static native int stat_download_payload_get();
public final static native int stat_download_protocol_get();
public final static native int stat_upload_ip_protocol_get();
public final static native int stat_download_ip_protocol_get();
public final static native int stat_num_channels_get();
public final static native long new_stat();
public final static native void delete_stat(long jarg1);
public final static native boolean bloom_filter_256_find(long jarg1, bloom_filter_256 jarg1_, long jarg2, sha1_hash jarg2_);
public final static native void bloom_filter_256_set(long jarg1, bloom_filter_256 jarg1_, long jarg2, sha1_hash jarg2_);
public final static native void bloom_filter_256_clear(long jarg1, bloom_filter_256 jarg1_);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/frostwire/jlibtorrent/swig/stat.java
Expand Up @@ -35,6 +35,10 @@ public synchronized void delete() {
}
}

public stat() {
this(libtorrent_jni.new_stat(), true);
}

public final static int upload_payload = libtorrent_jni.stat_upload_payload_get();
public final static int upload_protocol = libtorrent_jni.stat_upload_protocol_get();
public final static int download_payload = libtorrent_jni.stat_download_payload_get();
Expand Down
52 changes: 46 additions & 6 deletions swig/libtorrent.i
Expand Up @@ -33,7 +33,6 @@
#include "libtorrent/time.hpp"
#include "libtorrent/fingerprint.hpp"
#include "libtorrent/bitfield.hpp"
#include "libtorrent/stat.hpp"
#include "libtorrent/peer_request.hpp"
#include "libtorrent/address.hpp"
#include "libtorrent/entry.hpp"
Expand Down Expand Up @@ -73,6 +72,7 @@
#include "libtorrent/create_torrent.hpp"
#include "libtorrent/announce_entry.hpp"
#include "libtorrent/torrent_status.hpp"
#include "libtorrent/hasher.hpp"
#include "libtorrent/ed25519.hpp"

using namespace boost;
Expand Down Expand Up @@ -484,8 +484,9 @@ namespace std {
%ignore libtorrent::bitfield::const_iterator;
%ignore libtorrent::bitfield::begin;
%ignore libtorrent::bitfield::end;
%ignore libtorrent::stat::stat;
%ignore libtorrent::stat_channel;
%ignore libtorrent::hasher::hasher(const char*, int);
%ignore libtorrent::hasher::update(std::string const&);
%ignore libtorrent::hasher::update(const char*, int);

%ignore boost::throws;
%ignore boost::detail::throws;
Expand Down Expand Up @@ -541,9 +542,9 @@ namespace std {
%rename(libtorrent_errors) libtorrent::errors::error_code_enum;
%rename(bdecode_no_error) libtorrent::bdecode_errors::no_error;
%rename(bdecode_errors) libtorrent::bdecode_errors::error_code_enum;
%rename(final_hash) libtorrent::hasher::final;

%rename("$ignore", regextarget=1, %$isconstructor) ".*_alert$";
%rename("$ignore", regextarget=1, fullname=1, %$isfunction) "libtorrent::stat::.*";

%include <boost/system/error_code.hpp>

Expand All @@ -552,7 +553,6 @@ namespace std {
%include "libtorrent/time.hpp"
%include "libtorrent/fingerprint.hpp"
%include "libtorrent/bitfield.hpp"
%include "libtorrent/stat.hpp"
%include "libtorrent/peer_request.hpp"
%include "libtorrent/address.hpp"
%include "libtorrent/entry.hpp"
Expand Down Expand Up @@ -592,6 +592,7 @@ namespace std {
%include "libtorrent/create_torrent.hpp"
%include "libtorrent/announce_entry.hpp"
%include "libtorrent/torrent_status.hpp"
%include "libtorrent/hasher.hpp"
%include "libtorrent/ed25519.hpp"

namespace boost {
Expand Down Expand Up @@ -631,6 +632,20 @@ namespace boost {
bool op_lt(const address& a2) {
return *$self < a2;
}

void hash(libtorrent::sha1_hash& h) {
if ($self->is_v6()) {
address_v6::bytes_type b = $self->to_v6().to_bytes();
h = hasher(reinterpret_cast<char*>(&b[0]), b.size()).final();
} else {
address_v4::bytes_type b = $self->to_v4().to_bytes();
h = hasher(reinterpret_cast<char*>(&b[0]), b.size()).final();
}
}

static int compare(const address& a1, const address& a2) {
return a1 == a2 ? 0 : (a1 < a2 ? -1 : 1);
}
}
};

Expand Down Expand Up @@ -1046,6 +1061,31 @@ namespace libtorrent {
}
};

%extend hasher {
hasher(std::vector<int8_t> const& data) {
return new hasher(reinterpret_cast<char const*>(&data[0]), data.size());
}

hasher& update(std::vector<int8_t> const& data) {
$self->update(reinterpret_cast<char const*>(&data[0]), data.size());
return *$self;
}
};

class stat {
public:
enum
{
upload_payload,
upload_protocol,
download_payload,
download_protocol,
upload_ip_protocol,
download_ip_protocol,
num_channels
};
};

template <int N>
struct bloom_filter {

Expand All @@ -1064,7 +1104,7 @@ struct bloom_filter {
return std::vector<int8_t>(s.begin(), s.end());
}

void from_bytes(std::vector<int8_t> v) {
void from_bytes(std::vector<int8_t> const& v) {
$self->from_string(reinterpret_cast<char const*>(&v[0]));
}
}
Expand Down

0 comments on commit 8bc38bf

Please sign in to comment.