updated to use mapped constructers and removed build method

This commit is contained in:
Braydon Fuller 2015-03-13 20:49:24 -04:00
parent 97f39db081
commit 6007dc6faf
3 changed files with 9 additions and 20 deletions

View File

@ -25,10 +25,6 @@ Messages.PAYLOAD_START = 16;
Messages.Message = require('./message');
Messages.builder = require('./builder');
Messages.prototype.build = function(command, object) {
return this.builder.commands[command].fromObject(object);
};
Messages.prototype.parseBuffer = function(dataBuffer) {
/* jshint maxstatements: 18 */
if (dataBuffer.length < Messages.MINIMUM_LENGTH) {
@ -36,7 +32,9 @@ Messages.prototype.parseBuffer = function(dataBuffer) {
}
// Search the next magic number
if (!this._discardUntilNextMessage(dataBuffer)) return;
if (!this._discardUntilNextMessage(dataBuffer)) {
return;
}
var payloadLen = (dataBuffer.get(Messages.PAYLOAD_START)) +
(dataBuffer.get(Messages.PAYLOAD_START + 1) << 8) +

View File

@ -38,7 +38,7 @@ function Peer(options) {
return new Peer(options);
}
if(options.socket) {
if (options.socket) {
this.socket = options.socket;
this.host = this.socket.remoteAddress;
this.port = this.socket.remotePort;
@ -83,7 +83,7 @@ function Peer(options) {
self.subversion = message.subversion;
self.bestHeight = message.startHeight;
var verackResponse = self.messages.build('verack');
var verackResponse = self.messages.VerAck();
self.sendMessage(verackResponse);
if(!self.versionSent) {
@ -108,7 +108,6 @@ Peer.STATUS = {
/**
* Set a socks5 proxy for the connection. Enables the use of the TOR network.
*
* @param {String} host - IP address of the proxy
* @param {Number} port - Port number of the proxy
* @returns {Peer} The same Peer instance.
@ -125,7 +124,6 @@ Peer.prototype.setProxy = function(host, port) {
/**
* Init the connection with the remote peer.
*
* @returns {Socket} The same peer instance.
*/
Peer.prototype.connect = function() {
@ -168,7 +166,6 @@ Peer.prototype._onError = function(e) {
/**
* Disconnects the remote connection.
*
* @returns {Socket} The same peer instance.
*/
Peer.prototype.disconnect = function() {
@ -180,7 +177,6 @@ Peer.prototype.disconnect = function() {
/**
* Send a Message to the remote peer.
*
* @param {Message} message - A message instance
*/
Peer.prototype.sendMessage = function(message) {
@ -191,10 +187,8 @@ Peer.prototype.sendMessage = function(message) {
* Internal function that sends VERSION message to the remote peer.
*/
Peer.prototype._sendVersion = function() {
// todo: include sending ip address
var message = this.messages.build('version', {
relay: this.relay
});
// todo: include sending local ip address
var message = this.messages.Version({relay: this.relay});
this.versionSent = true;
this.sendMessage(message);
};
@ -203,9 +197,7 @@ Peer.prototype._sendVersion = function() {
* Send a PONG message to the remote peer.
*/
Peer.prototype._sendPong = function(nonce) {
var message = this.messages.build('pong', {
nonce: nonce
});
var message = this.messages.Pong({nonce: nonce});
this.sendMessage(message);
};
@ -222,7 +214,6 @@ Peer.prototype._readMessage = function() {
/**
* Internal function that creates a socket using a proxy if neccesary.
*
* @returns {Socket} A Socket instance not yet connected.
*/
Peer.prototype._getSocket = function() {

View File

@ -126,7 +126,7 @@ describe('Peer', function() {
it('send pong on ping', function(done) {
var peer = new Peer({host: 'localhost'});
var pingMessage = messages.build('ping');
var pingMessage = messages.Ping();
peer.sendMessage = function(message) {
message.command.should.equal('pong');
message.nonce.should.equal(pingMessage.nonce);