Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nodejs/node.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorRoman Shtylman <shtylman@gmail.com>2011-10-12 21:55:15 +0400
committerBen Noordhuis <info@bnoordhuis.nl>2012-01-21 06:21:51 +0400
commita38fd6056c2c6d4e2266a65fdefdbdc8dcbe4dee (patch)
tree25fd596fb5e3c51501038a8bca0e9a5cd24bbf10 /test
parent08ab306afd0161aebde527a145c49cb848718df2 (diff)
dgram: reintroduce addMembership() and dropMembership()
Removed during the early stages of node 0.5 refactoring to libuv.
Diffstat (limited to 'test')
-rw-r--r--test/simple/test-dgram-multicast-multi-process.js160
1 files changed, 160 insertions, 0 deletions
diff --git a/test/simple/test-dgram-multicast-multi-process.js b/test/simple/test-dgram-multicast-multi-process.js
new file mode 100644
index 00000000000..784bb6c0943
--- /dev/null
+++ b/test/simple/test-dgram-multicast-multi-process.js
@@ -0,0 +1,160 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common'),
+ assert = require('assert'),
+ cluster = require('cluster'),
+ dgram = require('dgram'),
+ util = require('util'),
+ assert = require('assert'),
+ Buffer = require('buffer').Buffer,
+ LOCAL_BROADCAST_HOST = '224.0.0.1',
+ messages = [
+ new Buffer('First message to send'),
+ new Buffer('Second message to send'),
+ new Buffer('Third message to send'),
+ new Buffer('Fourth message to send')
+ ];
+
+if (cluster.isMaster) {
+ var workers = {},
+ listeners = 3,
+ listening = 0,
+ i = 0,
+ done = 0;
+
+ //launch child processes
+ for (var x = 0; x < listeners; x++) {
+ (function () {
+ var worker = cluster.fork();
+ workers[worker.pid] = worker;
+
+ worker.messagesReceived = [];
+
+ worker.on('message', function (msg) {
+ if (msg.listening) {
+ listening += 1;
+
+ if (listening === listeners) {
+ //all child process are listening, so start sending
+ sendSocket.sendNext();
+ }
+ }
+ else if (msg.message) {
+ worker.messagesReceived.push(msg.message);
+
+ if (worker.messagesReceived.length === messages.length) {
+ done += 1;
+ console.error('%d received %d messages total.', worker.pid,
+ worker.messagesReceived.length);
+ }
+
+ if (done === listeners) {
+ console.error('All workers have received the required number of'
+ + 'messages. Will now compare.');
+
+ Object.keys(workers).forEach(function (pid) {
+ var worker = workers[pid];
+
+ var count = 0;
+
+ worker.messagesReceived.forEach(function(buf) {
+ for (var i = 0; i < messages.length; ++i) {
+ if (buf.toString() === messages[i].toString()) {
+ count++;
+ break;
+ }
+ }
+ });
+
+ console.error('%d received %d matching messges.', worker.pid
+ , count);
+
+ assert.equal(count, messages.length
+ ,'A worker received an invalid multicast message');
+ });
+ }
+ }
+ });
+ })(x);
+ }
+
+ var sendSocket = dgram.createSocket('udp4');
+
+ //sendSocket.setBroadcast(true);
+ //sendSocket.setMulticastTTL(1);
+ //sendSocket.setMulticastLoopback(true);
+
+ sendSocket.on('close', function() {
+ console.error('sendSocket closed');
+ });
+
+ sendSocket.sendNext = function() {
+ var buf = messages[i++];
+
+ if (!buf) {
+ try { sendSocket.close(); } catch (e) {}
+ return;
+ }
+
+ sendSocket.send(buf, 0, buf.length,
+ common.PORT, LOCAL_BROADCAST_HOST, function(err) {
+ if (err) throw err;
+ console.error('sent %s to %s', util.inspect(buf.toString()),
+ LOCAL_BROADCAST_HOST + common.PORT);
+ process.nextTick(sendSocket.sendNext);
+ });
+ };
+}
+
+if (!cluster.isMaster) {
+ var receivedMessages = [];
+ var listenSocket = dgram.createSocket('udp4');
+
+ listenSocket.addMembership(LOCAL_BROADCAST_HOST);
+
+ listenSocket.on('message', function(buf, rinfo) {
+ console.error('%s received %s from %j', process.pid
+ ,util.inspect(buf.toString()), rinfo);
+
+ receivedMessages.push(buf);
+
+ process.send({ message : buf.toString() });
+
+ if (receivedMessages.length == messages.length) {
+ listenSocket.dropMembership(LOCAL_BROADCAST_HOST);
+ process.nextTick(function() { // TODO should be changed to below.
+ // listenSocket.dropMembership(LOCAL_BROADCAST_HOST, function() {
+ listenSocket.close();
+ });
+ }
+ });
+
+ listenSocket.on('close', function() {
+ process.exit();
+ });
+
+ listenSocket.on('listening', function() {
+ process.send({ listening : true });
+ });
+
+ listenSocket.bind(common.PORT);
+}