Created
March 17, 2014 05:59
-
-
Save tangmi/9594619 to your computer and use it in GitHub Desktop.
hw8.CampusPaths node interface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This is a node api for the UW's CSE331 hw8.CampusPath's command-line interface. | |
It probably only allows one concurrent user before weird shit happens. | |
lives in a folder called `node` in the root of your svn repo: | |
bin/ | |
doc/ | |
lib/ | |
node/ | |
campus.js | |
src/ | |
call it from the `node` folder | |
*/ | |
var path = require('path'), | |
spawn = require('child_process').spawn, | |
EventEmitter = require('events').EventEmitter; | |
var campusPaths = spawn('java', ['-cp', 'bin:lib/junit-4.11.jar', 'hw8.CampusPaths'], { | |
cwd: path.join(__dirname, '..') | |
}); | |
var cli = new EventEmitter(); | |
var buffer = ""; | |
campusPaths.stdout.on('data', function(data) { | |
//add to the buffer | |
buffer += data; | |
//if we're "done", send an event and reset the buffer | |
if (buffer.indexOf('Enter an option') !== -1) { | |
var arr = buffer.substr(0, buffer.indexOf('Enter an option')).split('\n'); | |
prune(arr); //remove empty elements | |
cli.emit('done', arr); | |
buffer = ""; | |
} | |
}); | |
function findPath(start, end, cb) { | |
run(['r', start, end], function(data) { | |
var path = []; | |
var regex = /\tWalk ([0-9]+) feet ([NESW]{1,2}) to \(([0-9]+), ([0-9]+)\)/; | |
try { | |
//format the path | |
for (var i = 1; i < data.length - 1; i++) { | |
var matches = regex.exec(data[i]); | |
path.push({ | |
coordinates: { | |
x: new Number(matches[3]), | |
y: new Number(matches[4]) | |
}, | |
length: new Number(matches[1]), | |
direction: matches[2] | |
}); | |
} | |
var out = { | |
request: { | |
start: start, | |
end: end | |
}, | |
path: path, | |
totalLength: new Number(data[data.length - 1].replace(/[^0-9]/g, '')) | |
}; | |
cb(out); | |
} catch (e) { | |
//this happens if you load when the campuspaths process is doing something else | |
cb('error, try again'); | |
} | |
}); | |
} | |
function listBuildings(cb) { | |
run(['b'], function(data) { | |
var out = []; | |
for (var i = 0; i < data.length; i++) { | |
var parts = data[i].split(': '); | |
out.push({ | |
shortName: parts[0], | |
longName: parts[1] | |
}); | |
} | |
cb(out); | |
}); | |
} | |
function killProcess(cb) { | |
campusPaths.kill('SIGINT'); | |
campusPaths.on('close', function() { | |
cb(); | |
}); | |
} | |
module.exports = { | |
path: findPath, | |
list: listBuildings, | |
close: killProcess | |
}; | |
/* util */ | |
function run(arr, cb) { | |
//this method doesn't take into account for any concurrency in campuspaths access at all | |
// i doubt more than one person can use this without weird concurrency issues | |
cli.on('done', function(data) { | |
cb(data); | |
cli.removeAllListeners('done'); | |
}); | |
campusPaths.stdin.write(arr.join('\n') + '\n'); | |
} | |
function prune(arr) { | |
while (arr.indexOf('') != -1) { | |
arr.splice(arr.indexOf(''), 1); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment