Created
October 25, 2011 20:54
-
-
Save tomyan/1314226 to your computer and use it in GitHub Desktop.
demonstrates connections not accepted until child starts
This file contains hidden or 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 net = require('net'), | |
net_binding = process.binding('net'), | |
child_process = require('child_process'), | |
http = require('http'), | |
server; | |
if (process.argv[2]) { // child | |
server = http.createServer(function (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('Hello\n'); | |
}); | |
var stdin = new net.Stream(0, 'unix') | |
stdin.addListener('fd', function (fd) { | |
server.listenFD(fd, 'tcp4'); | |
}); | |
stdin.resume(); | |
} | |
else { // parent | |
function spawn () { | |
var control = net_binding.socketpair(); | |
var child = child_process.spawn( | |
process.argv[0], | |
[ __filename, 'x' ], | |
{ customFds: [ control[0], 1, 2 ] } | |
); | |
var childIn = new net.Stream(control[1], 'unix'); | |
return { | |
send: function (message, fd) { | |
childIn.write(message, 'ascii', fd); | |
}, | |
kill: function () { | |
child.kill(); | |
} | |
}; | |
} | |
var fd = net_binding.socket('tcp4'); | |
net_binding.bind(fd, 4242, '127.0.0.1'); | |
net_binding.listen(fd, 128); | |
process.nextTick(function () { | |
var child, | |
actions = [ | |
function () { | |
console.log('-> making first request'); | |
http.get({ host: '127.0.0.1', port: 4242, path: '/' }, function (response) { | |
console.log('<- got first response'); | |
}); | |
}, | |
function () { | |
console.log('## spawning first child'); | |
child = spawn(); | |
child.send('x', fd); | |
}, | |
function () { | |
console.log('-> making second request'); | |
http.get({ host: '127.0.0.1', port: 4242, path: '/' }, function (response) { | |
console.log('<- got second response'); | |
}); | |
}, | |
function () { | |
console.log('## killing first child'); | |
child.kill(); | |
}, | |
function () { | |
console.log('-> making third request'); | |
http.get({ host: '127.0.0.1', port: 4242, path: '/' }, function (response) { | |
console.log('<- got third response'); | |
}); | |
}, | |
function () { | |
console.log('## spawning second child'); | |
child = spawn(); | |
child.send('x', fd); | |
}, | |
function () { | |
console.log('-> making forth request'); | |
http.get({ host: '127.0.0.1', port: 4242, path: '/' }, function (response) { | |
console.log('<- got forth response'); | |
}); | |
}, | |
function () { | |
console.log('## killing second child and exiting'); | |
child.kill(); | |
process.exit(0); | |
} | |
]; | |
setInterval(function () { | |
actions.shift()(); | |
}, 1000); | |
}); | |
}; |
This file contains hidden or 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 net = require('net'), | |
child_process = require('child_process'), | |
http = require('http'), | |
server; | |
if (process.argv[2]) { // child | |
server = http.createServer(function (req, res) { | |
res.writeHead(200, {'Content-Type': 'text/plain'}); | |
res.end('Hello\n'); | |
}); | |
process.on('message', function (message, handle) { | |
server.listen(handle); | |
}); | |
} | |
else { // parent | |
server = net.createServer(); | |
server.listen(4242, '127.0.0.1', function () { | |
var child, | |
actions = [ | |
function () { | |
console.log('-> making first request'); | |
http.get({ host: '127.0.0.1', port: 4242, path: '/' }, function (response) { | |
console.log('<- got first response'); | |
}); | |
}, | |
function () { | |
console.log('## spawning first child'); | |
child = child_process.fork(__filename, [ 'x' ]); | |
child.send('x', server._handle); | |
}, | |
function () { | |
console.log('-> making second request'); | |
http.get({ host: '127.0.0.1', port: 4242, path: '/' }, function (response) { | |
console.log('<- got second response'); | |
}); | |
}, | |
function () { | |
console.log('## killing first child'); | |
child.kill(); | |
}, | |
function () { | |
console.log('-> making third request'); | |
http.get({ host: '127.0.0.1', port: 4242, path: '/' }, function (response) { | |
console.log('<- got third response'); | |
}); | |
}, | |
function () { | |
console.log('## spawning second child'); | |
child = child_process.fork(__filename, [ 'x' ]); | |
child.send('x', server._handle); | |
}, | |
function () { | |
console.log('-> making forth request'); | |
http.get({ host: '127.0.0.1', port: 4242, path: '/' }, function (response) { | |
console.log('<- got forth response'); | |
}); | |
}, | |
function () { | |
console.log('## killing second child and exiting'); | |
child.kill(); | |
process.exit(0); | |
} | |
]; | |
setInterval(function () { | |
actions.shift()(); | |
}, 1000); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment