Added basic API + first draft of miner moving methods
This commit is contained in:
parent
06e882d80e
commit
75dfeccd8c
45
lib/pool.js
45
lib/pool.js
@ -40,8 +40,17 @@ var pool = module.exports = function pool(options, authorizeFn){
|
|||||||
SetupJobManager();
|
SetupJobManager();
|
||||||
SetupVarDiff();
|
SetupVarDiff();
|
||||||
SetupDaemonInterface();
|
SetupDaemonInterface();
|
||||||
|
SetupApi();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function SetupApi() {
|
||||||
|
if (typeof(options.api) !== 'object' || typeof(options.api.start) !== 'function') {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
|
options.api.start(_this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function SetupPeer(){
|
function SetupPeer(){
|
||||||
if (!options.p2p || !options.p2p.enabled)
|
if (!options.p2p || !options.p2p.enabled)
|
||||||
return;
|
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.prototype.__proto__ = events.EventEmitter.prototype;
|
||||||
pool.daemon = daemon;
|
pool.daemon = daemon;
|
||||||
@ -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;
|
StratumClient.prototype.__proto__ = events.EventEmitter.prototype;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The actual stratum server.
|
* The actual stratum server.
|
||||||
* It emits the following Events:
|
* It emits the following Events:
|
||||||
@ -254,23 +260,28 @@ var StratumServer = exports.Server = function StratumServer(options){
|
|||||||
var stratumClients = {};
|
var stratumClients = {};
|
||||||
var subscriptionCounter = SubscriptionCounter();
|
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(){
|
(function init(){
|
||||||
_socketServer = socketServer = net.createServer({allowHalfOpen: true}, function(c){
|
_socketServer = socketServer = net.createServer({allowHalfOpen: true}, function(socket){
|
||||||
c.setKeepAlive(true);
|
handleNewClient(socket);
|
||||||
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.listen(options.port, function(){
|
_socketServer.listen(options.port, function(){
|
||||||
_this.emit('started');
|
_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;
|
StratumServer.prototype.__proto__ = events.EventEmitter.prototype;
|
||||||
Loading…
Reference in New Issue
Block a user