diff --git a/chrome/.bowerrc b/chrome/.bowerrc new file mode 100644 index 0000000..5d0c33e --- /dev/null +++ b/chrome/.bowerrc @@ -0,0 +1,3 @@ +{ + "directory": "app/vendors" +} diff --git a/chrome/app/main.js b/chrome/app/main.js index b6d48a8..97c5bb4 100644 --- a/chrome/app/main.js +++ b/chrome/app/main.js @@ -1,34 +1,48 @@ -var glosa = undefined; -var loaded = false; +var app = { + chooser: null, + glosa: undefined, + loaded: false, + lastReq: { + url: null, + millis: null, + response: null + } +}; function onLoadPlayer() { - console.log('onLoadPlayer glosa:' + glosa); - if ( glosa !== undefined ) { - SendMessage('Avatar', 'catchGlosa', glosa); + if ( app.glosa !== undefined ) { + SendMessage('Avatar', 'catchGlosa', app.glosa); } - loaded = true; + app.loaded = true; } chrome.runtime.onMessage.addListener( function(request, sender, sendResponse) { - req = new XMLHttpRequest(); - req.open('GET', 'http://150.165.204.30:9000/glosa?texto=' + request.selectedText, true); + app.chooser = app.chooser || new qdClient.Chooser(); document.getElementById('loading-screen').style.display = 'block'; - req.onreadystatechange = function () { - if (req.readyState == 4) { - console.log('Translate callback: loaded: ' + loaded); - glosa = req.responseText; - - document.getElementById('loading-screen').style.display = 'none'; - if (loaded == true) { - SendMessage('Avatar', 'catchGlosa', glosa); - } - } - }; - - req.send(); + app.chooser.choose( + app.lastReq.url, + app.lastReq.millis, + app.lastReq.response, + function (url) { + var start = new Date().getTime(); + + qdClient.request(url + '?texto=' + request.selectedText, "GET", {}, + function(status, response) { + app.lastReq.response = status === 404 ? -1 : status; + app.lastReq.millis = (new Date().getTime() - start); + app.lastReq.url = url; + + app.glosa = response; + + document.getElementById('loading-screen').style.display = 'none'; + if (app.loaded == true) { + SendMessage('Avatar', 'catchGlosa', app.glosa); + } + }); + }); }); chrome.runtime.sendMessage({ready: true}); diff --git a/chrome/app/vendors/qdclient/.bower.json b/chrome/app/vendors/qdclient/.bower.json new file mode 100644 index 0000000..52c53c5 --- /dev/null +++ b/chrome/app/vendors/qdclient/.bower.json @@ -0,0 +1,27 @@ +{ + "name": "qdclient", + "main": [ + "qdclient.js" + ], + "ignore": [ + "**/.*", + "Gulpfile.js", + "demo", + "node_modules", + "vendor", + "tests" + ], + "devDependencies": { + "mocha": "~2.2.5", + "chai": "~3.0.0" + }, + "_release": "fab153cf1a", + "_resolution": { + "type": "branch", + "branch": "master", + "commit": "fab153cf1a708cab90a72e4654d65d654f11370e" + }, + "_source": "git@git.lavid.ufpb.br:qdclient.git", + "_target": "*", + "_originalSource": "git@git.lavid.ufpb.br:qdclient.git" +} \ No newline at end of file diff --git a/chrome/app/vendors/qdclient/README b/chrome/app/vendors/qdclient/README new file mode 100644 index 0000000..15eab8e --- /dev/null +++ b/chrome/app/vendors/qdclient/README @@ -0,0 +1,32 @@ +qdClient +========= + +1. Tools +---------------- + +1. NodeJS +2. NPM - Node Package Manager +3. Bower - Frontend dependencies +4. Gulp - Build tools + +2. Installing dependencies +---------------- + +```bash +$ npm install +$ bower install +``` + +3. Demo +---------------- +```bash +$ gulp demo +``` + +4. Building +---------------- +```bash +$ gulp build +``` + +**Enjoy** diff --git a/chrome/app/vendors/qdclient/bower.json b/chrome/app/vendors/qdclient/bower.json new file mode 100644 index 0000000..98c58ce --- /dev/null +++ b/chrome/app/vendors/qdclient/bower.json @@ -0,0 +1,19 @@ +{ + "name": "qdclient", + "version": "0.0.1", + "main": [ + "qdclient.js" + ], + "ignore": [ + "**/.*", + "Gulpfile.js", + "demo", + "node_modules", + "vendor", + "tests" + ], + "devDependencies": { + "mocha": "~2.2.5", + "chai": "~3.0.0" + } +} diff --git a/chrome/app/vendors/qdclient/lib/chooser.js b/chrome/app/vendors/qdclient/lib/chooser.js new file mode 100644 index 0000000..351780a --- /dev/null +++ b/chrome/app/vendors/qdclient/lib/chooser.js @@ -0,0 +1,224 @@ +(function(window, localStorage, undefined) { + 'use strict'; + + function Chooser() { + this.server = 'http://150.165.204.39/vlibras-cloud/get_servers.php'; + + this.options = { + THRESHOLDSLOW: 2000, + SLOWLIMITTRIES: 5, + NBESTHEURISTIC1: 2, + QUANTITYPREVIOUSTIMESSTORED: 10 + }; + + this.mirrors = []; + + this.heuristics = { + FIRSTHEURISTIC: { + FIRSTRANDOM : {value: 0, name: "Random"}, + FIRSTRANDOMNBEST: {value: 1, name: "Random Among N Best"}, + FIRSTBEST : {value: 2, name: "Best"} + }, + SECONDHEURISTIC: { + SECONDRANDOM : {value: 0, name: "Random"}, + SECONDCLOSEST: {value: 1, name: "Closest"} + }, + THIRDHEURISTIC: { + THIRDSIMPLEAVERAGE : {value: 0, name: "Simple Average"}, + THIRDWEIGHTEDAVERAGE: {value: 1, name: "Weighted Average"} + } + }; + } + + // Exports + qdClient.Chooser = Chooser; + + Chooser.prototype.choose = function(urlLast, millisLast, response, callback) { + var $this = this; + + if (this.mirrors.length === 0) { + this.loadMirrors_(function() { + $this.chooseServer_(urlLast, millisLast, response, callback); + }); + } else { + this.chooseServer_(urlLast, millisLast, response, callback); + } + }; + + Chooser.prototype.chooseServer_ = function(urlLast, millisLast, response, callback) { + var firstHeuristic = this.heuristics.FIRSTHEURISTIC.FIRSTBEST; + var secondHeuristic = this.heuristics.SECONDHEURISTIC.SECONDRANDOM; + var thirdHeuristic = this.heuristics.THIRDHEURISTIC.THIRDWEIGHTEDAVERAGE; + + var j = 0, i = 0; /* Fix JSHint */ + + var sizeBest = this.options.NBESTHEURISTIC1; + if (firstHeuristic == this.heuristics.FIRSTHEURISTIC.FIRSTBEST) { + sizeBest = 1; + } + + if (urlLast && urlLast.length > 0) { + for (i = 0; i < this.mirrors.length; i++) { + if (this.mirrors[i].url.toLowerCase() == urlLast.toLowerCase()) { + if (this.mirrors[i].previousTimes.length < this.options.QUANTITYPREVIOUSTIMESSTORED) { + this.mirrors[i].previousTimes[this.mirrors[i].previousTimes.length] = millisLast; + + } else { + for (j = this.options.QUANTITYPREVIOUSTIMESSTORED-1; j > 0; j--) { + this.mirrors[i].previousTimes[j-1] = this.mirrors[i].previousTimes[j]; + } + + this.mirrors[i].previousTimes[this.options.QUANTITYPREVIOUSTIMESSTORED-1] = millisLast; + + } + + if (thirdHeuristic == this.heuristics.THIRDHEURISTIC.THIRDSIMPLEAVERAGE) { + var totalMillis = 0; + for (j = 0; j < this.mirrors[i].previousTimes.length; j++) { + totalMillis += this.mirrors[i].previousTimes[j]; + } + + this.mirrors[i].averageTime = totalMillis/this.mirrors[i].previousTimes.length; + + } else if (thirdHeuristic == this.heuristics.THIRDHEURISTIC.THIRDWEIGHTEDAVERAGE) { + var totalMillisAndWeights = 0; + var totalWeights = 0; + for (j = 0; j < this.mirrors[i].previousTimes.length; j++) { + totalMillisAndWeights += this.mirrors[i].previousTimes[j]*(j+1); + totalWeights += (j+1); + } + + this.mirrors[i].averageTime = totalMillisAndWeights/totalWeights; + + } + + if (millisLast > this.options.THRESHOLDSLOW || response == -1) { + this.mirrors[i].isSlow = true; + this.mirrors[i].remainingSlow = this.options.SLOWLIMITTRIES; + } + + localStorage.setItem("servers", JSON.stringify(this.mirrors)); + break; + } + + } + + } + + var position = Math.floor((Math.random() * this.mirrors.length)); + var closestPosition; + + if (firstHeuristic != this.heuristics.FIRSTHEURISTIC.FIRSTRANDOM) { + position = -1; + closestPosition = -1; + } + + var vectorBest = []; + var k = 0, z = 0; /* JSHint Fix */ + + for (j = 0; j < this.mirrors.length; j++) { + if (this.mirrors[j].remainingSlow > 0) { + this.mirrors[j].remainingSlow = this.mirrors[j].remainingSlow - 1; + } + + if (this.mirrors[j].remainingSlow === 0) { + this.mirrors[j].isSlow = false; + } + + if (firstHeuristic != this.heuristics.FIRSTHEURISTIC.FIRSTRANDOM && this.mirrors[j].isSlow === false) { + if (closestPosition == -1) { + closestPosition = j; + } + + var added = false; + + for (k = 0; k < vectorBest.length; k++) { + if (this.mirrors[j].averageTime < vectorBest[k].averageTime || (this.mirrors[j].averageTime == vectorBest[k].averageTime && (Math.floor(Math.random()) < 0.5) )) { + + added = true; + + if (vectorBest.length < sizeBest) { + for (z = vectorBest.length-1; z >= k; z--) { + vectorBest[z+1] = vectorBest[z]; + } + } else { + for (z = vectorBest.length-2; z >= k; z--) { + vectorBest[z+1] = vectorBest[z]; + } + } + + vectorBest[k] = this.mirrors[j]; + + break; + } + } + + if (added === false && vectorBest.length < sizeBest) { + vectorBest[vectorBest.length] = this.mirrors[j]; + } + + } + + } + + // pega aleatorio entre as N posicoes dos melhores (se for guloso, tera apenas um elemento no vetor) + if (firstHeuristic != this.heuristics.FIRSTHEURISTIC.FIRSTRANDOM && vectorBest.length > 0) { + position = Math.floor((Math.random() * vectorBest.length)); + } + + if (position == -1) { + if (secondHeuristic == this.heuristics.SECONDHEURISTIC.SECONDCLOSEST) { + // escolhendo o mais proximo, no caso, o vetor já está ordenado + if (closestPosition != -1) { + position = closestPosition; + } else { + // se nao marcou nenhum como closest, todos devem estar como slow, nesse caso seleciona o primeiro que é o mais próximo + position = 0; + } + + } else { + position = Math.floor((Math.random() * this.mirrors.length)); + } + } + + callback(this.mirrors[position].url); + }; + + Chooser.prototype.loadMirrors_ = function(callback) { + var $this = this; + + var localServers = JSON.parse(localStorage.getItem("servers")) || []; + + qdClient.getJSON(this.server, function (status, response) { + var servers = status === 200 ? response : []; + + for (var i = 0; i < servers.length; i++){ + var mirror = { + name: servers[i].nome, + url: servers[i].url, + averageTime: 99999999, + isSlow: false, + previousTimes: [], + remainingSlow: 0, + type: servers[i].type + }; + + for (var localServer in localServers) { + if (mirror.url == localServer.url) { + mirror.averageTime = localServer.averageTime; + mirror.isSlow = localServer.isSlow; + mirror.previousTimes = localServer.previousTimes; + mirror.remainingSlow = localServer.remainingSlow; + + break; + } + } + + $this.mirrors.push(mirror); + } + + if (callback instanceof Function) callback(); + }); + }; + +}(window, localStorage)); diff --git a/chrome/app/vendors/qdclient/lib/qdclient.js b/chrome/app/vendors/qdclient/lib/qdclient.js new file mode 100644 index 0000000..086d549 --- /dev/null +++ b/chrome/app/vendors/qdclient/lib/qdclient.js @@ -0,0 +1,34 @@ +(function(window, undefined) { + 'use strict'; + + window.qdClient = window.qdClient || {}; + + qdClient.request = function request(url, method, params, callback) { + var xhr = new XMLHttpRequest(); + var raw_params = ""; + + xhr.open(method, url, true); + + xhr.onload = function() { + callback(xhr.status, xhr.responseText); + }; + + if (method == "POST" && params instanceof Object) { + + for(var param in params) { + raw_params += param + '=' + params[param] + '&'; + } + + if (params.length > 1) raw_params.slice(0, -1); + } + + xhr.send(raw_params); + }; + + qdClient.getJSON = function getJSON(url, callback) { + qdClient.request(url, "GET", {}, function(status, response) { + callback(status, JSON.parse(response)); + }); + }; + +}(window)); diff --git a/chrome/app/vendors/qdclient/lib/serverChooser.js b/chrome/app/vendors/qdclient/lib/serverChooser.js new file mode 100644 index 0000000..1cc9e71 --- /dev/null +++ b/chrome/app/vendors/qdclient/lib/serverChooser.js @@ -0,0 +1,209 @@ +var THRESHOLDSLOW = 2000; +var SLOWLIMITTRIES = 5; +var NBESTHEURISTIC1 = 2; +var QUANTITYPREVIOUSTIMESSTORED = 10; + +var mirrors = []; + +var FIRSTHEURISTIC = { + FIRSTRANDOM : {value: 0, name: "Random"}, + FIRSTRANDOMNBEST: {value: 1, name: "Random Among N Best"}, + FIRSTBEST : {value: 2, name: "Best"} +}; + +var SECONDHEURISTIC = { + SECONDRANDOM : {value: 0, name: "Random"}, + SECONDCLOSEST: {value: 1, name: "Closest"} +}; + +var THIRDHEURISTIC = { + THIRDSIMPLEAVERAGE : {value: 0, name: "Simple Average"}, + THIRDWEIGHTEDAVERAGE: {value: 1, name: "Weighted Average"} +}; + +function httpGet(theUrl){ + var xmlHttp = null; + xmlHttp = new XMLHttpRequest(); + xmlHttp.open( "GET", theUrl, false ); + xmlHttp.send( null ); + return JSON.parse(xmlHttp.responseText); +} + + + +function loadMirrors() { + var localServersArray = JSON.parse(localStorage.getItem("serversDatabase")); + if(localServersArray == null){ + localServersArray = []; + } + + var servers = httpGet('http://fuze.cc/sobreApps/teste_lavid/get_servers.php'); + for (var i = 0; i < servers.length; i++){ + var mirror = {}; + mirror.name = servers[i].nome; + mirror.url = servers[i].url; + mirror.averageTime = 99999999; + mirror.isSlow = false; + mirror.previousTimes = []; + mirror.remainingSlow = 0; + mirror.type = servers[i].type; + + for (var j = 0; j < localServersArray.length; j++) { + if (mirror.url == localServersArray[j].url) { + mirror.averageTime = localServersArray[j].averageTime; + mirror.isSlow = localServersArray[j].isSlow; + mirror.previousTimes = localServersArray[j].previousTimes; + mirror.remainingSlow = localServersArray[j].remainingSlow; + + break; + } + } + + mirrors.push(mirror); + } + +} + +function chooseServer(urlLast, millisLast, response) { + if (mirrors.length == 0) { + loadMirrors(); + } + + if (mirrors.length == 0) { + alert("Sem mirrors disponíveis"); + } + + var firstHeuristic = FIRSTHEURISTIC.FIRSTBEST; + var secondHeuristic = SECONDHEURISTIC.SECONDRANDOM; + var thirdHeuristic = THIRDHEURISTIC.THIRDWEIGHTEDAVERAGE; + + var sizeBest = NBESTHEURISTIC1; + if (firstHeuristic == FIRSTHEURISTIC.FIRSTBEST) { + sizeBest = 1; + } + + if (urlLast.length > 0) { + for (var i = 0; i < mirrors.length; i++) { + if (mirrors[i].url.toLowerCase() == urlLast.toLowerCase()) { + if (mirrors[i].previousTimes.length < QUANTITYPREVIOUSTIMESSTORED) { + mirrors[i].previousTimes[mirrors[i].previousTimes.length] = millisLast; + + } else { + for (var j = QUANTITYPREVIOUSTIMESSTORED-1; j > 0; j--) { + mirrors[i].previousTimes[j-1] = mirrors[i].previousTimes[j]; + } + + mirrors[i].previousTimes[QUANTITYPREVIOUSTIMESSTORED-1] = millisLast; + + } + + if (thirdHeuristic == THIRDHEURISTIC.THIRDSIMPLEAVERAGE) { + var totalMillis = 0; + for (var j = 0; j < mirrors[i].previousTimes.length; j++) { + totalMillis += mirrors[i].previousTimes[j]; + } + + mirrors[i].averageTime = totalMillis/mirrors[i].previousTimes.length; + + } else if (thirdHeuristic == THIRDHEURISTIC.THIRDWEIGHTEDAVERAGE) { + var totalMillisAndWeights = 0; + var totalWeights = 0; + for (var j = 0; j < mirrors[i].previousTimes.length; j++) { + totalMillisAndWeights += mirrors[i].previousTimes[j]*(j+1); + totalWeights += (j+1); + } + + mirrors[i].averageTime = totalMillisAndWeights/totalWeights; + + } + + if (millisLast > THRESHOLDSLOW || response == "-1") { + mirrors[i].isSlow = true; + mirrors[i].remainingSlow = SLOWLIMITTRIES; + } + + localStorage.setItem("serversDatabase", JSON.stringify(mirrors)); + break; + } + + } + + } + + var position = Math.floor((Math.random() * mirrors.length)); + var closestPosition; + + if (firstHeuristic != FIRSTHEURISTIC.FIRSTRANDOM) { + position = -1; + closestPosition = -1; + } + + var vectorBest = []; + + for (var j = 0; j < mirrors.length; j++) { + if (mirrors[j].remainingSlow > 0) { + mirrors[j].remainingSlow = mirrors[j].remainingSlow - 1; + } + + if (mirrors[j].remainingSlow == 0) { + mirrors[j].isSlow = false; + } + + if (firstHeuristic != FIRSTHEURISTIC.FIRSTRANDOM && mirrors[j].isSlow == false) { + if (closestPosition == -1) { + closestPosition = j; + } + + var added = false; + + for (var k = 0; k < vectorBest.length; k++) { + if (mirrors[j].averageTime < vectorBest[k].averageTime || (mirrors[j].averageTime == vectorBest[k].averageTime && (Math.floor(Math.random()) < 0.5) )) { + + added = true; + + if (vectorBest.length < sizeBest) { + for (var z = vectorBest.length-1; z >= k; z--) { + vectorBest[z+1] = vectorBest[z]; + } + } else { + for (var z = vectorBest.length-2; z >= k; z--) { + vectorBest[z+1] = vectorBest[z]; + } + } + + vectorBest[k] = mirrors[j]; + + break; + } + } + + if (added == false && vectorBest.length < sizeBest) { + vectorBest[vectorBest.length] = mirrors[j]; + } + + } + + } + + // pega aleatorio entre as N posicoes dos melhores (se for guloso, tera apenas um elemento no vetor) + if (firstHeuristic != FIRSTHEURISTIC.FIRSTRANDOM && vectorBest.length > 0) { + position = Math.floor((Math.random() * vectorBest.length)); + } + + if (position == -1) { + if (secondHeuristic == SECONDHEURISTIC.SECONDCLOSEST) { + // escolhendo o mais proximo, no caso, o vetor já está ordenado + if (closestPosition != -1) { + position = closestPosition; + } else { + // se nao marcou nenhum como closest, todos devem estar como slow, nesse caso seleciona o primeiro que é o mais próximo + position = 0; + } + + } else { + position = Math.floor((Math.random() * mirrors.length)); + } + } + + return mirrors[position].url; +} diff --git a/chrome/app/vendors/qdclient/package.json b/chrome/app/vendors/qdclient/package.json new file mode 100644 index 0000000..b1fdf95 --- /dev/null +++ b/chrome/app/vendors/qdclient/package.json @@ -0,0 +1,18 @@ +{ + "name": "qdclient", + "version": "0.0.1", + "description": "", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "devDependencies": { + "browser-sync": "^2.7.13", + "gulp": "^3.9.0", + "gulp-concat": "^2.6.0", + "gulp-jshint": "^1.11.2", + "gulp-rename": "^1.2.2", + "gulp-rimraf": "^0.1.1", + "gulp-uglify": "^1.2.0", + "run-sequence": "^1.1.1" + } +} diff --git a/chrome/app/vendors/qdclient/qdclient.js b/chrome/app/vendors/qdclient/qdclient.js new file mode 100644 index 0000000..1d00f9d --- /dev/null +++ b/chrome/app/vendors/qdclient/qdclient.js @@ -0,0 +1,259 @@ +(function(window, undefined) { + 'use strict'; + + window.qdClient = window.qdClient || {}; + + qdClient.request = function request(url, method, params, callback) { + var xhr = new XMLHttpRequest(); + var raw_params = ""; + + xhr.open(method, url, true); + + xhr.onload = function() { + callback(xhr.status, xhr.responseText); + }; + + if (method == "POST" && params instanceof Object) { + + for(var param in params) { + raw_params += param + '=' + params[param] + '&'; + } + + if (params.length > 1) raw_params.slice(0, -1); + } + + xhr.send(raw_params); + }; + + qdClient.getJSON = function getJSON(url, callback) { + qdClient.request(url, "GET", {}, function(status, response) { + callback(status, JSON.parse(response)); + }); + }; + +}(window)); + +(function(window, localStorage, undefined) { + 'use strict'; + + function Chooser() { + this.server = 'http://150.165.204.39/vlibras-cloud/get_servers.php'; + + this.options = { + THRESHOLDSLOW: 2000, + SLOWLIMITTRIES: 5, + NBESTHEURISTIC1: 2, + QUANTITYPREVIOUSTIMESSTORED: 10 + }; + + this.mirrors = []; + + this.heuristics = { + FIRSTHEURISTIC: { + FIRSTRANDOM : {value: 0, name: "Random"}, + FIRSTRANDOMNBEST: {value: 1, name: "Random Among N Best"}, + FIRSTBEST : {value: 2, name: "Best"} + }, + SECONDHEURISTIC: { + SECONDRANDOM : {value: 0, name: "Random"}, + SECONDCLOSEST: {value: 1, name: "Closest"} + }, + THIRDHEURISTIC: { + THIRDSIMPLEAVERAGE : {value: 0, name: "Simple Average"}, + THIRDWEIGHTEDAVERAGE: {value: 1, name: "Weighted Average"} + } + }; + } + + // Exports + qdClient.Chooser = Chooser; + + Chooser.prototype.choose = function(urlLast, millisLast, response, callback) { + var $this = this; + + if (this.mirrors.length === 0) { + this.loadMirrors_(function() { + $this.chooseServer_(urlLast, millisLast, response, callback); + }); + } else { + this.chooseServer_(urlLast, millisLast, response, callback); + } + }; + + Chooser.prototype.chooseServer_ = function(urlLast, millisLast, response, callback) { + var firstHeuristic = this.heuristics.FIRSTHEURISTIC.FIRSTBEST; + var secondHeuristic = this.heuristics.SECONDHEURISTIC.SECONDRANDOM; + var thirdHeuristic = this.heuristics.THIRDHEURISTIC.THIRDWEIGHTEDAVERAGE; + + var j = 0, i = 0; /* Fix JSHint */ + + var sizeBest = this.options.NBESTHEURISTIC1; + if (firstHeuristic == this.heuristics.FIRSTHEURISTIC.FIRSTBEST) { + sizeBest = 1; + } + + if (urlLast && urlLast.length > 0) { + for (i = 0; i < this.mirrors.length; i++) { + if (this.mirrors[i].url.toLowerCase() == urlLast.toLowerCase()) { + if (this.mirrors[i].previousTimes.length < this.options.QUANTITYPREVIOUSTIMESSTORED) { + this.mirrors[i].previousTimes[this.mirrors[i].previousTimes.length] = millisLast; + + } else { + for (j = this.options.QUANTITYPREVIOUSTIMESSTORED-1; j > 0; j--) { + this.mirrors[i].previousTimes[j-1] = this.mirrors[i].previousTimes[j]; + } + + this.mirrors[i].previousTimes[this.options.QUANTITYPREVIOUSTIMESSTORED-1] = millisLast; + + } + + if (thirdHeuristic == this.heuristics.THIRDHEURISTIC.THIRDSIMPLEAVERAGE) { + var totalMillis = 0; + for (j = 0; j < this.mirrors[i].previousTimes.length; j++) { + totalMillis += this.mirrors[i].previousTimes[j]; + } + + this.mirrors[i].averageTime = totalMillis/this.mirrors[i].previousTimes.length; + + } else if (thirdHeuristic == this.heuristics.THIRDHEURISTIC.THIRDWEIGHTEDAVERAGE) { + var totalMillisAndWeights = 0; + var totalWeights = 0; + for (j = 0; j < this.mirrors[i].previousTimes.length; j++) { + totalMillisAndWeights += this.mirrors[i].previousTimes[j]*(j+1); + totalWeights += (j+1); + } + + this.mirrors[i].averageTime = totalMillisAndWeights/totalWeights; + + } + + if (millisLast > this.options.THRESHOLDSLOW || response == -1) { + this.mirrors[i].isSlow = true; + this.mirrors[i].remainingSlow = this.options.SLOWLIMITTRIES; + } + + localStorage.setItem("servers", JSON.stringify(this.mirrors)); + break; + } + + } + + } + + var position = Math.floor((Math.random() * this.mirrors.length)); + var closestPosition; + + if (firstHeuristic != this.heuristics.FIRSTHEURISTIC.FIRSTRANDOM) { + position = -1; + closestPosition = -1; + } + + var vectorBest = []; + var k = 0, z = 0; /* JSHint Fix */ + + for (j = 0; j < this.mirrors.length; j++) { + if (this.mirrors[j].remainingSlow > 0) { + this.mirrors[j].remainingSlow = this.mirrors[j].remainingSlow - 1; + } + + if (this.mirrors[j].remainingSlow === 0) { + this.mirrors[j].isSlow = false; + } + + if (firstHeuristic != this.heuristics.FIRSTHEURISTIC.FIRSTRANDOM && this.mirrors[j].isSlow === false) { + if (closestPosition == -1) { + closestPosition = j; + } + + var added = false; + + for (k = 0; k < vectorBest.length; k++) { + if (this.mirrors[j].averageTime < vectorBest[k].averageTime || (this.mirrors[j].averageTime == vectorBest[k].averageTime && (Math.floor(Math.random()) < 0.5) )) { + + added = true; + + if (vectorBest.length < sizeBest) { + for (z = vectorBest.length-1; z >= k; z--) { + vectorBest[z+1] = vectorBest[z]; + } + } else { + for (z = vectorBest.length-2; z >= k; z--) { + vectorBest[z+1] = vectorBest[z]; + } + } + + vectorBest[k] = this.mirrors[j]; + + break; + } + } + + if (added === false && vectorBest.length < sizeBest) { + vectorBest[vectorBest.length] = this.mirrors[j]; + } + + } + + } + + // pega aleatorio entre as N posicoes dos melhores (se for guloso, tera apenas um elemento no vetor) + if (firstHeuristic != this.heuristics.FIRSTHEURISTIC.FIRSTRANDOM && vectorBest.length > 0) { + position = Math.floor((Math.random() * vectorBest.length)); + } + + if (position == -1) { + if (secondHeuristic == this.heuristics.SECONDHEURISTIC.SECONDCLOSEST) { + // escolhendo o mais proximo, no caso, o vetor já está ordenado + if (closestPosition != -1) { + position = closestPosition; + } else { + // se nao marcou nenhum como closest, todos devem estar como slow, nesse caso seleciona o primeiro que é o mais próximo + position = 0; + } + + } else { + position = Math.floor((Math.random() * this.mirrors.length)); + } + } + + callback(this.mirrors[position].url); + }; + + Chooser.prototype.loadMirrors_ = function(callback) { + var $this = this; + + var localServers = JSON.parse(localStorage.getItem("servers")) || []; + + qdClient.getJSON(this.server, function (status, response) { + var servers = status === 200 ? response : []; + + for (var i = 0; i < servers.length; i++){ + var mirror = { + name: servers[i].nome, + url: servers[i].url, + averageTime: 99999999, + isSlow: false, + previousTimes: [], + remainingSlow: 0, + type: servers[i].type + }; + + for (var localServer in localServers) { + if (mirror.url == localServer.url) { + mirror.averageTime = localServer.averageTime; + mirror.isSlow = localServer.isSlow; + mirror.previousTimes = localServer.previousTimes; + mirror.remainingSlow = localServer.remainingSlow; + + break; + } + } + + $this.mirrors.push(mirror); + } + + if (callback instanceof Function) callback(); + }); + }; + +}(window, localStorage)); diff --git a/chrome/app/vendors/qdclient/qdclient.min.js b/chrome/app/vendors/qdclient/qdclient.min.js new file mode 100644 index 0000000..43d4976 --- /dev/null +++ b/chrome/app/vendors/qdclient/qdclient.min.js @@ -0,0 +1 @@ +!function(r,i){"use strict";r.qdClient=r.qdClient||{},qdClient.request=function(r,i,s,e){var t=new XMLHttpRequest,o="";if(t.open(i,r,!0),t.onload=function(){e(t.status,t.responseText)},"POST"==i&&s instanceof Object){for(var n in s)o+=n+"="+s[n]+"&";s.length>1&&o.slice(0,-1)}t.send(o)},qdClient.getJSON=function(r,i){qdClient.request(r,"GET",{},function(r,s){i(r,JSON.parse(s))})}}(window),function(r,i,s){"use strict";function e(){this.server="http://150.165.204.39/vlibras-cloud/get_servers.php",this.options={THRESHOLDSLOW:2e3,SLOWLIMITTRIES:5,NBESTHEURISTIC1:2,QUANTITYPREVIOUSTIMESSTORED:10},this.mirrors=[],this.heuristics={FIRSTHEURISTIC:{FIRSTRANDOM:{value:0,name:"Random"},FIRSTRANDOMNBEST:{value:1,name:"Random Among N Best"},FIRSTBEST:{value:2,name:"Best"}},SECONDHEURISTIC:{SECONDRANDOM:{value:0,name:"Random"},SECONDCLOSEST:{value:1,name:"Closest"}},THIRDHEURISTIC:{THIRDSIMPLEAVERAGE:{value:0,name:"Simple Average"},THIRDWEIGHTEDAVERAGE:{value:1,name:"Weighted Average"}}}}qdClient.Chooser=e,e.prototype.choose=function(r,i,s,e){var t=this;0===this.mirrors.length?this.loadMirrors_(function(){t.chooseServer_(r,i,s,e)}):this.chooseServer_(r,i,s,e)},e.prototype.chooseServer_=function(r,s,e,t){var o=this.heuristics.FIRSTHEURISTIC.FIRSTBEST,n=this.heuristics.SECONDHEURISTIC.SECONDRANDOM,h=this.heuristics.THIRDHEURISTIC.THIRDWEIGHTEDAVERAGE,T=0,S=0,a=this.options.NBESTHEURISTIC1;if(o==this.heuristics.FIRSTHEURISTIC.FIRSTBEST&&(a=1),r&&r.length>0)for(S=0;S0;T--)this.mirrors[S].previousTimes[T-1]=this.mirrors[S].previousTimes[T];this.mirrors[S].previousTimes[this.options.QUANTITYPREVIOUSTIMESSTORED-1]=s}if(h==this.heuristics.THIRDHEURISTIC.THIRDSIMPLEAVERAGE){var m=0;for(T=0;Tthis.options.THRESHOLDSLOW||-1==e)&&(this.mirrors[S].isSlow=!0,this.mirrors[S].remainingSlow=this.options.SLOWLIMITTRIES),i.setItem("servers",JSON.stringify(this.mirrors));break}var R,E=Math.floor(Math.random()*this.mirrors.length);o!=this.heuristics.FIRSTHEURISTIC.FIRSTRANDOM&&(E=-1,R=-1);var u=[],v=0,g=0;for(T=0;T0&&(this.mirrors[T].remainingSlow=this.mirrors[T].remainingSlow-1),0===this.mirrors[T].remainingSlow&&(this.mirrors[T].isSlow=!1),o!=this.heuristics.FIRSTHEURISTIC.FIRSTRANDOM&&this.mirrors[T].isSlow===!1){-1==R&&(R=T);var f=!1;for(v=0;v=v;g--)u[g+1]=u[g];else for(g=u.length-2;g>=v;g--)u[g+1]=u[g];u[v]=this.mirrors[T];break}f===!1&&u.length0&&(E=Math.floor(Math.random()*u.length)),-1==E&&(E=n==this.heuristics.SECONDHEURISTIC.SECONDCLOSEST?-1!=R?R:0:Math.floor(Math.random()*this.mirrors.length)),t(this.mirrors[E].url)},e.prototype.loadMirrors_=function(r){var s=this,e=JSON.parse(i.getItem("servers"))||[];qdClient.getJSON(this.server,function(i,t){for(var o=200===i?t:[],n=0;n + + - -

Hello World

- + +
Carregando...
+
+ +
+ + + + -- libgit2 0.21.2