Skip to content

Commit

Permalink
Refactor for better swig interface of stats_metric
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Apr 26, 2016
1 parent f573812 commit c7c178a
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 123 deletions.
11 changes: 6 additions & 5 deletions src/main/java/com/frostwire/jlibtorrent/LibTorrent.java
Expand Up @@ -4,6 +4,7 @@
import com.frostwire.jlibtorrent.swig.posix_wrapper;
import com.frostwire.jlibtorrent.swig.stats_metric_vector;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -78,17 +79,17 @@ public static Map<String, String> componentVersions() {
*
* @return
*/
public static StatsMetric[] sessionStatsMetrics() {
public static ArrayList<StatsMetric> sessionStatsMetrics() {
stats_metric_vector v = libtorrent.session_stats_metrics();

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

ArrayList<StatsMetric> l = new ArrayList<>(size);

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

return arr;
return l;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/frostwire/jlibtorrent/StatsMetric.java
Expand Up @@ -13,8 +13,8 @@ public final class StatsMetric {
public static final int TYPE_COUNTER = stats_metric.type_counter;
public static final int TYPE_GAUGE = stats_metric.type_gauge;

public StatsMetric(stats_metric sm) {
this.name = sm.getName();
StatsMetric(stats_metric sm) {
this.name = sm.get_name();
this.valueIndex = sm.getValue_index();
this.type = sm.getType();
}
Expand Down
Expand Up @@ -921,14 +921,13 @@ public class libtorrent_jni {
public final static native long add_torrent_params_create_instance_swig_storage(long jarg1, swig_storage_constructor jarg1_);
public final static native void delete_add_torrent_params(long jarg1);
public final static native int op_bittorrent_get();
public final static native void stats_metric_name_set(long jarg1, stats_metric jarg1_, String jarg2);
public final static native String stats_metric_name_get(long jarg1, stats_metric jarg1_);
public final static native void stats_metric_value_index_set(long jarg1, stats_metric jarg1_, int jarg2);
public final static native int stats_metric_value_index_get(long jarg1, stats_metric jarg1_);
public final static native int stats_metric_type_counter_get();
public final static native int stats_metric_type_gauge_get();
public final static native void stats_metric_type_set(long jarg1, stats_metric jarg1_, int jarg2);
public final static native int stats_metric_type_get(long jarg1, stats_metric jarg1_);
public final static native String stats_metric_get_name(long jarg1, stats_metric jarg1_);
public final static native long new_stats_metric();
public final static native void delete_stats_metric(long jarg1);
public final static native long session_stats_metrics();
Expand Down
12 changes: 4 additions & 8 deletions src/main/java/com/frostwire/jlibtorrent/swig/stats_metric.java
Expand Up @@ -35,14 +35,6 @@ public synchronized void delete() {
}
}

public void setName(String value) {
libtorrent_jni.stats_metric_name_set(swigCPtr, this, value);
}

public String getName() {
return libtorrent_jni.stats_metric_name_get(swigCPtr, this);
}

public void setValue_index(int value) {
libtorrent_jni.stats_metric_value_index_set(swigCPtr, this, value);
}
Expand All @@ -59,6 +51,10 @@ public int getType() {
return libtorrent_jni.stats_metric_type_get(swigCPtr, this);
}

public String get_name() {
return libtorrent_jni.stats_metric_get_name(swigCPtr, this);
}

public stats_metric() {
this(libtorrent_jni.new_stats_metric(), true);
}
Expand Down
25 changes: 25 additions & 0 deletions src/test/java/com/frostwire/jlibtorrent/StatsMetricTest.java
@@ -0,0 +1,25 @@
package com.frostwire.jlibtorrent;

import org.junit.Test;

import java.util.ArrayList;

import static org.junit.Assert.assertEquals;

/**
* @author gubatron
* @author aldenml
*/
public class StatsMetricTest {

@Test
public void testListStatsMetric() {
ArrayList<StatsMetric> metrics = LibTorrent.sessionStatsMetrics();

for (StatsMetric m : metrics) {
assertEquals(m.valueIndex, LibTorrent.findMetricIdx(m.name));
}

assertEquals(-1, LibTorrent.findMetricIdx("anything"));
}
}
7 changes: 7 additions & 0 deletions swig/libtorrent.i
Expand Up @@ -538,6 +538,7 @@ namespace std {
%ignore libtorrent::peer_info::last_active;
%ignore libtorrent::peer_info::download_queue_time;
%ignore libtorrent::create_torrent::set_root_cert;
%ignore libtorrent::stats_metric::name;

%ignore boost::throws;
%ignore boost::detail::throws;
Expand Down Expand Up @@ -1141,6 +1142,12 @@ namespace libtorrent {
}
};

%extend stats_metric {
std::string get_name() {
return std::string($self->name);
}
};

%extend dht_pkt_alert {
std::vector<int8_t> get_pkt_buf() {
return std::vector<int8_t>($self->pkt_buf(), $self->pkt_buf() + $self->pkt_size());
Expand Down
68 changes: 27 additions & 41 deletions swig/libtorrent_jni.cpp
Expand Up @@ -1292,6 +1292,9 @@ SWIGINTERN libtorrent::add_torrent_params libtorrent_add_torrent_params_create_i
SWIGINTERN libtorrent::add_torrent_params libtorrent_add_torrent_params_create_instance_swig_storage(swig_storage_constructor *sc){
return add_torrent_params(boost::bind(&swig_storage_constructor_cb, _1, sc));
}
SWIGINTERN std::string libtorrent_stats_metric_get_name(libtorrent::stats_metric *self){
return std::string(self->name);
}
SWIGINTERN libtorrent::torrent_alert const *libtorrent_alert_cast_to_torrent_alert(libtorrent::alert const *a){ return alert_cast<libtorrent::torrent_alert>(a); }
SWIGINTERN libtorrent::peer_alert const *libtorrent_alert_cast_to_peer_alert(libtorrent::alert const *a){ return alert_cast<libtorrent::peer_alert>(a); }
SWIGINTERN libtorrent::tracker_alert const *libtorrent_alert_cast_to_tracker_alert(libtorrent::alert const *a){ return alert_cast<libtorrent::tracker_alert>(a); }
Expand Down Expand Up @@ -24608,46 +24611,6 @@ SWIGEXPORT jint JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_op_1
}


SWIGEXPORT void JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_stats_1metric_1name_1set(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jstring jarg2) {
libtorrent::stats_metric *arg1 = (libtorrent::stats_metric *) 0 ;
char *arg2 = (char *) 0 ;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(libtorrent::stats_metric **)&jarg1;
arg2 = 0;
if (jarg2) {
arg2 = (char *)jenv->GetStringUTFChars(jarg2, 0);
if (!arg2) return ;
}
{
if (arg2) {
arg1->name = (char const *) (new char[strlen((const char *)arg2)+1]);
strcpy((char *)arg1->name, (const char *)arg2);
} else {
arg1->name = 0;
}
}
if (arg2) jenv->ReleaseStringUTFChars(jarg2, (const char *)arg2);
}


SWIGEXPORT jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_stats_1metric_1name_1get(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jstring jresult = 0 ;
libtorrent::stats_metric *arg1 = (libtorrent::stats_metric *) 0 ;
char *result = 0 ;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(libtorrent::stats_metric **)&jarg1;
result = (char *) ((arg1)->name);
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}


SWIGEXPORT void JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_stats_1metric_1value_1index_1set(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_, jint jarg2) {
libtorrent::stats_metric *arg1 = (libtorrent::stats_metric *) 0 ;
int arg2 ;
Expand Down Expand Up @@ -24728,6 +24691,29 @@ SWIGEXPORT jint JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_stat
}


SWIGEXPORT jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_stats_1metric_1get_1name(JNIEnv *jenv, jclass jcls, jlong jarg1, jobject jarg1_) {
jstring jresult = 0 ;
libtorrent::stats_metric *arg1 = (libtorrent::stats_metric *) 0 ;
std::string result;

(void)jenv;
(void)jcls;
(void)jarg1_;
arg1 = *(libtorrent::stats_metric **)&jarg1;
{
try {
result = libtorrent_stats_metric_get_name(arg1);
} catch (std::exception& e) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, e.what());
} catch (...) {
SWIG_JavaThrowException(jenv, SWIG_JavaRuntimeException, "Unknown exception type");
}
}
jresult = jenv->NewStringUTF((&result)->c_str());
return jresult;
}


SWIGEXPORT jlong JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_new_1stats_1metric(JNIEnv *jenv, jclass jcls) {
jlong jresult = 0 ;
libtorrent::stats_metric *result = 0 ;
Expand Down Expand Up @@ -59907,7 +59893,7 @@ SWIGEXPORT jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_J

(void)jenv;
(void)jcls;
result = (char *)("80235855f1085c558cac4da22422d91b1d914482");
result = (char *)("f5738121a569b852c76aab6ca5aa662956323099");
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}
Expand Down
98 changes: 33 additions & 65 deletions swig/libtorrent_node.cpp
Expand Up @@ -2658,6 +2658,9 @@ SWIGINTERN libtorrent::add_torrent_params libtorrent_add_torrent_params_create_i
SWIGINTERN libtorrent::add_torrent_params libtorrent_add_torrent_params_create_instance_swig_storage(swig_storage_constructor *sc){
return add_torrent_params(boost::bind(&swig_storage_constructor_cb, _1, sc));
}
SWIGINTERN std::string libtorrent_stats_metric_get_name(libtorrent::stats_metric *self){
return std::string(self->name);
}
SWIGINTERN libtorrent::torrent_alert const *libtorrent_alert_cast_to_torrent_alert(libtorrent::alert const *a){ return alert_cast<libtorrent::torrent_alert>(a); }
SWIGINTERN libtorrent::peer_alert const *libtorrent_alert_cast_to_peer_alert(libtorrent::alert const *a){ return alert_cast<libtorrent::peer_alert>(a); }
SWIGINTERN libtorrent::tracker_alert const *libtorrent_alert_cast_to_tracker_alert(libtorrent::alert const *a){ return alert_cast<libtorrent::tracker_alert>(a); }
Expand Down Expand Up @@ -36370,69 +36373,6 @@ static SwigV8ReturnValue _wrap_libtorrent_op_get_interface(v8::Local<v8::String>
}


static void _wrap_stats_metric_name_set(v8::Local<v8::String> property, v8::Local<v8::Value> value,
const SwigV8PropertyCallbackInfoVoid &info) {
SWIGV8_HANDLESCOPE();

libtorrent::stats_metric *arg1 = (libtorrent::stats_metric *) 0 ;
char *arg2 = (char *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
int res2 ;
char *buf2 = 0 ;
int alloc2 = 0 ;

res1 = SWIG_ConvertPtr(info.Holder(), &argp1,SWIGTYPE_p_libtorrent__stats_metric, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "stats_metric_name_set" "', argument " "1"" of type '" "libtorrent::stats_metric *""'");
}
arg1 = (libtorrent::stats_metric *)(argp1);
res2 = SWIG_AsCharPtrAndSize(value, &buf2, NULL, &alloc2);
if (!SWIG_IsOK(res2)) {
SWIG_exception_fail(SWIG_ArgError(res2), "in method '" "stats_metric_name_set" "', argument " "2"" of type '" "char const *""'");
}
arg2 = (char *)(buf2);
if (arg2) {
size_t size = strlen((const char *)((const char *)(arg2))) + 1;
arg1->name = (char const *)(char*)(memcpy((new char[size]), arg2, sizeof(char)*(size)));
} else {
arg1->name = 0;
}

if (alloc2 == SWIG_NEWOBJ) delete[] buf2;

goto fail;
fail:
return;
}


static SwigV8ReturnValue _wrap_stats_metric_name_get(v8::Local<v8::String> property, const SwigV8PropertyCallbackInfo &info) {
SWIGV8_HANDLESCOPE();

v8::Handle<v8::Value> jsresult;
libtorrent::stats_metric *arg1 = (libtorrent::stats_metric *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
char *result = 0 ;

res1 = SWIG_ConvertPtr(info.Holder(), &argp1,SWIGTYPE_p_libtorrent__stats_metric, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "stats_metric_name_get" "', argument " "1"" of type '" "libtorrent::stats_metric *""'");
}
arg1 = (libtorrent::stats_metric *)(argp1);
result = (char *) ((arg1)->name);
jsresult = SWIG_FromCharPtr((const char *)result);


SWIGV8_RETURN_INFO(jsresult, info);

goto fail;
fail:
SWIGV8_RETURN_INFO(SWIGV8_UNDEFINED(), info);
}


static void _wrap_stats_metric_value_index_set(v8::Local<v8::String> property, v8::Local<v8::Value> value,
const SwigV8PropertyCallbackInfoVoid &info) {
SWIGV8_HANDLESCOPE();
Expand Down Expand Up @@ -36577,6 +36517,34 @@ static SwigV8ReturnValue _wrap_stats_metric_type_get(v8::Local<v8::String> prope
}


static SwigV8ReturnValue _wrap_stats_metric_get_name(const SwigV8Arguments &args) {
SWIGV8_HANDLESCOPE();

v8::Handle<v8::Value> jsresult;
libtorrent::stats_metric *arg1 = (libtorrent::stats_metric *) 0 ;
void *argp1 = 0 ;
int res1 = 0 ;
std::string result;

if(args.Length() != 0) SWIG_exception_fail(SWIG_ERROR, "Illegal number of arguments for _wrap_stats_metric_get_name.");

res1 = SWIG_ConvertPtr(args.Holder(), &argp1,SWIGTYPE_p_libtorrent__stats_metric, 0 | 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "stats_metric_get_name" "', argument " "1"" of type '" "libtorrent::stats_metric *""'");
}
arg1 = (libtorrent::stats_metric *)(argp1);
result = libtorrent_stats_metric_get_name(arg1);
jsresult = SWIG_From_std_string((std::string)(result));


SWIGV8_RETURN(jsresult);

goto fail;
fail:
SWIGV8_RETURN(SWIGV8_UNDEFINED());
}


static SwigV8ReturnValue _wrap_new_stats_metric(const SwigV8Arguments &args) {
SWIGV8_HANDLESCOPE();

Expand Down Expand Up @@ -100932,7 +100900,7 @@ static SwigV8ReturnValue _wrap_JLIBTORRENT_REVISION_SHA1(v8::Local<v8::String> p

v8::Handle<v8::Value> jsresult;

jsresult = SWIG_FromCharPtr((const char *)"80235855f1085c558cac4da22422d91b1d914482");
jsresult = SWIG_FromCharPtr((const char *)"f5738121a569b852c76aab6ca5aa662956323099");

SWIGV8_RETURN_INFO(jsresult, info);

Expand Down Expand Up @@ -109854,9 +109822,9 @@ SWIGV8_AddStaticVariable(exports_obj, "op_encryption", _wrap_libtorrent_op_encry
SWIGV8_AddStaticVariable(exports_obj, "op_connect", _wrap_libtorrent_op_connect, JS_veto_set_variable);
SWIGV8_AddStaticVariable(exports_obj, "op_ssl_handshake", _wrap_libtorrent_op_ssl_handshake, JS_veto_set_variable);
SWIGV8_AddStaticVariable(exports_obj, "op_get_interface", _wrap_libtorrent_op_get_interface, JS_veto_set_variable);
SWIGV8_AddMemberVariable(_exports_stats_metric_class, "name", _wrap_stats_metric_name_get, _wrap_stats_metric_name_set);
SWIGV8_AddMemberVariable(_exports_stats_metric_class, "value_index", _wrap_stats_metric_value_index_get, _wrap_stats_metric_value_index_set);
SWIGV8_AddMemberVariable(_exports_stats_metric_class, "type", _wrap_stats_metric_type_get, _wrap_stats_metric_type_set);
SWIGV8_AddMemberFunction(_exports_stats_metric_class, "get_name", _wrap_stats_metric_get_name);
SWIGV8_AddMemberFunction(_exports_counters_class, "inc_stats_counter", _wrap_counters__wrap_counters_inc_stats_counter);
SWIGV8_AddMemberFunction(_exports_counters_class, "set_value", _wrap_counters_set_value);
SWIGV8_AddMemberFunction(_exports_counters_class, "blend_stats_counter", _wrap_counters_blend_stats_counter);
Expand Down

0 comments on commit c7c178a

Please sign in to comment.