8795026fa75d115fbdb0702bf2a339a85d51bbc3
[platform/upstream/nodejs.git] / test / disabled / test-dgram-multicast.js
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
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.
21
22
23
24
25 var common = require('../common');
26 var assert = require('assert');
27
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';
33 var sendMessages = [
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')
38 ];
39
40 var listenSockets = [];
41
42 var sendSocket = dgram.createSocket('udp4');
43
44 sendSocket.on('close', function() {
45   console.error('sendSocket closed');
46 });
47
48 sendSocket.setBroadcast(true);
49 sendSocket.setMulticastTTL(1);
50 sendSocket.setMulticastLoopback(true);
51
52 var i = 0;
53
54 sendSocket.sendNext = function() {
55   var buf = sendMessages[i++];
56
57   if (!buf) {
58     try { sendSocket.close(); } catch (e) {}
59     return;
60   }
61
62   sendSocket.send(buf, 0, buf.length,
63                   common.PORT, LOCAL_BROADCAST_HOST, function(err) {
64         if (err) throw err;
65         console.error('sent %s to %s', util.inspect(buf.toString()),
66                       LOCAL_BROADCAST_HOST + common.PORT);
67         process.nextTick(sendSocket.sendNext);
68       });
69 };
70
71 var listener_count = 0;
72
73 function mkListener() {
74   var receivedMessages = [];
75   var listenSocket = dgram.createSocket('udp4');
76   listenSocket.addMembership(LOCAL_BROADCAST_HOST);
77
78   listenSocket.on('message', function(buf, rinfo) {
79     console.error('received %s from %j', util.inspect(buf.toString()), rinfo);
80     receivedMessages.push(buf);
81
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() {
86         listenSocket.close();
87       });
88     }
89   });
90
91   listenSocket.on('close', function() {
92     console.error('listenSocket closed -- checking received messages');
93     var count = 0;
94     receivedMessages.forEach(function(buf) {
95       for (var i = 0; i < sendMessages.length; ++i) {
96         if (buf.toString() === sendMessages[i].toString()) {
97           count++;
98           break;
99         }
100       }
101     });
102     console.error('count %d', count);
103     //assert.strictEqual(count, sendMessages.length);
104   });
105
106   listenSocket.on('listening', function() {
107     listenSockets.push(listenSocket);
108     if (listenSockets.length == 3) {
109       sendSocket.sendNext();
110     }
111   });
112
113   listenSocket.bind(common.PORT);
114 }
115
116 mkListener();
117 mkListener();
118 mkListener();
119