pingAll 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/env node
  2. /* -*- Mode:js */
  3. /* vim: set expandtab ts=4 sw=4: */
  4. /*
  5. * You may redistribute this program and/or modify it under the terms of
  6. * the GNU General Public License as published by the Free Software Foundation,
  7. * either version 3 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. var Cjdns = require('./lib/cjdnsadmin/cjdnsadmin');
  18. var nThen = require('nthen');
  19. Cjdns.connectWithAdminInfo(function (cjdns) {
  20. var nodes = [];
  21. var lags = [];
  22. nThen(function (waitFor) {
  23. var again = function (i) {
  24. cjdns.NodeStore_dumpTable(i, waitFor(function (err, table) {
  25. if (err) { throw err; }
  26. var j;
  27. for (j = 0; j < table.routingTable.length; j++) {
  28. var r = table.routingTable[j];
  29. nodes.push(r);
  30. }
  31. if (j) {
  32. again(i+1);
  33. }
  34. }));
  35. };
  36. again(0);
  37. }).nThen(function (waitFor) {
  38. nodes.sort(function (a,b) { return (Number(a.link) < Number(b.link)) ? 1 : -1 });
  39. var ips = [];
  40. var uniques = [];
  41. nodes.forEach(function (node) {
  42. if (node.path !== '0000.0000.0000.0001' && ips.indexOf(node.ip) === -1) {
  43. uniques.push(node);
  44. ips.push(node.ip);
  45. }
  46. });
  47. var switchLabel;
  48. var nt = nThen;
  49. uniques.forEach(function (node) {
  50. nt = nt(function (waitFor) {
  51. cjdns.NodeStore_nodeForAddr(node.ip, waitFor(function (err, ret) {
  52. if (err) { throw err; }
  53. process.stdout.write(node.ip + '@' + ret.result.routeLabel);
  54. }));
  55. }).nThen(function (waitFor) {
  56. switchLabel = null;
  57. cjdns.RouterModule_pingNode(node.ip, 3000, waitFor(function (err, ret) {
  58. if (err) { throw err; }
  59. if (ret.result === 'pong') {
  60. process.stdout.write(' ' + ret.ms + 'ms v'+ret.protocol+' linkq:' + node.link);
  61. lags.push(Number(ret.ms));
  62. switchLabel = ret.from.replace(/.*@/, '');
  63. } else if (ret.error === 'not_found') {
  64. process.stdout.write(' not_found');
  65. } else {
  66. process.stdout.write(' ' + JSON.stringify(ret));
  67. lags.push(3000);
  68. }
  69. }));
  70. }).nThen(function (waitFor) {
  71. if (switchLabel === null) {
  72. process.stdout.write('\n');
  73. return
  74. }
  75. cjdns.SwitchPinger_ping(switchLabel, 3000, 0, '', waitFor(function (err, ret) {
  76. if (err) { throw err; }
  77. var out = ' switchPing ';
  78. if (ret.result === 'timeout') {
  79. out += 'timeout ' + ret.ms + 'ms';
  80. } else if (ret.result === 'pong') {
  81. out += ret.path + ' p' + ret.version + ' ' + ret.ms + 'ms';
  82. if (ret.key) {
  83. out += ' ' + ret.key;
  84. }
  85. } else {
  86. out += ret;
  87. }
  88. process.stdout.write(out + '\n');
  89. }));
  90. }).nThen;
  91. });
  92. nt(waitFor());
  93. }).nThen(function (waitFor) {
  94. var totalLag = 0;
  95. for (var i = 0; i < lags.length; i++) { totalLag += lags[i]; }
  96. console.log("Average lag: " + (totalLag / lags.length));
  97. cjdns.disconnect();
  98. });
  99. });