Skip to content

Commit

Permalink
Added print to DhtStorageBase
Browse files Browse the repository at this point in the history
Demo not working, seems to be some problem in the native side
  • Loading branch information
aldenml committed Feb 19, 2016
1 parent 0c4cbb9 commit c4b4aae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
Expand Up @@ -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<Sha1Hash, TorrentEntry> torrents;
private final HashMap<Sha1Hash, DhtImmutableItem> immutables;
private final HashMap<Sha1Hash, DhtMutableItem> 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<Sha1Hash, TorrentEntry>();
this.immutables = new HashMap<Sha1Hash, DhtImmutableItem>();
this.mutables = new HashMap<Sha1Hash, DhtMutableItem>();
}

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);
Expand Down Expand Up @@ -76,6 +82,10 @@ public boolean getPeers(Sha1Hash infoHash, boolean noseed, boolean scrape, entry
}
}

if (print) {
print("get_peers", peers);
}

return true;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -308,6 +328,11 @@ private void purgePeers(Set<PeerEntry> 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();

Expand Down
10 changes: 7 additions & 3 deletions src/test/java/com/frostwire/jlibtorrent/demo/DhtStorageTest.java
Expand Up @@ -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
Expand All @@ -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);

Expand Down

0 comments on commit c4b4aae

Please sign in to comment.