pathfinderTree 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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('../contrib/nodejs/cjdnsadmin/cjdnsadmin');
  18. var nThen = require('../contrib/nodejs/cjdnsadmin/nthen');
  19. var getAddresses = function (cjdns, callback) {
  20. var addresses = [];
  21. var again = function (i) {
  22. cjdns.NodeStore_dumpTable(i, function (err, table) {
  23. if (err) { throw err; }
  24. var j;
  25. for (j = 0; j < table.routingTable.length; j++) {
  26. var r = table.routingTable[j];
  27. if (addresses.indexOf(r.ip) === -1) { addresses.push(r.ip); }
  28. }
  29. if (j) {
  30. again(i+1);
  31. } else {
  32. callback(addresses);
  33. }
  34. });
  35. };
  36. again(0);
  37. };
  38. var buildTreeCycle = function (current, nodes) {
  39. current.peers = [];
  40. for (var i = nodes.length - 1; i >= 0; i--) {
  41. if (nodes[i].bestParent && nodes[i].bestParent.ip == current.ip) {
  42. current.peers.push(nodes[i]);
  43. nodes.splice(i, 1);
  44. }
  45. }
  46. for (var i = 0; i < current.peers.length; i++) {
  47. buildTreeCycle(current.peers[i], nodes);
  48. }
  49. return current;
  50. };
  51. var buildTree = function (origNodes) {
  52. var nodes = [];
  53. nodes.push.apply(nodes, origNodes);
  54. for (var i = 0; i < nodes.length; i++) {
  55. if (nodes[i].bestParent && nodes[i].ip === nodes[i].bestParent.ip) {
  56. var current = nodes[i];
  57. nodes.splice(i, 1);
  58. var out = buildTreeCycle(current, nodes);
  59. //if (nodes.length > 0) { throw new Error(); }
  60. return out;
  61. }
  62. }
  63. throw new Error();
  64. };
  65. var printTree = function (tree, spaces) {
  66. console.log(spaces + tree.ip + ' ' + tree.bestParent.parentChildLabel + ' ' + tree.routeLabel + ' ' + tree.reach);
  67. for (var i = 0; i < tree.peers.length; i++) {
  68. printTree(tree.peers[i], spaces + ' ');
  69. }
  70. };
  71. Cjdns.connectWithAdminInfo(function (cjdns) {
  72. var addrs;
  73. var nodes = [];
  74. nThen(function (waitFor) {
  75. getAddresses(cjdns, waitFor(function (addrs) { addresses = addrs; }));
  76. }).nThen(function (waitFor) {
  77. var nt = nThen;
  78. addresses.forEach(function (addr) {
  79. nt = nt(function (waitFor) {
  80. cjdns.NodeStore_nodeForAddr(addr, waitFor(function (err, ret) {
  81. if (err) { throw err; }
  82. if (ret.error !== 'none') { throw new Error(ret.error); }
  83. ret.result.ip = addr;
  84. nodes.push(ret.result);
  85. }));
  86. }).nThen;
  87. });
  88. nt(waitFor());
  89. }).nThen(function (waitFor) {
  90. //console.log(JSON.stringify(nodes, null, ' '));
  91. var tree = buildTree(nodes);
  92. printTree(tree, '');
  93. cjdns.disconnect();
  94. });
  95. });