Permalink
Browse files

Added log function activable with activateLog method and modified exa…

…mples to run in console.
  • Loading branch information...
1 parent 5299605 commit 93b832b1b9aaa09fb7e317120a96b83cb6d26c74 @albertosantini committed Sep 15, 2011
Showing with 67 additions and 42 deletions.
  1. +1 −0 .gitignore
  2. +7 −9 README.markdown
  3. +1 −1 examples/ex1.js
  4. +6 −1 examples/ex2.R
  5. +12 −6 examples/ex2.js
  6. +40 −25 lib/rio.js
View
@@ -0,0 +1 @@
+node_modules/
View
@@ -59,8 +59,6 @@ Notes
- Adding a better error handling if the communication fails.
-- Handling debug ouput
-
- If the authentication fails, the callback is not called
Methods
@@ -74,7 +72,9 @@ disconnecting.
The argument of the callback is false if there is any error.
- default options = {
+The defaults for the options parameter:
+
+ options = {
callback: function (res) { if (res !== false) sys.puts(res); },
host = "127.0.0.1",
port = 6311,
@@ -86,13 +86,11 @@ The argument of the callback is false if there is any error.
sourceAndEval(filename, options)
-------------
-It loads the content of a file and call "evaluate" method.
-
-An options parameter example:
+It loads the content of a R file and calls the "evaluate" method, merging,
+finally, the options parameter:
options = {
- entryPoint: "main" // after loading the file, entryPoint would be called
- data: { foo: "bar" } // after loading the file, data would be
- // stringified and would passed to the entryPoint
+ entryPoint: "main", // entryPoint is called
+ data: { foo: "bar" } // data is stringified and passed to entryPoint
}
View
@@ -1,6 +1,6 @@
/*jslint node:true sloppy:true */
-var rio = require('rio');
+var rio = require('../lib/rio');
rio.evaluate("pi / 2 * 2");
rio.evaluate('c(1, 2)');
View
@@ -45,4 +45,9 @@ getOptimalPortfolio <- function (jsonObj) {
}
# args <- '{"prods":["IBM","YHOO","MSFT"]}';
-# print(getOptimalPortfolio(args))
+# res <- getOptimalPortfolio(args)
+# fromJSON(res)$pw
+
+# Optimal weights: 0.27107,0.2688,0.46013
+
+
View
@@ -1,19 +1,25 @@
/*jslint node:true sloppy:true nomen:true */
var sys = require('sys'),
- rio = require('rio');
+ rio = require('../lib/rio');
+
+var args = {
+ prods: ["IBM", "YHOO", "MSFT"]
+};
function displayResponse(res) {
+ var i;
+
if (res !== false) {
res = JSON.parse(res);
- sys.puts("Optimal weights: " + res.pw);
+ for (i = 0; i < args.prods.length; i += 1) {
+ sys.puts("Optimal weight for " + args.prods[i] +
+ " is " + res.pw[i]);
+ }
+ // Optimal weights: 0.27107,0.2688,0.46013
}
}
-var args = {
- prods: ["IBM", "YHOO", "MSFT"]
-};
-
rio.sourceAndEval(__dirname + "/ex2.R", {
entryPoint: "getOptimalPortfolio",
data: args,
View
@@ -6,6 +6,21 @@ var sys = require("sys"),
hexy = require('hexy'),
jspack = require('./jspack.js').jspack;
+var debug = false;
+
+function activateLog(isDebug) {
+ debug = isDebug;
+}
+exports.activateLog = activateLog;
+
+function log(o) {
+ if (debug && typeof (o) === "object") {
+ console.log(hexy.hexy(o));
+ } else if (debug && typeof (o) === "string") {
+ sys.puts(o);
+ }
+}
+
var CMD_TYPE = {
LOGIN: 1,
EVAL: 3
@@ -66,10 +81,10 @@ function parse_SEXP(buf, offset) {
offset = i + rl;
eoa = offset;
if ((ra & 64) === 64) {
- sys.puts("sorry, long packets are not supported (yet).");
+ log("sorry, long packets are not supported (yet).");
}
- sys.puts("Type SEXP " + ra);
+ log("Type SEXP " + ra);
if (ra === 33) { // double array
while (i < eoa) {
@@ -95,7 +110,7 @@ function parse_SEXP(buf, offset) {
}
return res;
} else {
- sys.puts("Type " + ra + " is currently not implemented");
+ log("Type " + ra + " is currently not implemented");
}
return false;
@@ -109,7 +124,7 @@ function isResponseOk(data, offset) {
rr = res & 255;
if (rr !== 1) {
- sys.puts("Response with error code " + sc);
+ log("Response with error code " + sc);
return false;
}
@@ -121,12 +136,12 @@ function parsePacket(data, offset, callback) {
if (isResponseOk(data, offset)) {
if (int8(data, offset - 4) === 10) {
- sys.puts("Valid response (expecting SEXP)");
+ log("Valid response (expecting SEXP)");
res = parse_SEXP(data, offset);
- sys.puts("Response value: " + res);
+ log("Response value: " + res);
callback(res);
} else {
- sys.puts("Invalid response (expecting SEXP)");
+ log("Invalid response (expecting SEXP)");
callback(false);
}
}
@@ -135,29 +150,29 @@ function parsePacket(data, offset, callback) {
function dataHandler(data, callback) {
var n = data.length, rs = data.toString("ascii", 0, 4), rv, cap;
- sys.puts("Received data from Rserve");
+ log("Received data from Rserve");
- if (rs === "Rsrv" && n === 32) {
+ if (rs === "Rsrv" && n === 32) { // protocol packet
if (n === 32) {
- sys.puts("Valid header");
+ log("Valid header");
rv = data.toString("ascii", 4, 8);
if (rv !== "0103") {
- sys.puts("Unsupported protocol version " + rv);
+ log("Unsupported protocol version " + rv);
} else {
- sys.puts("Supported protocol version 0103");
+ log("Supported protocol version 0103");
cap = data.toString("ascii", 16, 32);
- sys.puts("Supported capabilities " + cap);
+ log("Supported capabilities " + cap);
CAPABILITIES.AUTH = false;
CAPABILITIES.CRYPT = false;
if (cap.search("ARpt") !== -1) {
- sys.puts("Authentication required and not crypted");
+ log("Authentication required and not crypted");
CAPABILITIES.AUTH = true;
}
if (cap.search("ARuc") !== -1) {
- sys.puts("Authentication required and crypted");
+ log("Authentication required and crypted");
CAPABILITIES.AUTH = true;
CAPABILITIES.CRYPT = true;
}
@@ -166,7 +181,7 @@ function dataHandler(data, callback) {
} else if (rs === "Rsrv" && n > 32) { // packet with header
parsePacket(data, 32 + 20, callback);
} else if (n === 16) { // packet login
- sys.puts("Login response");
+ log("Login response");
if (!isResponseOk(data, 20)) {
callback(false);
}
@@ -176,7 +191,7 @@ function dataHandler(data, callback) {
isReadyToSendCommands = true;
- console.log(hexy.hexy(data));
+ log(data);
}
function connect(host, port, callback) {
@@ -188,25 +203,25 @@ function connect(host, port, callback) {
client = net.createConnection(port, host);
client.on("connect", function () {
- sys.puts("Connected to Rserve");
+ log("Connected to Rserve");
});
client.on("data", function (data) {
dataHandler(data, callback);
});
client.on("end", function () {
- sys.puts("Disconnected from Rserve");
+ log("Disconnected from Rserve");
});
client.on("close", function (had_error) {
- sys.puts("Closed from Rserve");
+ log("Closed from Rserve");
callback(false); // hack for auth failed
});
client.on("error", function (e) {
- sys.puts("Rserve exception - " + e);
+ log("Rserve exception - " + e);
});
return client;
@@ -236,8 +251,8 @@ function sendCommand(conn, cmdType, cmd, len, desc) {
mkint24(n, buf, 17);
buf.write(cmd, 20);
- sys.puts(desc);
- console.log(hexy.hexy(buf));
+ log(desc);
+ log(buf);
conn.write(buf);
@@ -263,7 +278,7 @@ function evaluate(cmd, options) {
conn = connect(host, port, callback);
timer = setInterval(function () {
- sys.puts("Waiting loop for sending commands");
+ log("Waiting loop for sending commands");
if (isReadyToSendCommands) {
clearInterval(timer);
if (CAPABILITIES.AUTH) {
@@ -287,7 +302,7 @@ exports.evaluate = evaluate;
function sourceAndEval(filename, options) {
options = options || {};
- fs.readFile(filename, "utf8", function (err, source) {
+ fs.readFile(filename, "ascii", function (err, source) {
var cmd;
if (!err) {

0 comments on commit 93b832b

Please sign in to comment.