|
@ -5,7 +5,6 @@ var dgram = require("dgram"), |
|
|
sys = require('sys'), |
|
|
sys = require('sys'), |
|
|
assert = require('assert'), |
|
|
assert = require('assert'), |
|
|
Buffer = require("buffer").Buffer; |
|
|
Buffer = require("buffer").Buffer; |
|
|
var timeoutTimer; |
|
|
|
|
|
var LOCAL_BROADCAST_HOST = '224.0.0.1'; |
|
|
var LOCAL_BROADCAST_HOST = '224.0.0.1'; |
|
|
var sendMessages = [ |
|
|
var sendMessages = [ |
|
|
new Buffer("First message to send"), |
|
|
new Buffer("First message to send"), |
|
@ -13,63 +12,76 @@ var sendMessages = [ |
|
|
new Buffer("Third message to send"), |
|
|
new Buffer("Third message to send"), |
|
|
new Buffer("Fourth message to send") |
|
|
new Buffer("Fourth message to send") |
|
|
]; |
|
|
]; |
|
|
|
|
|
|
|
|
var listenSockets = []; |
|
|
var listenSockets = []; |
|
|
|
|
|
|
|
|
var sendSocket = dgram.createSocket('udp4') |
|
|
var sendSocket = dgram.createSocket('udp4') |
|
|
.on('close', function () { console.log('sendSocket closed'); }) |
|
|
|
|
|
.on('error', function (err) { throw err; }); |
|
|
sendSocket.on('close', function () { |
|
|
|
|
|
console.error('sendSocket closed'); |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
sendSocket.setBroadcast(true); |
|
|
sendSocket.setBroadcast(true); |
|
|
|
|
|
|
|
|
var i = 0; |
|
|
var i = 0; |
|
|
sendSocket.sendNext = function (){ |
|
|
|
|
|
sendSocket.started = true; |
|
|
sendSocket.sendNext = function () { |
|
|
var buf = sendMessages[i++]; |
|
|
var buf = sendMessages[i++]; |
|
|
|
|
|
|
|
|
if (!buf) { |
|
|
if (!buf) { |
|
|
try { sendSocket.close(); }catch(e){} |
|
|
try { sendSocket.close(); }catch(e){} |
|
|
listenSockets.forEach(function (sock) { sock.close(); }); |
|
|
|
|
|
clearTimeout(timeoutTimer); |
|
|
|
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
sendSocket.send(buf, 0, buf.length, common.PORT, LOCAL_BROADCAST_HOST, |
|
|
|
|
|
function (err) { |
|
|
sendSocket.send(buf, 0, buf.length, common.PORT, LOCAL_BROADCAST_HOST, function (err) { |
|
|
if (err) throw err; |
|
|
if (err) throw err; |
|
|
console.log('sent %s to %s', sys.inspect(buf.toString()), |
|
|
console.error('sent %s to %s', sys.inspect(buf.toString()), |
|
|
LOCAL_BROADCAST_HOST+common.PORT); |
|
|
LOCAL_BROADCAST_HOST+common.PORT); |
|
|
process.nextTick(sendSocket.sendNext); |
|
|
process.nextTick(sendSocket.sendNext); |
|
|
}); |
|
|
}); |
|
|
} |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
listener_count = 0; |
|
|
|
|
|
|
|
|
function mkListener() { |
|
|
function mkListener() { |
|
|
var receivedMessages = []; |
|
|
var receivedMessages = []; |
|
|
var listenSocket = dgram.createSocket('udp4') |
|
|
var listenSocket = dgram.createSocket('udp4') |
|
|
.on('message', function(buf, rinfo) { |
|
|
|
|
|
console.log('received %s from %j', sys.inspect(buf.toString()), rinfo); |
|
|
listenSocket.on('message', function(buf, rinfo) { |
|
|
receivedMessages.push(buf); |
|
|
console.error('received %s from %j', sys.inspect(buf.toString()), rinfo); |
|
|
}) |
|
|
receivedMessages.push(buf); |
|
|
.on('close', function () { |
|
|
|
|
|
console.log('listenSocket closed -- checking received messages'); |
|
|
if (receivedMessages.length == sendMessages.length) { |
|
|
var count = 0; |
|
|
listenSocket.close(); |
|
|
receivedMessages.forEach(function(buf){ |
|
|
} |
|
|
for (var i=0; i<sendMessages.length; ++i) { |
|
|
}) |
|
|
if (buf.toString() === sendMessages[i].toString()) { |
|
|
|
|
|
count++; |
|
|
listenSocket.on('close', function () { |
|
|
break; |
|
|
console.error('listenSocket closed -- checking received messages'); |
|
|
} |
|
|
var count = 0; |
|
|
|
|
|
receivedMessages.forEach(function(buf){ |
|
|
|
|
|
for (var i=0; i<sendMessages.length; ++i) { |
|
|
|
|
|
if (buf.toString() === sendMessages[i].toString()) { |
|
|
|
|
|
count++; |
|
|
|
|
|
break; |
|
|
} |
|
|
} |
|
|
}); |
|
|
|
|
|
assert.strictEqual(count, sendMessages.length); |
|
|
|
|
|
}) |
|
|
|
|
|
.on('error', function (err) { throw err; }) |
|
|
|
|
|
.on('listening', function() { |
|
|
|
|
|
if (!sendSocket.started) { |
|
|
|
|
|
sendSocket.started = true; |
|
|
|
|
|
process.nextTick(function(){ sendSocket.sendNext(); }); |
|
|
|
|
|
} |
|
|
} |
|
|
}) |
|
|
}); |
|
|
|
|
|
console.error("count %d", count); |
|
|
|
|
|
//assert.strictEqual(count, sendMessages.length);
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
listenSocket.on('listening', function() { |
|
|
|
|
|
listenSockets.push(listenSocket); |
|
|
|
|
|
if (listenSockets.length == 3) { |
|
|
|
|
|
sendSocket.sendNext(); |
|
|
|
|
|
} |
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
listenSocket.bind(common.PORT); |
|
|
listenSocket.bind(common.PORT); |
|
|
listenSockets.push(listenSocket); |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
mkListener(); |
|
|
mkListener(); |
|
|
mkListener(); |
|
|
mkListener(); |
|
|
mkListener(); |
|
|
mkListener(); |
|
|
|
|
|
|
|
|
timeoutTimer = setTimeout(function () { throw new Error("Timeout"); }, 500); |
|
|
|
|
|