Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
sync API with documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasMadsen committed Nov 8, 2011
1 parent 1681943 commit 5c1d481
Showing 1 changed file with 19 additions and 16 deletions.
35 changes: 19 additions & 16 deletions lib/cluster.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 });
}
}

Expand All @@ -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]);
}

Expand All @@ -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 });
}
}
}
Expand Down Expand Up @@ -304,7 +304,7 @@ cluster._setupWorker = function() {
});

//Tell master that the worker is online
cluster.worker.send({
cluster.worker.respond({
cmd: 'online',
_internal : true
});
Expand All @@ -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);

Expand All @@ -343,15 +347,14 @@ 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.
assert(cluster.isWorker);

//Send a listening message to the master
tcpSelf.once('listening', function () {
cluster.worker.send({
cluster.worker.respond({
cmd: "listening",
_internal : true,
address: address,
Expand All @@ -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);
});

Expand Down

0 comments on commit 5c1d481

Please sign in to comment.