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(service) {
142 self.service = service;
144 successCB(cloudeebusService);
147 function busServiceAddedErrorCB(error) {
152 cloudeebusService.add(busServiceAddedSuccessCB, busServiceAddedErrorCB);
156 /*****************************************************************************/
158 cloudeebus.Service = function(session, busConnection, name) {
159 this.wampSession = session;
160 this.busConnection = busConnection;
162 this.isCreated = false;
166 cloudeebus.Service.prototype.add = function(successCB, errorCB) {
167 function ServiceAddSuccessCB(dbusService) {
169 try { // calling dbus hook object function for un-translated types
170 successCB(dbusService);
173 alert(arguments.callee.name + "-> Method callback exception: " + e);
183 // call dbusSend with bus type, destination, object, message and arguments
184 this.wampSession.call("serviceAdd", arglist).then(ServiceAddSuccessCB, errorCB);
187 cloudeebus.Service.prototype.addAgent = function(objectPath, xmlTemplate, successCB, errorCB) {
188 function ServiceAddAgentSuccessCB(objPath) {
190 try { // calling dbus hook object function for un-translated types
194 alert(arguments.callee.name + "-> Method callback exception: " + e);
204 // call dbusSend with bus type, destination, object, message and arguments
205 this.wampSession.call("serviceAddAgent", arglist).then(ServiceAddAgentSuccessCB, errorCB);
208 cloudeebus.Service.prototype.delAgent = function(objectPath, successCB, errorCB) {
209 function ServiceDelAgentSuccessCB(agent) {
211 try { // calling dbus hook object function for un-translated types
215 alert(arguments.callee.name + "-> Method callback exception: " + e);
224 // call dbusSend with bus type, destination, object, message and arguments
225 this.wampSession.call("serviceDelAgent", arglist).then(ServiceDelAgentSuccessCB, errorCB);
228 cloudeebus.Service.prototype.registerMethod = function(methodId, methodHandler) {
229 this.wampSession.subscribe(methodId, methodHandler);
232 cloudeebus.Service.prototype.returnMethod = function(methodId, success, result, successCB, errorCB) {
239 this.wampSession.call("returnMethod", arglist).then(successCB, errorCB);
243 /*****************************************************************************/
245 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
246 this.wampSession = session;
247 this.busConnection = busConnection;
248 this.busName = busName;
249 this.objectPath = objectPath;
254 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
258 function getAllPropertiesSuccessCB(props) {
259 for (var prop in props)
260 self[prop] = props[prop];
261 getAllPropertiesNextInterfaceCB();
264 function getAllPropertiesNextInterfaceCB() {
265 if (self.propInterfaces.length > 0)
266 self.callMethod("org.freedesktop.DBus.Properties",
268 [self.propInterfaces.pop()],
269 getAllPropertiesSuccessCB,
270 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
272 self.propInterfaces = null;
278 function introspectSuccessCB(str) {
279 var parser = new DOMParser();
280 var xmlDoc = parser.parseFromString(str, "text/xml");
281 var interfaces = xmlDoc.getElementsByTagName("interface");
282 self.propInterfaces = [];
283 var supportDBusProperties = false;
284 for (var i=0; i < interfaces.length; i++) {
285 var ifName = interfaces[i].attributes.getNamedItem("name").value;
286 if (ifName == "org.freedesktop.DBus.Properties")
287 supportDBusProperties = true;
288 var hasProperties = false;
289 var ifChild = interfaces[i].firstChild;
291 if (ifChild.nodeName == "method") {
293 var metChild = ifChild.firstChild;
295 if (metChild.nodeName == "arg" &&
296 metChild.attributes.getNamedItem("direction").value == "in")
298 metChild = metChild.nextSibling;
300 self._addMethod(ifName,
301 ifChild.attributes.getNamedItem("name").value,
304 else if (ifChild.nodeName == "property") {
306 self.propInterfaces.push(ifName);
307 hasProperties = true;
309 ifChild = ifChild.nextSibling;
312 if (supportDBusProperties && self.propInterfaces.length > 0) {
313 self.callMethod("org.freedesktop.DBus.Properties",
315 [self.propInterfaces.pop()],
316 getAllPropertiesSuccessCB,
317 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
320 self.propInterfaces = null;
326 // call Introspect on self
327 self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", [], introspectSuccessCB, errorCB);
331 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
335 self[method] = function() {
336 if (arguments.length < nArgs || arguments.length > nArgs + 2)
337 throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
339 var successCB = null;
341 for (var i=0; i < nArgs; i++ )
342 args.push(arguments[i]);
343 if (arguments.length > nArgs)
344 successCB = arguments[nArgs];
345 if (arguments.length > nArgs + 1)
346 errorCB = arguments[nArgs + 1];
347 self.callMethod(ifName, method, args, successCB, errorCB);
353 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, successCB, errorCB) {
357 function callMethodSuccessCB(str) {
359 try { // calling dbus hook object function for un-translated types
360 successCB.apply(self, eval(str));
363 cloudeebus.log("Method callback exception: " + e);
370 function callMethodErrorCB(error) {
371 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
377 self.busConnection.name,
385 // call dbusSend with bus type, destination, object, message and arguments
386 self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
390 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
394 function signalHandler(id, data) {
396 try { // calling dbus hook object function for un-translated types
397 successCB.apply(self, eval(data));
400 cloudeebus.log("Signal handler exception: " + e);
407 function connectToSignalSuccessCB(str) {
409 self.wampSession.subscribe(str, signalHandler);
412 cloudeebus.log("Subscribe error: " + e);
416 function connectToSignalErrorCB(error) {
417 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
423 self.busConnection.name,
430 // call dbusSend with bus type, destination, object, message and arguments
431 self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
435 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
437 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
440 cloudeebus.log("Unsubscribe error: " + e);