sessionStats 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 printSession = function (session) {
  20. var state = session.state.replace(/CryptoAuth_/,'');
  21. while (state.length < ('ESTABLISHED').length) { state = state + ' ' }
  22. var out = [ session.addr, ' ', state, ' ', session.handle ];
  23. if (Number(session.duplicates) !== 0) { out.push(' DUP ', session.duplicates); }
  24. if (Number(session.lostPackets) !== 0) { out.push(' LOS ', session.lostPackets); }
  25. if (Number(session.receivedOutOfRange) !== 0) { out.push(' OOR ', session.receivedOutOfRange); }
  26. console.log(out.join(''));
  27. };
  28. var cjdns;
  29. var handles = [];
  30. var sessions = [];
  31. nThen(function (waitFor) {
  32. Cjdns.connectWithAdminInfo(waitFor(function (c) { cjdns = c; }));
  33. }).nThen(function (waitFor) {
  34. var more = function (i) {
  35. cjdns.SessionManager_getHandles(i, waitFor(function (err, ret) {
  36. if (err) { throw err; }
  37. handles.push.apply(handles, ret.handles);
  38. if (handles.more) { more(i+1); }
  39. }));
  40. };
  41. more(0);
  42. }).nThen(function (waitFor) {
  43. var next = function (i) {
  44. cjdns.SessionManager_sessionStats(Number(handles[i]), waitFor(function (err, ret) {
  45. if (err) { throw err; }
  46. sessions.push(ret);
  47. i++
  48. if (i < handles.length) { next(i); }
  49. }));
  50. };
  51. next(0);
  52. }).nThen(function (waitFor) {
  53. cjdns.disconnect();
  54. sessions.sort(function (a, b) {
  55. return (a.addr.substring(a.addr.indexOf('.')) < b.addr.substring(b.addr.indexOf('.')))
  56. ? -1 : 1;
  57. });
  58. for (var i = 0; i < sessions.length; i++) {
  59. printSession(sessions[i]);
  60. }
  61. });