<%+header%>

<%
	local uci = require("luci.model.uci").cursor()
%>

<script type="text/javascript" src="<%=resource%>/cbi.js?v=git-18.193.28471-ee087a1"></script>
<script type="text/javascript">//<![CDATA[
    var stxhr = new XHR();

    function update_speed(field, proto, mode,omit,parallel,transmit,bitrate)
    {
	update_upload(field,proto,mode,omit,parallel,transmit,bitrate);
    }
    
    function update_upload(field, proto, mode,omit,parallel,transmit,bitrate)
    {
	var tool = field.name;
	var addr = field.value;

	var upload = document.getElementById('iperf-upload');

	if (upload)
	{
	    upload.innerHTML =
		'<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" /> ' + '<%:Upload%> - ' +
		'<%:Waiting for command to complete...%>'
	    ;

	    stxhr.post('<%=url('admin/services/iperf')%>/run_test' + '/' + addr + '/' + proto + '/' + mode + '/upload' + '/' + omit + '/' + parallel + '/' + transmit + '/' + bitrate, { token: '<%=token%>' },
		function(x)
		{
		    if (x.responseText)
		    {
			var response = JSON.parse(x.responseText);
			if (response.error)
			{
				upload.innerHTML = String.format('<%:Upload%> - <pre>%s</pre>', response.error );
			} else {
				var sent_speed = (response.end.sum_sent.bits_per_second/1000000);
				var received_speed = (response.end.sum_received.bits_per_second/1000000);
				var server = response.start.connecting_to.host;
				upload.innerHTML = String.format('<pre><%:Upload%> - Server: %s - Sender: %sMb/s - Receiver: %sMb/s</pre>', server, sent_speed.toFixed(2), received_speed.toFixed(2) );
			}
		    }
		    else
		    {
			upload.innerHTML = '<%:Upload%> - <span class="error"><%:Bad address specified!%></span>';
		    }
		    update_download(field,proto,mode,omit,parallel,transmit,bitrate);
		}
	    );
	}
    }
    
    function update_download(field, proto, mode,omit,parallel,transmit,bitrate)
    {
	var tool = field.name;
	var addr = field.value;

	var download = document.getElementById('iperf-download');

	if (download)
	{
	    download.innerHTML =
		'<img src="<%=resource%>/icons/loading.gif" alt="<%:Loading%>" style="vertical-align:middle" /> ' + '<%:Download%> - ' +
		'<%:Waiting for command to complete...%>'
	    ;

	    stxhr.post('<%=url('admin/services/iperf')%>/run_test' + '/' + addr + '/' + proto + '/' + mode + '/download' + '/' + omit + '/' + parallel + '/' + transmit + '/' + bitrate, { token: '<%=token%>' },
		function(x)
		{
		    if (x.responseText)
		    {
			var response = JSON.parse(x.responseText);
			if (response.error)
			{
				download.innerHTML = String.format('<%:Download%> - <pre>%s</pre>', response.error );
			} else {
				var sent_speed = (response.end.sum_sent.bits_per_second/1000000);
				var received_speed = (response.end.sum_received.bits_per_second/1000000);
				var server = response.start.connecting_to.host;
				download.innerHTML = String.format('<pre><%:Download%> - Server: %s - Sender: %sMb/s - Receiver: %sMb/s</pre>', server, sent_speed.toFixed(2), received_speed.toFixed(2) );
			}
		    }
		    else
		    {
			download.innerHTML = '<%:Download%> - <span class="error"><%:Bad address specified!%></span>';
		    }
		}
	    );
	}
    }
//]]></script>

<% if stderr and #stderr > 0 then %><pre class="error"><%=pcdata(stderr)%></pre><% end %>
<form class="inline" method="post" action="<%=url('admin/services/iperf/run_test')%>">
    <div class="cbi-map">
	<h2 name="content"><%:iPerf speed tests%></h2>
	<div class="cbi-map-descr"><%:This iPerf interface is in bêta. No support for this.%></div>
	<fieldset class="cbi-section" id="networks">
	    <legend><%:Settings%></legend>
	    <div class="cbi-section-descr"></div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Mode of operation%></label>
		<div class="cbi-value-field">
		    <select class="cbi-input-select" name="mode">
			<option value="tcp">TCP</option>
			<option value="udp">UDP</option>
		    </select>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Internet protocol%></label>
		<div class="cbi-value-field">
		    <select class="cbi-input-select" name="proto">
			<option value="ipv4">IPv4</option>
			<option value="ipv6">IPv6</option>
		    </select>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Target bitrate (Mbits/s)%></label>
		<div class="cbi-value-field">
		    <input name="bitrate" data-type="uinteger" type="text" class="cbi-input-text" value="0"/>
		     <br />
		    <div class="cbi-value-description">
			<%:0 for unlimited. Need to be limited for UDP test%>
		    </div>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Number of parallel client streams to run%></label>
		<div class="cbi-value-field">
		    <input name="parallel" data-type="uinteger" type="text" class="cbi-input-text" value="1"/>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Omit the first n seconds%></label>
		<div class="cbi-value-field">
		    <input name="omit" data-type="uinteger" type="text" class="cbi-input-text" value="3"/>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Time to transmit for (s)%></label>
		<div class="cbi-value-field">
		    <input name="transmit" data-type="uinteger" type="text" class="cbi-input-text" value="5"/>
		</div>
	    </div>
	    <div class="cbi-value">
		<label class="cbi-value-title"><%:Server%></label>
		<div class="cbi-value-field">
		    <select class="cbi-input-select" name="addr">
		    <%
			uci:foreach("iperf","server", function(s)
			    local server = s[".name"]
		    %>
			<option value="<%=server%>"><%=string.gsub(server,"_","-")%></option>
		    <%
			end)
		    %>
		    </select>
		     <br />
		    <div class="cbi-value-description">
			<%:Server VPS IP is bypassed, so this will test only default route speed.%>
		    </div>
		</div>
	    </div>
	    <input type="button" value="<%:Test%>" class="cbi-button cbi-button-apply" onclick="update_speed(this.form.addr,this.form.proto.value,this.form.mode.value,this.form.omit.value,this.form.parallel.value,this.form.transmit.value,this.form.bitrate.value)" />
	</fieldset>
    </div>
</form>
    
<div class="cbi-section">
    <span id="iperf-upload"></span>
    <span id="iperf-download"></span>
</div>
<%+footer%>
