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 = {version: "0.3.1"};
33 cloudeebus.reset = function() {
34 cloudeebus.sessionBus = null;
35 cloudeebus.systemBus = null;
36 cloudeebus.wampSession = null;
37 cloudeebus.uri = null;
41 cloudeebus.log = function(msg) {
45 cloudeebus.connect = function(uri, manifest, successCB, errorCB) {
49 function onCloudeebusVersionCheckCB(version) {
50 if (cloudeebus.version == version) {
51 cloudeebus.log("Connected to " + cloudeebus.uri);
55 var errorMsg = "Cloudeebus server version " + version + " and client version " + cloudeebus.version + " mismatch";
56 cloudeebus.log(errorMsg);
62 function onWAMPSessionAuthErrorCB(error) {
63 cloudeebus.log("Authentication error: " + error.desc);
68 function onWAMPSessionAuthenticatedCB(permissions) {
69 cloudeebus.sessionBus = new cloudeebus.BusConnection("session", cloudeebus.wampSession);
70 cloudeebus.systemBus = new cloudeebus.BusConnection("system", cloudeebus.wampSession);
71 cloudeebus.wampSession.call("getVersion").then(onCloudeebusVersionCheckCB, errorCB);
74 function onWAMPSessionChallengedCB(challenge) {
75 var signature = cloudeebus.wampSession.authsign(challenge, manifest.key);
76 cloudeebus.wampSession.auth(signature).then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
79 function onWAMPSessionConnectedCB(session) {
80 cloudeebus.wampSession = session;
82 cloudeebus.wampSession.authreq(
84 {permissions: JSON.stringify(manifest.permissions)}
85 ).then(onWAMPSessionChallengedCB, onWAMPSessionAuthErrorCB);
87 cloudeebus.wampSession.authreq().then(function() {
88 cloudeebus.wampSession.auth().then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
89 }, onWAMPSessionAuthErrorCB);
92 function onWAMPSessionErrorCB(code, reason) {
93 if (code == ab.CONNECTION_UNSUPPORTED) {
94 cloudeebus.log("Browser is not supported");
97 cloudeebus.log("Failed to open session, code = " + code + ", reason = " + reason);
103 return ab.connect(cloudeebus.uri, onWAMPSessionConnectedCB, onWAMPSessionErrorCB);
107 cloudeebus.SessionBus = function() {
108 return cloudeebus.sessionBus;
112 cloudeebus.SystemBus = function() {
113 return cloudeebus.systemBus;
118 /*****************************************************************************/
120 cloudeebus.BusConnection = function(name, session) {
122 this.wampSession = session;
128 cloudeebus.BusConnection.prototype.getObject = function(busName, objectPath, introspectCB, errorCB) {
129 var proxy = new cloudeebus.ProxyObject(this.wampSession, this, busName, objectPath);
131 proxy._introspect(introspectCB, errorCB);
136 cloudeebus.BusConnection.prototype.addService = function(serviceName, successCB, errorCB) {
139 cloudeebusService = new cloudeebus.Service(this.wampSession, this, serviceName);
141 function busServiceAddedSuccessCB(service) {
142 self.service = cloudeebusService;
144 successCB(cloudeebusService);
147 cloudeebusService.add(busServiceAddedSuccessCB, errorCB);
148 return cloudeebusService;
151 cloudeebus.BusConnection.prototype.removeService = function(serviceName, successCB, errorCB) {
154 function busServiceRemovedSuccessCB(serviceName) {
155 // Be sure we are removing the service requested...
156 if (serviceName == self.service.name) {
159 successCB(serviceName);
163 cloudeebusService.remove(busServiceRemovedSuccessCB, errorCB);
167 /*****************************************************************************/
169 cloudeebus.Service = function(session, busConnection, name) {
170 this.wampSession = session;
171 this.busConnection = busConnection;
173 this.isCreated = false;
177 cloudeebus.Service.prototype.add = function(successCB, errorCB) {
180 function ServiceAddedSuccessCB(serviceName) {
182 try { // calling dbus hook object function for un-translated types
186 alert(arguments.callee.name + "-> Method callback exception: " + e);
196 // call dbusSend with bus type, destination, object, message and arguments
197 this.wampSession.call("serviceAdd", arglist).then(ServiceAddedSuccessCB, errorCB);
200 cloudeebus.Service.prototype.remove = function(successCB, errorCB) {
201 function ServiceRemovedSuccessCB(serviceName) {
203 try { // calling dbus hook object function for un-translated types
204 successCB(serviceName);
207 alert(arguments.callee.name + "-> Method callback exception: " + e);
216 // call dbusSend with bus type, destination, object, message and arguments
217 this.wampSession.call("serviceRelease", arglist).then(ServiceRemovedSuccessCB, errorCB);
220 cloudeebus.Service.prototype._searchMethod = function(ifName, method, objectJS) {
222 var funcToCall = null;
224 // Check if 'objectJS' has a member 'interfaceProxies' with an interface named 'ifName'
225 // and a method named 'method'
226 if (objectJS.interfaceProxies && objectJS.interfaceProxies[ifName] &&
227 objectJS.interfaceProxies[ifName][method]) {
228 funcToCall = objectJS.interfaceProxies[ifName][method];
230 // retrieve the method directly from 'root' of objectJs
231 funcToCall = objectJS[method];
237 cloudeebus.Service.prototype._addMethod = function(objectPath, ifName, method, objectJS) {
240 var methodId = this.name + "#" + objectPath + "#" + ifName + "#" + method;
241 var funcToCall = this._searchMethod(ifName, method, objectJS);
243 if (funcToCall == null)
244 cloudeebus.log("Method " + method + " doesn't exist in Javascript object");
246 objectJS.wrapperFunc[method] = function() {
248 var methodId = arguments[0];
249 var callDict = JSON.parse(arguments[1]);
251 result = funcToCall.apply(objectJS, callDict.args);
252 service._returnMethod(methodId, callDict.callIndex, true, result);
255 cloudeebus.log(arguments.callee.name + "-> Method callback exception: " + e);
256 service._returnMethod(methodId, callDict.callIndex, false, e.message);
259 this._registerMethod(methodId, objectJS.wrapperFunc[method]);
263 cloudeebus.Service.prototype._addSignal = function(objectPath, ifName, signal, objectJS) {
265 var methodExist = false;
267 if (objectJS.interfaceProxies && objectJS.interfaceProxies[ifName])
268 if (objectJS.interfaceProxies[ifName][signal]) {
271 objectJS.interfaceProxies[ifName][signal] = function() {
272 var result = JSON.parse(arguments[0]);
273 service.emitSignal(objectPath, signal, result);
278 if ((objectJS[signal] == undefined || objectJS[signal] == null) && !methodExist)
279 objectJS[signal] = function() {
280 var result = JSON.parse(arguments[0]);
281 service.emitSignal(objectPath, signal, result);
284 cloudeebus.log("Can not create new method to emit signal '" + signal + "' in object JS this method already exist!");
287 cloudeebus.Service.prototype._createWrapper = function(xmlTemplate, objectPath, objectJS) {
289 var parser = new DOMParser();
290 var xmlDoc = parser.parseFromString(xmlTemplate, "text/xml");
291 var ifXml = xmlDoc.getElementsByTagName("interface");
292 objectJS.wrapperFunc = []
293 for (var i=0; i < ifXml.length; i++) {
294 var ifName = ifXml[i].attributes.getNamedItem("name").value;
295 var ifChild = ifXml[i].firstChild;
297 if (ifChild.nodeName == "method") {
298 var metName = ifChild.attributes.getNamedItem("name").value;
299 self._addMethod(objectPath, ifName, metName, objectJS);
301 if (ifChild.nodeName == "signal") {
302 var metName = ifChild.attributes.getNamedItem("name").value;
303 self._addSignal(objectPath, ifName, metName, objectJS);
305 ifChild = ifChild.nextSibling;
310 cloudeebus.Service.prototype.addAgent = function(objectPath, xmlTemplate, objectJS, successCB, errorCB) {
311 function ServiceAddAgentSuccessCB(objPath) {
313 try { // calling dbus hook object function for un-translated types
317 alert(arguments.callee.name + "-> Method callback exception: " + e);
322 try { // calling dbus hook object function for un-translated types
323 this._createWrapper(xmlTemplate, objectPath, objectJS);
326 alert(arguments.callee.name + "-> Method callback exception: " + e);
336 // call dbusSend with bus type, destination, object, message and arguments
337 this.wampSession.call("serviceAddAgent", arglist).then(ServiceAddAgentSuccessCB, errorCB);
340 cloudeebus.Service.prototype.delAgent = function(objectPath, successCB, errorCB) {
341 function ServiceDelAgentSuccessCB(agent) {
343 try { // calling dbus hook object function for un-translated types
347 alert(arguments.callee.name + "-> Method callback exception: " + e);
356 // call dbusSend with bus type, destination, object, message and arguments
357 this.wampSession.call("serviceDelAgent", arglist).then(ServiceDelAgentSuccessCB, errorCB);
360 cloudeebus.Service.prototype._registerMethod = function(methodId, methodHandler) {
361 this.wampSession.subscribe(methodId, methodHandler);
364 cloudeebus.Service.prototype._returnMethod = function(methodId, callIndex, success, result, successCB, errorCB) {
372 this.wampSession.call("returnMethod", arglist).then(successCB, errorCB);
375 cloudeebus.Service.prototype.emitSignal = function(objectPath, signalName, result, successCB, errorCB) {
382 this.wampSession.call("emitSignal", arglist).then(successCB, errorCB);
386 /*****************************************************************************/
388 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
389 this.wampSession = session;
390 this.busConnection = busConnection;
391 this.busName = busName;
392 this.objectPath = objectPath;
393 this.interfaceProxies = {};
398 cloudeebus.ProxyObject.prototype.getInterface = function(ifName) {
399 return this.interfaceProxies[ifName];
403 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
407 function getAllPropertiesSuccessCB(props) {
408 var ifProxy = self.interfaceProxies[self.propInterfaces[self.propInterfaces.length-1]];
409 for (var prop in props)
410 ifProxy[prop] = self[prop] = props[prop];
411 getAllPropertiesNextInterfaceCB();
414 function getAllPropertiesNextInterfaceCB() {
415 self.propInterfaces.pop();
416 if (self.propInterfaces.length > 0)
417 self.callMethod("org.freedesktop.DBus.Properties",
419 [self.propInterfaces[self.propInterfaces.length-1]],
420 getAllPropertiesSuccessCB,
421 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
423 self.propInterfaces = null;
429 function introspectSuccessCB(str) {
430 var parser = new DOMParser();
431 var xmlDoc = parser.parseFromString(str, "text/xml");
432 var interfaces = xmlDoc.getElementsByTagName("interface");
433 self.propInterfaces = [];
434 var supportDBusProperties = false;
435 for (var i=0; i < interfaces.length; i++) {
436 var ifName = interfaces[i].attributes.getNamedItem("name").value;
437 self.interfaceProxies[ifName] = new cloudeebus.ProxyObject(self.wampSession, self.busConnection, self.busName, self.objectPath);
438 if (ifName == "org.freedesktop.DBus.Properties")
439 supportDBusProperties = true;
440 var hasProperties = false;
441 var ifChild = interfaces[i].firstChild;
443 if (ifChild.nodeName == "method") {
445 var metChild = ifChild.firstChild;
447 if (metChild.nodeName == "arg" &&
448 metChild.attributes.getNamedItem("direction").value == "in")
450 metChild = metChild.nextSibling;
452 var metName = ifChild.attributes.getNamedItem("name").value;
454 self._addMethod(ifName, metName, nArgs);
455 self.interfaceProxies[ifName]._addMethod(ifName, metName, nArgs);
457 else if (ifChild.nodeName == "property") {
459 self.propInterfaces.push(ifName);
460 hasProperties = true;
462 ifChild = ifChild.nextSibling;
465 if (supportDBusProperties && self.propInterfaces.length > 0) {
466 self.callMethod("org.freedesktop.DBus.Properties",
468 [self.propInterfaces[self.propInterfaces.length-1]],
469 getAllPropertiesSuccessCB,
470 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
473 self.propInterfaces = null;
479 // call Introspect on self
480 self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", [], introspectSuccessCB, errorCB);
484 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
488 self[method] = function() {
489 if (arguments.length < nArgs || arguments.length > nArgs + 2)
490 throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
492 var successCB = null;
494 for (var i=0; i < nArgs; i++ )
495 args.push(arguments[i]);
496 if (arguments.length > nArgs)
497 successCB = arguments[nArgs];
498 if (arguments.length > nArgs + 1)
499 errorCB = arguments[nArgs + 1];
500 self.callMethod(ifName, method, args, successCB, errorCB);
506 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, successCB, errorCB) {
510 function callMethodSuccessCB(str) {
512 try { // calling dbus hook object function for un-translated types
513 successCB.apply(self, eval(str));
516 cloudeebus.log("Method callback exception: " + e);
523 function callMethodErrorCB(error) {
524 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
530 self.busConnection.name,
538 // call dbusSend with bus type, destination, object, message and arguments
539 self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
543 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
547 function signalHandler(id, data) {
549 try { // calling dbus hook object function for un-translated types
550 successCB.apply(self, eval(data));
553 cloudeebus.log("Signal handler exception: " + e);
560 function connectToSignalSuccessCB(str) {
562 self.wampSession.subscribe(str, signalHandler);
565 cloudeebus.log("Subscribe error: " + e);
569 function connectToSignalErrorCB(error) {
570 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
576 self.busConnection.name,
583 // call dbusSend with bus type, destination, object, message and arguments
584 self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
588 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
590 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
593 cloudeebus.log("Unsubscribe error: " + e);