Skip to content

Commit

Permalink
Bug 1064845 — Use constants.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rémy HUBSCHER committed Sep 10, 2014
1 parent 5e6716d commit 06f765b
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 285 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ services: redis-server

before_install:
- "sudo apt-get install python-virtualenv"
- npm i grunt-cli -g

script: make travis

Expand Down
38 changes: 38 additions & 0 deletions loop/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

"use strict";

module.exports = {
CALL_STATES: {
INIT: "init",
HALF_INITIATED: "half-initiated",
ALERTING: "alerting",
CONNECTING: "connecting",
HALF_CONNECTED: "half-connected",
CONNECTED: "connected",
TERMINATED: "terminated"
},
MESSAGE_EVENTS: {
ACCEPT: "accept",
MEDIA_UP: "media-up",
TERMINATE: "terminate"
},
MESSAGE_TYPES: {
HELLO: "hello",
ACTION: "action",
PROGRESS: "progress",
ECHO: "echo",
ERROR: "error"
},
MESSAGE_REASONS: {
BUSY: "busy",
CANCEL: "cancel",
TIMEOUT: "timeout"
},
ERROR_REASONS: {
BAD_AUTHENTICATION: "bad authentication",
BAD_CALLID: "bad callId"
}
};
19 changes: 10 additions & 9 deletions loop/storage/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

"use strict";
var redis = require("redis");
var async = require('async');
var async = require("async");
var constants = require("../constants");

