Created
February 22, 2015 10:58
-
-
Save apaleslimghost/651e8484147bdfbff660 to your computer and use it in GitHub Desktop.
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
function modulo(a, b) { return (+a % (b = +b) + b) % b; }; | |
var h = require("highland"); | |
var net = require("net"); | |
var HOST = "127.0.0.1"; | |
function createServer(port) { | |
net.createServer(function(sock) { | |
if (port === 7001) { | |
setInterval((function() { | |
return sock.write("700" + ((Math.random() * 3 | 0) + 2) + "\r\n"); | |
}), 0); | |
return; | |
} | |
sock.on("data", function(data) { | |
if ((Math.random() * 3 | 0) + 1 === 3) { | |
sock.write("wait 10 seconds and retry\n"); | |
} else { | |
sock.write((data * 2) + "\n"); | |
} | |
return sock.close; | |
}); | |
}).listen(port, HOST); | |
console.log("server listening on " + port); | |
}; | |
for (var i = 7001; i <= 7004; ++i) { | |
createServer(i); | |
} | |
var g = net.createConnection(7001, HOST); | |
var tots = { | |
7002: 0, | |
7003: 0, | |
7004: 0 | |
}; | |
var portcnt = { | |
7002: 0, | |
7003: 0, | |
7004: 0 | |
}; | |
g.on("data", function(data) { | |
var port = parseInt(data); | |
if (isNaN(port)) { | |
return; | |
} | |
var w = net.createConnection(port, "127.0.0.1"); | |
w.theport = port; | |
w.write("50\n"); | |
w.on("data", function(d) { | |
var p = w.theport; | |
var ans = parseInt(d.toString()); | |
if (isNaN(ans)) { | |
w.end(); | |
return; | |
} | |
tots[p] += ans; | |
portcnt[p] += 1; | |
if (tots[p] % 10000 === 0) { | |
console.log("tots " + p + ": " + tots[p]); | |
} | |
if (portcnt[p] % 1000 === 0) { | |
console.log("portcnt " + p + ": " + portcnt[p]); | |
} | |
w.end(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment