/*****************************************************************************/
-var cloudeebus = window.cloudeebus = {};
+var dbus = { // hook object for dbus types not translated by python-json
+ Double: function(value, level) {
+ return value;
+ }
+};
+
+
+
+/*****************************************************************************/
+
+var cloudeebus = window.cloudeebus = {
+ version: "0.5.1",
+ minVersion: "0.3.2"
+};
cloudeebus.reset = function() {
cloudeebus.sessionBus = null;
};
-cloudeebus.log = function(msg) {
+cloudeebus.log = function(msg) {
};
+cloudeebus.getError = function(error) {
+ if (error.desc && error.uri)
+ return error.desc + " : " + error.uri;
+ if (error.desc)
+ return error.desc;
+ if (error.uri)
+ return error.uri;
+ if (error.name && error.message)
+ return error.name + " : " + error.message;
+ if (error.message)
+ return error.message;
+ return error;
+};
+
+cloudeebus.versionCheck = function(version) {
+ var ver = version.split(".");
+ var min = cloudeebus.minVersion.split(".");
+ for (var i=0; i<ver.length; i++) {
+ if (Number(ver[i]) > Number(min[i]))
+ return true;
+ if (Number(ver[i]) < Number(min[i]))
+ return false;
+ }
+ return true;
+};
-cloudeebus.connect = function(uri, successCB, errorCB) {
+
+cloudeebus.connect = function(uri, manifest, successCB, errorCB) {
cloudeebus.reset();
cloudeebus.uri = uri;
- function onWAMPSessionConnectedCB(session) {
- cloudeebus.wampSession = session;
+ function onCloudeebusVersionCheckCB(version) {
+ if (cloudeebus.versionCheck(version)) {
+ cloudeebus.log("Connected to " + cloudeebus.uri);
+ if (successCB)
+ successCB();
+ } else {
+ var errorMsg = "Cloudeebus server version " + version + " unsupported, need version " + cloudeebus.minVersion + " or superior";
+ cloudeebus.log(errorMsg);
+ if (errorCB)
+ errorCB(errorMsg);
+ }
+ }
+
+ function onWAMPSessionAuthErrorCB(error) {
+ var errorStr = cloudeebus.getError(error);
+ cloudeebus.log("Authentication error: " + errorStr);
+ if (errorCB)
+ errorCB(errorStr);
+ }
+
+ function onWAMPSessionAuthenticatedCB(permissions) {
cloudeebus.sessionBus = new cloudeebus.BusConnection("session", cloudeebus.wampSession);
cloudeebus.systemBus = new cloudeebus.BusConnection("system", cloudeebus.wampSession);
- cloudeebus.log("Connected to " + cloudeebus.uri);
- if (successCB)
- successCB();
- };
+ cloudeebus.wampSession.call("getVersion").then(onCloudeebusVersionCheckCB, errorCB);
+ }
+
+ function onWAMPSessionChallengedCB(challenge) {
+ var signature = cloudeebus.wampSession.authsign(challenge, manifest.key);
+ cloudeebus.wampSession.auth(signature).then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
+ }
+
+ function onWAMPSessionConnectedCB(session) {
+ cloudeebus.wampSession = session;
+ if (manifest)
+ cloudeebus.wampSession.authreq(
+ manifest.name,
+ {permissions: manifest.permissions}
+ ).then(onWAMPSessionChallengedCB, onWAMPSessionAuthErrorCB);
+ else
+ cloudeebus.wampSession.authreq().then(function() {
+ cloudeebus.wampSession.auth().then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
+ }, onWAMPSessionAuthErrorCB);
+ }
function onWAMPSessionErrorCB(code, reason) {
if (code == ab.CONNECTION_UNSUPPORTED) {
}
if (errorCB)
errorCB(reason);
- };
+ }
return ab.connect(cloudeebus.uri, onWAMPSessionConnectedCB, onWAMPSessionErrorCB);
};
this.name = name;
this.wampSession = session;
return this;
-}
+};
+
+
+cloudeebus.BusConnection.prototype.getObject = function(busName, objectPath, introspectCB, errorCB) {
+ var proxy = new cloudeebus.ProxyObject(this.wampSession, this, busName, objectPath);
+ if (introspectCB)
+ proxy._introspect(introspectCB, errorCB);
+ return proxy;
+};
-cloudeebus.BusConnection.prototype.listNames = function(successCB, errorCB) {
+cloudeebus.BusConnection.prototype.addService = function(serviceName) {
+ var self = this;
- var self = this;
+ var promise = new cloudeebus.Promise(function (resolver) {
+ 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);
+ }
+ }
+
+ function ServiceAddedErrorCB(error) {
+ var errorStr = cloudeebus.getError(error);
+ cloudeebus.log("Error adding service method: " + self.name + ", error: " + errorStr);
+ self.promise.resolver.reject(errorStr, true);
+ }
- function listNamesSuccessCB(str) {
- if (successCB)
- successCB(JSON.parse(str));
- };
+ var arglist = [
+ self.name,
+ serviceName
+ ];
- function listNamesErrorCB(error) {
- cloudeebus.log("Failed to list names for bus: " + self.name);
- cloudeebus.log(error.desc);
- if (errorCB)
- errorCB(error.desc);
- };
+ // call dbusSend with bus type, destination, object, message and arguments
+ self.wampSession.call("serviceAdd", arglist).then(ServiceAddedSuccessCB, ServiceAddedErrorCB);
+ });
+
+ return promise;
+};
+
+
+/*****************************************************************************/
+//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;
+ this.xml = xml;
+ this.objectPath = objPath;
+ this.jsHdl = jsHdl;
+ return this;
+};
+
+
+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]) {
+ 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) {
+ 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);
+ }
+ catch (e) {
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("Method callback exception: " + errorStr);
+ resolver.reject(errorStr, 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);
+ cloudeebus.log("Exception creating agent wrapper " + agent.objectPath + " : " + errorStr);
+ resolver.reject(errorStr, true);
+ return;
+ }
+
+ var arglist = [
+ agent.objectPath,
+ agent.xml
+ ];
+
+ // call dbusSend with bus type, destination, object, message and arguments
+ self.wampSession.call("serviceAddAgent", arglist).then(ServiceAddAgentSuccessCB, ServiceAddAgenterrorCB);
+ });
+
+ return promise;
+};
+
+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) {
+ 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 ServiceDelAgentErrorCB(error) {
+ var errorStr = cloudeebus.getError(error);
+ cloudeebus.log("Error removing agent : " + rmAgent.objectPath + ", error: " + errorStr);
+ self.promise.resolver.reject(errorStr, true);
+ }
+
+ try {
+ self._deleteWrapper(rmAgent);
+ }
+ catch (e) {
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("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);
+ });
+
+ return promise;
+};
+
+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);
+};
+
+
+
+/*****************************************************************************/
- // call listNames with bus name
- self.wampSession.call("listNames", [self.name]).then(listNamesSuccessCB, listNamesErrorCB);
+function _processWrappers(wrappers, value) {
+ for (var i=0; i<wrappers.length; i++)
+ wrappers[i](value);
}
-cloudeebus.BusConnection.prototype.getObject = function(busName, objectPath) {
- return new cloudeebus.ProxyObject(this.wampSession, this, busName, objectPath);
+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";
+ this.result = null;
+ this._fulfillWrappers = [];
+ this._rejectWrappers = [];
+ this.resolver = new cloudeebus.PromiseResolver(this);
+ if (init) {
+ try {
+ init.apply(this, [this.resolver]);
+ }
+ catch (e) {
+ this.resolver.reject(cloudeebus.getError(e), true);
+ }
+ }
+ return this;
+};
+
+
+cloudeebus.Promise.prototype.appendWrappers = function(fulfillWrapper, rejectWrapper) {
+ if (fulfillWrapper)
+ this._fulfillWrappers.push(fulfillWrapper);
+ if (rejectWrapper)
+ this._rejectWrappers.push(rejectWrapper);
+ if (this.state == "fulfilled")
+ _processWrappersAsync(this._fulfillWrappers, this.result);
+ if (this.state == "rejected")
+ _processWrappersAsync(this._rejectWrappers, this.result);
+};
+
+
+cloudeebus.Promise.prototype.then = function(fulfillCB, rejectCB) {
+ var promise = new cloudeebus.Promise();
+ var resolver = promise.resolver;
+ var fulfillWrapper, rejectWrapper;
+
+ if (fulfillCB)
+ fulfillWrapper = function(arg) {
+ try {
+ var value = fulfillCB.apply(promise, [arg]);
+ resolver.resolve(value, true);
+ }
+ catch (e) {
+ resolver.reject(cloudeebus.getError(e), true);
+ }
+ };
+ else
+ fulfillWrapper = function(arg) {
+ resolver.fulfill(arg, true);
+ };
+
+ if (rejectCB)
+ rejectWrapper = function(arg) {
+ try {
+ var value = rejectCB.apply(promise, [arg]);
+ resolver.resolve(value, true);
+ }
+ catch (e) {
+ resolver.reject(cloudeebus.getError(e), true);
+ }
+ };
+ else
+ rejectWrapper = function(arg) {
+ resolver.reject(arg, true);
+ };
+
+ this.appendWrappers(fulfillWrapper,rejectWrapper);
+ return promise;
+};
+
+
+cloudeebus.Promise.prototype["catch"] = function(rejectCB) {
+ return this.then(undefined,rejectCB);
+};
+
+
+cloudeebus.Promise.prototype.done = function(fulfillCB, rejectCB) {
+ this.appendWrappers(fulfillCB,rejectCB);
+};
+
+
+cloudeebus.Promise.resolve = function(value) {
+ var promise = new cloudeebus.Promise();
+ promise.resolver.resolve(value);
+ return promise;
+};
+
+
+cloudeebus.Promise.fulfill = function(value) {
+ var promise = new cloudeebus.Promise();
+ promise.resolver.fulfill(value);
+ return promise;
+};
+
+
+cloudeebus.Promise.reject = function(value) {
+ var promise = new cloudeebus.Promise();
+ promise.resolver.reject(value);
+ return promise;
+};
+
+
+cloudeebus.Promise.any = function() {
+ var promise = new cloudeebus.Promise();
+ var resolver = promise.resolver;
+ var fulfillCallback = function(arg) {
+ resolver.resolve(arg, true);
+ };
+ var rejectCallback = function(arg) {
+ resolver.reject(arg, true);
+ };
+ if (arguments.length == 0)
+ resolver.resolve(undefined, true);
+ else
+ for (i in arguments)
+ Promise.resolve(arguments[i]).appendWrappers(fulfillCallback,rejectCallback);
+ return promise;
+};
+
+
+cloudeebus.Promise.every = function() {
+ var promise = new cloudeebus.Promise();
+ var resolver = promise.resolver;
+ var index = 0;
+ var countdown = arguments.length;
+ var args = new Array(countdown);
+ var rejectCallback = function(arg) {
+ resolver.reject(arg, true);
+ };
+ if (arguments.length == 0)
+ resolver.resolve(undefined, true);
+ else
+ for (i in arguments) {
+ var fulfillCallback = function(arg) {
+ args[index] = arg;
+ countdown--;
+ if (countdown == 0)
+ resolver.resolve(args, true);
+ };
+ index++;
+ Promise.resolve(arguments[i]).appendWrappers(fulfillCallback,rejectCallback);
+ }
+
+ return promise;
+};
+
+
+cloudeebus.Promise.some = function() {
+ var promise = new cloudeebus.Promise();
+ var resolver = promise.resolver;
+ var index = 0;
+ var countdown = arguments.length;
+ var args = new Array(countdown);
+ var fulfillCallback = function(arg) {
+ resolver.resolve(arg, true);
+ };
+ if (arguments.length == 0)
+ resolver.resolve(undefined, true);
+ else
+ for (i in arguments) {
+ var rejectCallback = function(arg) {
+ args[index] = arg;
+ countdown--;
+ if (countdown == 0)
+ resolver.reject(args, true);
+ };
+ index++;
+ Promise.resolve(arguments[i]).appendWrappers(fulfillCallback,rejectCallback);
+ }
+
+ return promise;
+};
+
+
+
+/*****************************************************************************/
+
cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
this.wampSession = session;
this.busConnection = busConnection;
this.busName = busName;
this.objectPath = objectPath;
+ this.interfaceProxies = {};
return this;
-}
+};
+
+
+cloudeebus.ProxyObject.prototype.getInterface = function(ifName) {
+ return this.interfaceProxies[ifName];
+};
-cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
+cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
+
+ var self = this;
+
+ function getAllPropertiesSuccessCB(props) {
+ var ifProxy = self.interfaceProxies[self.propInterfaces[self.propInterfaces.length-1]];
+ for (var prop in props)
+ ifProxy[prop] = self[prop] = props[prop];
+ getAllPropertiesNextInterfaceCB();
+ }
+
+ function getAllPropertiesNextInterfaceCB() {
+ self.propInterfaces.pop();
+ if (self.propInterfaces.length > 0)
+ self.callMethod("org.freedesktop.DBus.Properties",
+ "GetAll",
+ [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB,
+ errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
+ else {
+ self.propInterfaces = null;
+ if (successCB)
+ successCB(self);
+ }
+ }
+ function introspectSuccessCB(str) {
+ var parser = new DOMParser();
+ var xmlDoc = parser.parseFromString(str, "text/xml");
+ var interfaces = xmlDoc.getElementsByTagName("interface");
+ self.propInterfaces = [];
+ var supportDBusProperties = false;
+ for (var i=0; i < interfaces.length; i++) {
+ var ifName = interfaces[i].attributes.getNamedItem("name").value;
+ self.interfaceProxies[ifName] = new cloudeebus.ProxyObject(self.wampSession, self.busConnection, self.busName, self.objectPath);
+ if (ifName == "org.freedesktop.DBus.Properties")
+ supportDBusProperties = true;
+ var hasProperties = false;
+ var ifChild = interfaces[i].firstChild;
+ while (ifChild) {
+ if (ifChild.nodeName == "method") {
+ var nArgs = 0;
+ var signature = "";
+ var metChild = ifChild.firstChild;
+ while (metChild) {
+ if (metChild.nodeName == "arg" &&
+ metChild.attributes.getNamedItem("direction").value == "in") {
+ signature += metChild.attributes.getNamedItem("type").value;
+ nArgs++;
+ }
+ metChild = metChild.nextSibling;
+ }
+ var metName = ifChild.attributes.getNamedItem("name").value;
+ if (!self[metName])
+ self._addMethod(ifName, metName, nArgs, signature);
+ self.interfaceProxies[ifName]._addMethod(ifName, metName, nArgs, signature);
+ }
+ else if (ifChild.nodeName == "property") {
+ if (!hasProperties)
+ self.propInterfaces.push(ifName);
+ hasProperties = true;
+ }
+ ifChild = ifChild.nextSibling;
+ }
+ }
+ if (supportDBusProperties && self.propInterfaces.length > 0) {
+ self.callMethod("org.freedesktop.DBus.Properties",
+ "GetAll",
+ [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB,
+ errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
+ }
+ else {
+ self.propInterfaces = null;
+ if (successCB)
+ successCB(self);
+ }
+ }
+
+ // call Introspect on self
+ self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", []).then(introspectSuccessCB, errorCB);
+};
+
+
+cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs, signature) {
+
var self = this;
self[method] = function() {
- if (arguments.length < nArgs || arguments.length > nArgs + 2)
- throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
var args = [];
- var successCB = null;
- var errorCB = null;
for (var i=0; i < nArgs; i++ )
args.push(arguments[i]);
- if (arguments.length > nArgs)
- successCB = arguments[nArgs];
- if (arguments.length > nArgs + 1)
- errorCB = arguments[nArgs + 1];
- self.callMethod(ifName, method, args, successCB, errorCB);
- }
-
-}
+ return self.callMethod(ifName, method, args, signature);
+ };
+};
-cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, successCB, errorCB) {
+cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, signature) {
- var self = this;
-
- function callMethodSuccessCB(str) {
- if (successCB)
- successCB(JSON.parse(str));
- };
-
- function callMethodErrorCB(error) {
- cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath);
- cloudeebus.log(error.desc);
- if (errorCB)
- errorCB(error.desc);
- };
+ var self = this;
+
+ var promise = new cloudeebus.Promise(function (resolver) {
+ function callMethodSuccessCB(str) {
+ try { // calling dbus hook object function for un-translated types
+ var result = eval(str);
+ resolver.fulfill(result[0], true);
+ }
+ catch (e) {
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("Method callback exception: " + errorStr);
+ resolver.reject(errorStr, true);
+ }
+ }
- var arglist = [
- self.busConnection.name,
- self.busName,
- self.objectPath,
- ifName,
- method,
- JSON.stringify(args)
- ];
+ function callMethodErrorCB(error) {
+ var errorStr = cloudeebus.getError(error);
+ cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + errorStr);
+ resolver.reject(errorStr, true);
+ }
- // call dbusSend with bus type, destination, object, message and arguments
- self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
-}
+ var arglist = [
+ self.busConnection.name,
+ self.busName,
+ self.objectPath,
+ ifName,
+ method,
+ JSON.stringify(args)
+ ];
+
+ // call dbusSend with bus type, destination, object, message and arguments
+ self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
+ });
+
+ return promise;
+};
-cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
+cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, handlerCB, errorCB) {
var self = this;
function signalHandler(id, data) {
- cloudeebus.log("Object: " + self.objectPath + " received signal: " + signal + " id: " + id);
- if (successCB)
- successCB(JSON.parse(data));
- };
+ if (handlerCB) {
+ try { // calling dbus hook object function for un-translated types
+ handlerCB.apply(self, eval(data));
+ }
+ catch (e) {
+ var errorStr = cloudeebus.getError(e);
+ cloudeebus.log("Signal handler exception: " + errorStr);
+ if (errorCB)
+ errorCB(errorStr);
+ }
+ }
+ }
function connectToSignalSuccessCB(str) {
- cloudeebus.log("Object: " + self.objectPath + " subscribing to signal: " + str);
- self.wampSession.subscribe(str, signalHandler);
- };
+ try {
+ self.wampSession.subscribe(str, signalHandler);
+ }
+ catch (e) {
+ cloudeebus.log("Subscribe error: " + cloudeebus.getError(e));
+ }
+ }
function connectToSignalErrorCB(error) {
- cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath);
- cloudeebus.log(error.desc);
+ var errorStr = cloudeebus.getError(error);
+ cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + errorStr);
if (errorCB)
- errorCB(error.desc);
- };
+ errorCB(cloudeebus.getError(errorStr));
+ }
- var arglist = [
+ var arglist = [
self.busConnection.name,
self.busName,
self.objectPath,
signal
];
- // call dbusSend with bus type, destination, object, message and arguments
- self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
-}
+ // call dbusSend with bus type, destination, object, message and arguments
+ self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
+};
+
+
+cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
+ try {
+ this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
+ }
+ catch (e) {
+ cloudeebus.log("Unsubscribe error: " + cloudeebus.getError(e));
+ }
+};