Skip to content

Commit

Permalink
Release
Browse files Browse the repository at this point in the history
Removed Coffee Script
Added timeout to receive data with Q.delay()
  • Loading branch information
michelem09 committed Jun 15, 2016
1 parent 5ca6488 commit 58a2131
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 115 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
node-cgminer
============

Forked from: [node-cgminer](https://github.com/tlrobinson/node-cgminer)

Usage
-----

CGMiner command return a Promises/A compatible promise (specifically a [Q](https://github.com/kriskowal/q) promise):

var client = new CGMinerClient(HOST, PORT);
var client = new CGMinerClient({host: HOST, port: PORT, timeout: TIMEOUT});
client.COMMAND(ARG1, ARG2).then(function(results) {
console.log(results);
}, function(err) {
// error handler
});

COMMAND corresponds to one of the commands detailed in [CGMiner's API documentation](https://github.com/ckolivas/cgminer/blob/master/API-README).

TODO
----

* Some commands return the raw JSON response object while other return a customized object (`devs`, etc).
* Documentation.
29 changes: 0 additions & 29 deletions bin/cgminer-cli

This file was deleted.

16 changes: 0 additions & 16 deletions commands.coffee

This file was deleted.

30 changes: 30 additions & 0 deletions commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
var REGEX;

REGEX = /^([\w-]+)(?:\|([^ ]+))?( \(\*\))?[\n\s]+(\w+)((?:.|\n)*)$/;

exports.getCommands = function() {
var command, commands, docs, i, len, parsed;
docs = require("fs").readFileSync(__dirname + "/commands.txt", "utf-8");
parsed = docs.split(/\n\n /g).map(function(command) {
return command.match(REGEX);
});
commands = {};
for (i = 0, len = parsed.length; i < len; i++) {
command = parsed[i];
if (command != null) {
commands[command[1]] = {
name: command[1],
args: command[2],
privileged: !!command[3],
reply: command[4] !== "none" ? command[4] : null,
details: command[5].split("\n").map(function(l) {
return l.replace(/^\s+/, "");
}).join("\n")
};
}
}
return commands;
};

// ---
// generated by coffee-script 1.9.2
9 changes: 0 additions & 9 deletions example.coffee

This file was deleted.

47 changes: 0 additions & 47 deletions index.coffee

This file was deleted.

117 changes: 117 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
var CGMinerClient, Q, command, device, fn, fn1, i, len, name, net, ref, ref1,
slice = [].slice;

net = require("net");
Q = require("q");

CGMinerClient = (function() {
function CGMinerClient(options) {
if (options == null) {
options = {};
}
this.host = options.host || "127.0.0.1";
this.port = options.port || 4028;
this.timeout = options.timeout || 3000;
}

CGMinerClient.prototype.request = function() {
var args, command, deferred, socket;
command = arguments[0], args = 2 <= arguments.length ? slice.call(arguments, 1) : [];

deferred = Q.defer();

// Set a timeout to receive data otherwise thrown an error
Q.delay(this.timeout).then(function () {
deferred.reject(new Error("Timed out"));
});

socket = net.connect({
host: this.host,
port: this.port
});

socket.on("error", function(err) {
return deferred.reject(err);
});

socket.on("connect", function() {
var buffer;
buffer = "";

socket.on("data", function(data) {
return buffer += data.toString();
});

socket.on("end", function() {
var err;
try {
Q.when(buffer, deferred.resolve(JSON.parse(buffer.replace(/[^\}]+$/, ""))));
// return deferred.resolve(JSON.parse(buffer.replace(/[^\}]+$/, "")));
} catch (_error) {
err = _error;
return deferred.reject(err);
}
});

return socket.write(JSON.stringify({
command: command,
parameter: args.join(",")
}));
});
return deferred.promise;
};

CGMinerClient.prototype._version = function(r) {
return r.VERSION[0];
};

CGMinerClient.prototype._devs = function(r) {
return r.DEVS;
};

return CGMinerClient;

})();

ref = ["pga", "gpu", "asc"];
fn = function(device) {
var deviceUC;
deviceUC = device.toUpperCase();
CGMinerClient.prototype["_" + device] = function(r) {
return r["" + deviceUC][0];
};
return CGMinerClient.prototype["_" + device + "count"] = function(r) {
return r[deviceUC + "S"][0].Count;
};
};
for (i = 0, len = ref.length; i < len; i++) {
device = ref[i];
fn(device);
}

CGMinerClient.commands = require("./commands").getCommands();

ref1 = CGMinerClient.commands;
fn1 = function(name, command) {
return CGMinerClient.prototype[name] = function() {
var args;
args = 1 <= arguments.length ? slice.call(arguments, 0) : [];
return this.request.apply(this, [name].concat(args)).then((function(_this) {
return function(result) {
if (_this["_" + name] != null) {
return Q["try"](function() {
return _this["_" + name](result);
});
} else {
return result;
}
};
})(this));
};
};
for (name in ref1) {
command = ref1[name];
fn1(name, command);
}

module.exports = CGMinerClient;
35 changes: 28 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
{
"name": "cgminer",
"version": "0.1.0",
"version": "0.1.1",
"description": "API client for CGMiner Bitcoin miner",
"main": "index.coffee",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/tlrobinson/node-cgminer.git"
"url": "git+https://github.com/minera/node-cgminer.git"
},
"keywords": [
"bitcoin",
"cgminer",
"bfgminer",
"client",
"promises",
"promises/a",
"promises/a+",
"q"
],
"author": "Tom Robinson",
"author": {
"name": "Michele Marcucci"
},
"license": "BSD",
"dependencies": {
"q": "~0.9.6",
"coffee-script": "~1.6.3"
"q": "~0.9.6"
},
"readme": "node-cgminer\n============\n\nUsage\n-----\n\nCGMiner command return a Promises/A compatible promise (specifically a [Q](https://github.com/kriskowal/q) promise):\n\n var client = new CGMinerClient(HOST, PORT);\n client.COMMAND(ARG1, ARG2).then(function(results) {\n console.log(results);\n }, function(err) {\n // error handler\n });\n\nCOMMAND corresponds to one of the commands detailed in [CGMiner's API documentation](https://github.com/ckolivas/cgminer/blob/master/API-README).\n\nTODO\n----\n\n* Some commands return the raw JSON response object while other return a customized object (`devs`, etc).\n* Documentation.",
"readmeFilename": "README.md",
"bugs": {
"url": "https://github.com/minera/node-cgminer/issues"
},
"bin": { "cgminer-cli": "./bin/cgminer-cli" }
"_id": "[email protected]",
"_from": "cgminer@latest",
"_npmVersion": "1.2.25",
"_npmUser": {
"name": "minera",
"email": "[email protected]"
},
"maintainers": [
{
"name": "minera",
"email": "[email protected]"
}
],
"directories": {},
"homepage": "https://github.com/minera/node-cgminer#readme"
}

0 comments on commit 58a2131

Please sign in to comment.