Skip to content

Commit

Permalink
First bits to create an asynchronous nodejs alerts loop.
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Dec 5, 2015
1 parent d8acc46 commit 71a6bce
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/test/node/session_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var lt = require('../../../swig/build/Release/jlibtorrent');

var alert_mask = lt.alert.all_categories;
var sp = new lt.settings_pack();

sp.set_int(lt.settings_pack.alert_mask, alert_mask);

var s = new lt.session(sp);

lt.session_alerts_loop(s);

process.stdout.write('Press ENTER to exit...');
process.stdin.once('data', function (data) {
process.exit(0);
});
1 change: 1 addition & 0 deletions swig/binding.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'BOOST_ALL_NO_LIB',
'BOOST_ASIO_ENABLE_CANCELIO',
'BOOST_ASIO_HASH_MAP_BUCKETS=1021',
'BOOST_ASIO_HAS_STD_CHRONO',
'BOOST_CHRONO_STATIC_LINK=1',
'BOOST_EXCEPTION_DISABLE',
'BOOST_MULTI_INDEX_DISABLE_SERIALIZATION',
Expand Down
33 changes: 33 additions & 0 deletions swig/libtorrent.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,36 @@ boost::shared_ptr<peer_plugin> swig_torrent_plugin::new_connection(libtorrent::p
swig_peer_plugin* swig_torrent_plugin::new_peer_connection(libtorrent::peer_connection_handle const& pc) {
return NULL;
}

This comment has been minimized.

Copy link
@gubatron

gubatron Dec 5, 2015

Collaborator

is it ok to add this directly on swig/libtorrent.h?
or is it better to add it from the libtorrent.i (swig) file.
... although I guess the LIBTORRENT_SWIG_NODE compilation flag check would save this code from going to java land.

This comment has been minimized.

Copy link
@gubatron

gubatron Dec 5, 2015

Collaborator

I guess I was just wondering if libtorrent.h was made programatically by swig. I suppose not.
I must be confused with libtorrent_jni.j or something similar.

This comment has been minimized.

Copy link
@aldenml

aldenml Dec 5, 2015

Author Collaborator

libtorrent.h is hand made by us, and all custom c++ code should move to this place (slowly).

#ifdef LIBTORRENT_SWIG_NODE

#include <uv.h>

void session_alerts_loop_fn(void *arg) {
libtorrent::session& s = *((libtorrent::session*) arg);

libtorrent::time_duration max_wait = std::chrono::milliseconds(500);

std::vector<alert*> v;
while (true) {
alert* ptr = s.wait_for_alert(max_wait);

if (ptr != NULL) {
s.pop_alerts(&v);
printf("alerts (%d)\n", v.size());
long size = v.size();
for (int i = 0; i < size; i++) {
alert* a = v[i];
printf("%d - %s - %s\n", a->type(), a->what(), a->message().c_str());
}
}
}
}

void session_alerts_loop(libtorrent::session& s) {

uv_thread_t t;
uv_thread_create(&t, session_alerts_loop_fn, &s);
}

#endif
6 changes: 6 additions & 0 deletions swig/libtorrent.i
Original file line number Diff line number Diff line change
Expand Up @@ -1225,4 +1225,10 @@ public:
%ignore swig_storage_constructor_cb;
%ignore dht_put_item_cb;

#ifdef LIBTORRENT_SWIG_NODE

%ignore session_alerts_loop_fn;

#endif

%include "libtorrent.h"
2 changes: 1 addition & 1 deletion swig/libtorrent_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51070,7 +51070,7 @@ SWIGEXPORT jstring JNICALL Java_com_frostwire_jlibtorrent_swig_libtorrent_1jni_J

(void)jenv;
(void)jcls;
result = (char *)("971282edcc01447fa1be9bc12b1e6c9f230bce62");
result = (char *)("d8acc461c8859ddd78d8cf082f72237bf5010f77");
if (result) jresult = jenv->NewStringUTF((const char *)result);
return jresult;
}
Expand Down
33 changes: 32 additions & 1 deletion swig/libtorrent_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111965,7 +111965,7 @@ static SwigV8ReturnValue _wrap_JLIBTORRENT_REVISION_SHA1(v8::Local<v8::String> p

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

jsresult = SWIG_FromCharPtr((const char *)"971282edcc01447fa1be9bc12b1e6c9f230bce62");
jsresult = SWIG_FromCharPtr((const char *)"d8acc461c8859ddd78d8cf082f72237bf5010f77");

SWIGV8_RETURN_INFO(jsresult, info);

Expand Down Expand Up @@ -115300,6 +115300,36 @@ static SwigV8ReturnValue _wrap_new_swig_peer_plugin(const SwigV8Arguments &args)
}


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

v8::Handle<v8::Value> jsresult;
libtorrent::session *arg1 = 0 ;
void *argp1 = 0 ;
int res1 = 0 ;

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

res1 = SWIG_ConvertPtr(args[0], &argp1, SWIGTYPE_p_libtorrent__session, 0 );
if (!SWIG_IsOK(res1)) {
SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "session_alerts_loop" "', argument " "1"" of type '" "libtorrent::session &""'");
}
if (!argp1) {
SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "session_alerts_loop" "', argument " "1"" of type '" "libtorrent::session &""'");
}
arg1 = (libtorrent::session *)(argp1);
session_alerts_loop(*arg1);
jsresult = SWIGV8_UNDEFINED();


SWIGV8_RETURN(jsresult);

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


/* -------- TYPE CONVERSION AND EQUIVALENCE RULES (BEGIN) -------- */

static void *_p_libtorrent__piece_managerTo_p_libtorrent__disk_job_fence(void *x, int *SWIGUNUSEDPARM(newmemory)) {
Expand Down Expand Up @@ -125059,6 +125089,7 @@ SWIGV8_AddStaticFunction(exports_obj, "add_files_ex", _wrap_add_files_ex);
SWIGV8_AddStaticFunction(exports_obj, "set_piece_hashes_ex", _wrap_set_piece_hashes_ex);
SWIGV8_AddStaticFunction(exports_obj, "boost_version", _wrap_boost_version);
SWIGV8_AddStaticFunction(exports_obj, "boost_lib_version", _wrap_boost_lib_version);
SWIGV8_AddStaticFunction(exports_obj, "session_alerts_loop", _wrap_session_alerts_loop);


/* register classes */
Expand Down

0 comments on commit 71a6bce

Please sign in to comment.