Skip to content
This repository has been archived by the owner on Jul 12, 2023. It is now read-only.

Commit

Permalink
Check on connection to KMS that supports all loaded modules
Browse files Browse the repository at this point in the history
Change-Id: I82934857e73722d23789f25445c5527c5ad063e7
  • Loading branch information
piranna committed Apr 14, 2015
1 parent bcf58d7 commit 3043364
Showing 1 changed file with 125 additions and 89 deletions.
214 changes: 125 additions & 89 deletions lib/KurentoClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ function KurentoClient(ws_uri, options, callback) {
};

options = options || {};
if (callback) callback = callback.bind(this)

var failAfter = options.failAfter
if (failAfter == undefined) failAfter = 5
Expand Down Expand Up @@ -219,95 +220,6 @@ function KurentoClient(ws_uri, options, callback) {
console.error('Invalid request instance', request);
});

function connect(callback) {
//
// Reconnect websockets
//

var closed = false;
var re = reconnect({
failAfter: failAfter
}, function (ws_stream) {
if (closed)
ws_stream.writable = false;

rpc.transport = ws_stream;
})
.connect(ws_uri);

Object.defineProperty(this, '_re', {
configurable: true,
get: function () {
return re
}
})

this.close = function () {
closed = true;

prevRpc_result.then(re.disconnect.bind(re));
};

re.on('fail', this.emit.bind(this, 'disconnect'));

//
// Promise interface ("thenable")
//

this.then = function (onFulfilled, onRejected) {
return new Promise(function (resolve, reject) {
function success() {
re.removeListener('fail', failure);

var result;

if (onFulfilled)
try {
result = onFulfilled.call(self, self);
} catch (exception) {
if (!onRejected)
console.trace('Uncaugh exception', exception)

return reject(exception);
}

resolve(result);
};

function failure() {
re.removeListener('connection', success);

var result = new Error('Connection error');

if (onRejected)
try {
result = onRejected.call(self, result);
} catch (exception) {
return reject(exception);
} else
console.trace('Uncaugh exception', result)

reject(result);
};

if (re.connected)
success()
else if (!re.reconnect)
failure()
else {
re.once('connection', success);
re.once('fail', failure);
}
});
};

this.catch = this.then.bind(this, null);

if (callback)
this.then(callback.bind(undefined, null), callback);
};
connect.call(self, callback);

// Select what transactions mechanism to use
var encodeTransaction = options.enableTransactions ? commitTransactional :
commitSerial;
Expand Down Expand Up @@ -661,6 +573,130 @@ function KurentoClient(ws_uri, options, callback) {
* @param {module:core/abstract~MediaElement} result
* The created MediaElement
*/

function connect(callback) {
//
// Reconnect websockets
//

var closed = false;
var re = reconnect({
failAfter: failAfter
}, function (ws_stream) {
if (closed)
ws_stream.writable = false;

rpc.transport = ws_stream;
})
.connect(ws_uri);

// Object.defineProperty(this, '_re', {
// configurable: true,
// get: function () {
// return re
// }
// })

this.close = function () {
closed = true;

prevRpc_result.then(re.disconnect.bind(re));
};

re.on('fail', this.emit.bind(this, 'disconnect'));

//
// Promise interface ("thenable")
//

this.then = function (onFulfilled, onRejected) {
return new Promise(function (resolve, reject) {
function success() {
re.removeListener('fail', failure);

var result;

if (onFulfilled)
try {
result = onFulfilled.call(self, self);
} catch (exception) {
if (!onRejected)
console.trace('Uncaugh exception', exception)

return reject(exception);
}

resolve(result);
};

function failure() {
re.removeListener('connection', success);

var result = new Error('Connection error');

if (onRejected)
try {
result = onRejected.call(self, result);
} catch (exception) {
return reject(exception);
} else
console.trace('Uncaugh exception', result)

reject(result);
};

if (re.connected)
success()
else if (!re.reconnect)
failure()
else {
re.once('connection', success);
re.once('fail', failure);
}
});
};

this.catch = this.then.bind(this, null);

// Check for available modules in the Kurento Media Server

var promise = this.getServerManager()
.then(function (serverManager) {
return serverManager.getInfo()
})
.then(function (info) {
var serverModules = info.modules.map(function (module) {
return module.name
})

var notInstalled = KurentoClient.register.modules.filter(
function (module) {
return serverModules.indexOf(module) < 0
})

var length = notInstalled.length
if (length) {
if (length === 1)
var message = "Module '" + notInstalled[0] +
"' is not installed in the Kurento Media Server"
else
var message = "Modules '" + notInstalled.slice(0, -1).join(
"', '") + "' and '" + notInstalled[length - 1] +
"' are not installed in the Kurento Media Server"

var error = new Error(message)
error.modules = notInstalled

return Promise.reject(error)
}

return Promise.resolve(self)
})

if (callback)
promise.then(callback.bind(undefined, null), callback);
};
connect.call(self, callback);
};
inherits(KurentoClient, EventEmitter);
/**
Expand Down

0 comments on commit 3043364

Please sign in to comment.