Skip to content

Commit

Permalink
[node] Using nodejs even emitter framework
Browse files Browse the repository at this point in the history
  • Loading branch information
aldenml committed Feb 3, 2016
1 parent 5e2283e commit d554b18
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 11 additions & 4 deletions node/jlibtorrent.js
@@ -1,4 +1,7 @@
var swig = require('./jlibtorrent.node');
const EventEmitter = require('events');
const util = require('util');

const swig = require('./jlibtorrent.node');

// swig
(function () {
Expand Down Expand Up @@ -111,7 +114,7 @@ var swig = require('./jlibtorrent.node');
return new swig.session(sp);
}

function alertsLoop(s) {
function alertsLoop(emitter, s) {
var session_alerts_loop = function () {
var max_wait = swig.to_milliseconds(100);
var alert = s.wait_for_alert(max_wait);
Expand All @@ -122,7 +125,7 @@ var swig = require('./jlibtorrent.node');
var size = vector.size();
for (var i = 0; i < size; i++) {
var a = vector.get(i);
console.log(a.type() + " - " + a.what() + " - " + a.message());
emitter.emit('alert', a);
}
vector.clear();
}
Expand All @@ -132,14 +135,18 @@ var swig = require('./jlibtorrent.node');
}

function Session(settings, logging) {
EventEmitter.call(this);

settings = settings || new exports.SettingsPack();
logging = logging || false;

this.s = createSession(settings, logging);

alertsLoop(this.s);
alertsLoop(this, this.s);
}

util.inherits(Session, EventEmitter);

Session.prototype.swig = function () {
return this.sp;

This comment has been minimized.

Copy link
@gubatron

gubatron Feb 4, 2016

Collaborator

in this class I don't see where this.sp gets initialized.

I only see this in the constructor:

this.s = createSession(settings, logging);

I suppose s stands for session, and sp for session pointer. My guess is that, after that assignment, maybe you wanted to do something like:

this.sp = s.swig or something similar.

In the case of SettingsPack you pass the sp in its constructor.

https://github.com/frostwire/frostwire-jlibtorrent/blob/9c9b3580a9f7888746c6c49019a05a1215f0c140/node/jlibtorrent.js

This comment has been minimized.

Copy link
@gubatron

gubatron Feb 4, 2016

Collaborator

or wait a minute, now that I read the fetch magnet code, I see that sp is settings pack and you pass it in the constructor.

This comment has been minimized.

Copy link
@gubatron

gubatron Feb 4, 2016

Collaborator

which means, in this code:

function Session(settings, logging, listener) {
        EventEmitter.call(this);

        settings = settings || new exports.SettingsPack();
        logging = logging || false;

        this.s = createSession(settings, logging);

        if (listener) {
            this.on('alert', listener);
        }

        alertsLoop(this, this.s);

        var arr = defaultRouters();
        var size = arr.length;
        for (var i = 0; i < size; i++) {
            this.s.add_dht_router(arr[i]);
        }
    }

either you forgot to do:
this.sp = settings;

or it happens internally in one of the other functions.

This comment has been minimized.

Copy link
@aldenml

aldenml Feb 4, 2016

Author Collaborator

It was just a copy/paste error (when copied from SettingsPack). It should be this.s. (the wonders of JS)

}
Expand Down
10 changes: 7 additions & 3 deletions node/session_test.js
@@ -1,9 +1,13 @@
var jlibtorrent = require('./jlibtorrent.js');
const jlibtorrent = require('./jlibtorrent.js');

console.log("Using libtorrent version: " + jlibtorrent.LibTorrent.fullVersion());

var sp = new jlibtorrent.SettingsPack();
var s = new jlibtorrent.Session(sp, false);
const sp = new jlibtorrent.SettingsPack();
const s = new jlibtorrent.Session(sp, false);

s.on('alert', function (a) {
console.log(a.type() + " - " + a.what() + " - " + a.message());
});

process.stdout.write('Press ENTER to exit...');
process.stdin.once('data', function (data) {
Expand Down

0 comments on commit d554b18

Please sign in to comment.