/******************************************************************************
- * Copyright 2012 Intel Corporation.
+ * Copyright 2012 - 2013 Intel Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
/*****************************************************************************/
var cloudeebus = window.cloudeebus = {
- version: "0.5.1",
- minVersion: "0.3.2"
+ version: "0.6.1",
+ minVersion: "0.6.0"
};
cloudeebus.reset = function() {
cloudeebus.getError = function(error) {
if (error.desc && error.uri)
- return error.desc + " : " + error.uri;
+ return error.desc + " : " + error.uri; // Python exception (cloudeebus.py)
if (error.desc)
return error.desc;
if (error.uri)
return error.uri;
if (error.name && error.message)
- return error.name + " : " + error.message;
+ return error.name + " : " + error.message; // Javascript exception
if (error.message)
return error.message;
- return error;
+ if (error.name)
+ return error.name;
+ return error; // Autobahn error
};
cloudeebus.versionCheck = function(version) {
if (manifest)
cloudeebus.wampSession.authreq(
manifest.name,
- {permissions: manifest.permissions}
+ {permissions: manifest.permissions,
+ services: manifest.services}
).then(onWAMPSessionChallengedCB, onWAMPSessionAuthErrorCB);
else
cloudeebus.wampSession.authreq().then(function() {
cloudeebus.BusConnection.prototype.addService = function(serviceName) {
var self = this;
-
+
var promise = new cloudeebus.Promise(function (resolver) {
- cloudeebusService = new cloudeebus.Service(self.wampSession, self, serviceName);
+ var cloudeebusService = new cloudeebus.Service(self.wampSession, self, serviceName);
function ServiceAddedSuccessCB(serviceName) {
- try { // calling dbus hook object function for un-translated types
- cloudeebusService.isCreated = true;
- var result = [ cloudeebusService ];
- resolver.fulfill(result[0], true);
- }
- catch (e) {
- var errorStr = cloudeebus.getError(e);
- cloudeebus.log("Method callback exception: " + errorStr);
- resolver.reject(errorStr, true);
- }
+ cloudeebusService.isCreated = true;
+ resolver.fulfill(cloudeebusService, true);
}
function ServiceAddedErrorCB(error) {
var errorStr = cloudeebus.getError(error);
cloudeebus.log("Error adding service method: " + self.name + ", error: " + errorStr);
- self.promise.resolver.reject(errorStr, true);
+ resolver.reject(errorStr, true);
}
var arglist = [
};
+
+/******************************************************************************
+ * Copyright 2012 - 2013 Intel Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *****************************************************************************/
+
+
+
/*****************************************************************************/
-//Generic definition for an agent. An agent need :
-//srvDbusName : the DBus parent service
-//objPath : a DBus path to access it
-//jsHdl : a Javascript handler to process methods,
-//xml : the xml which describe interface/methods/signals...
-cloudeebus.Agent = function(srvDbusName, objPath, jsHdl, xml) {
- this.srvName = srvDbusName;
- this.registered = false;
+
+cloudeebus.Agent = function(objectPath, handler, xml) {
this.xml = xml;
- this.objectPath = objPath;
- this.jsHdl = jsHdl;
+ this.objectPath = objectPath;
+ this.handler = handler;
return this;
};
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);
- }
+ resolver.fulfill(serviceName, 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]) {
- self.delAgent(self.agents[idx]);
- }
+ resolver.reject(errorStr, true);
}
var arglist = [
return promise;
};
+
cloudeebus.Service.prototype._searchMethod = function(ifName, method, objectJS) {
var funcToCall = null;
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);
+ var funcToCall = this._searchMethod(ifName, method, agent.handler);
if (funcToCall == null)
cloudeebus.log("Method " + method + " doesn't exist in Javascript object");
else {
- agent.jsHdl.wrapperFunc[method] = function() {
+ agent.handler.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);
+ result = funcToCall.apply(agent.handler, callDict.args);
service._returnMethod(methodId, callDict.callIndex, true, result);
}
catch (e) {
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]);
+ agent.handler.methodId[agent.objectPath].push(methodId);
+ this.wampSession.subscribe(methodId, agent.handler.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]);
+ if (agent.handler[signal])
+ cloudeebus.log("Signal '" + signal + "' emitter already implemented");
+ else {
+ agent.handler[signal] = function() {
+ var args = [];
+ for (var i=0; i < arguments.length; i++ )
+ args.push(arguments[i]);
+ service._emitSignal(agent.objectPath, signal, args);
};
- 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] = [];
+ agent.handler.wrapperFunc = {};
+ agent.handler.methodId = {};
+ agent.handler.methodId[agent.objectPath] = [];
for (var i=0; i < ifXml.length; i++) {
var ifName = ifXml[i].attributes.getNamedItem("name").value;
var ifChild = ifXml[i].firstChild;
}
};
+
cloudeebus.Service.prototype.addAgent = function(agent) {
var self = this;
var promise = new cloudeebus.Promise(function (resolver) {
function ServiceAddAgentSuccessCB(objPath) {
- try { // calling dbus hook object function for un-translated types
- self.agents.push(agent);
- agent.registered = true;
- var result = [ objPath ];
- resolver.fulfill(result[0], true);
+ self.agents.push(agent);
+ try {
+ self._createWrapper(agent);
}
catch (e) {
var errorStr = cloudeebus.getError(e);
- cloudeebus.log("Method callback exception: " + errorStr);
+ cloudeebus.log("Exception creating agent wrapper " + agent.objectPath + " : " + errorStr);
resolver.reject(errorStr, true);
+ return;
}
+ resolver.fulfill(objPath, true);
}
function ServiceAddAgenterrorCB(error) {
var errorStr = cloudeebus.getError(error);
cloudeebus.log("Error adding agent : " + agent.objectPath + ", error: " + errorStr);
- self.promise.resolver.reject(errorStr, true);
- }
-
- try {
- self._createWrapper(agent);
- }
- catch (e) {
- var errorStr = cloudeebus.getError(e);
- alert("Exception creating agent wrapper " + agent.objectPath + " : " + errorStr);
resolver.reject(errorStr, true);
- return;
}
var arglist = [
+ self.name,
agent.objectPath,
agent.xml
];
return promise;
};
+
cloudeebus.Service.prototype._deleteWrapper = function(agent) {
- var objJs = agent.jsHdl;
+ var objJs = agent.handler;
if (objJs.methodId[agent.objectPath]) {
- for (var idx in objJs.methodId[agent.objectPath]) {
+ while (objJs.methodId[agent.objectPath].length) {
try {
- cloudeebus.log("unsubscribe " + objJs.methodId[agent.objectPath][idx]);
- this.wampSession.unsubscribe(objJs.methodId[agent.objectPath][idx]);
- objJs.methodId[agent.objectPath][idx] = null;
+ this.wampSession.unsubscribe( objJs.methodId[agent.objectPath].pop() );
}
catch (e) {
cloudeebus.log("Unsubscribe error: " + cloudeebus.getError(e));
}
}
- delete objJs.methodId[agent.objectPath];
+ objJs.methodId[agent.objectPath] = null;
}
};
-cloudeebus.Service.prototype.delAgent = function(rmAgent, successCB, errorCB) {
+
+cloudeebus.Service.prototype.removeAgent = function(rmAgent) {
var self = this;
var promise = new cloudeebus.Promise(function (resolver) {
- function ServiceDelAgentSuccessCB(agent) {
- try { // calling dbus hook object function for un-translated types
- self.agents.push(agent);
- agent.registered = true;
- var result = [ agent ];
- resolver.fulfill(result[0], true);
- }
- catch (e) {
- var errorStr = cloudeebus.getError(e);
- cloudeebus.log("Method callback exception: " + errorStr);
- resolver.reject(errorStr, true);
- }
+ function ServiceRemoveAgentSuccessCB(objectPath) {
+ // Searching agent in list
+ for (var idx in self.agents)
+ if (self.agents[idx].objectPath == objectPath) {
+ agent = self.agents[idx];
+ break;
+ }
+
+ self.agents.splice(idx, 1);
+ self._deleteWrapper(agent);
+ resolver.fulfill(agent, true);
}
- function ServiceDelAgentErrorCB(error) {
+ function ServiceRemoveAgentErrorCB(error) {
var errorStr = cloudeebus.getError(error);
cloudeebus.log("Error removing agent : " + rmAgent.objectPath + ", error: " + errorStr);
- self.promise.resolver.reject(errorStr, true);
+ resolver.reject(errorStr, true);
}
- try {
- self._deleteWrapper(rmAgent);
- }
- catch (e) {
- var errorStr = cloudeebus.getError(e);
- alert("Exception removing wrapper of agent " + rmAgent.objectPath + " : " + errorStr);
- errorCB(errorStr);
- }
-
var arglist = [
rmAgent.objectPath
];
// call dbusSend with bus type, destination, object, message and arguments
- self.wampSession.call("serviceDelAgent", arglist).then(ServiceDelAgentSuccessCB, ServiceDelAgentErrorCB);
+ self.wampSession.call("serviceDelAgent", arglist).then(ServiceRemoveAgentSuccessCB, ServiceRemoveAgentErrorCB);
});
return promise;
};
+
cloudeebus.Service.prototype._returnMethod = function(methodId, callIndex, success, result, successCB, errorCB) {
var arglist = [
methodId,
this.wampSession.call("returnMethod", arglist).then(successCB, errorCB);
};
-cloudeebus.Service.prototype._emitSignal = function(objectPath, signalName, result, successCB, errorCB) {
+
+cloudeebus.Service.prototype._emitSignal = function(objectPath, signalName, args, successCB, errorCB) {
var arglist = [
objectPath,
signalName,
- result
+ JSON.stringify(args)
];
this.wampSession.call("emitSignal", arglist).then(successCB, errorCB);
+/******************************************************************************
+ * Copyright 2012 - 2013 Intel Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *****************************************************************************/
+
+
+
/*****************************************************************************/
function _processWrappers(wrappers, value) {
function _processWrappersAsync(wrappers, value) {
var taskid = -1;
function processAsyncOnce() {
- _processWrappers(wrappers, value);
+ if (!wrappers.processed)
+ _processWrappers(wrappers, value);
clearInterval(taskid);
+ wrappers.processed = true;
}
taskid = setInterval(processAsyncOnce, 200);
}
resolver.resolve(undefined, true);
else
for (i in arguments)
- Promise.resolve(arguments[i]).appendWrappers(fulfillCallback,rejectCallback);
+ cloudeebus.Promise.resolve(arguments[i]).appendWrappers(fulfillCallback,rejectCallback);
return promise;
};
resolver.resolve(args, true);
};
index++;
- Promise.resolve(arguments[i]).appendWrappers(fulfillCallback,rejectCallback);
+ cloudeebus.Promise.resolve(arguments[i]).appendWrappers(fulfillCallback,rejectCallback);
}
return promise;
resolver.reject(args, true);
};
index++;
- Promise.resolve(arguments[i]).appendWrappers(fulfillCallback,rejectCallback);
+ cloudeebus.Promise.resolve(arguments[i]).appendWrappers(fulfillCallback,rejectCallback);
}
return promise;
+/******************************************************************************
+ * Copyright 2012 - 2013 Intel Corporation.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *****************************************************************************/
+
+
+
/*****************************************************************************/
cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
this.busName = busName;
this.objectPath = objectPath;
this.interfaceProxies = {};
+ this.childNodeNames = [];
return this;
};
function introspectSuccessCB(str) {
var parser = new DOMParser();
var xmlDoc = parser.parseFromString(str, "text/xml");
+ var nodes = xmlDoc.getElementsByTagName("node");
+ // first node is the parent/head node
+ for(var i=1; i < nodes.length; i++)
+ self.childNodeNames.push(nodes[i].getAttribute("name"));
var interfaces = xmlDoc.getElementsByTagName("interface");
self.propInterfaces = [];
var supportDBusProperties = false;
var errorStr = cloudeebus.getError(error);
cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + errorStr);
if (errorCB)
- errorCB(cloudeebus.getError(errorStr));
+ errorCB(errorStr);
}
var arglist = [