2015-07-28 18:22:11 +00:00
|
|
|
/* global APP, require */
|
2015-09-11 02:42:15 +00:00
|
|
|
/* jshint -W101 */
|
2016-01-29 15:33:16 +00:00
|
|
|
import EventEmitter from "events";
|
|
|
|
|
|
|
|
import CQEvents from "../../service/connectionquality/CQEvents";
|
|
|
|
|
|
|
|
const eventEmitter = new EventEmitter();
|
2015-01-24 14:28:02 +00:00
|
|
|
|
2015-01-08 12:11:53 +00:00
|
|
|
/**
|
|
|
|
* local stats
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
|
|
|
var stats = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* remote stats
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
|
|
|
var remoteStats = {};
|
|
|
|
|
2016-05-12 22:48:19 +00:00
|
|
|
/**
|
|
|
|
* Quality percent( 100% - good, 0% - bad.) for the local user.
|
|
|
|
*/
|
|
|
|
var localConnectionQuality = 100;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Quality percent( 100% - good, 0% - bad.) stored per id.
|
|
|
|
*/
|
|
|
|
var remoteConnectionQuality = {};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the quality percent based on passed new and old value.
|
|
|
|
* @param newVal the new value
|
|
|
|
* @param oldVal the old value
|
|
|
|
*/
|
|
|
|
function calculateQuality(newVal, oldVal) {
|
|
|
|
return (newVal <= oldVal) ? newVal : (9*oldVal + newVal) / 10;
|
|
|
|
}
|
|
|
|
|
2016-01-29 15:33:16 +00:00
|
|
|
export default {
|
2015-01-08 12:11:53 +00:00
|
|
|
/**
|
|
|
|
* Updates the local statistics
|
|
|
|
* @param data new statistics
|
2016-07-22 18:42:41 +00:00
|
|
|
* @param dontUpdateLocalConnectionQuality {boolean} if true -
|
|
|
|
* localConnectionQuality wont be recalculated.
|
2015-01-08 12:11:53 +00:00
|
|
|
*/
|
2016-07-22 18:42:41 +00:00
|
|
|
updateLocalStats: function (data, dontUpdateLocalConnectionQuality) {
|
2015-01-08 12:11:53 +00:00
|
|
|
stats = data;
|
2016-07-22 18:42:41 +00:00
|
|
|
if(!dontUpdateLocalConnectionQuality) {
|
|
|
|
var newVal = 100 - stats.packetLoss.total;
|
|
|
|
localConnectionQuality =
|
|
|
|
calculateQuality(newVal, localConnectionQuality);
|
|
|
|
}
|
|
|
|
eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, localConnectionQuality,
|
|
|
|
stats);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates only the localConnectionQuality value
|
|
|
|
* @param values {int} the new value. should be from 0 - 100.
|
|
|
|
*/
|
|
|
|
updateLocalConnectionQuality: function (value) {
|
|
|
|
localConnectionQuality = value;
|
2016-05-12 22:48:19 +00:00
|
|
|
eventEmitter.emit(CQEvents.LOCALSTATS_UPDATED, localConnectionQuality,
|
|
|
|
stats);
|
2015-01-08 12:11:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Updates remote statistics
|
2015-12-02 13:05:29 +00:00
|
|
|
* @param id the id associated with the statistics
|
2015-01-08 12:11:53 +00:00
|
|
|
* @param data the statistics
|
|
|
|
*/
|
2015-12-02 13:05:29 +00:00
|
|
|
updateRemoteStats: function (id, data) {
|
2016-07-21 22:00:57 +00:00
|
|
|
if (!data || !("packetLoss" in data) || !("total" in data.packetLoss)) {
|
2015-12-02 13:05:29 +00:00
|
|
|
eventEmitter.emit(CQEvents.REMOTESTATS_UPDATED, id, null, null);
|
2015-01-08 12:11:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-01-04 23:16:58 +00:00
|
|
|
remoteStats[id] = data;
|
2015-01-08 12:11:53 +00:00
|
|
|
|
2016-05-12 22:48:19 +00:00
|
|
|
var newVal = 100 - data.packetLoss.total;
|
|
|
|
var oldVal = remoteConnectionQuality[id];
|
2016-06-24 17:22:51 +00:00
|
|
|
remoteConnectionQuality[id] = calculateQuality(newVal, oldVal || 100);
|
2016-05-12 22:48:19 +00:00
|
|
|
|
2015-12-02 13:05:29 +00:00
|
|
|
eventEmitter.emit(
|
2016-05-12 22:48:19 +00:00
|
|
|
CQEvents.REMOTESTATS_UPDATED, id, remoteConnectionQuality[id],
|
|
|
|
remoteStats[id]);
|
2015-01-08 12:11:53 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the local statistics.
|
|
|
|
*/
|
|
|
|
getStats: function () {
|
|
|
|
return stats;
|
2015-01-24 14:28:02 +00:00
|
|
|
},
|
2015-12-02 13:05:29 +00:00
|
|
|
|
2015-01-24 14:28:02 +00:00
|
|
|
addListener: function (type, listener) {
|
|
|
|
eventEmitter.on(type, listener);
|
2015-12-02 13:05:29 +00:00
|
|
|
}
|
2015-01-08 12:11:53 +00:00
|
|
|
};
|