Add transport/peer class
This commit is contained in:
parent
f1d2009418
commit
4faa1a4a46
1
index.js
1
index.js
|
@ -27,6 +27,7 @@ bitcore.util.js = require('./lib/util/js');
|
||||||
// transport
|
// transport
|
||||||
bitcore.transport = {};
|
bitcore.transport = {};
|
||||||
bitcore.transport.Connection = require('./lib/transport/connection');
|
bitcore.transport.Connection = require('./lib/transport/connection');
|
||||||
|
bitcore.transport.Peer = require('./lib/transport/peer');
|
||||||
|
|
||||||
// errors thrown by the library
|
// errors thrown by the library
|
||||||
bitcore.errors = require('./lib/errors');
|
bitcore.errors = require('./lib/errors');
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
var Net = require('net');
|
||||||
|
var Put = require('bufferput');
|
||||||
|
var buffertools = require('buffertools');
|
||||||
|
|
||||||
|
function Peer(host, port, services) {
|
||||||
|
if ("string" === typeof host) {
|
||||||
|
if (host.indexOf(':') && !port) {
|
||||||
|
var parts = host.split(':');
|
||||||
|
host = parts[0];
|
||||||
|
port = parts[1];
|
||||||
|
}
|
||||||
|
this.host = host;
|
||||||
|
this.port = +port || 8333;
|
||||||
|
} else if (host instanceof Peer) {
|
||||||
|
this.host = host.host;
|
||||||
|
this.port = host.port;
|
||||||
|
} else if (Buffer.isBuffer(host)) {
|
||||||
|
if (buffertools.compare(Peer.IPV6_IPV4_PADDING, host.slice(0, 12)) != 0) {
|
||||||
|
throw new Error('IPV6 not supported yet! Cannot instantiate host.');
|
||||||
|
}
|
||||||
|
this.host = Array.prototype.slice.apply(host.slice(12)).join('.');
|
||||||
|
this.port = +port || 8333;
|
||||||
|
} else {
|
||||||
|
throw new Error('Could not instantiate peer, invalid parameter type: ' +
|
||||||
|
typeof host);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.services = (services) ? services : null;
|
||||||
|
this.lastSeen = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
Peer.IPV6_IPV4_PADDING = new Buffer([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255]);
|
||||||
|
|
||||||
|
Peer.prototype.createConnection = function() {
|
||||||
|
this.connection = Net.createConnection(this.port, this.host);
|
||||||
|
return this.connection;
|
||||||
|
};
|
||||||
|
|
||||||
|
Peer.prototype.getHostAsBuffer = function() {
|
||||||
|
return new Buffer(this.host.split('.'));
|
||||||
|
};
|
||||||
|
|
||||||
|
Peer.prototype.toString = function() {
|
||||||
|
return this.host + ":" + this.port;
|
||||||
|
};
|
||||||
|
|
||||||
|
Peer.prototype.toBuffer = function() {
|
||||||
|
var put = new Put();
|
||||||
|
put.word32le(this.lastSeen);
|
||||||
|
put.word64le(this.services);
|
||||||
|
put.put(this.getHostAsBuffer());
|
||||||
|
put.word16be(this.port);
|
||||||
|
return put.buffer();
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = Peer;
|
|
@ -9,14 +9,16 @@ var ConnectionModule = bitcore.transport.Connection;
|
||||||
var Connection;
|
var Connection;
|
||||||
var nop = function() {};
|
var nop = function() {};
|
||||||
|
|
||||||
describe.only('Connection', function() {
|
describe('Connection', function() {
|
||||||
it('should initialze the main object', function() {
|
it('should initialze the main object', function() {
|
||||||
should.exist(ConnectionModule);
|
should.exist(ConnectionModule);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to create class', function() {
|
it('should be able to create class', function() {
|
||||||
Connection = ConnectionModule;
|
Connection = ConnectionModule;
|
||||||
should.exist(Connection);
|
should.exist(Connection);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to create instance', function() {
|
it('should be able to create instance', function() {
|
||||||
var mSocket = {server: null, addListener: nop},
|
var mSocket = {server: null, addListener: nop},
|
||||||
mPeer;
|
mPeer;
|
||||||
|
@ -34,8 +36,3 @@ describe.only('Connection', function() {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,52 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
var chai = require('chai');
|
||||||
|
var bitcore = require('../..');
|
||||||
|
|
||||||
|
var should = chai.should();
|
||||||
|
|
||||||
|
var PeerModule = bitcore.transport.Peer;
|
||||||
|
var Peer;
|
||||||
|
|
||||||
|
describe('Peer', function() {
|
||||||
|
it('should initialze the main object', function() {
|
||||||
|
should.exist(PeerModule);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be able to create class', function() {
|
||||||
|
Peer = PeerModule;
|
||||||
|
should.exist(Peer);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be able to create instance', function() {
|
||||||
|
var p = new Peer('localhost', 8333);
|
||||||
|
should.exist(p);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be able to create instance', function() {
|
||||||
|
var p = new Peer('localhost:8333');
|
||||||
|
should.exist(p);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be able to create instance', function() {
|
||||||
|
var p = new Peer('localhost:8333');
|
||||||
|
var p2 = new Peer(p);
|
||||||
|
should.exist(p2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not be able to create instance', function() {
|
||||||
|
should.throw(function() {
|
||||||
|
new Peer(8333);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should be able to create instance', function() {
|
||||||
|
var p = new Peer('localhost', 8333);
|
||||||
|
p.toString().should.equal('localhost:8333');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('check host as buffer', function() {
|
||||||
|
var p = new Peer('127.0.0.1', 8333);
|
||||||
|
p.getHostAsBuffer().toString('hex').should.equal('7f000001');
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue