2011-03-10 09:54:52 +01:00
|
|
|
// 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.
|
|
|
|
|
2011-11-03 00:26:43 +01:00
|
|
|
|
2011-08-24 08:42:23 +02:00
|
|
|
|
|
|
|
|
2010-12-05 00:20:34 +01:00
|
|
|
var common = require('../common');
|
|
|
|
var assert = require('assert');
|
2010-06-12 11:40:24 +02:00
|
|
|
var Buffer = require('buffer').Buffer;
|
2010-12-05 20:15:30 +01:00
|
|
|
var dgram = require('dgram');
|
2010-06-12 11:40:24 +02:00
|
|
|
|
|
|
|
var tests_run = 0;
|
|
|
|
|
2010-12-05 20:15:30 +01:00
|
|
|
function pingPongTest(port, host) {
|
2010-06-16 20:00:43 +02:00
|
|
|
var callbacks = 0;
|
2010-06-12 11:40:24 +02:00
|
|
|
var N = 500;
|
|
|
|
var count = 0;
|
|
|
|
var sent_final_ping = false;
|
|
|
|
|
2010-12-05 20:15:30 +01:00
|
|
|
var server = dgram.createSocket('udp4', function(msg, rinfo) {
|
|
|
|
console.log('server got: ' + msg +
|
|
|
|
' from ' + rinfo.address + ':' + rinfo.port);
|
2010-06-12 11:40:24 +02:00
|
|
|
|
|
|
|
if (/PING/.exec(msg)) {
|
|
|
|
var buf = new Buffer(4);
|
|
|
|
buf.write('PONG');
|
2010-12-05 20:15:30 +01:00
|
|
|
server.send(buf, 0, buf.length,
|
|
|
|
rinfo.port, rinfo.address,
|
|
|
|
function(err, sent) {
|
|
|
|
callbacks++;
|
|
|
|
});
|
2010-06-12 11:40:24 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-12-05 20:15:30 +01:00
|
|
|
server.on('error', function(e) {
|
2010-06-12 11:40:24 +02:00
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
|
2010-12-05 20:15:30 +01:00
|
|
|
server.on('listening', function() {
|
|
|
|
console.log('server listening on ' + port + ' ' + host);
|
2010-06-12 11:40:24 +02:00
|
|
|
|
2010-12-05 20:15:30 +01:00
|
|
|
var buf = new Buffer('PING'),
|
|
|
|
client = dgram.createSocket('udp4');
|
2010-06-12 11:40:24 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
client.on('message', function(msg, rinfo) {
|
2010-12-05 20:15:30 +01:00
|
|
|
console.log('client got: ' + msg +
|
|
|
|
' from ' + rinfo.address + ':' + rinfo.port);
|
|
|
|
assert.equal('PONG', msg.toString('ascii'));
|
2010-06-12 11:40:24 +02:00
|
|
|
|
|
|
|
count += 1;
|
|
|
|
|
|
|
|
if (count < N) {
|
2010-12-05 20:15:30 +01:00
|
|
|
client.send(buf, 0, buf.length, port, 'localhost');
|
2010-06-12 11:40:24 +02:00
|
|
|
} else {
|
|
|
|
sent_final_ping = true;
|
2011-10-21 19:21:23 +02:00
|
|
|
client.send(buf, 0, buf.length, port, 'localhost', function() {
|
2010-06-12 11:40:24 +02:00
|
|
|
client.close();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2010-12-05 20:15:30 +01:00
|
|
|
client.on('close', function() {
|
2010-07-05 22:38:13 +02:00
|
|
|
console.log('client has closed, closing server');
|
2010-06-12 11:40:24 +02:00
|
|
|
assert.equal(N, count);
|
|
|
|
tests_run += 1;
|
|
|
|
server.close();
|
2010-12-05 20:15:30 +01:00
|
|
|
assert.equal(N - 1, callbacks);
|
2010-06-12 11:40:24 +02:00
|
|
|
});
|
|
|
|
|
2010-12-05 20:15:30 +01:00
|
|
|
client.on('error', function(e) {
|
2010-06-12 11:40:24 +02:00
|
|
|
throw e;
|
|
|
|
});
|
|
|
|
|
2010-12-05 20:15:30 +01:00
|
|
|
console.log('Client sending to ' + port + ', localhost ' + buf);
|
|
|
|
client.send(buf, 0, buf.length, port, 'localhost', function(err, bytes) {
|
2010-07-05 22:38:13 +02:00
|
|
|
if (err) {
|
|
|
|
throw err;
|
|
|
|
}
|
2010-12-05 20:15:30 +01:00
|
|
|
console.log('Client sent ' + bytes + ' bytes');
|
2010-07-05 22:38:13 +02:00
|
|
|
});
|
2010-06-12 11:40:24 +02:00
|
|
|
count += 1;
|
|
|
|
});
|
2010-07-05 22:38:13 +02:00
|
|
|
server.bind(port, host);
|
2010-06-12 11:40:24 +02:00
|
|
|
}
|
|
|
|
|
2010-07-05 22:38:13 +02:00
|
|
|
// All are run at once, so run on different ports
|
2010-12-05 20:15:30 +01:00
|
|
|
pingPongTest(20989, 'localhost');
|
|
|
|
pingPongTest(20990, 'localhost');
|
2010-06-12 11:40:24 +02:00
|
|
|
pingPongTest(20988);
|
2010-12-05 20:15:30 +01:00
|
|
|
//pingPongTest('/tmp/pingpong.sock');
|
2010-06-12 11:40:24 +02:00
|
|
|
|
2011-10-15 01:08:36 +02:00
|
|
|
process.on('exit', function() {
|
2010-07-05 22:38:13 +02:00
|
|
|
assert.equal(3, tests_run);
|
2010-06-24 02:40:51 +02:00
|
|
|
console.log('done');
|
2010-06-12 11:40:24 +02:00
|
|
|
});
|