Comparing version 0.0.7 to 0.0.8
@@ -10,2 +10,4 @@ /*jshint node: true */ | ||
var RSVP = require('rsvp'); | ||
var PeerConnection = module.exports = function(socket){ | ||
@@ -45,3 +47,2 @@ events.EventEmitter.call(this); | ||
}); | ||
this.bindSocket(socket); | ||
@@ -68,2 +69,3 @@ }; | ||
self.emit('close', had_error); | ||
// check for mock socket | ||
if (typeof(self._socket.unref) === "function"){ | ||
@@ -88,3 +90,3 @@ self._socket.unref(); | ||
this.clearDeathTimer(); | ||
this._deathTimer = setTimeout(PeerConnection.prototype.close.bind(this),120000); | ||
this._deathTimer = setTimeout(PeerConnection.prototype.close.bind(this), 120000); | ||
}; | ||
@@ -218,5 +220,7 @@ | ||
} | ||
var defer = RSVP.defer(); | ||
// throttle writes | ||
this._writeQueue.push(msg); | ||
this._writeQueue.push({ defered: defer, msg: msg }); | ||
process.nextTick(PeerConnection.prototype._write.bind(this)); | ||
return defer.promise; | ||
}; | ||
@@ -226,5 +230,6 @@ | ||
if (this._writeQueue.length === 0) return; | ||
var msg = this._writeQueue.shift(); | ||
var obj = this._writeQueue.shift(); | ||
var self = this; | ||
this._socket.write(msg.toBuffer(), null, function() { | ||
this._socket.write(obj.msg.toBuffer(), null, function() { | ||
obj.defered.resolve(); | ||
process.nextTick(PeerConnection.prototype._write.bind(self)); | ||
@@ -245,27 +250,27 @@ }); | ||
// send the message | ||
this.write(msg); | ||
return this.write(msg); | ||
}; | ||
PeerConnection.prototype.keepAlive = function() { | ||
this.write(new Messages.KeepAlive()); | ||
return this.write(new Messages.KeepAlive()); | ||
}; | ||
PeerConnection.prototype.choke = function() { | ||
this.write(new Messages.Choke()); | ||
return this.write(new Messages.Choke()); | ||
}; | ||
PeerConnection.prototype.unchoke = function() { | ||
this.write(new Messages.Unchoke()); | ||
return this.write(new Messages.Unchoke()); | ||
}; | ||
PeerConnection.prototype.interested = function() { | ||
this.write(new Messages.Interested()); | ||
return this.write(new Messages.Interested()); | ||
}; | ||
PeerConnection.prototype.notInterested = function() { | ||
this.write(new Messages.NotInterested()); | ||
return this.write(new Messages.NotInterested()); | ||
}; | ||
PeerConnection.prototype.have = function(pieceIndex) { | ||
this.write((new Messages.Have()).init({ | ||
return this.write((new Messages.Have()).init({ | ||
pieceIndex : pieceIndex | ||
@@ -276,3 +281,3 @@ })); | ||
PeerConnection.prototype.bitfield = function(bitfield) { | ||
this.write((new Messages.Bitfield()).init({ | ||
return this.write((new Messages.Bitfield()).init({ | ||
bitfield : bitfield | ||
@@ -283,3 +288,3 @@ })); | ||
PeerConnection.prototype._request = function(Constructor, index, begin, length) { | ||
this.write((new Constructor()).init({ | ||
return this.write((new Constructor()).init({ | ||
index : index, | ||
@@ -292,11 +297,11 @@ begin : begin, | ||
PeerConnection.prototype.request = function(index, begin, length){ | ||
this._request(Messages.Request, index, begin, length); | ||
return this._request(Messages.Request, index, begin, length); | ||
}; | ||
PeerConnection.prototype.cancel = function(index, begin, length){ | ||
this._request(Messages.Cancel, index, begin, length); | ||
return this._request(Messages.Cancel, index, begin, length); | ||
}; | ||
PeerConnection.prototype.piece = function(index, begin, block){ | ||
this.write((new Messages.Piece()).init({ | ||
return this.write((new Messages.Piece()).init({ | ||
index : index, | ||
@@ -309,5 +314,5 @@ begin : begin, | ||
PeerConnection.prototype.port = function(listenPort) { | ||
this.write((new Messages.Port()).init({ | ||
return this.write((new Messages.Port()).init({ | ||
listenPort : listenPort | ||
})); | ||
}; |
{ | ||
"name": "peerface", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "bittorrent peer communication library", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,8 +26,8 @@ Peerface | ||
// connect to a peer (promises) | ||
Peerface.connect(ipAddress, port).then(function(peer){ ... }); | ||
// create the server, and listen for incoming messages | ||
var server = Peerface.listen(6881); | ||
// connect to a peer | ||
var peer = Peerface.connect(ipAddress, port); | ||
// listen for a peer | ||
@@ -54,3 +54,5 @@ server.on('peer-connected', function(peer){ | ||
// send protocol messages to peer | ||
// send protocol messages to peer (returns promises) | ||
// promise is resolved when message has been fully written | ||
// to the underlying socket | ||
peer.handshake(peerId, infoHash); // infohash buffer or base64 string | ||
@@ -57,0 +59,0 @@ peer.keepAlive(); |
@@ -609,3 +609,3 @@ /*jshint node: true */ | ||
t.plan(13); | ||
t.plan(24); | ||
@@ -693,35 +693,35 @@ var defered = []; | ||
next().then(function(){ | ||
remote.keepAlive(); | ||
t.ok(remote.keepAlive() instanceof RSVP.Promise, "should return promise"); | ||
return next(); | ||
}).then(function(){ | ||
remote.choke(); | ||
t.ok(remote.choke() instanceof RSVP.Promise, "should return promise"); | ||
return next(); | ||
}).then(function(){ | ||
remote.unchoke(); | ||
t.ok(remote.unchoke() instanceof RSVP.Promise, "should return promise"); | ||
return next(); | ||
}).then(function(){ | ||
remote.interested(); | ||
t.ok(remote.interested() instanceof RSVP.Promise, "should return promise"); | ||
return next(); | ||
}).then(function(){ | ||
remote.notInterested(); | ||
t.ok(remote.notInterested() instanceof RSVP.Promise, "should return promise"); | ||
return next(); | ||
}).then(function(){ | ||
var msg = new lib.Messages.Have(messages.have); | ||
remote.have(msg.pieceIndex); | ||
t.ok(remote.have(msg.pieceIndex) instanceof RSVP.Promise, "should return promise"); | ||
return next(); | ||
}).then(function(){ | ||
var msg = new lib.Messages.Bitfield(messages.bitfield); | ||
remote.bitfield(msg.bitfield); | ||
t.ok(remote.bitfield(msg.bitfield) instanceof RSVP.Promise, "should return promise"); | ||
return next(); | ||
}).then(function(){ | ||
var msg = new lib.Messages.Request(messages.request); | ||
remote.request(msg.index, msg.begin, msg.length); | ||
t.ok(remote.request(msg.index, msg.begin, msg.length) instanceof RSVP.Promise, "should return promise"); | ||
return next(); | ||
}).then(function(){ | ||
var msg = new lib.Messages.Cancel(messages.cancel); | ||
remote.cancel(msg.index, msg.begin, msg.length); | ||
t.ok(remote.cancel(msg.index, msg.begin, msg.length) instanceof RSVP.Promise, "should return promise"); | ||
return next(); | ||
}).then(function(){ | ||
var msg = new lib.Messages.Piece(messages.piece); | ||
remote.piece(msg.index, msg.begin, msg.block); | ||
t.ok(remote.piece(msg.index, msg.begin, msg.block) instanceof RSVP.Promise, "should return promise"); | ||
return next(); | ||
@@ -732,3 +732,3 @@ }).then(function(){ | ||
}); | ||
remote.port(msg.listenPort); | ||
t.ok(remote.port(msg.listenPort) instanceof RSVP.Promise, "should return promise"); | ||
return next(); | ||
@@ -770,4 +770,6 @@ }).then(function(){ | ||
server.stop(); | ||
}); |
51256
1520
114