Permalink
Browse files

Merge branch 'master' of github.com:dvv/stereo

Conflicts:
	index.coffee
	index.js
  • Loading branch information...
2 parents 10a8b45 + ccdc682 commit 0b062ecb3b1917bcb2e9ce06bc640b4fbfe74132 @dvv committed Mar 15, 2011
Showing with 58 additions and 35 deletions.
  1. +1 −0 .gitignore
  2. +2 −2 Readme.md
  3. +13 −13 index.coffee
  4. +23 −20 index.js
  5. +19 −0 test.js
View
@@ -0,0 +1 @@
+.ipc
View
@@ -8,7 +8,7 @@
server.js:
- var server = require('stereo')({
+ var server = require('stereo')(null, {
port: 3000,
repl: true,
workers: 4
@@ -20,7 +20,7 @@ server.js:
process.on('message', function(message){...});
} else { // master process
// broadcast a message
- process.publish({sos: 'to all, all, all'});
+ setTimeout(function(){process.publish({sos: 'to all, all, all'});}, 2000);
}
## Authors
View
@@ -31,7 +31,7 @@ framing = (chunk) ->
node cluster factory, takes options:
-options.host - host to bind server to = *
+options.host - host to bind server to = '0.0.0.0'
options.port - port to bind server to = 80
options.connections - listener capacity = 1024
@@ -68,13 +68,14 @@ options.ssl.cert
options.ssl.caCerts
###
-module.exports = (options = {}) ->
+module.exports = (server, options = {}) ->
net = require 'net'
fs = require 'fs'
# options
options.port ?= 3000
+ options.host ?= '0.0.0.0'
nworkers = options.workers or require('os').cpus().length
options.ipc ?= '.ipc'
@@ -95,18 +96,17 @@ module.exports = (options = {}) ->
#
# setup HTTP(S) server
- #
- if options.ssl
- credentials =
- key: fs.readFileSync options.ssl.key, 'utf8'
- cert: fs.readFileSync options.ssl.cert, 'utf8'
- #ca: options.ssl.caCerts.map (fname) -> fs.readFileSync fname, 'utf8'
- server = require('https').createServer credentials
- else
- server = require('http').createServer()
- #
# N.B. request handler to be attached elsewhere
#
+ unless server
+ if options.ssl
+ credentials =
+ key: fs.readFileSync options.ssl.key, 'utf8'
+ cert: fs.readFileSync options.ssl.cert, 'utf8'
+ #ca: options.ssl.caCerts.map (fname) -> fs.readFileSync fname, 'utf8'
+ server = require('https').createServer credentials
+ else
+ server = require('http').createServer()
#
# setup signals
@@ -215,7 +215,7 @@ module.exports = (options = {}) ->
#
netBinding = process.binding 'net'
socket = netBinding.socket 'tcp' + (if netBinding.isIP(options.host) is 6 then 6 else 4)
- netBinding.bind socket, options.port
+ netBinding.bind socket, options.port, options.host
netBinding.listen socket, options.connections or 1024
#
View
@@ -29,7 +29,7 @@ framing = function(chunk) {
node cluster factory, takes options:
-options.host - host to bind server to = *
+options.host - host to bind server to = '0.0.0.0'
options.port - port to bind server to = 80
options.connections - listener capacity = 1024
@@ -66,31 +66,34 @@ options.ssl.cert
options.ssl.caCerts
*/
-module.exports = function(options) {
- var REPL, args, cmd, comm, credentials, env, fs, ipc, k, net, netBinding, nworkers, server, socket, spawnWorker, v, watch, workers, _ref, _ref2, _ref3, _ref4;
+module.exports = function(server, options) {
+ var REPL, args, cmd, comm, credentials, env, fs, ipc, k, net, netBinding, nworkers, socket, spawnWorker, v, watch, workers, _ref, _ref2, _ref3, _ref4, _ref5;
if (options == null) {
options = {};
}
net = require('net');
fs = require('fs');
(_ref = options.port) != null ? _ref : options.port = 3000;
+ (_ref2 = options.host) != null ? _ref2 : options.host = '0.0.0.0';
nworkers = options.workers || require('os').cpus().length;
- (_ref2 = options.ipc) != null ? _ref2 : options.ipc = '.ipc';
+ (_ref3 = options.ipc) != null ? _ref3 : options.ipc = '.ipc';
if (process.env._NODE_WORKER_FOR_) {
process.log = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
args[0] = ("" + (Date.now()) + " WORKER " + process.pid + ": ") + args[0];
return console.error.apply(console, args);
};
- if (options.ssl) {
- credentials = {
- key: fs.readFileSync(options.ssl.key, 'utf8'),
- cert: fs.readFileSync(options.ssl.cert, 'utf8')
- };
- server = require('https').createServer(credentials);
- } else {
- server = require('http').createServer();
+ if (!server) {
+ if (options.ssl) {
+ credentials = {
+ key: fs.readFileSync(options.ssl.key, 'utf8'),
+ cert: fs.readFileSync(options.ssl.cert, 'utf8')
+ };
+ server = require('https').createServer(credentials);
+ } else {
+ server = require('http').createServer();
+ }
}
if (options.workerShutdownTimeout) {
process.on('SIGQUIT', function() {
@@ -149,7 +152,7 @@ module.exports = function(options) {
};
netBinding = process.binding('net');
socket = netBinding.socket('tcp' + (netBinding.isIP(options.host) === 6 ? 6 : 4));
- netBinding.bind(socket, options.port);
+ netBinding.bind(socket, options.port, options.host);
netBinding.listen(socket, options.connections || 1024);
if (process.getuid() === 0) {
if (options.uid) {
@@ -165,18 +168,18 @@ module.exports = function(options) {
workers = {};
args = options.args || process.argv;
env = {};
- _ref3 = process.env;
- for (k in _ref3) {
- if (!__hasProp.call(_ref3, k)) continue;
- v = _ref3[k];
- env[k] = v;
- }
- _ref4 = options.env || {};
+ _ref4 = process.env;
for (k in _ref4) {
if (!__hasProp.call(_ref4, k)) continue;
v = _ref4[k];
env[k] = v;
}
+ _ref5 = options.env || {};
+ for (k in _ref5) {
+ if (!__hasProp.call(_ref5, k)) continue;
+ v = _ref5[k];
+ env[k] = v;
+ }
spawnWorker = function() {
var worker;
env._NODE_WORKER_FOR_ = process.pid;
View
19 test.js
@@ -0,0 +1,19 @@
+var server = require('./')(null, {
+ port: 3000,
+ repl: true,
+ workers: 4
+});
+if (server) { // worker process
+ // HTTP(S) server instance for further tuning
+ server.on('request', function(req, res){
+ res.writeHead(200, {'Content-Type': 'text/plain'});
+ res.end('HELLO FROM WORKER ' + process.pid);
+ });
+ // inter-workers message arrives
+ process.on('message', function(message){
+ console.log(JSON.stringify(message));
+ });
+} else { // master process
+ // broadcast a message
+ setTimeout(function(){process.publish({sos: 'to all, all, all'});}, 2000);
+}

0 comments on commit 0b062ec

Please sign in to comment.