run_tests.js 902 B

12345678910111213141516171819202122232425262728293031323334353637
  1. ////////////////////////////////////////////////////
  2. //
  3. // Usage : node run_tests.js "command" [nb packets to send]
  4. //
  5. // Example : node run_tests.js "gorets:10|c" 100
  6. //
  7. //
  8. var dgram = require('dgram');
  9. // Splice arguments
  10. var arguments = process.argv.splice(2);
  11. var message = new Buffer(arguments[0]);
  12. var count = parseInt(arguments[1]);
  13. // Open udp socket
  14. var client = dgram.createSocket("udp4");
  15. var send = function(host, port, message, index, closeSocket) {
  16. client.send(message, 0, message.length, port, host, function(err, bytes) {
  17. if(err) {
  18. client.close();
  19. console.log("Error when sending packet n° " + index +" ! Exit...");
  20. process.exit(-1);
  21. }
  22. if(closeSocket) {
  23. console.log('Done.');
  24. client.close();
  25. }
  26. });
  27. }
  28. // Loop and send udp messages
  29. for(var i=0; i < count; i++) {
  30. send("localhost", 8125, message, i, i == count-1);
  31. }