1
0

pingAll.js 3.0 KB

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