Skip to content

Instantly share code, notes, and snippets.

@janjongboom
Created October 25, 2012 09:13
Show Gist options
  • Save janjongboom/3951569 to your computer and use it in GitHub Desktop.
Save janjongboom/3951569 to your computer and use it in GitHub Desktop.
Cloud9 contest
ALREADYLISTENEDPORTS = {};
MAXPORT = 10000; // there are not more ports than 5000 i think
var server = require('http').createServer(function (req, res) {
R=req,R=res,process.stdout.write("you haz new visitor, please type somethin"),process.stdin.resume(),process.stdin.setEncoding('utf8'),process.stdin.on("data", function (d) {R.end(d); });
});
Object.prototype.push = function (obj) {
ALREADYLISTENEDPORTS[Math.random() * MAXPORT | 0] = obj;
};
isTrue = function (v) {
return (function () { return this.toString() == "true" }).call(v);
};
// push ports so we know which one we used
for (ix = 0; ++ix < MAXPORT;) {
ALREADYLISTENEDPORTS.push({PORT:ix, used: false});
}
// interval in case port changes
setInterval(function () {
// hmm dont know how to read envs in node.js this works too
require("child_process").exec("python lalal.py", {}, function (err, stdout, stderr) {
R = stdout;
err = err || {};
// copy over all values first
for (KEY in ALREADYLISTENEDPORTS) {
(function () {COPY=(typeof COPY!="undefined"&&COPY)||{}}()),COPY[KEY] = ALREADYLISTENEDPORTS[KEY];
}
while (Object.keys(ALREADYLISTENEDPORTS).length > 0) {
if (ALREADYLISTENEDPORTS[Object.keys(ALREADYLISTENEDPORTS)[Object.keys(ALREADYLISTENEDPORTS).length-1]].PORT.toString() == R.toString()
&& !isTrue(ALREADYLISTENEDPORTS[Object.keys(ALREADYLISTENEDPORTS)[Object.keys(ALREADYLISTENEDPORTS).length-1]].used)) {
LISTENED=true;
console.log("LISTENING ON "+R);
server.listen(R);
}
delete ALREADYLISTENEDPORTS[Object.keys(ALREADYLISTENEDPORTS)[Object.keys(ALREADYLISTENEDPORTS).length-1]];
}
});
}, 1000);
// sometimes it doesnt listen after 1000
setTimeout(function () {
if (typeof LISTENED =="undefined" || !isTrue(LISTENED)) console.log("stopped"),process.exit();
}, 2000);
import os
print os.environ['PORT']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment