Prevents breakages of our functionality caused by side-effect callstats.io API failures.

This commit is contained in:
Lyubomir Marinov 2016-01-14 09:27:12 +02:00
parent 2b570a2251
commit 9b28e64e5d
1 changed files with 78 additions and 52 deletions

View File

@ -29,44 +29,70 @@ function initCallback (err, msg) {
var callStatsIntegrationEnabled = config.callStatsID && config.callStatsSecret;
/**
* Returns a function which invokes f in a try/catch block, logs any exception
* to the console, and then swallows it.
*
* @param f the function to invoke in a try/catch block
* @return a function which invokes f in a try/catch block, logs any exception
* to the console, and then swallows it
*/
function _try_catch (f) {
return function () {
try {
f.apply(this, arguments);
} catch (e) {
console.error(e);
}
};
}
var CallStats = {
init: function (jingleSession) {
init: _try_catch(function (jingleSession) {
if(!this.isEnabled() || callStats !== null) {
return;
}
try {
callStats = new callstats($, io, jsSHA);
this.session = jingleSession;
this.peerconnection = jingleSession.peerconnection.peerconnection;
this.userID = Settings.getCallStatsUserName();
var location = window.location;
this.confID = location.hostname + location.pathname;
//userID is generated or given by the origin server
callStats.initialize(config.callStatsID,
config.callStatsSecret,
this.userID,
callStats.initialize(
config.callStatsID, config.callStatsSecret,
this.userID /* generated or given by the origin server */,
initCallback);
var usage = callStats.fabricUsage.multiplex;
callStats.addNewFabric(this.peerconnection,
callStats.addNewFabric(
this.peerconnection,
Strophe.getResourceFromJid(jingleSession.peerjid),
usage,
this.confID,
this.pcCallback.bind(this));
// notify callstats about failures if there were any
if (pendingErrors.length) {
} catch (e) {
// The callstats.io API failed to initialize (e.g. because its
// download failed to succeed in general or on time). Further
// attempts to utilize it cannot possibly succeed.
callStats = null;
console.error(e);
}
// Notify callstats about pre-init failures if there were any.
if (callStats && pendingErrors.length) {
pendingErrors.forEach(function (error) {
this._reportError(error.type, error.error, error.pc);
}, this);
pendingErrors.length = 0;
}
},
}),
/**
* Returns true if the callstats integration is enabled, otherwise returns
* false.
@ -77,15 +103,17 @@ var CallStats = {
isEnabled: function() {
return callStatsIntegrationEnabled;
},
pcCallback: function (err, msg) {
pcCallback: _try_catch(function (err, msg) {
if (!callStats) {
return;
}
console.log("Monitoring status: "+ err + " msg: " + msg);
callStats.sendFabricEvent(this.peerconnection,
callStats.fabricEvent.fabricSetup, this.confID);
},
sendMuteEvent: function (mute, type) {
}),
sendMuteEvent: _try_catch(function (mute, type) {
if (!callStats) {
return;
}
@ -99,21 +127,23 @@ var CallStats = {
callStats.fabricEvent.audioUnmute);
}
callStats.sendFabricEvent(this.peerconnection, event, this.confID);
},
sendTerminateEvent: function () {
}),
sendTerminateEvent: _try_catch(function () {
if(!callStats) {
return;
}
callStats.sendFabricEvent(this.peerconnection,
callStats.fabricEvent.fabricTerminated, this.confID);
},
sendSetupFailedEvent: function () {
}),
sendSetupFailedEvent: _try_catch(function () {
if(!callStats) {
return;
}
callStats.sendFabricEvent(this.peerconnection,
callStats.fabricEvent.fabricSetupFailed, this.confID);
},
}),
/**
* Sends the given feedback through CallStats.
@ -122,7 +152,7 @@ var CallStats = {
* user feedback
* @param detailedFeedback detailed feedback from the user. Not yet used
*/
sendFeedback: function(overallFeedback, detailedFeedback) {
sendFeedback: _try_catch(function(overallFeedback, detailedFeedback) {
if(!callStats) {
return;
}
@ -132,9 +162,9 @@ var CallStats = {
var feedbackJSON = JSON.parse(feedbackString);
callStats.sendUserFeedback(
this.confID, feedbackJSON);
},
callStats.sendUserFeedback(this.confID, feedbackJSON);
}),
/**
* Reports an error to callstats.
*
@ -147,11 +177,7 @@ var CallStats = {
if (callStats) {
callStats.reportError(pc, this.confID, type, e);
} else if (this.isEnabled()) {
pendingErrors.push({
type: type,
error: e,
pc: pc
});
pendingErrors.push({ type: type, error: e, pc: pc });
}
// else just ignore it
},
@ -161,9 +187,9 @@ var CallStats = {
*
* @param {Error} e error to send
*/
sendGetUserMediaFailed: function (e) {
sendGetUserMediaFailed: _try_catch(function (e) {
this._reportError(wrtcFuncNames.getUserMedia, e, null);
},
}),
/**
* Notifies CallStats that peer connection failed to create offer.
@ -171,9 +197,9 @@ var CallStats = {
* @param {Error} e error to send
* @param {RTCPeerConnection} pc connection on which failure occured.
*/
sendCreateOfferFailed: function (e, pc) {
sendCreateOfferFailed: _try_catch(function (e, pc) {
this._reportError(wrtcFuncNames.createOffer, e, pc);
},
}),
/**
* Notifies CallStats that peer connection failed to create answer.
@ -181,9 +207,9 @@ var CallStats = {
* @param {Error} e error to send
* @param {RTCPeerConnection} pc connection on which failure occured.
*/
sendCreateAnswerFailed: function (e, pc) {
sendCreateAnswerFailed: _try_catch(function (e, pc) {
this._reportError(wrtcFuncNames.createAnswer, e, pc);
},
}),
/**
* Notifies CallStats that peer connection failed to set local description.
@ -191,9 +217,9 @@ var CallStats = {
* @param {Error} e error to send
* @param {RTCPeerConnection} pc connection on which failure occured.
*/
sendSetLocalDescFailed: function (e, pc) {
sendSetLocalDescFailed: _try_catch(function (e, pc) {
this._reportError(wrtcFuncNames.setLocalDescription, e, pc);
},
}),
/**
* Notifies CallStats that peer connection failed to set remote description.
@ -201,9 +227,9 @@ var CallStats = {
* @param {Error} e error to send
* @param {RTCPeerConnection} pc connection on which failure occured.
*/
sendSetRemoteDescFailed: function (e, pc) {
sendSetRemoteDescFailed: _try_catch(function (e, pc) {
this._reportError(wrtcFuncNames.setRemoteDescription, e, pc);
},
}),
/**
* Notifies CallStats that peer connection failed to add ICE candidate.
@ -211,8 +237,8 @@ var CallStats = {
* @param {Error} e error to send
* @param {RTCPeerConnection} pc connection on which failure occured.
*/
sendAddIceCandidateFailed: function (e, pc) {
sendAddIceCandidateFailed: _try_catch(function (e, pc) {
this._reportError(wrtcFuncNames.addIceCandidate, e, pc);
}
})
};
module.exports = CallStats;