+cloudeebus.Service = function(session, busConnection, name) {
+ this.wampSession = session;
+ this.busConnection = busConnection;
+ this.name = name;
+ this.agents = [];
+ this.isCreated = false;
+ return this;
+};
+
+cloudeebus.Service.prototype.remove = function() {
+ var self = this;
+
+ var promise = new cloudeebus.Promise(function (resolver) {
+ function ServiceRemovedSuccessCB(serviceName) {
+ try { // calling dbus hook object function for un-translated types
+ var result = [ serviceName ];
+ resolver.fulfill(result[0], true);
+ }
+ catch (e) {
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("Method callback exception: " + errorStr);
+ resolver.reject(errorStr, true);
+ }
+ }
+
+ function ServiceRemovedErrorCB(error) {
+ var errorStr = cloudeebus.getError(error);
+ cloudeebus.log("Error removing service : " + self.name + ", error: " + errorStr);
+ self.promise.resolver.reject(errorStr, true);
+ }
+
+ for (var idx in self.agents) {
+ if (self.agents[idx]) {
+ cloudeebus.log("Removing agent : " + JSON.stringify(self.agents[idx]));
+ self.delAgent(self.agents[idx]);
+ }
+ }
+
+ var arglist = [
+ self.name
+ ];
+
+ // call dbusSend with bus type, destination, object, message and arguments
+ self.wampSession.call("serviceRelease", arglist).then(ServiceRemovedSuccessCB, ServiceRemovedErrorCB);
+ });
+
+ return promise;
+};
+
+cloudeebus.Service.prototype._searchMethod = function(ifName, method, objectJS) {
+
+ var funcToCall = null;
+
+ // Check if 'objectJS' has a member 'interfaceProxies' with an interface named 'ifName'
+ // and a method named 'method'
+ if (objectJS.interfaceProxies && objectJS.interfaceProxies[ifName] &&
+ objectJS.interfaceProxies[ifName][method]) {
+ funcToCall = objectJS.interfaceProxies[ifName][method];
+ } else {
+ // retrieve the method directly from 'root' of objectJs
+ funcToCall = objectJS[method];
+ }
+
+ return funcToCall;
+};
+
+cloudeebus.Service.prototype._addMethod = function(ifName, method, agent) {
+
+ var service = this;
+ var methodId = this.name + "#" + agent.objectPath + "#" + ifName + "#" + method;
+ var funcToCall = this._searchMethod(ifName, method, agent.jsHdl);
+
+ if (funcToCall == null)
+ cloudeebus.log("Method " + method + " doesn't exist in Javascript object");
+ else {
+ agent.jsHdl.wrapperFunc[method] = function() {
+ var result;
+ var methodId = arguments[0];
+ var callDict = {};
+ // affectation of callDict in eval, otherwise dictionary(='{}') interpreted as block of code by eval
+ eval("callDict = " + arguments[1]);
+ try {
+ result = funcToCall.apply(agent.jsHdl, callDict.args);
+ service._returnMethod(methodId, callDict.callIndex, true, result);
+ }
+ catch (e) {
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("Method " + ifName + "." + method + " call on " + agent.objectPath + " exception: " + errorStr);
+ service._returnMethod(methodId, callDict.callIndex, false, errorStr);
+ }
+ };
+ agent.jsHdl.methodId[agent.objectPath].push(methodId);
+ cloudeebus.log("subscribe " + methodId);
+ this.wampSession.subscribe(methodId, agent.jsHdl.wrapperFunc[method]);
+ }
+};
+
+cloudeebus.Service.prototype._addSignal = function(ifName, signal, agent) {
+ var service = this;
+ var methodExist = false;
+
+ if (agent.jsHdl.interfaceProxies && agent.jsHdl.interfaceProxies[ifName])
+ if (agent.jsHdl.interfaceProxies[ifName][signal]) {
+ methodExist = true;
+ } else {
+ agent.jsHdl.interfaceProxies[ifName][signal] = function() {
+ service._emitSignal(agent.objectPath, signal, arguments[0]);
+ };
+ return;
+ }
+
+ if ((agent.jsHdl[signal] == undefined || agent.jsHdl[signal] == null) && !methodExist)
+ agent.jsHdl[signal] = function() {
+ service._emitSignal(agent.objectPath, signal, arguments[0]);
+ };
+ else
+ cloudeebus.log("Can not create new method to emit signal '" + signal + "' in object JS this method already exist!");
+};
+
+cloudeebus.Service.prototype._createWrapper = function(agent) {
+ var self = this;
+ var parser = new DOMParser();
+ var xmlDoc = parser.parseFromString(agent.xml, "text/xml");
+ var ifXml = xmlDoc.getElementsByTagName("interface");
+ agent.jsHdl.wrapperFunc = [];
+ agent.jsHdl.methodId = [];
+ agent.jsHdl.methodId[agent.objectPath] = [];
+ for (var i=0; i < ifXml.length; i++) {
+ var ifName = ifXml[i].attributes.getNamedItem("name").value;
+ var ifChild = ifXml[i].firstChild;
+ while (ifChild) {
+ if (ifChild.nodeName == "method") {
+ var metName = ifChild.attributes.getNamedItem("name").value;
+ self._addMethod(ifName, metName, agent);
+ }
+ if (ifChild.nodeName == "signal") {
+ var metName = ifChild.attributes.getNamedItem("name").value;
+ self._addSignal(ifName, metName, agent);
+ }
+ ifChild = ifChild.nextSibling;
+ }
+ }
+};
+
+cloudeebus.Service.prototype.addAgent = function(agent, successCB, errorCB) {
+ var self = this;
+
+ function ServiceAddAgentSuccessCB(objPath) {
+ self.agents.push(agent);
+ agent.registered = true;
+ cloudeebus.log("Agent pushed : " + JSON.stringify(agent))
+ if (successCB) {
+ try {
+ successCB(objPath);
+ }
+ catch (e) {
+ alert("Exception adding agent " + agent.objectPath + " : " + cloudeebus.getError(e));
+ }
+ }
+ }
+
+ try {
+ this._createWrapper(agent);
+ }
+ catch (e) {
+ var errorStr = cloudeebus.getError(e);
+ alert("Exception creating agent wrapper " + agent.objectPath + " : " + errorStr);
+ errorCB(errorStr);
+ return;
+ }
+
+ var arglist = [
+ agent.objectPath,
+ agent.xml
+ ];
+
+ // call dbusSend with bus type, destination, object, message and arguments
+ this.wampSession.call("serviceAddAgent", arglist).then(ServiceAddAgentSuccessCB, errorCB);
+};
+
+cloudeebus.Service.prototype._deleteWrapper = function(agent) {
+ var objJs = agent.jsHdl;
+ if (objJs.methodId[agent.objectPath]) {
+ for (var idx in objJs.methodId[agent.objectPath]) {
+ try {
+ cloudeebus.log("unsubscribe " + objJs.methodId[agent.objectPath][idx]);
+ this.wampSession.unsubscribe(objJs.methodId[agent.objectPath][idx]);
+ objJs.methodId[agent.objectPath][idx] = null;
+ }
+ catch (e) {
+ cloudeebus.log("Unsubscribe error: " + cloudeebus.getError(e));
+ }
+ }
+ delete objJs.methodId[agent.objectPath];
+ }
+};
+
+cloudeebus.Service.prototype.delAgent = function(rmAgent, successCB, errorCB) {
+ function ServiceDelAgentSuccessCB(agent) {
+ if (successCB) {
+ try {
+ successCB(agent);
+ }
+ catch (e) {
+ var errorStr = cloudeebus.getError(e);
+ alert("Exception deleting agent " + rmAgent.objectPath + " : " + errorStr);
+ errorCB(errorStr);
+ }
+ }
+ }
+
+ try {
+ this._deleteWrapper(rmAgent);
+ }
+ catch (e) {
+ var errorStr = cloudeebus.getError(e);
+ alert("Exception deleting agent wrapper " + rmAgent.objectPath + " : " + errorStr);
+ errorCB(errorStr);
+ }
+
+ var arglist = [
+ rmAgent.objectPath
+ ];
+
+ // call dbusSend with bus type, destination, object, message and arguments
+ this.wampSession.call("serviceDelAgent", arglist).then(ServiceDelAgentSuccessCB, errorCB);
+};
+
+cloudeebus.Service.prototype._returnMethod = function(methodId, callIndex, success, result, successCB, errorCB) {
+ var arglist = [
+ methodId,
+ callIndex,
+ success,
+ result
+ ];
+
+ this.wampSession.call("returnMethod", arglist).then(successCB, errorCB);
+};
+
+cloudeebus.Service.prototype._emitSignal = function(objectPath, signalName, result, successCB, errorCB) {
+ var arglist = [
+ objectPath,
+ signalName,
+ result
+ ];
+
+ this.wampSession.call("emitSignal", arglist).then(successCB, errorCB);
+};
+
+
+
+/*****************************************************************************/
+
+function _processWrappers(wrappers, value) {
+ for (var i=0; i<wrappers.length; i++)
+ wrappers[i](value);
+}
+
+
+function _processWrappersAsync(wrappers, value) {
+ var taskid = -1;
+ function processAsyncOnce() {
+ _processWrappers(wrappers, value);
+ clearInterval(taskid);
+ }
+ taskid = setInterval(processAsyncOnce, 200);
+}
+
+
+
+/*****************************************************************************/
+
+cloudeebus.PromiseResolver = function(promise) {
+ this.promise = promise;
+ this.resolved = null;
+ return this;
+};
+
+
+cloudeebus.PromiseResolver.prototype.resolve = function(value, sync) {
+ if (this.resolved)
+ return;
+
+ var then = (value && value.then && value.then.apply) ? value.then : null;
+ if (then) {
+ var self = this;
+ var fulfillCallback = function(arg) {
+ self.resolve(arg, true);
+ };
+ var rejectCallback = function(arg) {
+ self.reject(arg, true);
+ };
+ try {
+ then.apply(value, [fulfillCallback, rejectCallback]);
+ }
+ catch (e) {
+ this.reject(cloudeebus.getError(e), true);
+ }
+ }
+
+ this.fulfill(value, sync);
+};
+
+
+cloudeebus.PromiseResolver.prototype.fulfill = function(value, sync) {
+ if (this.resolved)
+ return;
+
+ var promise = this.promise;
+ promise.state = "fulfilled";
+ promise.result = value;
+
+ this.resolved = true;
+ if (sync)
+ _processWrappers(promise._fulfillWrappers, value);
+ else
+ _processWrappersAsync(promise._fulfillWrappers, value);
+};
+
+
+cloudeebus.PromiseResolver.prototype.reject = function(value, sync) {
+ if (this.resolved)
+ return;
+
+ var promise = this.promise;
+ promise.state = "rejected";
+ promise.result = value;
+
+ this.resolved = true;
+ if (sync)
+ _processWrappers(promise._rejectWrappers, value);
+ else
+ _processWrappersAsync(promise._rejectWrappers, value);
+};
+
+
+
+/*****************************************************************************/
+
+cloudeebus.Promise = function(init) {
+ this.state = "pending";