forked from cjdelisle/cjdns
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsessionStats
executable file
·76 lines (65 loc) · 2.46 KB
/
sessionStats
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env node
/* -*- Mode:Js */
/* vim: set expandtab ts=4 sw=4: */
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var Cjdns = require('./lib/cjdnsadmin/cjdnsadmin');
var nThen = require('nthen');
var printSession = function (session) {
var state = session.state.replace(/CryptoAuth_/,'');
while (state.length < ('ESTABLISHED').length) { state = state + ' ' }
var out = [ session.addr, ' ', state, ' ', session.handle, ' ', session.sendHandle ];
if (Number(session.duplicates) !== 0) { out.push(' DUP ', session.duplicates); }
if (Number(session.lostPackets) !== 0) { out.push(' LOS ', session.lostPackets); }
if (Number(session.receivedOutOfRange) !== 0) { out.push(' OOR ', session.receivedOutOfRange); }
console.log(out.join(''));
};
var cjdns;
var handles = [];
var sessions = [];
nThen(function (waitFor) {
Cjdns.connectWithAdminInfo(waitFor(function (c) { cjdns = c; }));
}).nThen(function (waitFor) {
var more = function (i) {
cjdns.SessionManager_getHandles(i, waitFor(function (err, ret) {
if (err) { throw err; }
handles.push.apply(handles, ret.handles);
if (ret.more) { more(i+1); }
}));
};
more(0);
}).nThen(function (waitFor) {
var next = function (i) {
cjdns.SessionManager_sessionStats(Number(handles[i]), waitFor(function (err, ret) {
if (err) { throw err; }
sessions.push(ret);
i++
if (i < handles.length) { next(i); }
}));
};
if (handles.length) {
next(0);
} else {
console.log("No active sessions");
}
}).nThen(function (waitFor) {
cjdns.disconnect();
sessions.sort(function (a, b) {
return (a.addr.substring(a.addr.indexOf('.')) < b.addr.substring(b.addr.indexOf('.')))
? -1 : 1;
});
for (var i = 0; i < sessions.length; i++) {
printSession(sessions[i]);
}
});