rendered paste bodyimport flash.external.ExternalInterface;
var connections = new Array();
var results = new Object();
var executedTests, total;
Array.prototype.indexOf = function(elem) {
for (var i=0; i < this.length; i++) {
if (this[i] == elem) {
return i;
}
}
return -1;
};
start_ports_and_protocol_test = function(server, application) {
ExternalInterface.call("console.log", "Iniciando teste de portas e protocolo\n");
executedTests = 0;
total = 4;
doTest(server, application, "rtmp", 1935);
doTest(server, application, "rtmp", 80);
doTest(server, application, "rtmpt", 1935);
doTest(server, application, "rtmpt", 80);
};
function doTest(host, application, protocol, port) {
var nc = new NetConnection();
var url = protocol +"://" + host + ":" + port + "/" + application;
nc.onStatus = function(info) {
if (connectionExists(nc)) {
removeConnection(nc); // eh o evento de close
} else {
addConnection(nc);
addResult(nc, info);
if (executedTests == total) {
finishTests();
}
}
};
nc.connect(url);
}
function connectionExists(nc) {
return (connections.indexOf(nc) != -1);
}
function removeConnection(nc) {
connections.splice( connections.indexOf(nc), 1 );
}
function addConnection(nc) {
connections.push(nc);
}
function addResult(nc, info) {
executedTests++;
results[nc.uri] = info.code;
}
function finishTests() {
for (var prop in results) {
if (results.hasOwnProperty(prop)) {
ExternalInterface.call("ports_and_protocol_test_finished", results[prop]);
}
}
}
function closeAllConnections() {
ExternalInterface.call("console.log", "Connections.length: " + connections.length + "\n");
if (connections.length) {
for (var i=0; i < connections.length; i++) {
ExternalInterface.call("console.log", "fechando conexao #" + i + "\n");
connections[i].close();
}
} else {
ExternalInterface.call("console.log", "Nao tem mais nada pra fechar\n");
}
}
/* #################################################################################### */
function connectMe(backend_test_server, backend_test_application) {
rec_nc = new NetConnection();
rec_nc.onStatus = function(info) {
if (info.code == "NetConnection.Connect.Success") {
speedTest(rec_nc);
}
};
rec_nc.connect(backend_test_server + backend_test_application);
}
start_speed_test = function(backend_test_server, backend_test_application) {
connectMe(backend_test_server, backend_test_application);
}
function speedTest(nc) {
_global.bwInfo = new BandwidthInfo(nc);
ExternalInterface.call("console.log", "Iniciando o teste de velocidade de conexao");
_global.bwInfo.start();
}
for (i=0; i<1000; i++) {
data += "C->S";
}
function BandwidthInfo(nc) {
this.nc = nc;
this.maxLength = 10;
this.bwInHistory = new Array(this.maxLength);
this.bwInCtr = 0;
this.bwOutHistory = new Array(this.maxLength);
this.bwOutCtr = 0;
this.pingHistory = new Array(this.maxLength);
this.headIn = 0;
this.headOut = 0;
this.headPing = 0;
this.bBWOutStop = false;
this.bBWInStop = false;
this.onBWInTimeout = function() {
clearInterval(this.bwInfoTimeout);
this.bwInfoTimeout = null;
delete this.bwInfoTimeout;
this.bBWInStop = true;
if (this.bwInCtr == 0) {
return;
}
this.stop();
};
this.onBWOutTimeout = function() {
clearInterval(this.bwInfoTimeout);
this.bwInfoTimeout = null;
delete this.bwInfoTimeout;
this.bBWOutStop = true;
if (this.bwOutCtr == 0) {
return;
}
this.bwInfoTimeout = setInterval(this, "onBWInTimeout", 5*1000);
this.serverToClient();
};
this.clientToServer = function() {
this.time = getTimer();
size = 0;
bwinfo = this;
ExternalInterface.call("console.log", "Cliente pra servidor");
this.nc.ack = function(pingVal) {
if (!bwinfo.bBWOutStop) {
bwinfo.bwOutHistory[bwinfo.headOut++%bwinfo.maxLength] = Math.floor(size/(getTimer()-bwinfo.time)*1000);
bwinfo.pingHistory[bwinfo.headPing++%bwinfo.maxLength] = pingVal;
bwinfo.nc.call("recData", 0, data);
size += 4000;
bwinfo.bwOutCtr++;
}
};
this.nc.call("recData", 0, data);
this.nc.call("recData", 0, data);
};
this.serverToClient = function() {
this.time = getTimer();
size = 0;
bwinfo = this;
ExternalInterface.call("console.log", "Servidor pra cliente");
nc.onEcho = function() {
if (!bwinfo.bBWInStop) {
bwinfo.bwInHistory[bwinfo.headIn++%bwinfo.maxLength] = Math.floor(size/(getTimer()-bwinfo.time)*1000);
this.call("echoData", 0, 0);
size += 4000;
bwinfo.bwInCtr++;
}
};
nc.call("echoData", 0, 0);
nc.call("echoData", 0, 0);
};
this.start = function() {
clearInterval(this.bwInfoTimeout);
this.bwInfoTimeout = null;
delete this.bwInfoTimeout;
this.bwInfoTimeout = setInterval(this, "onBWOutTimeout", 5*1000);
this.clientToServer();
};
this.stop = function() {
this.nc = null;
var ping_rtt = 0;
var ping_rtt_max = 0;
var bw_out = 0;
var bw_in = 0;
ExternalInterface.call("console.log", "Finalizado, fazendo matematica");
for (var i = 0; i<this.maxLength && i<this.bwOutCtr; i++) {
ping_rtt += this.pingHistory[i];
ping_rtt_max = Math.max(ping_rtt_max, this.pingHistory[i]);
}
ping_rtt = ping_rtt/i;
for (var i = 0; i<this.maxLength && i<this.bwOutCtr; i++) {
bw_out += this.bwOutHistory[i];
}
bw_out /= Math.min(this.maxLength, this.bwOutCtr);
bw_out = Math.round((bw_out/1024)*8);
for (var i = 0; i<this.maxLength && i<this.bwInCtr; i++) {
bw_in += this.bwInHistory[i];
}
bw_in /= Math.min(this.maxLength, this.bwInCtr);
bw_in = Math.round((bw_in/1024)*8);
ExternalInterface.call("speed_test_finished", bw_out, bw_in, ping_rtt_max, ping_rtt);
};
}
ExternalInterface.addCallback("start_speed_test", this, start_speed_test);
ExternalInterface.addCallback("start_ports_and_protocol_test", this, start_ports_and_protocol_test);
stop();