From 75dfeccd8c15468a00a1d3f979bfa3c9bb78a55b Mon Sep 17 00:00:00 2001 From: Andrea Baccega Date: Sat, 1 Mar 2014 20:04:13 +0100 Subject: [PATCH] Added basic API + first draft of miner moving methods --- lib/pool.js | 45 +++++++++++++++++++++++++++++++++++++++ lib/stratum.js | 57 ++++++++++++++++++++++++++++++++++++-------------- 2 files changed, 86 insertions(+), 16 deletions(-) diff --git a/lib/pool.js b/lib/pool.js index f807420..3d25b67 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -40,8 +40,17 @@ var pool = module.exports = function pool(options, authorizeFn){ SetupJobManager(); SetupVarDiff(); SetupDaemonInterface(); + SetupApi(); }; + function SetupApi() { + if (typeof(options.api) !== 'object' || typeof(options.api.start) !== 'function') { + return; + } else { + options.api.start(_this); + } + } + function SetupPeer(){ if (!options.p2p || !options.p2p.enabled) return; @@ -447,6 +456,42 @@ var pool = module.exports = function pool(options, authorizeFn){ } } + this.relinquishMiners = function(filterFn, resultCback) { + var origStratumClients = this.stratumServer.getStratumClients(); + + var stratumClients = []; + Object.keys(origStratumClients).forEach(function (subId) { + stratumClients.push({subId: subId, client: origStratumClients[subId]}); + }); + async.filter( + stratumClients, + filterFn, + function (clientsToRelinquish) { + clientsToRelinquish.forEach(function(cObj) { + cObj.client.removeAllListeners(); + cObj.client.socket.removeAllListeners(); + _this.stratumServer.removeStratumClientBySubId(cObj.subId); + }); + + process.nextTick(function () { + resultCback( + clientsToRelinquish.map( + function (item) { + return item.client; + } + ) + ); + }); + } + ) + }; + + this.attachMiners = function(miners) { + miners.forEach(function (clientObj) { + _this.stratumServer.manuallyAddStratumClient(clientObj); + }); + } + }; pool.prototype.__proto__ = events.EventEmitter.prototype; pool.daemon = daemon; \ No newline at end of file diff --git a/lib/stratum.js b/lib/stratum.js index 0303323..568c4fe 100644 --- a/lib/stratum.js +++ b/lib/stratum.js @@ -233,11 +233,17 @@ var StratumClient = function(options){ }); }; + + this.manuallyInitClient = function (username, password) { + handleAuthorize({id: 1, params: [username, password]}); + handleSubscribe({id: 2}); + } }; StratumClient.prototype.__proto__ = events.EventEmitter.prototype; + /** * The actual stratum server. * It emits the following Events: @@ -254,23 +260,28 @@ var StratumServer = exports.Server = function StratumServer(options){ var stratumClients = {}; var subscriptionCounter = SubscriptionCounter(); + var handleNewClient = function (socket) { + c.setKeepAlive(true); + var subscriptionId = subscriptionCounter.next(); + var client = new StratumClient( + { + subscriptionId : subscriptionId, + socket : socket, + authorizeFn : options.authorizeFn + } + ); + stratumClients[subscriptionId] = client; + _this.emit('client.connected', client); + client.on('socketDisconnect', function() { + _this.removeStratumClientBySubId(subscriptionId); + _this.emit('client.disconnected', client); + }); + return subscriptionId; + } + (function init(){ - _socketServer = socketServer = net.createServer({allowHalfOpen: true}, function(c){ - c.setKeepAlive(true); - var subscriptionId = subscriptionCounter.next(); - var client = new StratumClient( - { - subscriptionId : subscriptionId, - socket : c, - authorizeFn : options.authorizeFn - } - ); - stratumClients[subscriptionId] = client; - _this.emit('client.connected', client); - client.on('socketDisconnect', function() { - delete stratumClients[subscriptionId]; - _this.emit('client.disconnected', client); - }); + _socketServer = socketServer = net.createServer({allowHalfOpen: true}, function(socket){ + handleNewClient(socket); }); _socketServer.listen(options.port, function(){ _this.emit('started'); @@ -289,5 +300,19 @@ var StratumServer = exports.Server = function StratumServer(options){ } } }; + + this.getStratumClients = function () { + return stratumClients; + }; + + this.removeStratumClientBySubId = function (subscriptionId) { + delete stratumClients[subscriptionId]; + }; + + this.manuallyAddStratumClient = function(clientObj) { + var subId = handleNewClient(clientObj.socket); + stratumClients[subscriptionId].manuallyInit(clientObj.workerName, clientObj.workerPass); + } + }; StratumServer.prototype.__proto__ = events.EventEmitter.prototype; \ No newline at end of file