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.0"};
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._addMethod = function(objectPath, objectJS, method, nArgs) {
223 var methodId = self.name + "#" + objectPath + "#" + method;
224 var object = objectJS;
226 objectJS.wrapperFunc[method] = function() {
228 if (arguments.length < nArgs || arguments.length > nArgs + 2)
229 throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
231 var successCB = null;
233 var methodId = arguments[0];
234 var callDict = JSON.parse(arguments[1]);
235 for (var i=0; i < nArgs; i++ )
236 args.push(arguments[i]);
237 if (arguments.length > nArgs)
238 successCB = arguments[nArgs];
239 if (arguments.length > nArgs + 1)
240 errorCB = arguments[nArgs + 1];
242 var str_exec = "object.prototype."+method+"()";
244 result = eval(str_exec);
245 self._returnMethod(methodId, callDict.callIndex, true, result);
248 alert(arguments.callee.name + "-> Method callback exception: " + e);
249 self._returnMethod(methodId, callDict.callIndex, false, e);
253 self._registerMethod(methodId, objectJS.wrapperFunc[method]);
258 cloudeebus.Service.prototype._createWrapper = function(xmlTemplate, objectPath, objectJS) {
260 var parser = new DOMParser();
261 var xmlDoc = parser.parseFromString(xmlTemplate, "text/xml");
262 var interfaces = xmlDoc.getElementsByTagName("interface");
263 objectJS.wrapperFunc = []
264 for (var i=0; i < interfaces.length; i++) {
265 // var ifName = interfaces[i].attributes.getNamedItem("name").value;
266 var ifChild = interfaces[i].firstChild;
268 if (ifChild.nodeName == "method") {
270 var metChild = ifChild.firstChild;
272 if (metChild.nodeName == "arg" &&
273 metChild.attributes.getNamedItem("direction") != null &&
274 metChild.attributes.getNamedItem("direction").value == "in")
276 metChild = metChild.nextSibling;
278 var metName = ifChild.attributes.getNamedItem("name").value;
279 self._addMethod(objectPath, objectJS, metName, nArgs);
281 ifChild = ifChild.nextSibling;
286 cloudeebus.Service.prototype.addAgent = function(objectPath, xmlTemplate, objectJS, successCB, errorCB) {
287 function ServiceAddAgentSuccessCB(objPath) {
289 try { // calling dbus hook object function for un-translated types
293 alert(arguments.callee.name + "-> Method callback exception: " + e);
298 try { // calling dbus hook object function for un-translated types
299 this._createWrapper(xmlTemplate, objectPath, objectJS);
302 alert(arguments.callee.name + "-> Method callback exception: " + e);
312 // call dbusSend with bus type, destination, object, message and arguments
313 this.wampSession.call("serviceAddAgent", arglist).then(ServiceAddAgentSuccessCB, errorCB);
316 cloudeebus.Service.prototype.delAgent = function(objectPath, successCB, errorCB) {
317 function ServiceDelAgentSuccessCB(agent) {
319 try { // calling dbus hook object function for un-translated types
323 alert(arguments.callee.name + "-> Method callback exception: " + e);
332 // call dbusSend with bus type, destination, object, message and arguments
333 this.wampSession.call("serviceDelAgent", arglist).then(ServiceDelAgentSuccessCB, errorCB);
336 cloudeebus.Service.prototype._registerMethod = function(methodId, methodHandler) {
337 this.wampSession.subscribe(methodId, methodHandler);
340 cloudeebus.Service.prototype._returnMethod = function(methodId, callIndex, success, result, successCB, errorCB) {
348 this.wampSession.call("returnMethod", arglist).then(successCB, errorCB);
351 cloudeebus.Service.prototype.emitSignal = function(objectPath, signalName, result, successCB, errorCB) {
358 this.wampSession.call("emitSignal", arglist).then(successCB, errorCB);
362 /*****************************************************************************/
364 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
365 this.wampSession = session;
366 this.busConnection = busConnection;
367 this.busName = busName;
368 this.objectPath = objectPath;
369 this.interfaceProxies = {};
374 cloudeebus.ProxyObject.prototype.getInterface = function(ifName) {
375 return this.interfaceProxies[ifName];
379 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
383 function getAllPropertiesSuccessCB(props) {
384 var ifProxy = self.interfaceProxies[self.propInterfaces[self.propInterfaces.length-1]];
385 for (var prop in props)
386 ifProxy[prop] = self[prop] = props[prop];
387 getAllPropertiesNextInterfaceCB();
390 function getAllPropertiesNextInterfaceCB() {
391 self.propInterfaces.pop();
392 if (self.propInterfaces.length > 0)
393 self.callMethod("org.freedesktop.DBus.Properties",
395 [self.propInterfaces[self.propInterfaces.length-1]],
396 getAllPropertiesSuccessCB,
397 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
399 self.propInterfaces = null;
405 function introspectSuccessCB(str) {
406 var parser = new DOMParser();
407 var xmlDoc = parser.parseFromString(str, "text/xml");
408 var interfaces = xmlDoc.getElementsByTagName("interface");
409 self.propInterfaces = [];
410 var supportDBusProperties = false;
411 for (var i=0; i < interfaces.length; i++) {
412 var ifName = interfaces[i].attributes.getNamedItem("name").value;
413 self.interfaceProxies[ifName] = new cloudeebus.ProxyObject(self.wampSession, self.busConnection, self.busName, self.objectPath);
414 if (ifName == "org.freedesktop.DBus.Properties")
415 supportDBusProperties = true;
416 var hasProperties = false;
417 var ifChild = interfaces[i].firstChild;
419 if (ifChild.nodeName == "method") {
421 var metChild = ifChild.firstChild;
423 if (metChild.nodeName == "arg" &&
424 metChild.attributes.getNamedItem("direction").value == "in")
426 metChild = metChild.nextSibling;
428 var metName = ifChild.attributes.getNamedItem("name").value;
430 self._addMethod(ifName, metName, nArgs);
431 self.interfaceProxies[ifName]._addMethod(ifName, metName, nArgs);
433 else if (ifChild.nodeName == "property") {
435 self.propInterfaces.push(ifName);
436 hasProperties = true;
438 ifChild = ifChild.nextSibling;
441 if (supportDBusProperties && self.propInterfaces.length > 0) {
442 self.callMethod("org.freedesktop.DBus.Properties",
444 [self.propInterfaces[self.propInterfaces.length-1]],
445 getAllPropertiesSuccessCB,
446 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
449 self.propInterfaces = null;
455 // call Introspect on self
456 self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", [], introspectSuccessCB, errorCB);
460 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
464 self[method] = function() {
465 if (arguments.length < nArgs || arguments.length > nArgs + 2)
466 throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
468 var successCB = null;
470 for (var i=0; i < nArgs; i++ )
471 args.push(arguments[i]);
472 if (arguments.length > nArgs)
473 successCB = arguments[nArgs];
474 if (arguments.length > nArgs + 1)
475 errorCB = arguments[nArgs + 1];
476 self.callMethod(ifName, method, args, successCB, errorCB);
482 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, successCB, errorCB) {
486 function callMethodSuccessCB(str) {
488 try { // calling dbus hook object function for un-translated types
489 successCB.apply(self, eval(str));
492 cloudeebus.log("Method callback exception: " + e);
499 function callMethodErrorCB(error) {
500 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
506 self.busConnection.name,
514 // call dbusSend with bus type, destination, object, message and arguments
515 self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
519 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
523 function signalHandler(id, data) {
525 try { // calling dbus hook object function for un-translated types
526 successCB.apply(self, eval(data));
529 cloudeebus.log("Signal handler exception: " + e);
536 function connectToSignalSuccessCB(str) {
538 self.wampSession.subscribe(str, signalHandler);
541 cloudeebus.log("Subscribe error: " + e);
545 function connectToSignalErrorCB(error) {
546 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
552 self.busConnection.name,
559 // call dbusSend with bus type, destination, object, message and arguments
560 self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
564 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
566 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
569 cloudeebus.log("Unsubscribe error: " + e);