#!/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 <https://www.gnu.org/licenses/>.
 */
var Cjdns = require('./lib/cjdnsadmin/cjdnsadmin');
var PublicToIp6 = require('./lib/publicToIp6');

Cjdns.connectAsAnon(function (cjdns) {

    var again = function (i) {
        cjdns.InterfaceController_peerStats(i, function (err, ret) {
            if (err) { throw err; }
            ret.peers.forEach(function (peer) {
                p = peer['lladdr'] + ' ' + peer['addr'] + ' ' + peer['state'] +
                    ' in ' + peer['recvKbps'] + 'kb/s' +
                    ' out ' + peer['sendKbps'] + 'kb/s';

                    if (Number(peer['duplicates']) !== 0) {
                        p += ' ' + ' DUP ' + peer['duplicates'];
                    }
                    if (Number(peer['lostPackets']) !== 0) {
                        p += ' ' + ' LOS ' + peer['lostPackets'];
                    }
                    if (Number(peer['receivedOutOfRange']) !== 0) {
                        p += ' ' + ' OOR ' + peer['receivedOutOfRange'];
                    }
                if (typeof(peer.user) === 'string') {
                    p += ' "' + peer['user'] + '"';
                }
                console.log(p);
            });
            if (typeof(ret.more) !== 'undefined') {
                again(i+1);
            } else {
                cjdns.disconnect();
            }
        });
    };
    again(0);

});
