diff --git a/bin/start-node.js b/bin/start-node.js new file mode 100644 index 00000000..bfcafcc3 --- /dev/null +++ b/bin/start-node.js @@ -0,0 +1,78 @@ +'use strict'; + +var BitcoinNode = require('..').Node; +var chainlib = require('chainlib'); +var io = require('socket.io'); +var log = chainlib.log; +log.debug = function() {}; + +var configuration = { + datadir: process.env.BITCOINDJS_DIR || '~/.bitcoin', + network: process.env.BITCOINDJS_NETWORK || 'livenet' +}; + +var node = new BitcoinNode(configuration); + +var count = 0; +var interval; + +node.on('ready', function() { + + interval = setInterval(function() { + log.info('Sync Status: Tip:', node.chain.tip.hash, 'Height:', node.chain.tip.__height, 'Rate:', count/10, 'blocks per second'); + count = 0; + }, 10000); + + io.on('connection', function(socket) { + + var bus = node.openBus(); + + var methods = node.getAllAPIMethods(); + var methodsMap = {}; + + methods.forEach(function(data) { + var name = data[0]; + var instance = data[1]; + var method = data[2]; + methodsMap[name] = function() { + return method.apply(instance, arguments); + }; + }); + + socket.on('message', function(message) { + if (methodsMap[message.command]) { + methodsMap[message.command](message.params); + } + }); + + socket.on('subscribe', function(name, params) { + bus.subscribe(name, params); + }); + + socket.on('unsubscribe', function(name, params) { + bus.unsubscribe(name, params); + }); + + var events = node.getAllPublishEvents(); + + events.forEach(function(event) { + bus.on(event.name, function(data) { + socket.emit(event.name, data); + }); + }); + + socket.on('disconnect', function() { + bus.close(); + }); + + }); + +}); + +node.on('error', function(err) { + log.error(err); +}); + +node.chain.on('addblock', function(block) { + count++; +}); diff --git a/lib/node.js b/lib/node.js index 00a929bd..7c82de52 100644 --- a/lib/node.js +++ b/lib/node.js @@ -29,6 +29,24 @@ Node.prototype.openBus = function() { return new Bus({db: this.db}); }; +Node.prototype.getAllAPIMethods = function() { + var methods = []; + for (var i = 0; i < this.db.modules.length; i++) { + var mod = this.db.modules[i]; + methods = methods.concat(mod.getAPIMethods()); + } + return methods; +}; + +Node.prototype.getAllPublishEvents = function() { + var events = []; + for (var i = 0; i < this.db.modules.length; i++) { + var mod = this.db.modules[i]; + events = events.concat(mod.getPublishEvents()); + } + return events; +}; + Node.prototype._loadConfiguration = function(config) { var self = this; this._loadBitcoinConf(config); diff --git a/package.json b/package.json index a5cd18da..466d302f 100644 --- a/package.json +++ b/package.json @@ -29,7 +29,7 @@ "scripts": { "preinstall": "./bin/build-libbitcoind", "install": "./bin/build-bindings", - "start": "node example", + "start": "node bin/start.js", "test": "NODE_ENV=test mocha --recursive", "coverage": "istanbul cover _mocha -- --recursive", "libbitcoind": "node bin/start-libbitcoind.js" @@ -45,7 +45,9 @@ "errno": "^0.1.2", "memdown": "^1.0.0", "mkdirp": "0.5.0", - "nan": "1.3.0" + "nan": "1.3.0", + "socket.io": "^1.3.6", + "tiny": "0.0.10" }, "devDependencies": { "benchmark": "1.0.0",