Added basic API + first draft of miner moving methods

This commit is contained in:
Andrea Baccega 2014-03-01 20:04:13 +01:00
parent 06e882d80e
commit 75dfeccd8c
2 changed files with 86 additions and 16 deletions

View File

@ -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;

View File

@ -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;