Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A small exercise in writing a node.js kernel for IPython Notebook
var fs = require('fs'),
zmq_sockets = require('./zmq_sockets'),
log4js = require('log4js');
log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('/tmp/INode.log'));
var logger = log4js.getLogger("app");
function startKernel(configFile) {
logger.debug("startKernel: " + configFile);
var config = JSON.parse(fs.readFileSync(configFile, 'utf8'));
logger.debug("config: " + JSON.stringify(config));
zmq_sockets.createSocket({type: 'xrep', protocol: config.transport, ip: config.ip, port: config.hb_port});
var shell = zmq_sockets.createSocket({type: 'router', protocol: config.transport, ip: config.ip, port: config.shell_port});
shell.on('message', function (data) {
logger.debug('shell.on("message", : ' + data + ')');
logger.debug('shell.on("message") arguments:');
for (var i = 0; i < arguments.length; i++) {
logger.debug("\t" + arguments[i]);
}
var requestHeader = JSON.parse(arguments[3].toString());
var ident = "";
var delim = "<IDS|MSG>";
var signature = "";
var metadata = {};
var responseHeader ={
msg_id:1,
session:requestHeader.session,
msg_type:"execute_reply",
};
var content = JSON.stringify({
execution_count:1,
data:{
"text/plain": "undefined"
}
});
shell.send([
ident,
delim,
signature,
JSON.stringify(responseHeader),
JSON.stringify(requestHeader),
JSON.stringify(metadata),
content
]);
});
}
for (var i = 2; i < process.argv.length; i++) {
var arg = process.argv[i];
if (fs.existsSync(arg) && fs.statSync(arg).isFile) {
if (arg.match(/^.+\.json$/)) {
startKernel(arg);
}
}
}
c = get_config()
c.ZMQInteractiveShell.debug = True
c.ZMQInteractiveShell.pdb = True
c.KernelManager.kernel_cmd = ["/usr/local/bin/node", "--debug", "/Users/mksenzov/WebstormProjects/INode/app.js", "{connection_file}"]
c.Session.key = ''
c.Session.debug = True
c.Session.keyfile = ''
{
"name": "INode",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"log4js": "0.6",
"zmq": "2.5.1"
}
}
var zmq = require('zmq'),
log4js = require('log4js'),
util = require('util');
var logger = log4js.getLogger("zmq_sockets");
/* Options:
* * type - socket type [required]
* * protocol - network protocol [optional, default: tcp]
* * ip - ip address [optional, default: 127.0.0.1]
* * port - socket port [required]
*/
exports.createSocket = function (options) {
logger.debug("createSocket(" + util.inspect(options) + ')');
var socket = zmq.createSocket(options.type);
var protocol = options.protocol ? options.protocol : "tcp";
var ip = options.ip ? options.ip : "127.0.0.1";
var address = protocol + '://' + ip + ':' + options.port;
logger.info("create socket on " + address);
switch (options.type) {
case 'pub':
case 'push':
case 'rep':
case 'xrep':
case 'router':
socket.bind(address);
break;
case 'sub':
case 'pull':
case 'req':
case 'xreq':
case 'dealer':
socket.connect(address);
break;
case 'pair':
default:
throw new Exception("Unsupported socket type: " + options.type);
}
logger.info("created");
return socket;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.