1 // Copyright Joyent, Inc. and other Node contributors.
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
25 var common = require('../common');
26 var assert = require('assert');
28 var dgram = require('dgram'),
29 util = require('util'),
30 assert = require('assert'),
31 Buffer = require('buffer').Buffer;
32 var LOCAL_BROADCAST_HOST = '224.0.0.1';
34 new Buffer('First message to send'),
35 new Buffer('Second message to send'),
36 new Buffer('Third message to send'),
37 new Buffer('Fourth message to send')
40 var listenSockets = [];
42 var sendSocket = dgram.createSocket('udp4');
44 sendSocket.on('close', function() {
45 console.error('sendSocket closed');
48 sendSocket.setBroadcast(true);
49 sendSocket.setMulticastTTL(1);
50 sendSocket.setMulticastLoopback(true);
54 sendSocket.sendNext = function() {
55 var buf = sendMessages[i++];
58 try { sendSocket.close(); } catch (e) {}
62 sendSocket.send(buf, 0, buf.length,
63 common.PORT, LOCAL_BROADCAST_HOST, function(err) {
65 console.error('sent %s to %s', util.inspect(buf.toString()),
66 LOCAL_BROADCAST_HOST + common.PORT);
67 process.nextTick(sendSocket.sendNext);
71 var listener_count = 0;
73 function mkListener() {
74 var receivedMessages = [];
75 var listenSocket = dgram.createSocket('udp4');
76 listenSocket.addMembership(LOCAL_BROADCAST_HOST);
78 listenSocket.on('message', function(buf, rinfo) {
79 console.error('received %s from %j', util.inspect(buf.toString()), rinfo);
80 receivedMessages.push(buf);
82 if (receivedMessages.length == sendMessages.length) {
83 listenSocket.dropMembership(LOCAL_BROADCAST_HOST);
84 process.nextTick(function() { // TODO should be changed to below.
85 // listenSocket.dropMembership(LOCAL_BROADCAST_HOST, function() {
91 listenSocket.on('close', function() {
92 console.error('listenSocket closed -- checking received messages');
94 receivedMessages.forEach(function(buf) {
95 for (var i = 0; i < sendMessages.length; ++i) {
96 if (buf.toString() === sendMessages[i].toString()) {
102 console.error('count %d', count);
103 //assert.strictEqual(count, sendMessages.length);
106 listenSocket.on('listening', function() {
107 listenSockets.push(listenSocket);
108 if (listenSockets.length == 3) {
109 sendSocket.sendNext();
113 listenSocket.bind(common.PORT);