2014-08-12 12:03:04 -07:00
|
|
|
/**
|
|
|
|
* bitcoind.js
|
|
|
|
* Copyright (c) 2014, BitPay (MIT License)
|
|
|
|
* A bitcoind node.js binding.
|
|
|
|
*/
|
|
|
|
|
|
|
|
var net = require('net');
|
|
|
|
var EventEmitter = require('events').EventEmitter;
|
|
|
|
var bitcoindjs = require('../build/Release/bitcoindjs.node');
|
2014-09-02 19:28:20 -07:00
|
|
|
var util = require('util');
|
2014-08-12 12:03:04 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Bitcoin
|
|
|
|
*/
|
|
|
|
|
2014-08-29 13:54:54 -07:00
|
|
|
function Bitcoin(options) {
|
2014-08-12 12:03:04 -07:00
|
|
|
var self = this;
|
|
|
|
|
2014-08-19 16:40:19 -07:00
|
|
|
if (!(this instanceof Bitcoin)) {
|
2014-08-29 13:54:54 -07:00
|
|
|
return new Bitcoin(options);
|
2014-08-12 12:03:04 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
EventEmitter.call(this);
|
|
|
|
|
2014-08-29 13:54:54 -07:00
|
|
|
this.options = options;
|
2014-09-11 17:18:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
Bitcoin.prototype.__proto__ = EventEmitter.prototype;
|
|
|
|
|
|
|
|
Bitcoin.prototype.start = function(callback) {
|
|
|
|
var self = this;
|
2014-08-29 13:54:54 -07:00
|
|
|
|
2014-09-02 19:28:20 -07:00
|
|
|
this.log_pipe = bitcoindjs.start(function(err, status) {
|
2014-09-11 17:18:36 -07:00
|
|
|
if (callback) {
|
|
|
|
callback(err);
|
|
|
|
callback = null;
|
|
|
|
}
|
2014-08-29 13:54:54 -07:00
|
|
|
if (err) {
|
|
|
|
self.emit('error', err);
|
2014-09-11 17:18:36 -07:00
|
|
|
} else {
|
|
|
|
self.emit('open', status);
|
2014-08-29 13:54:54 -07:00
|
|
|
}
|
2014-08-12 12:03:04 -07:00
|
|
|
});
|
2014-09-02 19:28:20 -07:00
|
|
|
|
2014-09-10 16:57:18 -07:00
|
|
|
// bitcoind's boost threads aren't in the thread pool
|
|
|
|
// or on node's event loop, so we need to keep node open.
|
|
|
|
this._interval = setInterval(function() {
|
2014-09-17 12:52:35 -07:00
|
|
|
if (bitcoindjs.stopped()) {
|
|
|
|
clearInterval(self._interval);
|
|
|
|
delete self._interval;
|
|
|
|
}
|
2014-09-10 16:57:18 -07:00
|
|
|
}, 10000);
|
|
|
|
|
2014-09-11 17:18:36 -07:00
|
|
|
return this.log('log pipe opened: %d', this.log_pipe);
|
|
|
|
};
|
2014-08-12 12:03:04 -07:00
|
|
|
|
2014-09-02 19:28:20 -07:00
|
|
|
Bitcoin.prototype.log =
|
|
|
|
Bitcoin.prototype.info = function() {
|
|
|
|
if (typeof arguments[0] !== 'string') {
|
|
|
|
var out = util.inspect(arguments[0], null, 20, true);
|
|
|
|
return process.stdout.write('bitcoind: ' + out + '\n');
|
|
|
|
}
|
|
|
|
var out = util.format.apply(util, arguments);
|
|
|
|
return process.stdout.write('bitcoind: ' + out + '\n');
|
|
|
|
};
|
|
|
|
|
|
|
|
Bitcoin.prototype.error = function() {
|
|
|
|
if (typeof arguments[0] !== 'string') {
|
|
|
|
var out = util.inspect(arguments[0], null, 20, true);
|
|
|
|
return process.stderr.write('bitcoind: ' + out + '\n');
|
|
|
|
}
|
|
|
|
var out = util.format.apply(util, arguments);
|
|
|
|
return process.stderr.write('bitcoind: ' + out + '\n');
|
|
|
|
};
|
|
|
|
|
2014-09-11 17:18:36 -07:00
|
|
|
Bitcoin.prototype.stop =
|
|
|
|
Bitcoin.prototype.close = function(callback) {
|
|
|
|
var self = this;
|
2014-09-17 12:52:35 -07:00
|
|
|
if (this._interval) {
|
|
|
|
clearInterval(this._interval);
|
|
|
|
delete this._interval;
|
|
|
|
}
|
2014-09-11 17:18:36 -07:00
|
|
|
return bitcoindjs.stop(function(err, status) {
|
|
|
|
if (err) {
|
|
|
|
self.error(err.message);
|
|
|
|
} else {
|
|
|
|
self.log(status);
|
|
|
|
}
|
|
|
|
if (!callback) return;
|
|
|
|
return callback(err, status);
|
|
|
|
});
|
2014-09-10 16:57:18 -07:00
|
|
|
};
|
2014-09-02 19:28:20 -07:00
|
|
|
|
2014-08-12 12:03:04 -07:00
|
|
|
/**
|
|
|
|
* Expose
|
|
|
|
*/
|
|
|
|
|
|
|
|
module.exports = exports = Bitcoin;
|
|
|
|
exports.Bitcoin = Bitcoin;
|
|
|
|
exports.native = bitcoindjs;
|