From f47dc3d1a003b7be114b47450f25e4cbff7a0f39 Mon Sep 17 00:00:00 2001 From: Nick Kotenberg Date: Thu, 3 Aug 2017 16:42:28 -0600 Subject: [PATCH] Allow workers to respond to IPC messages from master --- index.js | 61 ++- lib/scworker.js | 56 ++- lib/workercluster.js | 3 + package-lock.json | 655 ++++++++++++++++++++++++++++++++ package.json | 4 + test/internal/serverToWorker.js | 42 ++ test/internal/worker.js | 7 + 7 files changed, 819 insertions(+), 9 deletions(-) create mode 100644 package-lock.json create mode 100644 test/internal/serverToWorker.js diff --git a/index.js b/index.js index c66fb8c6..e1e94e7d 100644 --- a/index.js +++ b/index.js @@ -2,11 +2,11 @@ var path = require('path'); var crypto = require('crypto'); var EventEmitter = require('events').EventEmitter; var domain = require('sc-domain'); +var uuid = require('uuid'); var fork = require('child_process').fork; var os = require('os'); var fs = require('fs-extra'); var uidNumber = require('uid-number'); -var uuid = require('uuid'); var pkg = require('./package.json'); var argv = require('minimist')(process.argv.slice(2)); var cluster = require('cluster'); @@ -17,6 +17,7 @@ var InvalidActionError = scErrors.InvalidActionError; var BrokerError = scErrors.BrokerError; var ProcessExitError = scErrors.ProcessExitError; var UnknownError = scErrors.UnknownError; +var TimeoutError = scErrors.TimeoutError; var decycle = scErrors.decycle; var socketClusterSingleton = null; @@ -42,6 +43,8 @@ var SocketCluster = function (options) { self.EVENT_WORKER_CLUSTER_READY = 'workerClusterReady'; self.EVENT_WORKER_CLUSTER_EXIT = 'workerClusterExit'; + self._pendingResponseHandlers = {}; + self._errorAnnotations = { 'EADDRINUSE': 'Failed to bind to a port because it was already used by another process.' }; @@ -89,6 +92,7 @@ SocketCluster.prototype._init = function (options) { logLevel: 2, handshakeTimeout: 10000, ackTimeout: 10000, + ipcAckTimeout: 10000, pingInterval: 8000, pingTimeout: 20000, origins: '*:*', @@ -147,6 +151,7 @@ SocketCluster.prototype._init = function (options) { }; verifyDuration('ackTimeout'); + verifyDuration('ipcAckTimeout'); verifyDuration('pingInterval'); verifyDuration('pingTimeout'); verifyDuration('workerStatusInterval'); @@ -635,7 +640,18 @@ SocketCluster.prototype._launchWorkerCluster = function () { } else if (m.type == 'workerExit') { self._workerExitHandler(m.data); } else if (m.type == 'workerMessage') { - self.emit('workerMessage', m.workerId, m.data); + self.emit('workerMessage', m.workerId, m.data, function (data) { + if (m.cid) { + self.respondToWorker(data, m.workerId, m.cid); + } + }); + } else if (m.type == 'workerResponse') { + var responseHandler = self._pendingResponseHandlers[m.rid]; + if (responseHandler) { + clearTimeout(responseHandler.timeout); + delete self._pendingResponseHandlers[m.rid]; + responseHandler.callback(null, m.data, m.workerId); + } } }); @@ -648,6 +664,14 @@ SocketCluster.prototype._launchWorkerCluster = function () { this.workerCluster.on('exit', this._handleWorkerClusterExit.bind(this)); }; +SocketCluster.prototype.respondToWorker = function (data, workerId, rid) { + this.workerCluster.send({ + type: 'masterResponse', + data: data, + rid: rid + }); +}; + SocketCluster.prototype._logDeploymentDetails = function () { if (this.options.logLevel > 0) { console.log(' ' + this.colorText('[Active]', 'green') + ' SocketCluster started'); @@ -734,15 +758,40 @@ SocketCluster.prototype._start = function () { launchBrokerEngine(); }; -SocketCluster.prototype.sendToWorker = function (workerId, data) { - this.workerCluster.send({ +SocketCluster.prototype._createIPCResponseHandler = function (callback) { + var cid = uuid.v4(); + + var responseTimeout = setTimeout(function () { + var responseHandler = self._pendingResponseHandlers[cid]; + delete self._pendingResponseHandlers[cid]; + var timeoutError = new TimeoutError('IPC response timed out'); + responseHandler.callback(timeoutError); + }, this.options.ipcAckTimeout); + + this._pendingResponseHandlers[cid] = { + callback: callback, + timeout: responseTimeout + }; + + return cid; +}; + +SocketCluster.prototype.sendToWorker = function (workerId, data, callback) { + var self = this; + + var messagePacket = { type: 'masterMessage', workerId: workerId, data: data - }); + }; + + if (callback) { + messagePacket.cid = this._createIPCResponseHandler(callback); + } + this.workerCluster.send(messagePacket); }; -SocketCluster.prototype.sendToBroker = function (brokerId, data) { +SocketCluster.prototype.sendToBroker = function (brokerId, data, callback) { this._brokerEngineServer.sendToBroker(brokerId, data); }; diff --git a/lib/scworker.js b/lib/scworker.js index dc1020a6..7f1bf431 100644 --- a/lib/scworker.js +++ b/lib/scworker.js @@ -2,6 +2,7 @@ var socketClusterServer = require('socketcluster-server'); var EventEmitter = require('events').EventEmitter; var crypto = require('crypto'); var domain = require('sc-domain'); +var uuid = require('uuid'); var http = require('http'); var https = require('https'); var fs = require('fs'); @@ -14,6 +15,7 @@ var InvalidActionError = scErrors.InvalidActionError; var ResourceLimitError = scErrors.ResourceLimitError; var BrokerError = scErrors.BrokerError; var HTTPServerError = scErrors.HTTPServerError; +var TimeoutError = scErrors.TimeoutError; var SCWorker = function (options) { var self = this; @@ -27,6 +29,7 @@ var SCWorker = function (options) { this.MIDDLEWARE_START = 'start'; this.type = 'worker'; + self._pendingResponseHandlers = {}; this._errorDomain = domain.create(); this._errorDomain.on('error', function () { @@ -345,11 +348,52 @@ SCWorker.prototype.getStatus = function () { }; }; -SCWorker.prototype.sendToMaster = function (data) { - process.send({ +SCWorker.prototype._createIPCResponseHandler = function (callback) { + var cid = uuid.v4(); + + var responseTimeout = setTimeout(function () { + var responseHandler = self._pendingResponseHandlers[cid]; + delete self._pendingResponseHandlers[cid]; + var timeoutError = new TimeoutError('IPC response timed out'); + responseHandler.callback(timeoutError); + }, this.options.ipcAckTimeout); + + this._pendingResponseHandlers[cid] = { + callback: callback, + timeout: responseTimeout + }; + + return cid; +}; + +SCWorker.prototype.handleMasterResponse = function (message) { + var responseHandler = this._pendingResponseHandlers[message.rid]; + if (responseHandler) { + clearTimeout(responseHandler.timeout); + delete this._pendingResponseHandlers[message.rid]; + responseHandler.callback(null, message.data); + } +}; + +SCWorker.prototype.sendToMaster = function (data, callback) { + var messagePacket = { type: 'workerMessage', data: data, workerId: this.id + }; + + if (callback) { + messagePacket.cid = this._createIPCResponseHandler(callback); + } + process.send(messagePacket); +}; + +SCWorker.prototype.respondToMaster = function (data, rid) { + process.send({ + type: 'workerResponse', + data: data, + workerId: this.id, + rid: rid }); }; @@ -358,7 +402,13 @@ SCWorker.prototype.handleMasterEvent = function () { }; SCWorker.prototype.handleMasterMessage = function (message) { - this.emit('masterMessage', message.data); + var self = this + + self.emit('masterMessage', message.data, function (data) { + if (message.cid) { + self.respondToMaster(data, message.cid); + } + }); }; SCWorker.prototype.errorHandler = function (err) { diff --git a/lib/workercluster.js b/lib/workercluster.js index 3a1dad00..57ebd50a 100644 --- a/lib/workercluster.js +++ b/lib/workercluster.js @@ -175,6 +175,7 @@ if (cluster.isMaster) { var worker; process.on('message', function (m) { + if (m.type == 'init') { if (m.data.processTermTimeout) { processTermTimeout = m.data.processTermTimeout; @@ -210,6 +211,8 @@ if (cluster.isMaster) { } } else if (m.type == 'masterMessage') { worker.handleMasterMessage(m); + } else if (m.type == 'masterResponse') { + worker.handleMasterResponse(m); } else if (m.type == 'terminate') { if (worker && !m.data.immediate) { worker.close(function () { diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..7ecfa41a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,655 @@ +{ + "name": "socketcluster", + "version": "6.5.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "ansi-escapes": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-1.4.0.tgz", + "integrity": "sha1-06ioOzGapneTZisT52HHkRQiMG4=" + }, + "ansi-regex": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.1.1.tgz", + "integrity": "sha1-w7M6te42DYbg5ijwRorn7yfWVN8=" + }, + "ansi-styles": { + "version": "2.2.1", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-2.2.1.tgz", + "integrity": "sha1-tDLdM1i2NM914eRmQ2gkBTPB3b4=" + }, + "async": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/async/-/async-2.0.0.tgz", + "integrity": "sha1-0JAK04WvE4BFQKEJxCFm4657K50=", + "requires": { + "lodash": "4.17.4" + } + }, + "base-64": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/base-64/-/base-64-0.1.0.tgz", + "integrity": "sha1-eAqZyE59YAJgNhURxId2E78k9rs=", + "dev": true + }, + "base64id": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/base64id/-/base64id-0.1.0.tgz", + "integrity": "sha1-As4P3u4M709ACA4ec+g08LG/zj8=" + }, + "base64url": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/base64url/-/base64url-2.0.0.tgz", + "integrity": "sha1-6sFuA+oUOO/5Qj1puqNiYu0fcLs=" + }, + "buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=" + }, + "chalk": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-1.1.3.tgz", + "integrity": "sha1-qBFcVeSnAv5NFQq9OHKCKn4J/Jg=", + "requires": { + "ansi-styles": "2.2.1", + "escape-string-regexp": "1.0.5", + "has-ansi": "2.0.0", + "strip-ansi": "3.0.1", + "supports-color": "2.0.0" + } + }, + "cli-cursor": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-1.0.2.tgz", + "integrity": "sha1-ZNo/fValRBLll5S9Ytw1KV6PKYc=", + "requires": { + "restore-cursor": "1.0.1" + } + }, + "cli-width": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/cli-width/-/cli-width-2.1.0.tgz", + "integrity": "sha1-sjTKIJsp72b8UY2bmNWEewDt8Ao=" + }, + "clone": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/clone/-/clone-2.1.1.tgz", + "integrity": "sha1-0hfR6WERjjrJpLi7oyhVU79kfNs=", + "dev": true + }, + "code-point-at": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.1.0.tgz", + "integrity": "sha1-DQcLTQQ6W+ozovGkDi7bPZpMz3c=" + }, + "component-emitter": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/component-emitter/-/component-emitter-1.2.0.tgz", + "integrity": "sha1-zNETqGOI0GSC0D3j/H35hSa6jv4=" + }, + "concat-stream": { + "version": "1.6.0", + "resolved": "https://registry.npmjs.org/concat-stream/-/concat-stream-1.6.0.tgz", + "integrity": "sha1-CqxmL9Ur54lk1VMvaUeE5wEQrPc=", + "requires": { + "inherits": "2.0.3", + "readable-stream": "2.3.3", + "typedarray": "0.0.6" + } + }, + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", + "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=" + }, + "ecdsa-sig-formatter": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.9.tgz", + "integrity": "sha1-S8kmJ07Dtau1AW5+HWCSGsJisqE=", + "requires": { + "base64url": "2.0.0", + "safe-buffer": "5.1.1" + } + }, + "escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=" + }, + "exit-hook": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/exit-hook/-/exit-hook-1.1.1.tgz", + "integrity": "sha1-8FyiM7SMBdVP/wd2XfhQfpXAL/g=" + }, + "expirymanager": { + "version": "0.9.3", + "resolved": "https://registry.npmjs.org/expirymanager/-/expirymanager-0.9.3.tgz", + "integrity": "sha1-5fazugDY12z2MxHCtx19/JvePk8=" + }, + "extend": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.1.tgz", + "integrity": "sha1-p1Xqe8Gt/MWjHOfnYtuq3F5jZEQ=" + }, + "external-editor": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/external-editor/-/external-editor-1.1.1.tgz", + "integrity": "sha1-Etew24UPf/fnCBuvQAVwAGDEYAs=", + "requires": { + "extend": "3.0.1", + "spawn-sync": "1.0.15", + "tmp": "0.0.29" + } + }, + "figures": { + "version": "1.7.0", + "resolved": "https://registry.npmjs.org/figures/-/figures-1.7.0.tgz", + "integrity": "sha1-y+Hjr/zxzUS4DK3+0o3Hk6lwHS4=", + "requires": { + "escape-string-regexp": "1.0.5", + "object-assign": "4.1.1" + } + }, + "fleximap": { + "version": "0.9.10", + "resolved": "https://registry.npmjs.org/fleximap/-/fleximap-0.9.10.tgz", + "integrity": "sha1-GqUP9qj+oAN8w3jjjdrMCRAlrBA=" + }, + "fs-extra": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-2.0.0.tgz", + "integrity": "sha1-M3NSve1KC3FPPrhN6M6nZenTdgA=", + "requires": { + "graceful-fs": "4.1.11", + "jsonfile": "2.4.0" + } + }, + "graceful-fs": { + "version": "4.1.11", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.1.11.tgz", + "integrity": "sha1-Dovf5NHduIVNZOBOp8AOKgJuVlg=" + }, + "has-ansi": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/has-ansi/-/has-ansi-2.0.0.tgz", + "integrity": "sha1-NPUEnOHs3ysGSa8+8k5F7TVBbZE=", + "requires": { + "ansi-regex": "2.1.1" + } + }, + "hoek": { + "version": "2.16.3", + "resolved": "https://registry.npmjs.org/hoek/-/hoek-2.16.3.tgz", + "integrity": "sha1-ILt0A9POo5jpHcRxCo/xuCdKJe0=" + }, + "inherits": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", + "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=" + }, + "inquirer": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-1.1.3.tgz", + "integrity": "sha1-bNKpP3CfpQd5cx/SJixpgVXKsvo=", + "requires": { + "ansi-escapes": "1.4.0", + "chalk": "1.1.3", + "cli-cursor": "1.0.2", + "cli-width": "2.1.0", + "external-editor": "1.1.1", + "figures": "1.7.0", + "lodash": "4.17.4", + "mute-stream": "0.0.6", + "pinkie-promise": "2.0.1", + "run-async": "2.3.0", + "rx": "4.1.0", + "string-width": "1.0.2", + "strip-ansi": "3.0.1", + "through": "2.3.8" + } + }, + "is-fullwidth-code-point": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", + "integrity": "sha1-754xOG8DGn8NZDr4L95QxFfvAMs=", + "requires": { + "number-is-nan": "1.0.1" + } + }, + "is-promise": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/is-promise/-/is-promise-2.1.0.tgz", + "integrity": "sha1-eaKp7OfwlugPNtKy87wWwf9L8/o=" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "isemail": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/isemail/-/isemail-1.2.0.tgz", + "integrity": "sha1-vgPfjMPineTSxd9lASY/H6RZXpo=" + }, + "joi": { + "version": "6.10.1", + "resolved": "https://registry.npmjs.org/joi/-/joi-6.10.1.tgz", + "integrity": "sha1-TVDDGAeRIgAP5fFq8f+OGRe3fgY=", + "requires": { + "hoek": "2.16.3", + "isemail": "1.2.0", + "moment": "2.18.1", + "topo": "1.1.0" + } + }, + "jsonfile": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-2.4.0.tgz", + "integrity": "sha1-NzaitCi4e72gzIO1P6PWM6NcKug=", + "requires": { + "graceful-fs": "4.1.11" + } + }, + "jwa": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.1.5.tgz", + "integrity": "sha1-oFUs4CIHQs1S4VN3SjKQXDDnVuU=", + "requires": { + "base64url": "2.0.0", + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.9", + "safe-buffer": "5.1.1" + } + }, + "jws": { + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.1.4.tgz", + "integrity": "sha1-+ei5M46KhHJ31kRLFGT2GIDgUKI=", + "requires": { + "base64url": "2.0.0", + "jwa": "1.1.5", + "safe-buffer": "5.1.1" + } + }, + "linked-list": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/linked-list/-/linked-list-0.1.0.tgz", + "integrity": "sha1-eYsP+X0bkqT9CEgPVa6k6dSdN78=", + "dev": true + }, + "lodash": { + "version": "4.17.4", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.4.tgz", + "integrity": "sha1-eCA6TRwyiuHYbcpkYONptX9AVa4=" + }, + "lodash.clonedeep": { + "version": "4.5.0", + "resolved": "https://registry.npmjs.org/lodash.clonedeep/-/lodash.clonedeep-4.5.0.tgz", + "integrity": "sha1-4j8/nE+Pvd6HJSnBBxhXoIblzO8=" + }, + "lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=" + }, + "minimist": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.1.0.tgz", + "integrity": "sha1-zfIl6ImPhAolje1E/JF3Z3Cv3JM=" + }, + "moment": { + "version": "2.18.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.18.1.tgz", + "integrity": "sha1-w2GT3Tzhwu7SrbfIAtu8d6gbHA8=" + }, + "ms": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" + }, + "mute-stream": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.6.tgz", + "integrity": "sha1-SJYrGeFp/R38JAs/HnMXYnu8R9s=" + }, + "ncom": { + "version": "0.11.1", + "resolved": "https://registry.npmjs.org/ncom/-/ncom-0.11.1.tgz", + "integrity": "sha1-5B+YoTxIbTU/EelnIXZXzsyBMps=", + "requires": { + "sc-domain": "1.0.1", + "sc-formatter": "3.0.0" + } + }, + "number-is-nan": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.1.tgz", + "integrity": "sha1-CXtgK1NCKlIsGvuHkDGDNpQaAR0=" + }, + "object-assign": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz", + "integrity": "sha1-IQmtx5ZYh8/AXLvUQsrIv7s2CGM=" + }, + "onetime": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-1.1.0.tgz", + "integrity": "sha1-ofeDj4MUxRbwXs78vEzP4EtO14k=" + }, + "os-shim": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/os-shim/-/os-shim-0.1.3.tgz", + "integrity": "sha1-a2LDeRz3kJ6jXtRuF2WLtBfLORc=" + }, + "os-tmpdir": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", + "integrity": "sha1-u+Z0BseaqFxc/sdm/lc0VV36EnQ=" + }, + "pinkie": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/pinkie/-/pinkie-2.0.4.tgz", + "integrity": "sha1-clVrgM+g1IqXToDnckjoDtT3+HA=" + }, + "pinkie-promise": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/pinkie-promise/-/pinkie-promise-2.0.1.tgz", + "integrity": "sha1-ITXW36ejWMBprJsXh3YogihFD/o=", + "requires": { + "pinkie": "2.0.4" + } + }, + "process-nextick-args": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-1.0.7.tgz", + "integrity": "sha1-FQ4gt1ZZCtP5EJPyWk8q2L/zC6M=" + }, + "querystring": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/querystring/-/querystring-0.2.0.tgz", + "integrity": "sha1-sgmEkgO7Jd+CDadW50cAWHhSFiA=", + "dev": true + }, + "readable-stream": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.3.tgz", + "integrity": "sha512-m+qzzcn7KUxEmd1gMbchF+Y2eIUbieUaxkWtptyHywrX0rE8QEYqPC07Vuy4Wm32/xE16NcdBctb8S0Xe/5IeQ==", + "requires": { + "core-util-is": "1.0.2", + "inherits": "2.0.3", + "isarray": "1.0.0", + "process-nextick-args": "1.0.7", + "safe-buffer": "5.1.1", + "string_decoder": "1.0.3", + "util-deprecate": "1.0.2" + } + }, + "restore-cursor": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-1.0.1.tgz", + "integrity": "sha1-NGYfRohjJ/7SmRR5FSJS35LapUE=", + "requires": { + "exit-hook": "1.1.1", + "onetime": "1.1.0" + } + }, + "run-async": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/run-async/-/run-async-2.3.0.tgz", + "integrity": "sha1-A3GrSuC91yDUFm19/aZP96RFpsA=", + "requires": { + "is-promise": "2.1.0" + } + }, + "rx": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/rx/-/rx-4.1.0.tgz", + "integrity": "sha1-pfE/957zt0D+MKqAP7CfmIBdR4I=" + }, + "safe-buffer": { + "version": "5.1.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz", + "integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg==" + }, + "sc-auth": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/sc-auth/-/sc-auth-4.1.1.tgz", + "integrity": "sha1-hSKxs8EVaObJ9fKTajj+h4fbN6M=", + "requires": { + "sc-errors": "1.3.3", + "sc-jsonwebtoken": "7.4.2" + } + }, + "sc-broker": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/sc-broker/-/sc-broker-3.0.0.tgz", + "integrity": "sha1-o0SRgWvcKq9vI1YslC65UF61R30=", + "requires": { + "expirymanager": "0.9.3", + "fleximap": "0.9.10", + "ncom": "0.11.1", + "sc-domain": "1.0.1", + "sc-errors": "1.3.3" + } + }, + "sc-broker-cluster": { + "version": "4.0.3", + "resolved": "https://registry.npmjs.org/sc-broker-cluster/-/sc-broker-cluster-4.0.3.tgz", + "integrity": "sha1-WcA7McOW30QQ5tGYulDsB0BQjw8=", + "requires": { + "async": "2.0.0", + "sc-broker": "3.0.0", + "sc-channel": "1.0.6", + "sc-domain": "1.0.1", + "sc-errors": "1.3.3", + "sc-hasher": "1.0.0" + } + }, + "sc-channel": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/sc-channel/-/sc-channel-1.0.6.tgz", + "integrity": "sha1-s4vUepk+eCkPvFNGeGf2sqCghjk=", + "requires": { + "sc-emitter": "1.1.0" + } + }, + "sc-domain": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/sc-domain/-/sc-domain-1.0.1.tgz", + "integrity": "sha1-qkAlCbh5unYBLpcy3B0H9lO4NLw=" + }, + "sc-emitter": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/sc-emitter/-/sc-emitter-1.1.0.tgz", + "integrity": "sha1-7xGdQiL0xk+Ie0hpZO8REWzdDnU=", + "requires": { + "component-emitter": "1.2.0" + } + }, + "sc-errors": { + "version": "1.3.3", + "resolved": "https://registry.npmjs.org/sc-errors/-/sc-errors-1.3.3.tgz", + "integrity": "sha1-wAvEx2apcMyNWTfQjNWOkx19rgU=" + }, + "sc-formatter": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/sc-formatter/-/sc-formatter-3.0.0.tgz", + "integrity": "sha1-yRsf5WwmCr1aai5q+YxyS8eZijg=" + }, + "sc-hasher": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/sc-hasher/-/sc-hasher-1.0.0.tgz", + "integrity": "sha1-uyKuH1opW4R8cK/0UVU2IklQ/xE=" + }, + "sc-jsonwebtoken": { + "version": "7.4.2", + "resolved": "https://registry.npmjs.org/sc-jsonwebtoken/-/sc-jsonwebtoken-7.4.2.tgz", + "integrity": "sha1-Kp9n2JHlroNCIQhSC4NoroM2x0k=", + "requires": { + "joi": "6.10.1", + "jws": "3.1.4", + "lodash.once": "4.1.1", + "ms": "2.0.0", + "xtend": "4.0.1" + } + }, + "sc-simple-broker": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/sc-simple-broker/-/sc-simple-broker-2.0.0.tgz", + "integrity": "sha1-Leb9NSNaC3bXrmNJ0cf53R4YCRw=", + "requires": { + "sc-channel": "1.0.6" + } + }, + "socketcluster-client": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/socketcluster-client/-/socketcluster-client-6.3.0.tgz", + "integrity": "sha1-e2hKsiJBRSngCs+2VhdPSjvAz54=", + "dev": true, + "requires": { + "base-64": "0.1.0", + "clone": "2.1.1", + "linked-list": "0.1.0", + "querystring": "0.2.0", + "sc-channel": "1.0.6", + "sc-emitter": "1.1.0", + "sc-errors": "1.3.3", + "sc-formatter": "3.0.0", + "ws": "3.0.0" + } + }, + "socketcluster-server": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/socketcluster-server/-/socketcluster-server-6.2.0.tgz", + "integrity": "sha1-EFDVmdFodPG31E0PlrQJCY1Dcuc=", + "requires": { + "async": "2.0.0", + "base64id": "0.1.0", + "lodash.clonedeep": "4.5.0", + "sc-auth": "4.1.1", + "sc-domain": "1.0.1", + "sc-emitter": "1.1.0", + "sc-errors": "1.3.3", + "sc-formatter": "3.0.0", + "sc-simple-broker": "2.0.0", + "uuid": "3.0.1", + "uws": "8.14.0", + "ws": "3.0.0" + } + }, + "spawn-sync": { + "version": "1.0.15", + "resolved": "https://registry.npmjs.org/spawn-sync/-/spawn-sync-1.0.15.tgz", + "integrity": "sha1-sAeZVX63+wyDdsKdROih6mfldHY=", + "requires": { + "concat-stream": "1.6.0", + "os-shim": "0.1.3" + } + }, + "string_decoder": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.0.3.tgz", + "integrity": "sha512-4AH6Z5fzNNBcH+6XDMfA/BTt87skxqJlO0lAh3Dker5zThcAxG6mKz+iGu308UKoPPQ8Dcqx/4JhujzltRa+hQ==", + "requires": { + "safe-buffer": "5.1.1" + } + }, + "string-width": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.2.tgz", + "integrity": "sha1-EYvfW4zcUaKn5w0hHgfisLmxB9M=", + "requires": { + "code-point-at": "1.1.0", + "is-fullwidth-code-point": "1.0.0", + "strip-ansi": "3.0.1" + } + }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "integrity": "sha1-ajhfuIU9lS1f8F0Oiq+UJ43GPc8=", + "requires": { + "ansi-regex": "2.1.1" + } + }, + "supports-color": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-2.0.0.tgz", + "integrity": "sha1-U10EXOa2Nj+kARcIRimZXp3zJMc=" + }, + "through": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" + }, + "tmp": { + "version": "0.0.29", + "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.29.tgz", + "integrity": "sha1-8lEl/w3Z2jzLDC3Tce4SiLuRKMA=", + "requires": { + "os-tmpdir": "1.0.2" + } + }, + "topo": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/topo/-/topo-1.1.0.tgz", + "integrity": "sha1-6ddRYV0buH3IZdsYL6HKCl71NtU=", + "requires": { + "hoek": "2.16.3" + } + }, + "typedarray": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/typedarray/-/typedarray-0.0.6.tgz", + "integrity": "sha1-hnrHTjhkGHsdPUfZlqeOxciDB3c=" + }, + "uid-number": { + "version": "0.0.5", + "resolved": "https://registry.npmjs.org/uid-number/-/uid-number-0.0.5.tgz", + "integrity": "sha1-Wj2yPvXb1VuB/ODsmirG/M3ruB4=" + }, + "ultron": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/ultron/-/ultron-1.1.0.tgz", + "integrity": "sha1-sHoualQagV/Go0zNRTO67DB8qGQ=" + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + }, + "uuid": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-3.0.1.tgz", + "integrity": "sha1-ZUS7ot/ajBzxfmKaOjBeK7H+5sE=" + }, + "uws": { + "version": "8.14.0", + "resolved": "https://registry.npmjs.org/uws/-/uws-8.14.0.tgz", + "integrity": "sha1-rMFIjRPssj/i+UKn6vsGaB+pFDE=" + }, + "ws": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/ws/-/ws-3.0.0.tgz", + "integrity": "sha1-mN2wAFbIOQy3Ued4h4hJf5kQO2w=", + "requires": { + "safe-buffer": "5.0.1", + "ultron": "1.1.0" + }, + "dependencies": { + "safe-buffer": { + "version": "5.0.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.0.1.tgz", + "integrity": "sha1-0mPKVGls2KMGtcplUekt5XkY++c=" + } + } + }, + "xtend": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", + "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=" + } + } +} diff --git a/package.json b/package.json index b16d46ba..614e5002 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,10 @@ "type": "git", "url": "git://github.com/SocketCluster/socketcluster.git" }, + "scripts":{ + "test":"npm run test::masterToWorkerCallback", + "test::masterToWorkerCallback":"node test/internal/serverToWorker.js" + }, "dependencies": { "async": "2.0.0", "base64id": "0.1.0", diff --git a/test/internal/serverToWorker.js b/test/internal/serverToWorker.js new file mode 100644 index 00000000..a17f0458 --- /dev/null +++ b/test/internal/serverToWorker.js @@ -0,0 +1,42 @@ +var SocketCluster = require('../../index').SocketCluster; + +var socketCluster = new SocketCluster({ + workers: 3, + brokers: 3, + port: 8000, + appName: 'myapp', + workerController: __dirname + '/worker.js', + //brokerController: __dirname + '/broker.js', + socketChannelLimit: 100, + crashWorkerOnError: true, + rebootWorkerOnCrash: true +}); + +socketCluster.on('workerClusterReady',function () { + + var packet = { + id:0, + name:'SocketCluster' + } + + console.log('MASTER::Sending packet to worker 0') + socketCluster.sendToWorker(0,packet,function(workerId,data) { + console.log(`MASTER::Worker ${ workerId } data packet`) + console.log(data) + console.log('MASTER::End data packet') + }); + + console.log('MASTER::Sending packet to worker 1') + socketCluster.sendToWorker(1,packet,function(workerId,data) { + console.log(`MASTER::Worker ${ workerId } data packet`) + console.log(data) + console.log('MASTER::End data packet') + }); + + console.log('MASTER::Sending packet to worker 2') + socketCluster.sendToWorker(2,packet,function(workerId,data) { + console.log(`MASTER::Worker ${ workerId } data packet`) + console.log(data) + console.log('MASTER::End data packet') + }); +}); \ No newline at end of file diff --git a/test/internal/worker.js b/test/internal/worker.js index d033cd93..f08cd73b 100644 --- a/test/internal/worker.js +++ b/test/internal/worker.js @@ -7,4 +7,11 @@ module.exports.run = function (worker) { var httpServer = worker.getHTTPServer(); // Get a reference to our WebSocket server var wsServer = worker.getSCServer(); + + worker.on('masterMessage',function(data,res) { + console.log('WORKER::Received data packet from server') + console.log(arguments) + console.log('WORKER::Responding back to server.') + res && res({ id:1,name:'TestName' }) + }); }; \ No newline at end of file