<script type="text/javascript">//<![CDATA[

	var peersURI = '<%=luci.dispatcher.build_url("admin", "services", "cjdns", "peers")%>';
	var updatePeers = function(x, peers) {
		var table = document.getElementById('cjdns-peerings');
		while (table.rows.length > 1) {
			table.deleteRow(1);
		}

		if ((peers) && ((peers.err) || (typeof peers.length === 'undefined'))) {
			var errpeer = (peers.err)
						? 'Socket Error: unable to connect to Admin API'
						: 'No active peers';
			var row = table.insertRow(-1);
			row.className = 'cbi-section-table-row';
			var cell = row.insertCell(-1);
			cell.colSpan = 7;
			cell.textContent = errpeer;
			return;
		};

		peers.forEach(function(peer, i) {
			if (peer.user == null) {
				var user = '';
			} else if (peer.user == 'Local Peers') {
				var user = 'beacon';
			} else {
				var user = peer.user;
			}

			if (peer.isIncoming === 0) {
				var interface = 'outgoing';
			} else {
				var interface = 'incoming';
			}

			var status = interface + ', ' + peer.state.toLowerCase();

			if (peer.version === 0) {
				var version = '-';
			} else {
				var version = 'v' + peer.version;
			}

			var rxtx = lbbytes(peer.bytesIn) + ' / ' + lbbytes(peer.bytesOut);

			var row = table.insertRow(-1);
			row.className = 'cbi-section-table-row cbi-rowstyle-' + ((i % 2) + 1);
			row.insertCell(-1).textContent = user;
			row.insertCell(-1).textContent = peer.ipv6;
			row.insertCell(-1).textContent = status;
			row.insertCell(-1).textContent = version;
			row.insertCell(-1).textContent = rxtx;
			var latencyCell = row.insertCell(-1);
			latencyCell.textContent = 'waiting';

			var pingURI = '<%=luci.dispatcher.build_url("admin", "services", "cjdns", "ping")%>';
			var timeout = 2000;
			XHR.get(pingURI, { label: peer.switchLabel, timeout: timeout }, function(x, pong) {
				var pongrsp = ((pong.err == "ai:recv > timeout") || (pong == "undefined") || (pong.ms >= timeout))
					? '> ' + timeout + ' ms'
					: pong.ms + ' ms';
				latencyCell.textContent = pongrsp;
			})
		});

	};

	XHR.get(peersURI, null, updatePeers);
	XHR.poll(5, peersURI, null, updatePeers);

//]]></script>

<script type="text/javascript">
<%# Author: [GitHub/75lb] -%>
//<![CDATA[
function lbbytes (bytes){

	var kilobyte = 1024,
	    megabyte = kilobyte * 1024,
	    gigabyte = megabyte * 1024,
	    terabyte = gigabyte * 1024;

	if ((bytes >= 0) && (bytes < kilobyte)) {
		return bytes + " B";
	} else if ((bytes >= kilobyte) && (bytes < megabyte)) {
		return (bytes / kilobyte).toFixed(2) + " KB";
	} else if ((bytes >= megabyte) && (bytes < gigabyte)) {
		return (bytes / megabyte).toFixed(2) + " MB";
	} else if ((bytes >= gigabyte) && (bytes < terabyte)) {
		return (bytes / gigabyte).toFixed(2) + " GB";
	} else if (bytes >= terabyte) {
		return (bytes / terabyte).toFixed(2) + " TB";
	} else {
		return bytes + " B";
	}
};
//]]>
</script>

<fieldset class="cbi-section">
	<legend>Active cjdns peers</legend>
	<table class="cbi-section-table" id="cjdns-peerings">
		<tr class="cbi-section-table-titles">
			<th class="cbi-section-table-cell">User/Name</th>
			<th class="cbi-section-table-cell">IPv6</th>
			<th class="cbi-section-table-cell">Status</th>
			<th class="cbi-section-table-cell">Version</th>
			<th class="cbi-section-table-cell">Rx / Tx</th>
			<th class="cbi-section-table-cell">Latency</th>
		</tr>
		<tr class="cbi-section-table-row">
			<td colspan="7">Querying Admin API</td>
		</tr>
	</table>
</fieldset>
