From c4b4aaea595f921445e6a4c89228ccae7eb9efec Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Fri, 19 Feb 2016 17:15:49 -0500 Subject: [PATCH] Added print to DhtStorageBase Demo not working, seems to be some problem in the native side --- .../jlibtorrent/plugins/DhtStorageBase.java | 27 ++++++++++++++++++- .../jlibtorrent/demo/DhtStorageTest.java | 10 ++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/frostwire/jlibtorrent/plugins/DhtStorageBase.java b/src/main/java/com/frostwire/jlibtorrent/plugins/DhtStorageBase.java index 85e653692..0a6118e9b 100644 --- a/src/main/java/com/frostwire/jlibtorrent/plugins/DhtStorageBase.java +++ b/src/main/java/com/frostwire/jlibtorrent/plugins/DhtStorageBase.java @@ -14,21 +14,27 @@ public class DhtStorageBase implements DhtStorage { private final Sha1Hash id; private final DhtSettings settings; private final Counters counters; + private final boolean print; private final HashMap torrents; private final HashMap immutables; private final HashMap mutables; - public DhtStorageBase(Sha1Hash id, DhtSettings settings) { + public DhtStorageBase(Sha1Hash id, DhtSettings settings, boolean print) { this.id = id; this.settings = settings; this.counters = new Counters(); + this.print = print; this.torrents = new HashMap(); this.immutables = new HashMap(); this.mutables = new HashMap(); } + public DhtStorageBase(Sha1Hash id, DhtSettings settings) { + this(id, settings, false); + } + @Override public boolean getPeers(Sha1Hash infoHash, boolean noseed, boolean scrape, entry peers) { TorrentEntry v = torrents.get(infoHash); @@ -76,6 +82,10 @@ public boolean getPeers(Sha1Hash infoHash, boolean noseed, boolean scrape, entry } } + if (print) { + print("get_peers", peers); + } + return true; } @@ -153,6 +163,11 @@ public boolean getImmutableItem(Sha1Hash target, entry item) { } item.set("v", entry.bdecode(Vectors.bytes2byte_vector(i.value))); + + if (print) { + print("get_immutable_item", item); + } + return true; } @@ -198,6 +213,11 @@ public boolean getMutableItem(Sha1Hash target, long seq, boolean forceFill, entr item.set("sig", Vectors.bytes2byte_vector(f.sig)); item.set("k", Vectors.bytes2byte_vector(f.key)); } + + if (print) { + print("get_mutable_item", item); + } + return true; } @@ -308,6 +328,11 @@ private void purgePeers(Set peers) { } } + private static void print(String operation, entry entry) { + System.out.println("DHT OP: " + operation); + System.out.println(entry.to_string()); + } + private static void touchItem(DhtImmutableItem f, address address) { f.last_seen = System.currentTimeMillis(); diff --git a/src/test/java/com/frostwire/jlibtorrent/demo/DhtStorageTest.java b/src/test/java/com/frostwire/jlibtorrent/demo/DhtStorageTest.java index 934c6dd91..fc8cb3c70 100644 --- a/src/test/java/com/frostwire/jlibtorrent/demo/DhtStorageTest.java +++ b/src/test/java/com/frostwire/jlibtorrent/demo/DhtStorageTest.java @@ -3,8 +3,9 @@ import com.frostwire.jlibtorrent.*; import com.frostwire.jlibtorrent.alerts.Alert; import com.frostwire.jlibtorrent.plugins.DhtStorage; -import com.frostwire.jlibtorrent.plugins.DhtStorageConstructor; import com.frostwire.jlibtorrent.plugins.DhtStorageBase; +import com.frostwire.jlibtorrent.plugins.DhtStorageConstructor; +import com.frostwire.jlibtorrent.swig.settings_pack; /** * @author gubatron @@ -27,17 +28,20 @@ public void alert(Alert alert) { }; SettingsPack sp = new SettingsPack(); + sp.setString(settings_pack.string_types.listen_interfaces.swigValue(), "0.0.0.0:0"); sp.enableDht(false); - Session s = new Session(sp, false, l); + //Session s = new Session(sp, false, l); + Session s = new Session("0.0.0.0", 0, 10, false, l); s.setDhtStorage(new DhtStorageConstructor() { @Override public DhtStorage create(Sha1Hash id, DhtSettings settings) { - return new DhtStorageBase(id, settings); + return new DhtStorageBase(id, settings, true); } }); + sp = new SettingsPack(); sp.enableDht(true); s.applySettings(sp);