Permalink
Browse files

UDP working again after refactor.

  • Loading branch information...
1 parent 269da00 commit 544ac7885a548ab730286d30324285c1165a1196 @superafroman committed Sep 7, 2012
Showing with 18 additions and 14 deletions.
  1. +3 −3 README.md
  2. +2 −5 lib/protocol/index.js
  3. +10 −4 lib/protocol/udp.js
  4. +3 −2 lib/tracker.js
View
@@ -6,21 +6,21 @@ A simple bittorrent client for node.
* Downloading based on .torrent file
* Resume
* Seeding
+ * UDP support
## In progress
- * UDP support
+ * Hook up DHT (magnet links)
## TODO
* Share ratio
* Accurate reporting on download/upload speeds
* Limit download/upload speeds
- * Hook up DHT, possibly using node-dht
* Expose nice programmatic API for interacting with torrents/peers/trackers
## Usage
var Client = require('node-torrent');
- var client = new Client();
+ var client = new Client({logLevel: 'DEBUG'});
client.addTorrent('a.torrent');
## License
View
@@ -1,9 +1,6 @@
-var HTTP = require('./http');
-var UDP = require('./udp');
-
module.exports = {
- 'http:': new HTTP(),
- 'udp:': new UDP()
+ 'http:': require('./http'),
+ 'udp:': require('./udp')
};
View
@@ -61,7 +61,7 @@ UDP.prototype = {
BufferUtils.fromInt(0), BufferUtils.fromInt(this.data['downloaded'] || 0), // int64, TODO: split data into two parts etc
BufferUtils.fromInt(0), BufferUtils.fromInt(this.data['left'] || 0), // 64
BufferUtils.fromInt(0), BufferUtils.fromInt(this.data['uploaded'] || 0), //64
- BufferUtils.fromInt(event),
+ BufferUtils.fromInt(this.event),
BufferUtils.fromInt(0),
BufferUtils.fromInt(Math.random() * 255),
BufferUtils.fromInt(200),
@@ -90,7 +90,9 @@ UDP.prototype = {
},
_complete: function(trackerInfo, err) {
- this.socket.close();
+ try {
+ this.socket.close();
+ } catch(e) {}
this.callback(trackerInfo, err);
},
@@ -102,6 +104,7 @@ UDP.prototype = {
},
_generateTransactionId: function() {
+ LOGGER.debug('generating transaction id');
var id = new Buffer(4);
id[0] = Math.random() * 255;
id[1] = Math.random() * 255;
@@ -111,9 +114,12 @@ UDP.prototype = {
},
_handleMessage: function(msg) {
+ LOGGER.debug('handling message from tracker');
var action = BufferUtils.readInt(msg);
var responseTransactionId = BufferUtils.slice(msg, 4, 8);
+ console.log(responseTransactionId, this.transactionId);
if (BufferUtils.equal(responseTransactionId, this.transactionId)) {
+ LOGGER.debug('transactionIds equals, action = ' + action);
switch (action) {
case Action.CONNECT:
this.connectionId = BufferUtils.slice(msg, 8, 16);
@@ -122,7 +128,7 @@ UDP.prototype = {
break;
case Action.ANNOUNCE:
LOGGER.debug('Received announce response.');
- this.__announceResponse(msg);
+ this._announceResponse(msg);
break;
case Action.SCRAPE:
break;
@@ -141,7 +147,7 @@ UDP.prototype = {
_send: function(packet) {
var self = this;
- socket.send(packet, 0, packet.length, this.tracker.url.port, this.tracker.url.hostname, function(err) {
+ this.socket.send(packet, 0, packet.length, this.tracker.url.port, this.tracker.url.hostname, function(err) {
LOGGER.debug('packet sent, err = ', err);
if (err) {
self._complete(null, err);
View
@@ -27,9 +27,10 @@ Tracker.prototype.stop = function(cb) {
function announce(self, event, cb) {
- var handler = protocol[self.url.protocol];
+ var handlerClass = protocol[self.url.protocol];
- if (handler) {
+ if (handlerClass) {
+ var handler = new handlerClass();
var data = self.torrent.trackerInfo();
self.state = CONNECTING;
handler.handle(self, data, event, function(info, error) {

0 comments on commit 544ac78

Please sign in to comment.