function RedisStorage(options, settings) {
this._settings = settings;
Expand Down Expand Up @@ -412,7 +413,7 @@ RedisStorage.prototype = {

var key = 'callstate.' + callId;

if(state === "terminated") {
if(state === constants.CALL_STATES.TERMINATED) {
self._client.del(key, callback);
return;
}
Expand Down Expand Up @@ -450,22 +451,22 @@ RedisStorage.prototype = {
}
switch (score) {
case 1:
callback(null, "init");
callback(null, constants.CALL_STATES.INIT);
break;
case 2:
callback(null, "half-initiated");
callback(null, constants.CALL_STATES.HALF_INITIATED);
break;
case 3:
callback(null, "alerting");
callback(null, constants.CALL_STATES.ALERTING);
break;
case 4:
callback(null, "connecting");
callback(null, constants.CALL_STATES.CONNECTING);
break;
case 5:
callback(null, "half-connected");
callback(null, constants.CALL_STATES.HALF_CONNECTED);
break;
case 6:
callback(null, "connected");
callback(null, constants.CALL_STATES.CONNECTED);
break;
default:
// Ensure a call exists if nothing is stored on this key.
Expand All @@ -475,7 +476,7 @@ RedisStorage.prototype = {
return;
}
if (result !== null) {
callback(null, "terminated");
callback(null, constants.CALL_STATES.TERMINATED);
return;
}
callback(null, null);
Expand Down
89 changes: 57 additions & 32 deletions loop/websockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var PubSub = require('./pubsub');
var conf = require('./config').conf;
var hekaLogger = require('./logger').hekaLogger;
var isoDateString = require("./utils").isoDateString;
var constants = require("./constants");

/**
* Sends an error to the given callback, if there is any.
Expand Down Expand Up @@ -99,7 +100,7 @@ MessageHandler.prototype = {
if (serverError(err, callback)) return;

if (call === null) {
callback(new Error("bad callId"));
callback(new Error(constants.ERROR_REASONS.BAD_CALLID));
return;
}

Expand All @@ -108,7 +109,7 @@ MessageHandler.prototype = {
} else if (call.wsCallerToken === tokenId) {
session.type = "caller";
} else {
callback(new Error("bad authentication"));
callback(new Error(constants.ERROR_REASONS.BAD_AUTHENTICATION));
return;
}

Expand All @@ -124,15 +125,15 @@ MessageHandler.prototype = {
var terminate;

if (channel === session.callId) {
if (receivedState === "terminated" ||
receivedState === "connected") {
if (receivedState === constants.CALL_STATES.TERMINATED ||
receivedState === constants.CALL_STATES.CONNECTED) {
terminate = "closeConnection";
}
if (session.receivedState !== receivedState) {
session.receivedState = receivedState;

var message = {
messageType: "progress",
messageType: constants.MESSAGE_TYPES.PROGRESS,
state: receivedState
};
if (reason !== undefined) {
Expand All @@ -154,9 +155,12 @@ MessageHandler.prototype = {
setTimeout(function() {
self.storage.getCallState(session.callId, function(err, state) {
if (serverError(err, callback)) return;
if (state === 'terminated' || state === 'half-initiated') {
self.broadcastState(session.callId, "terminated:timeout");
self.storage.setCallState(session.callId, 'terminated');
if (state === constants.CALL_STATES.TERMINATED ||
state === constants.CALL_STATES.HALF_INITIATED) {
self.broadcastState(session.callId,
constants.CALL_STATES.TERMINATED + ":" +
constants.MESSAGE_REASONS.TIMEOUT);
self.storage.setCallState(session.callId, constants.CALL_STATES.TERMINATED);
}
});
}, timeoutTTL * 1000);
Expand All @@ -167,28 +171,40 @@ MessageHandler.prototype = {
// Don't publish the half-initiated state, it's only for internal
// use.
var helloState = currentState;
if (currentState === "half-initiated") {
helloState = "init";
if (currentState === constants.CALL_STATES.HALF_INITIATED) {
helloState = constants.CALL_STATES.INIT;
}

callback(null, {
messageType: "hello",
messageType: constants.MESSAGE_TYPES.HELLO,
state: helloState
});

// After the hello phase and as soon the callee is connected,
// the call changes to the "alerting" state.
if (currentState === "init" || currentState === "half-initiated") {
self.broadcastState(session.callId, "init." + session.type,
timeoutTTL);
if (currentState === constants.CALL_STATES.INIT ||
currentState === constants.CALL_STATES.HALF_INITIATED) {
self.broadcastState(
session.callId,
constants.CALL_STATES.INIT + "." + session.type,
timeoutTTL
);
if (session.type === "callee") {
// We are now in "alerting" mode.
setTimeout(function() {
self.storage.getCallState(session.callId, function(err, state) {
if (serverError(err, callback)) return;
if (state === "alerting" || state === "terminated") {
self.broadcastState(session.callId, "terminated:timeout");
self.storage.setCallState(session.callId, 'terminated');
if (state === constants.CALL_STATES.ALERTING ||
state === constants.CALL_STATES.TERMINATED) {
self.broadcastState(
session.callId,
constants.CALL_STATES.TERMINATED + ":" +
constants.MESSAGE_REASONS.TIMEOUT
);
self.storage.setCallState(
session.callId,
constants.CALL_STATES.TERMINATED
);
}
});
}, self.conf.ringingDuration * 1000);
Expand All @@ -214,7 +230,11 @@ MessageHandler.prototype = {
return;
}

var validEvents = ["accept", "media-up", "terminate"];
var validEvents = [
constants.MESSAGE_EVENTS.ACCEPT,
constants.MESSAGE_EVENTS.MEDIA_UP,
constants.MESSAGE_EVENTS.TERMINATE
];
var event = message.event;
var self = this;

Expand All @@ -227,7 +247,7 @@ MessageHandler.prototype = {
}

// If terminate, close the call
if (event === "terminate") {
if (event === constants.MESSAGE_EVENTS.TERMINATE) {
var state = "terminated";

// Check the reason is valid.
Expand All @@ -248,8 +268,8 @@ MessageHandler.prototype = {

// Ensure half-connected is not send twice by the same party.
var validateState = function(currentState) {
if (currentState === "connecting" ||
currentState === "half-connected") {
if (currentState === constants.CALL_STATES.CONNECTING ||
currentState === constants.CALL_STATES.HALF_CONNECTED) {
return "connected." + session.type;
}
return null;
Expand All @@ -258,24 +278,28 @@ MessageHandler.prototype = {
var stateMachine = {
"accept": {
transitions: [
["alerting", "connecting"]
[constants.CALL_STATES.ALERTING, constants.CALL_STATES.CONNECTING]
],
actuator: function() {
setTimeout(function() {
// Alerting for too long
self.storage.getCallState(session.callId, function(err, state) {
if (serverError(err, callback)) return;
if (state !== "connected") {
self.broadcastState(session.callId, "terminated:timeout");
self.storage.setCallState(session.callId, 'terminated');
if (state !== constants.CALL_STATES.CONNECTED) {
self.broadcastState(session.callId,
constants.CALL_STATES.TERMINATED + ":" +
constants.MESSAGE_REASONS.TIMEOUT);
self.storage.setCallState(session.callId,
constants.CALL_STATES.TERMINATED);
}
});
}, self.conf.connectionDuration * 1000);
}
},
"media-up": {
transitions: [
["connecting"],
["half-connected"]
[constants.CALL_STATES.CONNECTING],
[constants.CALL_STATES.HALF_CONNECTED]
],
validator: validateState
}
Expand Down Expand Up @@ -334,19 +358,20 @@ MessageHandler.prototype = {
self.storage.getCallState(callId, function(err, redisCurrentState) {
if (serverError(err)) return;

if (redisCurrentState === "terminated" && parts[1] !== undefined) {
if (redisCurrentState === constants.CALL_STATES.TERMINATED &&
parts[1] !== undefined) {
redisCurrentState += ":" + parts[1];
}

if (redisCurrentState !== "half-initiated") {
if (redisCurrentState !== constants.CALL_STATES.HALF_INITIATED) {
self.pub.publish(callId, redisCurrentState, function(err) {
if (serverError(err)) return;
});
}

if (conf.get("metrics") &&
(redisCurrentState === "connected" ||
redisCurrentState === "terminated")) {
(redisCurrentState === constants.CALL_STATES.CONNECTED ||
redisCurrentState === constants.CALL_STATES.TERMINATED)) {
hekaLogger.log('info', {
op: 'websocket.summary',
callId: callId,
Expand All @@ -363,7 +388,7 @@ MessageHandler.prototype = {
**/
createError: function(errorMessage) {
return this.encode({
messageType: "error",
messageType: constants.MESSAGE_TYPES.ERROR,
reason: errorMessage
});
},
Expand Down
11 changes: 6 additions & 5 deletions test/functional_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var sinon = require("sinon");
var randomBytes = require("crypto").randomBytes;
var assert = sinon.assert;

var constants = require("../loop/constants");
var loop = require("../loop");
var apiRouter = loop.apiRouter;
var app = loop.app;
Expand Down Expand Up @@ -923,7 +924,7 @@ function runOnPrefix(apiPrefix) {
callToken: callToken,
callType: 'audio',
urlCreationDate: urlCreationDate,
callState: "init",
callState: constants.CALL_STATES.INIT,
timestamp: parseInt(Date.now() / 1000, 10)
},
{
Expand All @@ -938,7 +939,7 @@ function runOnPrefix(apiPrefix) {
callToken: callToken,
callType: 'audio-video',
urlCreationDate: urlCreationDate,
callState: "init",
callState: constants.CALL_STATES.INIT,
timestamp: parseInt(Date.now() / 1000, 10) + 1
},
{
Expand All @@ -950,7 +951,7 @@ function runOnPrefix(apiPrefix) {
apiKey: tokBoxConfig.credentials.default.apiKey,
sessionId: fakeCallInfo.session3,
calleeToken: fakeCallInfo.token2,
callState: "terminated",
callState: constants.CALL_STATES.TERMINATED,
callToken: callToken,
callType: 'audio-video',
urlCreationDate: urlCreationDate,
Expand Down Expand Up @@ -979,7 +980,7 @@ function runOnPrefix(apiPrefix) {
if (err) throw err;

var callsList = calls.filter(function(call) {
return call.callState !== "terminated";
return call.callState !== constants.CALL_STATES.TERMINATED;
}).map(function(call) {
return {
callId: call.callId,
Expand Down Expand Up @@ -1039,7 +1040,7 @@ function runOnPrefix(apiPrefix) {
apiKey: tokBoxConfig.credentials.default.apiKey,
sessionId: fakeCallInfo.session3,
calleeToken: fakeCallInfo.token2,
callState: "init",
callState: constants.CALL_STATES.INIT,
callType: 'audio-video',
timestamp: parseInt(Date.now() / 1000, 10) + 3
};
Expand Down
Loading

0 comments on commit 06f765b

Please sign in to comment.