1 /******************************************************************************
2 * Copyright 2012 Intel Corporation.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
19 /*****************************************************************************/
21 var dbus = { // hook object for dbus types not translated by python-json
22 Double: function(value, level) {
29 /*****************************************************************************/
31 var cloudeebus = window.cloudeebus = {
36 cloudeebus.reset = function() {
37 cloudeebus.sessionBus = null;
38 cloudeebus.systemBus = null;
39 cloudeebus.wampSession = null;
40 cloudeebus.uri = null;
44 cloudeebus.log = function(msg) {
48 cloudeebus.versionCheck = function(version) {
49 var ver = version.split(".");
50 var min = cloudeebus.minVersion.split(".");
51 for (var i=0; i<ver.length; i++) {
52 if (Number(ver[i]) > Number(min[i]))
54 if (Number(ver[i]) < Number(min[i]))
61 cloudeebus.connect = function(uri, manifest, successCB, errorCB) {
65 function onCloudeebusVersionCheckCB(version) {
66 if (cloudeebus.versionCheck(version)) {
67 cloudeebus.log("Connected to " + cloudeebus.uri);
71 var errorMsg = "Cloudeebus server version " + version + " unsupported, need version " + cloudeebus.minVersion + " or superior";
72 cloudeebus.log(errorMsg);
78 function onWAMPSessionAuthErrorCB(error) {
79 cloudeebus.log("Authentication error: " + error.desc);
84 function onWAMPSessionAuthenticatedCB(permissions) {
85 cloudeebus.sessionBus = new cloudeebus.BusConnection("session", cloudeebus.wampSession);
86 cloudeebus.systemBus = new cloudeebus.BusConnection("system", cloudeebus.wampSession);
87 cloudeebus.wampSession.call("getVersion").then(onCloudeebusVersionCheckCB, errorCB);
90 function onWAMPSessionChallengedCB(challenge) {
91 var signature = cloudeebus.wampSession.authsign(challenge, manifest.key);
92 cloudeebus.wampSession.auth(signature).then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
95 function onWAMPSessionConnectedCB(session) {
96 cloudeebus.wampSession = session;
98 cloudeebus.wampSession.authreq(
100 {permissions: manifest.permissions}
101 ).then(onWAMPSessionChallengedCB, onWAMPSessionAuthErrorCB);
103 cloudeebus.wampSession.authreq().then(function() {
104 cloudeebus.wampSession.auth().then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
105 }, onWAMPSessionAuthErrorCB);
108 function onWAMPSessionErrorCB(code, reason) {
109 if (code == ab.CONNECTION_UNSUPPORTED) {
110 cloudeebus.log("Browser is not supported");
113 cloudeebus.log("Failed to open session, code = " + code + ", reason = " + reason);
119 return ab.connect(cloudeebus.uri, onWAMPSessionConnectedCB, onWAMPSessionErrorCB);
123 cloudeebus.SessionBus = function() {
124 return cloudeebus.sessionBus;
128 cloudeebus.SystemBus = function() {
129 return cloudeebus.systemBus;
134 /*****************************************************************************/
136 cloudeebus.BusConnection = function(name, session) {
138 this.wampSession = session;
144 cloudeebus.BusConnection.prototype.getObject = function(busName, objectPath, introspectCB, errorCB) {
145 var proxy = new cloudeebus.ProxyObject(this.wampSession, this, busName, objectPath);
147 proxy._introspect(introspectCB, errorCB);
152 cloudeebus.BusConnection.prototype.addService = function(serviceName, successCB, errorCB) {
155 cloudeebusService = new cloudeebus.Service(this.wampSession, this, serviceName);
157 function busServiceAddedSuccessCB(service) {
158 self.service = cloudeebusService;
160 successCB(cloudeebusService);
163 cloudeebusService.add(busServiceAddedSuccessCB, errorCB);
164 return cloudeebusService;
167 cloudeebus.BusConnection.prototype.removeService = function(serviceName, successCB, errorCB) {
170 function busServiceRemovedSuccessCB(serviceName) {
171 // Be sure we are removing the service requested...
172 if (serviceName == self.service.name) {
175 successCB(serviceName);
179 cloudeebusService.remove(busServiceRemovedSuccessCB, errorCB);
183 /*****************************************************************************/
185 cloudeebus.Service = function(session, busConnection, name) {
186 this.wampSession = session;
187 this.busConnection = busConnection;
189 this.isCreated = false;
193 cloudeebus.Service.prototype.add = function(successCB, errorCB) {
196 function ServiceAddedSuccessCB(serviceName) {
202 alert("Exception adding service " + serviceName + " : " + e);
212 // call dbusSend with bus type, destination, object, message and arguments
213 this.wampSession.call("serviceAdd", arglist).then(ServiceAddedSuccessCB, errorCB);
216 cloudeebus.Service.prototype.remove = function(successCB, errorCB) {
217 function ServiceRemovedSuccessCB(serviceName) {
220 successCB(serviceName);
223 alert("Exception removing service " + serviceName + " : " + e);
232 // call dbusSend with bus type, destination, object, message and arguments
233 this.wampSession.call("serviceRelease", arglist).then(ServiceRemovedSuccessCB, errorCB);
236 cloudeebus.Service.prototype._searchMethod = function(ifName, method, objectJS) {
238 var funcToCall = null;
240 // Check if 'objectJS' has a member 'interfaceProxies' with an interface named 'ifName'
241 // and a method named 'method'
242 if (objectJS.interfaceProxies && objectJS.interfaceProxies[ifName] &&
243 objectJS.interfaceProxies[ifName][method]) {
244 funcToCall = objectJS.interfaceProxies[ifName][method];
246 // retrieve the method directly from 'root' of objectJs
247 funcToCall = objectJS[method];
253 cloudeebus.Service.prototype._addMethod = function(objectPath, ifName, method, objectJS) {
256 var methodId = this.name + "#" + objectPath + "#" + ifName + "#" + method;
257 var funcToCall = this._searchMethod(ifName, method, objectJS);
259 if (funcToCall == null)
260 cloudeebus.log("Method " + method + " doesn't exist in Javascript object");
262 objectJS.wrapperFunc[method] = function() {
264 var methodId = arguments[0];
266 // affectation of callDict in eval, otherwise dictionary(='{}') interpreted as block of code by eval
267 eval("callDict = " + arguments[1]);
269 result = funcToCall.apply(objectJS, callDict.args);
270 service._returnMethod(methodId, callDict.callIndex, true, result);
273 cloudeebus.log("Method " + ifName + "." + method + " call on " + objectPath + " exception: " + e);
274 service._returnMethod(methodId, callDict.callIndex, false, e.message);
277 this._registerMethod(methodId, objectJS.wrapperFunc[method]);
281 cloudeebus.Service.prototype._addSignal = function(objectPath, ifName, signal, objectJS) {
283 var methodExist = false;
285 if (objectJS.interfaceProxies && objectJS.interfaceProxies[ifName])
286 if (objectJS.interfaceProxies[ifName][signal]) {
289 objectJS.interfaceProxies[ifName][signal] = function() {
290 service.emitSignal(objectPath, signal, arguments[0]);
295 if ((objectJS[signal] == undefined || objectJS[signal] == null) && !methodExist)
296 objectJS[signal] = function() {
297 service.emitSignal(objectPath, signal, arguments[0]);
300 cloudeebus.log("Can not create new method to emit signal '" + signal + "' in object JS this method already exist!");
303 cloudeebus.Service.prototype._createWrapper = function(xmlTemplate, objectPath, objectJS) {
305 var parser = new DOMParser();
306 var xmlDoc = parser.parseFromString(xmlTemplate, "text/xml");
307 var ifXml = xmlDoc.getElementsByTagName("interface");
308 objectJS.wrapperFunc = [];
309 for (var i=0; i < ifXml.length; i++) {
310 var ifName = ifXml[i].attributes.getNamedItem("name").value;
311 var ifChild = ifXml[i].firstChild;
313 if (ifChild.nodeName == "method") {
314 var metName = ifChild.attributes.getNamedItem("name").value;
315 self._addMethod(objectPath, ifName, metName, objectJS);
317 if (ifChild.nodeName == "signal") {
318 var metName = ifChild.attributes.getNamedItem("name").value;
319 self._addSignal(objectPath, ifName, metName, objectJS);
321 ifChild = ifChild.nextSibling;
326 cloudeebus.Service.prototype.addAgent = function(objectPath, xmlTemplate, objectJS, successCB, errorCB) {
327 function ServiceAddAgentSuccessCB(objPath) {
333 alert("Exception adding agent " + objectPath + " : " + e);
339 this._createWrapper(xmlTemplate, objectPath, objectJS);
342 alert("Exception creating agent wrapper " + objectPath + " : " + e);
352 // call dbusSend with bus type, destination, object, message and arguments
353 this.wampSession.call("serviceAddAgent", arglist).then(ServiceAddAgentSuccessCB, errorCB);
356 cloudeebus.Service.prototype.delAgent = function(objectPath, successCB, errorCB) {
357 function ServiceDelAgentSuccessCB(agent) {
363 alert("Exception deleting agent " + objectPath + " : " + e);
372 // call dbusSend with bus type, destination, object, message and arguments
373 this.wampSession.call("serviceDelAgent", arglist).then(ServiceDelAgentSuccessCB, errorCB);
376 cloudeebus.Service.prototype._registerMethod = function(methodId, methodHandler) {
377 this.wampSession.subscribe(methodId, methodHandler);
380 cloudeebus.Service.prototype._returnMethod = function(methodId, callIndex, success, result, successCB, errorCB) {
388 this.wampSession.call("returnMethod", arglist).then(successCB, errorCB);
391 cloudeebus.Service.prototype.emitSignal = function(objectPath, signalName, result, successCB, errorCB) {
398 this.wampSession.call("emitSignal", arglist).then(successCB, errorCB);
402 /*****************************************************************************/
404 function _processWrappers(wrappers, value) {
405 for (var i=0; i<wrappers.length; i++)
410 function _processWrappersAsync(wrappers, value) {
412 function processAsyncOnce() {
413 _processWrappers(wrappers, value);
414 clearInterval(taskid);
416 taskid = setInterval(processAsyncOnce, 200);
421 /*****************************************************************************/
423 cloudeebus.FutureResolver = function(future) {
424 this.future = future;
425 this.resolved = null;
430 cloudeebus.FutureResolver.prototype.resolve = function(value, sync) {
434 var then = (value && value.then && value.then.apply) ? value.then : null;
437 var acceptCallback = function(arg) {
438 self.accept(arg, true);
440 var rejectCallback = function(arg) {
441 self.reject(arg, true);
444 then.apply(value, [acceptCallback, rejectCallback]);
447 this.reject(e, true);
451 this.accept(value, sync);
455 cloudeebus.FutureResolver.prototype.accept = function(value, sync) {
459 var future = this.future;
460 future.state = "accepted";
461 future.result = value;
463 this.resolved = true;
465 _processWrappers(future._acceptWrappers, value);
467 _processWrappersAsync(future._acceptWrappers, value);
471 cloudeebus.FutureResolver.prototype.reject = function(value, sync) {
475 var future = this.future;
476 future.state = "rejected";
477 future.result = value;
479 this.resolved = true;
481 _processWrappers(future._rejectWrappers, value);
483 _processWrappersAsync(future._rejectWrappers, value);
488 /*****************************************************************************/
490 cloudeebus.Future = function(init) {
491 this.state = "pending";
493 this._acceptWrappers = [];
494 this._rejectWrappers = [];
495 this.resolver = new cloudeebus.FutureResolver(this);
498 init.apply(this, [this.resolver]);
501 this.resolver.reject(e, true);
508 cloudeebus.Future.prototype.appendWrappers = function(acceptWrapper, rejectWrapper) {
509 this._acceptWrappers.push(acceptWrapper);
510 this._rejectWrappers.push(rejectWrapper);
511 if (this.state == "accepted")
512 _processWrappersAsync(this._acceptWrappers, this.result);
513 if (this.state == "rejected")
514 _processWrappersAsync(this._rejectWrappers, this.result);
518 cloudeebus.Future.prototype.then = function(acceptCB, rejectCB) {
519 var future = new cloudeebus.Future();
520 var resolver = future.resolver;
521 var acceptWrapper, rejectWrapper;
524 acceptWrapper = function(arg) {
526 var value = acceptCB.apply(future, [arg]);
527 resolver.resolve(value, true);
530 resolver.reject(e, true);
534 acceptWrapper = function(arg) {
535 resolver.accept(arg, true);
539 rejectWrapper = function(arg) {
541 var value = rejectCB.apply(future, [arg]);
542 resolver.resolve(value, true);
545 resolver.reject(e, true);
549 rejectWrapper = function(arg) {
550 resolver.reject(arg, true);
553 this.appendWrappers(acceptWrapper,rejectWrapper);
559 /*****************************************************************************/
561 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
562 this.wampSession = session;
563 this.busConnection = busConnection;
564 this.busName = busName;
565 this.objectPath = objectPath;
566 this.interfaceProxies = {};
571 cloudeebus.ProxyObject.prototype.getInterface = function(ifName) {
572 return this.interfaceProxies[ifName];
576 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
580 function getAllPropertiesSuccessCB(props) {
581 var ifProxy = self.interfaceProxies[self.propInterfaces[self.propInterfaces.length-1]];
582 for (var prop in props)
583 ifProxy[prop] = self[prop] = props[prop];
584 getAllPropertiesNextInterfaceCB();
587 function getAllPropertiesNextInterfaceCB() {
588 self.propInterfaces.pop();
589 if (self.propInterfaces.length > 0)
590 self.callMethod("org.freedesktop.DBus.Properties",
592 [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB,
593 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
595 self.propInterfaces = null;
601 function introspectSuccessCB(str) {
602 var parser = new DOMParser();
603 var xmlDoc = parser.parseFromString(str, "text/xml");
604 var interfaces = xmlDoc.getElementsByTagName("interface");
605 self.propInterfaces = [];
606 var supportDBusProperties = false;
607 for (var i=0; i < interfaces.length; i++) {
608 var ifName = interfaces[i].attributes.getNamedItem("name").value;
609 self.interfaceProxies[ifName] = new cloudeebus.ProxyObject(self.wampSession, self.busConnection, self.busName, self.objectPath);
610 if (ifName == "org.freedesktop.DBus.Properties")
611 supportDBusProperties = true;
612 var hasProperties = false;
613 var ifChild = interfaces[i].firstChild;
615 if (ifChild.nodeName == "method") {
618 var metChild = ifChild.firstChild;
620 if (metChild.nodeName == "arg" &&
621 metChild.attributes.getNamedItem("direction").value == "in") {
622 signature += metChild.attributes.getNamedItem("type").value;
625 metChild = metChild.nextSibling;
627 var metName = ifChild.attributes.getNamedItem("name").value;
629 self._addMethod(ifName, metName, nArgs, signature);
630 self.interfaceProxies[ifName]._addMethod(ifName, metName, nArgs, signature);
632 else if (ifChild.nodeName == "property") {
634 self.propInterfaces.push(ifName);
635 hasProperties = true;
637 ifChild = ifChild.nextSibling;
640 if (supportDBusProperties && self.propInterfaces.length > 0) {
641 self.callMethod("org.freedesktop.DBus.Properties",
643 [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB,
644 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
647 self.propInterfaces = null;
653 // call Introspect on self
654 self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", []).then(introspectSuccessCB, errorCB);
658 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs, signature) {
662 self[method] = function() {
664 for (var i=0; i < nArgs; i++ )
665 args.push(arguments[i]);
666 return self.callMethod(ifName, method, args, signature);
671 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, signature) {
675 var future = new cloudeebus.Future(function (resolver) {
676 function callMethodSuccessCB(str) {
677 try { // calling dbus hook object function for un-translated types
678 var result = eval(str);
679 resolver.accept(result[0]);
682 cloudeebus.log("Method callback exception: " + e);
687 function callMethodErrorCB(error) {
688 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
689 resolver.reject(error.desc);
693 self.busConnection.name,
701 // call dbusSend with bus type, destination, object, message and arguments
702 self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
709 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
713 function signalHandler(id, data) {
715 try { // calling dbus hook object function for un-translated types
716 successCB.apply(self, eval(data));
719 cloudeebus.log("Signal handler exception: " + e);
726 function connectToSignalSuccessCB(str) {
728 self.wampSession.subscribe(str, signalHandler);
731 cloudeebus.log("Subscribe error: " + e);
735 function connectToSignalErrorCB(error) {
736 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
742 self.busConnection.name,
749 // call dbusSend with bus type, destination, object, message and arguments
750 self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
754 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
756 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
759 cloudeebus.log("Unsubscribe error: " + e);