From 5c1d4816d2122bdc986b6409d4cea743ed65b3c3 Mon Sep 17 00:00:00 2001 From: Andreas Madsen Date: Tue, 8 Nov 2011 10:36:58 +0100 Subject: [PATCH] sync API with documentation --- lib/cluster.js | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/lib/cluster.js b/lib/cluster.js index 748426ceff3..e4f74e2cedd 100644 --- a/lib/cluster.js +++ b/lib/cluster.js @@ -59,7 +59,7 @@ cluster.isWorker = 'NODE_WORKER_ID' in process.env; cluster.isMaster = ! cluster.isWorker; //The worker object is only used in a worker -cluster.worker = cluster.isWorker ? {} : null; +cluster.worker = cluster.isWorker ? new EventEmitter() : null; //The workers array is oly used in the naster cluster.workers = cluster.isMaster ? [] : null; @@ -134,7 +134,7 @@ function handleWorkerMessage(message, worker) { //echo callback id, if one was requested if (message._queryId) { - worker.send({ _queryId: message._queryId }); + worker.send({ _internal: true, _queryId: message._queryId }); } } @@ -155,7 +155,7 @@ function handleWorkerMessage(message, worker) { } //echo callback id, with the fd handler associated with it - var response = { _queryId: message._queryId }; + var response = { _internal: true, _queryId: message._queryId }; worker.send(response, servers[key]); } @@ -172,14 +172,14 @@ function handleWorkerMessage(message, worker) { //echo callback id, if one was requested if (message._queryId) { - worker.send({ _queryId: message._queryId }); + worker.send({ _internal: true, _queryId: message._queryId }); } } //echo callback id, if one was requested else { if (message._queryId) { - worker.send({ _queryId: message._queryId }); + worker.send({ _internal: true, _queryId: message._queryId }); } } } @@ -304,7 +304,7 @@ cluster._setupWorker = function() { }); //Tell master that the worker is online - cluster.worker.send({ + cluster.worker.respond({ cmd: 'online', _internal : true }); @@ -313,19 +313,23 @@ cluster._setupWorker = function() { process.on('message', function(msg, handle) { debug("recv " + JSON.stringify(msg)); - if (msg._queryId && msg._queryId in queryCallbacks) { - var cb = queryCallbacks[msg._queryId]; - if (typeof cb == 'function') { - cb(msg, handle); + if (msg._internal && msg._internal === true) { + if (msg._queryId && msg._queryId in queryCallbacks) { + var cb = queryCallbacks[msg._queryId]; + if (typeof cb == 'function') { + cb(msg, handle); + } + delete queryCallbacks[msg._queryId]; } - delete queryCallbacks[msg._queryId]; + } else { + cluster.worker.emit("message", msg); } }); }; -//Send message to the master, and run callback when the master echo +// Send message to the master, and run callback when the master echo if (cluster.isWorker) { - cluster.worker.send = function (msg, cb) { + cluster.worker.respond = function (msg, cb) { // This can only be called from a worker. assert(cluster.isWorker); @@ -343,7 +347,6 @@ if (cluster.isWorker) { }; } - // Internal function. Called by lib/net.js when attempting to bind a server. cluster._getServer = function(tcpSelf, address, port, addressType, cb) { // This can only be called from a worker. @@ -351,7 +354,7 @@ cluster._getServer = function(tcpSelf, address, port, addressType, cb) { //Send a listening message to the master tcpSelf.once('listening', function () { - cluster.worker.send({ + cluster.worker.respond({ cmd: "listening", _internal : true, address: address, @@ -369,7 +372,7 @@ cluster._getServer = function(tcpSelf, address, port, addressType, cb) { addressType: addressType }; //The callback will be stored until the master has responed - cluster.worker.send(message, function(msg, handle) { + cluster.worker.respond(message, function(msg, handle) { cb(handle); });