Add inheritance to messages and expose them as a module

This commit is contained in:
Yemel Jardi 2014-12-09 11:34:41 -03:00
parent 68f50b2f61
commit 5dc124a438
2 changed files with 47 additions and 25 deletions

View File

@ -25,7 +25,7 @@ module.exports.buildMessage = function(command, payload) {
var Message = MESSAGES[command];
try {
console.log('Message Class', Message);
return Message.fromBuffer(payload);
return new Message().fromBuffer(payload);
} catch (err) {
console.log('Error while parrsing message', command);
console.log(err);
@ -40,7 +40,7 @@ function Version(subversion, nonce) {
this.nonce = nonce || CONNECTION_NONCE;
};
Version.fromBuffer = function(payload) {
Version.prototype.fromBuffer = function(payload) {
var message = new Version();
var parser = new BufferReader(payload);
@ -73,13 +73,13 @@ Version.prototype.serialize = function() {
module.exports.Version = Version;
// ====== INV/GETDATA MESSAGE ======
// ====== INV MESSAGE ======
function Inventory(inventory) {
this.command = 'inv';
this.inventory = inventory || [];
}
Inventory.fromBuffer = function(payload) {
Inventory.prototype.fromBuffer = function(payload) {
var message = new Inventory();
var parser = new BufferReader(payload);
@ -106,21 +106,24 @@ Inventory.prototype.serialize = function() {
return put.buffer();
};
module.exports.Inventory = Inventory;
// ====== GETDATA MESSAGE ======
function GetData(inventory) {
this.command = 'getdata';
this.inventory = inventory || [];
}
GetData.fromBuffer = Inventory.fromBuffer;
GetData.prototype.serialize = Inventory.prototype.serialize;
util.inherits(GetData, Inventory);
module.exports.GetData = GetData;
// ====== PING/PONG MESSAGE ======
// ====== PING MESSAGE ======
function Ping(nonce) {
this.command = 'ping';
this.nonce = nonce || CONNECTION_NONCE;
}
Ping.fromBuffer = function(payload) {
Ping.prototype.fromBuffer = function(payload) {
var nonce = new BufferReader(payload).read(8);
return new Ping(nonce);
};
@ -129,14 +132,16 @@ Ping.prototype.serialize = function() {
return this.nonce;
};
module.exports.Ping = Ping;
// ====== PONG MESSAGE ======
function Pong(nonce) {
this.command = 'pong';
this.nonce = nonce || CONNECTION_NONCE;
}
Pong.fromBuffer = Ping.fromBuffer;
Pong.prototype.serialize = Ping.prototype.serialize;
util.inherits(Pong, Ping);
module.exports.Pong = Pong;
// ====== ADDR MESSAGE ======
function Addresses(nonce) {
@ -144,7 +149,7 @@ function Addresses(nonce) {
this.addresses = [];
}
Address.fromBuffer = function(payload) {
Address.prototype.fromBuffer = function(payload) {
var message = new Address();
var parser = new BufferReader(payload);
@ -168,12 +173,14 @@ Address.prototype.serialize = function() {
return BufferUtil.EMPTY_BUFFER; // TODO
};
module.exports.Address = Address;
// ====== GETADDR MESSAGE ======
function GetAddresses() {
this.command = 'getaddr';
}
GetAddresses.fromBuffer = function() {
GetAddresses.prototype.fromBuffer = function() {
return new GetAddresses();
};
@ -181,13 +188,14 @@ GetAddresses.prototype.serialize = function() {
return BufferUtil.EMPTY_BUFFER;
};
module.exports.GetAddresses = GetAddresses;
// ====== VERACK MESSAGE ======
function VerAck() {
this.command = 'verack';
}
VerAck.fromBuffer = function() {
VerAck.prototype.fromBuffer = function() {
return new VerAck();
};
@ -195,13 +203,15 @@ VerAck.prototype.serialize = function() {
return BufferUtil.EMPTY_BUFFER;
};
module.exports.VerAck = VerAck;
// ====== REJECT MESSAGE ======
// TODO: Parse REJECT message
function Reject() {
this.command = 'reject';
}
Reject.fromBuffer = function() {
Reject.prototype.fromBuffer = function() {
return new Reject();
};
@ -209,12 +219,14 @@ Reject.prototype.serialize = function() {
return BufferUtil.EMPTY_BUFFER;
};
module.exports.Reject = Reject;
// ====== ALERT MESSAGE ======
function Alert(payload) {
this.command = 'reject';
}
Alert.fromBuffer = function() {
Alert.prototype.fromBuffer = function() {
var message = new Alert();
var parser = new BufferReader(payload);
@ -227,13 +239,15 @@ Alert.prototype.serialize = function() {
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
};
module.exports.Alert = Alert;
// ====== HEADERS MESSAGE ======
function Headers(blockheaders) {
this.command = 'headers';
this.headers = blockheaders || [];
}
Headers.fromBuffer = function() {
Headers.prototype.fromBuffer = function() {
var message = new Headers();
var parser = new BufferReader(payload);
@ -252,13 +266,15 @@ Headers.prototype.serialize = function() {
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
};
module.exports.Headers = Headers;
// ====== BLOCK MESSAGE ======
function Block(block) {
this.command = 'block';
this.block = block;
}
Block.fromBuffer = function() {
Block.prototype.fromBuffer = function() {
var parser = new BufferReader(payload);
var block = Block().fromBufferReader(parser);
return new Block(block);
@ -268,13 +284,15 @@ Block.prototype.serialize = function() {
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
};
module.exports.Block = Block;
// ====== TX MESSAGE ======
function Transaction(transaction) {
this.command = 'tx';
this.transaction = transaction;
}
Transaction.fromBuffer = function() {
Transaction.prototype.fromBuffer = function() {
var parser = new BufferReader(payload);
var transaction = Transaction().fromBufferReader(parser);
return new Transaction(transaction);
@ -284,7 +302,9 @@ Transaction.prototype.serialize = function() {
return BufferUtil.EMPTY_BUFFER; // TODO: Serialize
};
// ====== GETBLOCKS/GETHEADERS MESSAGE ======
module.exports.Transaction = Transaction;
// ====== GETBLOCKS MESSAGE ======
function GetBlocks(starts, stop) {
this.command = 'getblocks';
this.version = PROTOCOL_VERSION;
@ -292,7 +312,7 @@ function GetBlocks(starts, stop) {
this.stop = stop || BufferUtil.NULL_HASH;
}
GetBlocks.fromBuffer = function() {
GetBlocks.prototype.fromBuffer = function() {
var message = new GetBlocks();
var parser = new BufferReader(payload);
@ -326,6 +346,9 @@ GetBlocks.prototype.serialize = function() {
return put.buffer();
};
module.exports.GetBlocks = GetBlocks;
// ====== GETHEADERS MESSAGE ======
function GetHeaders(starts, stop) {
this.command = 'getheaders';
this.version = PROTOCOL_VERSION;
@ -333,7 +356,5 @@ function GetHeaders(starts, stop) {
this.stop = stop || BufferUtil.NULL_HASH;
}
GetHeaders.fromBuffer = GetBlocks.fromBuffer;
GetHeaders.prototype.serialize = GetBlocks.prototype.serialize;
util.inherits(GetHeaders, GetBlocks);
module.exports.GetHeaders = GetHeaders;

View File

@ -1,5 +1,6 @@
'use strict';
var util = require('util');
var Net = require('net');
var Put = require('bufferput');
var Buffers = require('buffers');