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.2.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(serviceName) {
142 self.service = cloudeebusService;
144 successCB(serviceName);
147 cloudeebusService.add(busServiceAddedSuccessCB, errorCB);
150 cloudeebus.BusConnection.prototype.removeService = function(serviceName, successCB, errorCB) {
153 function busServiceRemovedSuccessCB(serviceName) {
154 // Be sure we are removing the service requested...
155 if (serviceName == self.service.name) {
158 successCB(serviceName);
162 cloudeebusService.remove(busServiceRemovedSuccessCB, errorCB);
166 /*****************************************************************************/
168 cloudeebus.Service = function(session, busConnection, name) {
169 this.wampSession = session;
170 this.busConnection = busConnection;
172 this.isCreated = false;
176 cloudeebus.Service.prototype.add = function(successCB, errorCB) {
177 function ServiceAddedSuccessCB(serviceName) {
179 try { // calling dbus hook object function for un-translated types
180 successCB(serviceName);
183 alert(arguments.callee.name + "-> Method callback exception: " + e);
193 // call dbusSend with bus type, destination, object, message and arguments
194 this.wampSession.call("serviceAdd", arglist).then(ServiceAddedSuccessCB, errorCB);
197 cloudeebus.Service.prototype.remove = function(successCB, errorCB) {
198 function ServiceRemovedSuccessCB(serviceName) {
200 try { // calling dbus hook object function for un-translated types
201 successCB(serviceName);
204 alert(arguments.callee.name + "-> Method callback exception: " + e);
213 // call dbusSend with bus type, destination, object, message and arguments
214 this.wampSession.call("serviceRelease", arglist).then(ServiceRemovedSuccessCB, errorCB);
217 cloudeebus.Service.prototype.addAgent = function(objectPath, xmlTemplate, successCB, errorCB) {
218 function ServiceAddAgentSuccessCB(objPath) {
220 try { // calling dbus hook object function for un-translated types
224 alert(arguments.callee.name + "-> Method callback exception: " + e);
234 // call dbusSend with bus type, destination, object, message and arguments
235 this.wampSession.call("serviceAddAgent", arglist).then(ServiceAddAgentSuccessCB, errorCB);
238 cloudeebus.Service.prototype.delAgent = function(objectPath, successCB, errorCB) {
239 function ServiceDelAgentSuccessCB(agent) {
241 try { // calling dbus hook object function for un-translated types
245 alert(arguments.callee.name + "-> Method callback exception: " + e);
254 // call dbusSend with bus type, destination, object, message and arguments
255 this.wampSession.call("serviceDelAgent", arglist).then(ServiceDelAgentSuccessCB, errorCB);
258 cloudeebus.Service.prototype.registerMethod = function(methodId, methodHandler) {
259 this.wampSession.subscribe(methodId, methodHandler);
262 cloudeebus.Service.prototype.returnMethod = function(methodId, success, result, successCB, errorCB) {
269 this.wampSession.call("returnMethod", arglist).then(successCB, errorCB);
273 /*****************************************************************************/
275 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
276 this.wampSession = session;
277 this.busConnection = busConnection;
278 this.busName = busName;
279 this.objectPath = objectPath;
284 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
288 function getAllPropertiesSuccessCB(props) {
289 for (var prop in props)
290 self[prop] = props[prop];
291 getAllPropertiesNextInterfaceCB();
294 function getAllPropertiesNextInterfaceCB() {
295 if (self.propInterfaces.length > 0)
296 self.callMethod("org.freedesktop.DBus.Properties",
298 [self.propInterfaces.pop()],
299 getAllPropertiesSuccessCB,
300 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
302 self.propInterfaces = null;
308 function introspectSuccessCB(str) {
309 var parser = new DOMParser();
310 var xmlDoc = parser.parseFromString(str, "text/xml");
311 var interfaces = xmlDoc.getElementsByTagName("interface");
312 self.propInterfaces = [];
313 var supportDBusProperties = false;
314 for (var i=0; i < interfaces.length; i++) {
315 var ifName = interfaces[i].attributes.getNamedItem("name").value;
316 if (ifName == "org.freedesktop.DBus.Properties")
317 supportDBusProperties = true;
318 var hasProperties = false;
319 var ifChild = interfaces[i].firstChild;
321 if (ifChild.nodeName == "method") {
323 var metChild = ifChild.firstChild;
325 if (metChild.nodeName == "arg" &&
326 metChild.attributes.getNamedItem("direction").value == "in")
328 metChild = metChild.nextSibling;
330 self._addMethod(ifName,
331 ifChild.attributes.getNamedItem("name").value,
334 else if (ifChild.nodeName == "property") {
336 self.propInterfaces.push(ifName);
337 hasProperties = true;
339 ifChild = ifChild.nextSibling;
342 if (supportDBusProperties && self.propInterfaces.length > 0) {
343 self.callMethod("org.freedesktop.DBus.Properties",
345 [self.propInterfaces.pop()],
346 getAllPropertiesSuccessCB,
347 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
350 self.propInterfaces = null;
356 // call Introspect on self
357 self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", [], introspectSuccessCB, errorCB);
361 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
365 self[method] = function() {
366 if (arguments.length < nArgs || arguments.length > nArgs + 2)
367 throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
369 var successCB = null;
371 for (var i=0; i < nArgs; i++ )
372 args.push(arguments[i]);
373 if (arguments.length > nArgs)
374 successCB = arguments[nArgs];
375 if (arguments.length > nArgs + 1)
376 errorCB = arguments[nArgs + 1];
377 self.callMethod(ifName, method, args, successCB, errorCB);
383 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, successCB, errorCB) {
387 function callMethodSuccessCB(str) {
389 try { // calling dbus hook object function for un-translated types
390 successCB.apply(self, eval(str));
393 cloudeebus.log("Method callback exception: " + e);
400 function callMethodErrorCB(error) {
401 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
407 self.busConnection.name,
415 // call dbusSend with bus type, destination, object, message and arguments
416 self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
420 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
424 function signalHandler(id, data) {
426 try { // calling dbus hook object function for un-translated types
427 successCB.apply(self, eval(data));
430 cloudeebus.log("Signal handler exception: " + e);
437 function connectToSignalSuccessCB(str) {
439 self.wampSession.subscribe(str, signalHandler);
442 cloudeebus.log("Subscribe error: " + e);
446 function connectToSignalErrorCB(error) {
447 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
453 self.busConnection.name,
460 // call dbusSend with bus type, destination, object, message and arguments
461 self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
465 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
467 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
470 cloudeebus.log("Unsubscribe error: " + e);