From: Luc Yriarte Date: Mon, 23 Sep 2013 16:53:50 +0000 (+0200) Subject: split JS components X-Git-Tag: upstream/0.6.1~12 X-Git-Url: http://review.tizen.org/git/?p=contrib%2Fcloudeebus.git;a=commitdiff_plain;h=7362e8be2114373c42259fefa0f151a9a78af1ea split JS components --- diff --git a/cloudeebus/cloudeebus.js b/cloudeebus/cloudeebus.js index b8d568f..cafd3c7 100644 --- a/cloudeebus/cloudeebus.js +++ b/cloudeebus/cloudeebus.js @@ -1,5 +1,5 @@ /****************************************************************************** - * 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. @@ -196,6 +196,24 @@ cloudeebus.BusConnection.prototype.addService = function(serviceName) { +/****************************************************************************** + * 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.Agent = function(objectPath, handler, xml) { @@ -445,6 +463,24 @@ cloudeebus.Service.prototype._emitSignal = function(objectPath, signalName, args +/****************************************************************************** + * 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) { @@ -707,6 +743,24 @@ cloudeebus.Promise.some = function() { +/****************************************************************************** + * 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) { diff --git a/src/js/Makefile b/src/js/Makefile new file mode 100644 index 0000000..482947d --- /dev/null +++ b/src/js/Makefile @@ -0,0 +1,17 @@ +SOURCES = \ + cloudeebus-connection.js \ + cloudeebus-service.js \ + cloudeebus-promise.js \ + cloudeebus-proxy.js + +DESTINATION = ../../cloudeebus + +TARGET = $(DESTINATION)/cloudeebus.js + +$(TARGET): $(SOURCES) + cat $(SOURCES) > $(TARGET) + +all: $(TARGET) + +clean: + rm $(TARGET) diff --git a/src/js/cloudeebus-connection.js b/src/js/cloudeebus-connection.js new file mode 100644 index 0000000..4cf39fd --- /dev/null +++ b/src/js/cloudeebus-connection.js @@ -0,0 +1,198 @@ +/****************************************************************************** + * 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. + *****************************************************************************/ + + + +/*****************************************************************************/ + +var dbus = { // hook object for dbus types not translated by python-json + Double: function(value, level) { + return value; + } +}; + + + +/*****************************************************************************/ + +var cloudeebus = window.cloudeebus = { + version: "0.6.0", + minVersion: "0.6.0" +}; + +cloudeebus.reset = function() { + cloudeebus.sessionBus = null; + cloudeebus.systemBus = null; + cloudeebus.wampSession = null; + cloudeebus.uri = null; +}; + + +cloudeebus.log = function(msg) { +}; + +cloudeebus.getError = function(error) { + if (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; // Javascript exception + if (error.message) + return error.message; + if (error.name) + return error.name; + return error; // Autobahn error +}; + +cloudeebus.versionCheck = function(version) { + var ver = version.split("."); + var min = cloudeebus.minVersion.split("."); + for (var i=0; i Number(min[i])) + return true; + if (Number(ver[i]) < Number(min[i])) + return false; + } + return true; +}; + + +cloudeebus.connect = function(uri, manifest, successCB, errorCB) { + cloudeebus.reset(); + cloudeebus.uri = uri; + + 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.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, + services: manifest.services} + ).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) { + cloudeebus.log("Browser is not supported"); + } + else { + cloudeebus.log("Failed to open session, code = " + code + ", reason = " + reason); + } + if (errorCB) + errorCB(reason); + } + + return ab.connect(cloudeebus.uri, onWAMPSessionConnectedCB, onWAMPSessionErrorCB); +}; + + +cloudeebus.SessionBus = function() { + return cloudeebus.sessionBus; +}; + + +cloudeebus.SystemBus = function() { + return cloudeebus.systemBus; +}; + + + +/*****************************************************************************/ + +cloudeebus.BusConnection = function(name, session) { + 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.addService = function(serviceName) { + var self = this; + + var promise = new cloudeebus.Promise(function (resolver) { + var cloudeebusService = new cloudeebus.Service(self.wampSession, self, serviceName); + + function ServiceAddedSuccessCB(serviceName) { + 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); + resolver.reject(errorStr, true); + } + + var arglist = [ + self.name, + serviceName + ]; + + // call dbusSend with bus type, destination, object, message and arguments + self.wampSession.call("serviceAdd", arglist).then(ServiceAddedSuccessCB, ServiceAddedErrorCB); + }); + + return promise; +}; + + + diff --git a/src/js/cloudeebus-promise.js b/src/js/cloudeebus-promise.js new file mode 100644 index 0000000..2bc6b0e --- /dev/null +++ b/src/js/cloudeebus-promise.js @@ -0,0 +1,280 @@ +/****************************************************************************** + * 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) { + for (var i=0; i 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 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; + 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() { + var args = []; + for (var i=0; i < nArgs; i++ ) + args.push(arguments[i]); + return self.callMethod(ifName, method, args, signature); + }; +}; + + +cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, signature) { + + 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); + } + } + + function callMethodErrorCB(error) { + var errorStr = cloudeebus.getError(error); + cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + errorStr); + resolver.reject(errorStr, true); + } + + 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, handlerCB, errorCB) { + + var self = this; + + function signalHandler(id, 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) { + try { + self.wampSession.subscribe(str, signalHandler); + } + catch (e) { + cloudeebus.log("Subscribe error: " + cloudeebus.getError(e)); + } + } + + function connectToSignalErrorCB(error) { + var errorStr = cloudeebus.getError(error); + cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + errorStr); + if (errorCB) + errorCB(errorStr); + } + + var arglist = [ + self.busConnection.name, + self.busName, + self.objectPath, + ifName, + signal + ]; + + // 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)); + } +}; diff --git a/src/js/cloudeebus-service.js b/src/js/cloudeebus-service.js new file mode 100644 index 0000000..a9ce1d7 --- /dev/null +++ b/src/js/cloudeebus-service.js @@ -0,0 +1,267 @@ +/****************************************************************************** + * 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.Agent = function(objectPath, handler, xml) { + this.xml = xml; + this.objectPath = objectPath; + this.handler = handler; + 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) { + resolver.fulfill(serviceName, true); + } + + function ServiceRemovedErrorCB(error) { + var errorStr = cloudeebus.getError(error); + resolver.reject(errorStr, true); + } + + 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.handler); + + if (funcToCall == null) + cloudeebus.log("Method " + method + " doesn't exist in Javascript object"); + else { + 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.handler, 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.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; + + 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); + }; + } +}; + + +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.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; + 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) { + self.agents.push(agent); + 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; + } + resolver.fulfill(objPath, true); + } + + function ServiceAddAgenterrorCB(error) { + var errorStr = cloudeebus.getError(error); + cloudeebus.log("Error adding agent : " + agent.objectPath + ", error: " + errorStr); + resolver.reject(errorStr, true); + } + + var arglist = [ + self.name, + 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.handler; + if (objJs.methodId[agent.objectPath]) { + while (objJs.methodId[agent.objectPath].length) { + try { + this.wampSession.unsubscribe( objJs.methodId[agent.objectPath].pop() ); + } + catch (e) { + cloudeebus.log("Unsubscribe error: " + cloudeebus.getError(e)); + } + } + objJs.methodId[agent.objectPath] = null; + } +}; + + +cloudeebus.Service.prototype.removeAgent = function(rmAgent) { + var self = this; + + var promise = new cloudeebus.Promise(function (resolver) { + 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 ServiceRemoveAgentErrorCB(error) { + var errorStr = cloudeebus.getError(error); + cloudeebus.log("Error removing agent : " + rmAgent.objectPath + ", error: " + errorStr); + resolver.reject(errorStr, true); + } + + var arglist = [ + rmAgent.objectPath + ]; + + // call dbusSend with bus type, destination, object, message and arguments + self.wampSession.call("serviceDelAgent", arglist).then(ServiceRemoveAgentSuccessCB, ServiceRemoveAgentErrorCB); + }); + + 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, args, successCB, errorCB) { + var arglist = [ + objectPath, + signalName, + JSON.stringify(args) + ]; + + this.wampSession.call("emitSignal", arglist).then(successCB, errorCB); +}; + + +