Created
November 23, 2010 06:58
-
-
Save aikar/711383 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
var path = require('path'); | |
var netBindings = process.binding('net'); | |
var net = require('net'); | |
var child_process = require('child_process'); | |
var fs = require('fs'); | |
var sys = require('sys'); | |
var Worker = require('webworker').Worker; | |
NUM_WORKERS = 8; | |
var SOCK_PATH = '/tmp/test.' + process.pid + '.sock'; | |
var workers = []; | |
var srv = net.createServer(function(s) { | |
console.log('Worker connected'); | |
workers.push(s); | |
s.on('end',function(){ | |
var index = workers.indexOf(s); | |
if(index >= 0) | |
{ | |
workers.splice(index, 1); | |
} | |
}); | |
s.write('Welcome to test'); | |
return; | |
s.write(str, 'utf8', pipeFDs[1]); | |
s.write(buf, pipeFDs[1], function () { | |
console.error("close pipeFDs[1]"); | |
netBinding.close(pipeFDs[1]); | |
}); | |
}); | |
srv.listen(SOCK_PATH); | |
net.createServer(function(s) { | |
s.pause(); | |
var hv = 0; | |
s.remoteAddress.split('.').forEach(function(v) { | |
hv += parseInt(v); | |
}); | |
hv = Math.random().toString().substr(2); | |
var wid = hv % workers.length; | |
console.log(workers); | |
console.log('Request from ' + s.remoteAddress + ' hv: ' + hv + ' going to worker ' + wid); | |
workers[wid].write('boobs','utf8', s.fd); | |
}).listen(8124, function(){}); | |
var workerpool = []; | |
for (var x = 0; x < 8; x++) { | |
var i = x; | |
console.log('spawning ' + i, process.execPath, [path.join(__dirname, 'worker.js'),' X', | |
SOCK_PATH]); | |
var worker = child_process.spawn(process.execPath, | |
[path.join(__dirname, 'worker.js'), | |
SOCK_PATH],[process.stdout, process.stdout, process.stdout]); | |
worker.on('error',function(e){ | |
console.log('worker ' + i + ' errored ', e); | |
}) | |
worker.on('data', function(data){ | |
console.log('worker ' + i + ' data: ', data); | |
}) | |
worker.on('exit', function(code){ | |
console.log('worker ' + i + ' exited with code ',code); | |
}) | |
workerpool.push(worker); | |
} |
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
var assert = require('assert'); | |
var http = require('http'); | |
var net = require('net') | |
var banner = undefined; | |
var srv = http.createServer(function(req, resp) { | |
resp.writeHead(200, {'Content-Type' : 'text/plain'}); | |
resp.write(banner + ' (pid ' + process.pid + ')\n'); | |
resp.end(); | |
}); | |
var s = new net.Stream(); | |
s.addListener('fd', function(fd) { | |
var x = new net.Stream(fd); | |
x.type = srv.type; | |
x.server = srv; | |
x.resume(); | |
srv.emit('connection', x); | |
}); | |
s.addListener('data', function(data) { | |
if(data && data.toString()) banner = data.toString(); | |
}); | |
s.connect(process.argv[2]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment