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;
127 cloudeebus.BusConnection.prototype.getObject = function(busName, objectPath, introspectCB, errorCB) {
128 var proxy = new cloudeebus.ProxyObject(this.wampSession, this, busName, objectPath);
130 proxy._introspect(introspectCB, errorCB);
135 cloudeebus.BusConnection.prototype.addService = function(serviceName, successCB, errorCB) {
138 cloudeebusService = new cloudeebus.Service(this.wampSession, this, serviceName);
140 function busServiceAddedSuccessCB() {
142 successCB(cloudeebusService);
145 function busServiceAddedErrorCB(error) {
150 cloudeebusService.add(busServiceAddedSuccessCB, busServiceAddedErrorCB);
152 return cloudeebusService;
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) {
169 function addServiceSuccessCB(dbusService) {
171 try { // calling dbus hook object function for un-translated types
172 successCB(dbusService);
175 alert("Method callback exception: " + e);
180 function addServiceErrorCB(error) {
190 // call dbusSend with bus type, destination, object, message and arguments
191 self.wampSession.call("serviceAdd", arglist).then(addServiceSuccessCB, addServiceErrorCB);
194 cloudeebus.Service.prototype.addAgent = function(objectPath, xmlTemplate, successCB, errorCB) {
197 function addAgentSuccessCB(dbusService) {
199 try { // calling dbus hook object function for un-translated types
200 successCB(dbusService);
203 alert("Method callback exception: " + e);
208 function addAgentErrorCB(error) {
218 // call dbusSend with bus type, destination, object, message and arguments
219 self.wampSession.call("serviceAddAgent", arglist).then(addAgentSuccessCB, addAgentErrorCB);
222 cloudeebus.Service.prototype.registerMethod = function(methodId, methodHandler) {
223 self.wampSession.subscribe(methodId, methodHandler);
227 /*****************************************************************************/
229 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
230 this.wampSession = session;
231 this.busConnection = busConnection;
232 this.busName = busName;
233 this.objectPath = objectPath;
238 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
242 function getAllPropertiesSuccessCB(props) {
243 for (var prop in props)
244 self[prop] = props[prop];
245 getAllPropertiesNextInterfaceCB();
248 function getAllPropertiesNextInterfaceCB() {
249 if (self.propInterfaces.length > 0)
250 self.callMethod("org.freedesktop.DBus.Properties",
252 [self.propInterfaces.pop()],
253 getAllPropertiesSuccessCB,
254 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
256 self.propInterfaces = null;
262 function introspectSuccessCB(str) {
263 var parser = new DOMParser();
264 var xmlDoc = parser.parseFromString(str, "text/xml");
265 var interfaces = xmlDoc.getElementsByTagName("interface");
266 self.propInterfaces = [];
267 var supportDBusProperties = false;
268 for (var i=0; i < interfaces.length; i++) {
269 var ifName = interfaces[i].attributes.getNamedItem("name").value;
270 if (ifName == "org.freedesktop.DBus.Properties")
271 supportDBusProperties = true;
272 var hasProperties = false;
273 var ifChild = interfaces[i].firstChild;
275 if (ifChild.nodeName == "method") {
277 var metChild = ifChild.firstChild;
279 if (metChild.nodeName == "arg" &&
280 metChild.attributes.getNamedItem("direction").value == "in")
282 metChild = metChild.nextSibling;
284 self._addMethod(ifName,
285 ifChild.attributes.getNamedItem("name").value,
288 else if (ifChild.nodeName == "property") {
290 self.propInterfaces.push(ifName);
291 hasProperties = true;
293 ifChild = ifChild.nextSibling;
296 if (supportDBusProperties && self.propInterfaces.length > 0) {
297 self.callMethod("org.freedesktop.DBus.Properties",
299 [self.propInterfaces.pop()],
300 getAllPropertiesSuccessCB,
301 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
304 self.propInterfaces = null;
310 // call Introspect on self
311 self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", [], introspectSuccessCB, errorCB);
315 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
319 self[method] = function() {
320 if (arguments.length < nArgs || arguments.length > nArgs + 2)
321 throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
323 var successCB = null;
325 for (var i=0; i < nArgs; i++ )
326 args.push(arguments[i]);
327 if (arguments.length > nArgs)
328 successCB = arguments[nArgs];
329 if (arguments.length > nArgs + 1)
330 errorCB = arguments[nArgs + 1];
331 self.callMethod(ifName, method, args, successCB, errorCB);
337 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, successCB, errorCB) {
341 function callMethodSuccessCB(str) {
343 try { // calling dbus hook object function for un-translated types
344 successCB.apply(self, eval(str));
347 cloudeebus.log("Method callback exception: " + e);
354 function callMethodErrorCB(error) {
355 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
361 self.busConnection.name,
369 // call dbusSend with bus type, destination, object, message and arguments
370 self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
374 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
378 function signalHandler(id, data) {
380 try { // calling dbus hook object function for un-translated types
381 successCB.apply(self, eval(data));
384 cloudeebus.log("Signal handler exception: " + e);
391 function connectToSignalSuccessCB(str) {
393 self.wampSession.subscribe(str, signalHandler);
396 cloudeebus.log("Subscribe error: " + e);
400 function connectToSignalErrorCB(error) {
401 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
407 self.busConnection.name,
414 // call dbusSend with bus type, destination, object, message and arguments
415 self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
419 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
421 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
424 cloudeebus.log("Unsubscribe error: " + e);