Refactored handleAuthorize to accept a new parameter used to reply or not to the socket

This commit is contained in:
Andrea Baccega 2014-03-02 15:57:06 +01:00
parent 89434f29dc
commit 1e3c47d527

View File

@ -40,7 +40,7 @@ var StratumClient = function(options){
handleSubscribe(message); handleSubscribe(message);
break; break;
case 'mining.authorize': case 'mining.authorize':
handleAuthorize(message); handleAuthorize(message, true /*reply to socket*/);
break; break;
case 'mining.submit': case 'mining.submit':
handleSubmit(message); handleSubmit(message);
@ -87,17 +87,20 @@ var StratumClient = function(options){
); );
} }
function handleAuthorize(message){ function handleAuthorize(message, replyToSocket){
_this.workerIP = options.socket.address().address; _this.workerIP = options.socket.address().address;
_this.workerName = message.params[0]; _this.workerName = message.params[0];
_this.workerPass = message.params[1]; _this.workerPass = message.params[1];
options.authorizeFn(_this.workerIP, _this.workerName, _this.workerPass, function(result) { options.authorizeFn(_this.workerIP, _this.workerName, _this.workerPass, function(result) {
_this.authorized = (!result.error && result.authorized); _this.authorized = (!result.error && result.authorized);
sendJson({
id : message.id, if (replyToSocket) {
result : _this.authorized, sendJson({
error : result.error id : message.id,
}); result : _this.authorized,
error : result.error
});
}
// If the authorizer wants us to close the socket lets do it. // If the authorizer wants us to close the socket lets do it.
if (result.disconnect === true) { if (result.disconnect === true) {
@ -235,7 +238,7 @@ var StratumClient = function(options){
}; };
this.manuallyInitClient = function (username, password) { this.manuallyInitClient = function (username, password) {
handleAuthorize({id: 1, params: [username, password]}); handleAuthorize({id: 1, params: [username, password]}, false /*do not reply to miner*/);
handleSubscribe({id: 2}); handleSubscribe({id: 2});
} }
}; };