pathfinderTree 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. 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 + ' ' +
  67. tree.routeLabel + ' ' + tree.cost + ' ' + tree.bestParent.isOneHop);
  68. for (var i = 0; i < tree.peers.length; i++) {
  69. printTree(tree.peers[i], spaces + ' ');
  70. }
  71. };
  72. Cjdns.connectWithAdminInfo(function (cjdns) {
  73. var addrs;
  74. var nodes = [];
  75. nThen(function (waitFor) {
  76. getAddresses(cjdns, waitFor(function (addrs) { addresses = addrs; }));
  77. }).nThen(function (waitFor) {
  78. var nt = nThen;
  79. addresses.forEach(function (addr) {
  80. nt = nt(function (waitFor) {
  81. cjdns.NodeStore_nodeForAddr(addr, waitFor(function (err, ret) {
  82. if (err) { throw err; }
  83. if (ret.error !== 'none') { throw new Error(ret.error); }
  84. ret.result.ip = addr;
  85. nodes.push(ret.result);
  86. }));
  87. }).nThen;
  88. });
  89. nt(waitFor());
  90. }).nThen(function (waitFor) {
  91. //console.log(JSON.stringify(nodes, null, ' '));
  92. var tree = buildTree(nodes);
  93. printTree(tree, '');
  94. cjdns.disconnect();
  95. });
  96. });