Skip to content

Commit

Permalink
Added DhtStatsAlert.
Browse files Browse the repository at this point in the history
Improved SessionStats.
Working DhtRouter test.
  • Loading branch information
aldenml committed Feb 26, 2015
1 parent bcfb0b7 commit c8d3e7e
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 13 deletions.
Binary file modified libjlibtorrent.dylib
Binary file not shown.
35 changes: 28 additions & 7 deletions src/com/frostwire/jlibtorrent/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public final class Session {

private long lastStatsRequestTime;
private long[] lastStatsValues;
private long lastDHTNodes;

private final SparseArray<ArrayList<AlertListener>> listeners;
private final SparseArray<AlertListener[]> listenerSnapshots;
Expand Down Expand Up @@ -749,6 +750,10 @@ public void deletePortMapping(int handle) {
s.delete_port_mapping(handle);
}

public SessionStats getStats() {
return new SessionStats(lastStatsValues, lastDHTNodes);
}

@Override
protected void finalize() throws Throwable {
this.running = false;
Expand Down Expand Up @@ -839,19 +844,25 @@ public void run() {

Alert<?> alert = null;

if (type == AlertType.SESSION_STATS_ALERT.getSwig()) {
if (type == AlertType.SESSION_STATS.getSwig()) {
alert = castAlert(swigAlert);
lastStatsValues = ((SessionStatsAlert) alert).getValues();
}

if (type == AlertType.DHT_STATS.getSwig()) {
alert = castAlert(swigAlert);
lastDHTNodes = countDHTNodes((DhtStatsAlert) alert);
}

if (listeners.indexOfKey(type) >= 0) {
if (alert == null) {
alert = castAlert(swigAlert);
}
fireAlert(alert, type);
}

if (type != AlertType.SESSION_STATS_ALERT.getSwig() &&
if (type != AlertType.SESSION_STATS.getSwig() &&
type != AlertType.DHT_STATS.getSwig() &&
listeners.indexOfKey(-1) >= 0) {
if (alert == null) {
alert = castAlert(swigAlert);
Expand All @@ -866,6 +877,7 @@ public void run() {
if ((now - lastStatsRequestTime) >= REQUEST_STATS_RESOLUTION_MILLIS) {
lastStatsRequestTime = now;
postSessionStats();
postDHTStats();
}
}
}
Expand Down Expand Up @@ -919,6 +931,18 @@ private static List<Pair<String, Integer>> defaultRouters() {
return list;
}

private static long countDHTNodes(DhtStatsAlert alert) {
DHTRoutingBucket[] buckets = alert.getRoutingTable();

long n = 0;

for (DHTRoutingBucket b : buckets) {
n += b.getNumNodes();
}

return n;
}

private static Map<Integer, CastAlertFunction> buildCastAlertTable() {
Map<Integer, CastAlertFunction> map = new HashMap<Integer, CastAlertFunction>();

Expand Down Expand Up @@ -1000,6 +1024,7 @@ private static Map<Integer, CastAlertFunction> buildCastAlertTable() {
CAST_ALERT_METHOD(dht_mutable_item_alert.class, map);
CAST_ALERT_METHOD(dht_put_alert.class, map);
CAST_ALERT_METHOD(i2p_alert.class, map);
CAST_ALERT_METHOD(dht_stats_alert.class, map);

CAST_ALERT_METHOD(dht_get_peers_reply_alert.class, map);

Expand All @@ -1018,7 +1043,7 @@ private static void CAST_ALERT_METHOD(Class<? extends alert> clazz, Map<Integer,
}
}

private Alert<?> castAlert(alert a) {
private static Alert<?> castAlert(alert a) {
CastAlertFunction function = CAST_TABLE.get(a.type());

Alert<?> r;
Expand All @@ -1032,10 +1057,6 @@ private Alert<?> castAlert(alert a) {
return r;
}

public SessionStats getStats() {
return new SessionStats(lastStatsValues);
}

/**
* Flags to be passed in to remove_torrent().
*/
Expand Down
29 changes: 26 additions & 3 deletions src/com/frostwire/jlibtorrent/SessionStats.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,43 @@
package com.frostwire.jlibtorrent;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

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

private static final int dhtNodesIdx = LibTorrent.findMetricIdx("dht.dht_nodes");
private static final StatsMetric[] statsMetrics = LibTorrent.sessionStatsMetrics();

private final long[] statsValues;
private final long dhtNodes;

SessionStats(long[] statsValues) {
SessionStats(long[] statsValues, long dhtNodes) {
this.statsValues = statsValues;
this.dhtNodes = dhtNodes;
}

private Map<String, Long> getNonZeroValues() {
if (statsValues == null) {
return Collections.emptyMap();
}

HashMap<String, Long> m = new HashMap<String, Long>();

for (StatsMetric sm : statsMetrics) {
long value = statsValues[sm.getValueIndex()];
if (value != 0) {
m.put(sm.getName(), value);
}
}

return Collections.unmodifiableMap(m);
}

public long getDHTNodes() {
return statsValues != null ? statsValues[dhtNodesIdx] : 0;
return dhtNodes;
}
}
3 changes: 3 additions & 0 deletions src/com/frostwire/jlibtorrent/TorrentAlertAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,9 @@ public void trackerid(TrackeridAlert alert) {
public void unwantedBlock(UnwantedBlockAlert alert) {
}

public void dhtStats(DhtStatsAlert alert) {
}

public void torrentPrioritize(TorrentPrioritizeAlert alert) {
}

Expand Down
3 changes: 2 additions & 1 deletion src/com/frostwire/jlibtorrent/alerts/AlertType.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public enum AlertType {
EXTERNAL_IP(external_ip_alert.alert_type),
LISTEN_SUCCEEDED(listen_succeeded_alert.alert_type),
STATE_UPDATE(state_update_alert.alert_type),
SESSION_STATS_ALERT(session_stats_alert.alert_type),
SESSION_STATS(session_stats_alert.alert_type),
SCRAPE_REPLY(scrape_reply_alert.alert_type),
SCRAPE_FAILED(scrape_failed_alert.alert_type),
LSD_PEER(lsd_peer_alert.alert_type),
Expand Down Expand Up @@ -81,6 +81,7 @@ public enum AlertType {
DHT_PUT(dht_put_alert.alert_type),
DHT_MUTABLE_ITEM(dht_mutable_item_alert.alert_type),
DHT_IMMUTABLE_ITEM(dht_immutable_item_alert.alert_type),
DHT_STATS(dht_stats_alert.alert_type),
DHT_GET_PEERS_REPLY_ALERT(dht_get_peers_reply_alert.alert_type),
UNKNOWN(-1),
TORRENT_PRIORITIZE(-2);
Expand Down
46 changes: 46 additions & 0 deletions src/com/frostwire/jlibtorrent/alerts/DhtStatsAlert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.frostwire.jlibtorrent.alerts;

import com.frostwire.jlibtorrent.DHTLookup;
import com.frostwire.jlibtorrent.DHTRoutingBucket;
import com.frostwire.jlibtorrent.swig.dht_lookup_vector;
import com.frostwire.jlibtorrent.swig.dht_routing_bucket_vector;
import com.frostwire.jlibtorrent.swig.dht_stats_alert;

/**
* Contains current DHT state. Posted in response to {@link com.frostwire.jlibtorrent.Session#postDHTStats()}.
*
* @author gubatron
* @author aldenml
*/
public final class DhtStatsAlert extends AbstractAlert<dht_stats_alert> {

public DhtStatsAlert(dht_stats_alert alert) {
super(alert);
}

public DHTLookup[] getActiveRequests() {
dht_lookup_vector v = alert.getActive_requests();

int size = (int) v.size();
DHTLookup[] arr = new DHTLookup[size];

for (int i = 0; i < size; i++) {
arr[i] = new DHTLookup(v.get(i));
}

return arr;
}

public DHTRoutingBucket[] getRoutingTable() {
dht_routing_bucket_vector v = alert.getRouting_table();

int size = (int) v.size();
DHTRoutingBucket[] arr = new DHTRoutingBucket[size];

for (int i = 0; i < size; i++) {
arr[i] = new DHTRoutingBucket(v.get(i));
}

return arr;
}
}
9 changes: 7 additions & 2 deletions src/com/frostwire/jlibtorrent/demo/DhtRouter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@ public int[] types() {

@Override
public void alert(Alert<?> alert) {
//System.out.println(alert.getType() + " - " + alert.getSwig().what() + " - " + alert.getSwig().message());
System.out.println(alert.getType() + " - " + alert.getSwig().what() + " - " + alert.getSwig().message());
}
});

Thread.sleep(5000);
s.dhtGetPeers(new Sha1Hash("86d0502ead28e495c9e67665340f72aa72fe304e"));

DHT dht = new DHT(s);

System.out.println("Waiting for nodes in DHT");
Expand All @@ -36,7 +40,8 @@ public void alert(Alert<?> alert) {
private static List<Pair<String, Integer>> defaultRouters() {
List<Pair<String, Integer>> list = new LinkedList<Pair<String, Integer>>();

list.add(new Pair<String, Integer>("54.205.168.85", 6881));
list.add(new Pair<String, Integer>("router.bittorrent.com", 6881));
list.add(new Pair<String, Integer>("dht.transmissionbt.com", 6881));

return list;
}
Expand Down
5 changes: 5 additions & 0 deletions src/com/frostwire/jlibtorrent/swig/alert.java
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ public static i2p_alert cast_to_i2p_alert(alert alert) {
return (cPtr == 0) ? null : new i2p_alert(cPtr, false);
}

public static dht_stats_alert cast_to_dht_stats_alert(alert alert) {
long cPtr = libtorrent_jni.alert_cast_to_dht_stats_alert(alert.getCPtr(alert), alert);
return (cPtr == 0) ? null : new dht_stats_alert(cPtr, false);
}

public static dht_get_peers_reply_alert cast_to_dht_get_peers_reply_alert(alert alert) {
long cPtr = libtorrent_jni.alert_cast_to_dht_get_peers_reply_alert(alert.getCPtr(alert), alert);
return (cPtr == 0) ? null : new dht_get_peers_reply_alert(cPtr, false);
Expand Down
1 change: 1 addition & 0 deletions src/com/frostwire/jlibtorrent/swig/libtorrent_jni.java
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,7 @@ public class libtorrent_jni {
public final static native long alert_cast_to_dht_mutable_item_alert(long jarg1, alert jarg1_);
public final static native long alert_cast_to_dht_put_alert(long jarg1, alert jarg1_);
public final static native long alert_cast_to_i2p_alert(long jarg1, alert jarg1_);
public final static native long alert_cast_to_dht_stats_alert(long jarg1, alert jarg1_);
public final static native long alert_cast_to_dht_get_peers_reply_alert(long jarg1, alert jarg1_);
public final static native String operation_name(int jarg1);
public final static native int user_alert_id_get();
Expand Down
1 change: 1 addition & 0 deletions swig/libtorrent.i
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@ namespace libtorrent {
CAST_ALERT_METHOD(dht_mutable_item_alert)
CAST_ALERT_METHOD(dht_put_alert)
CAST_ALERT_METHOD(i2p_alert)
CAST_ALERT_METHOD(dht_stats_alert)

CAST_ALERT_METHOD(dht_get_peers_reply_alert)
};
Expand Down
23 changes: 23 additions & 0 deletions swig/libtorrent_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1258,6 +1258,7 @@ SWIGINTERN libtorrent::dht_immutable_item_alert *libtorrent_alert_cast_to_dht_im
SWIGINTERN libtorrent::dht_mutable_item_alert *libtorrent_alert_cast_to_dht_mutable_item_alert(libtorrent::alert *alert){ return dynamic_cast<libtorrent::dht_mutable_item_alert *>(alert); }
SWIGINTERN libtorrent::dht_put_alert *libtorrent_alert_cast_to_dht_put_alert(libtorrent::alert *alert){ return dynamic_cast<libtorrent::dht_put_alert *>(alert); }
SWIGINTERN libtorrent::i2p_alert *libtorrent_alert_cast_to_i2p_alert(libtorrent::alert *alert){ return dynamic_cast<libtorrent::i2p_alert *>(alert); }
SWIGINTERN libtorrent::dht_stats_alert *libtorrent_alert_cast_to_dht_stats_alert(libtorrent::alert *alert){ return dynamic_cast<libtorrent::dht_stats_alert *>(alert); }
SWIGINTERN libtorrent::dht_get_peers_reply_alert *libtorrent_alert_cast_to_dht_get_peers_reply_alert(libtorrent::alert *alert){ return dynamic_cast<libtorrent::dht_get_peers_reply_alert *>(alert); }
SWIGINTERN std::vector< int > libtorrent_stats_alert_transferred_v(libtorrent::stats_alert *self){
return std::vector<int>(self->transferred, self->transferred + stats_alert::stats_channel::num_channels);
Expand Down Expand Up @@ -33433,6 +33434,28 @@ SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_ale
}


SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_alert_1cast_1to_1dht_1stats_1alert(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jlong jresult = 0 ;
libtorrent::alert *arg1 = (libtorrent::alert *) 0 ;
libtorrent::dht_stats_alert *result = 0 ;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(libtorrent::alert **)&jarg1;
{
try {
result = (libtorrent::dht_stats_alert *)libtorrent_alert_cast_to_dht_stats_alert(arg1);
} catch (...) {
translate_cpp_exception(jenv);
return 0;
}
}
*(libtorrent::dht_stats_alert **)&jresult = result;
return jresult;
}


SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_alert_1cast_1to_1dht_1get_1peers_1reply_1alert(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jlong jresult = 0 ;
libtorrent::alert *arg1 = (libtorrent::alert *) 0 ;
Expand Down

0 comments on commit c8d3e7e

Please sign in to comment.