Skip to content

Instantly share code, notes, and snippets.

@jadonk
Created May 11, 2010 17:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jadonk/397547 to your computer and use it in GitHub Desktop.
Save jadonk/397547 to your computer and use it in GitHub Desktop.
The main script is maintained synchronized with node.js to make sure it doesn't break with upgrades. Here is the standalone script.
#!/usr/bin/env node
var sys = require('sys');
sys.puts('Hello World!');
<html>
<head>
<title>Matrix Command Shell</title>
</head>
<body>
<h1>Matrix Command Shell</h1>
<h2>Input:</h2>
<form name="command" action="/">
Command: <input type="text" name="command" />
<input type="submit" value="Submit" />
</form>
<h2>Output:</h2>
<pre>
<span id="output"><!--%OUTPUT%--></span>
</pre>
<script type="text/javascript"><!--
var update = function(req) {
if(req.readyState == 4) {
if(req.status == 200) {
document.getElementById("output").innerHTML=req.responseText;
}
req.go();
}
}
var pollData = function() {
var httpReq = new XMLHttpRequest();
httpReq.onreadystatechange = function() { update(httpReq); };
httpReq.go = function() {
httpReq.open("GET", "/event", true);
httpReq.send();
}
httpReq.go();
};
var sendCommand = function() {
var command = document.forms['command'].command.value;
var httpReq = new XMLHttpRequest();
httpReq.open("POST", "/data?command=" + escape(command), false);
httpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
httpReq.send();
document.getElementById("output").innerHTML=httpReq.responseText;
};
document.forms['command'].onsubmit = function() {
sendCommand();
pollData();
return(false);
}
pollData();
--></script>
</body>
</html>
#!/usr/bin/env node
// Copyright (C) 2010 Texas Instruments, Jason Kridner
var sys = require('sys');
var http = require('http');
var fs = require('fs');
var url = require('url');
var child_process = require('child_process');
var path = require('path');
var events = require('events');
// Spawn child process
sys.puts('Spawning child process');
var child = child_process.spawn('cat');
var matrix = {};
matrix.data = '';
matrix.emitter = new events.EventEmitter;
child.stdout.addListener(
'data',
function (data) {
sys.puts('New data: ' + data);
matrix.data += data;
matrix.emitter.emit('data', matrix.data);
}
);
matrix.getData = function(res, wait) {
var myListener = {};
myListener = function(data) {
sys.puts("Responding");
res.writeHead(200, {"Content-Type": "text/plain"});
res.write(matrix.data + data);
res.end();
if(wait) {
matrix.emitter.removeListener('data', myListener);
}
};
if(wait) {
sys.puts("Waiting for data");
matrix.emitter.addListener('data', myListener);
setTimeout(function() {sys.puts("Timeout"); myListener('');}, 10000);
} else {
sys.puts("Not waiting");
myListener('');
}
}
// Serve web page and notify user
function loadHTMLFile(uri, res) {
var filename = path.join(process.cwd(), uri);
path.exists(
filename,
function(exists) {
if(!exists) {
res.writeHead(404, {"Content-Type": "text/plain"});
res.write("404 Not Found\n");
res.end();
return;
}
fs.readFile(
filename,
'text',
function(err, file) {
if(err) {
res.writeHead(500, {"Content-Type": "text/plain"});
res.write(err + "\n");
res.end();
return;
}
res.writeHead(200, {"Content-Type": "text/html"});
var str = ("" + file).replace("<!--%OUTPUT%-->", matrix.data);
res.write(str);
res.end();
}
);
}
);
}
sys.puts('Creating server');
var server = http.createServer(
function(req, res) {
var uri = url.parse(req.url).pathname;
sys.puts("Got request for " + uri);
var query = url.parse(req.url, true).query;
var command = false;
if(typeof(query) != 'undefined') {
//sys.puts("Request included query: " + query);
if('command' in query) {
command = query.command;
sys.puts("Query included command " + command);
child.stdin.write(command + "\n");
}
}
if(uri == '/') {
loadHTMLFile('/index.html', res);
} else if(uri == '/data') {
setTimeout(function() { matrix.getData(res, false); }, 100);
} else if(uri == '/event') {
matrix.getData(res, true);
} else {
loadHTMLFile(uri, res);
}
}
);
if(!server.listen(3000)) {
sys.puts('Server running at http://127.0.0.1:3000/');
} else {
sys.puts('Server failed to connect to socket');
}
#!/bin/sh
ssh -i ~/.ssh/beagle2.key $*
#!/bin/sh
export GIT_SSH=`pwd`/sssh
echo Setting GIT_SSH to $GIT_SSH
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment