Merge pull request #14 from fpaut/request
[contrib/cloudeebus.git] / cloudeebus / cloudeebus.js
1 /******************************************************************************
2  * Copyright 2012 Intel Corporation.
3  * 
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
7  * 
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * 
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  *****************************************************************************/
16
17
18
19 /*****************************************************************************/
20
21 var dbus = { // hook object for dbus types not translated by python-json
22                 Double: function(value, level) {
23                         return value;
24                 }
25 };
26
27
28
29 /*****************************************************************************/
30
31 var cloudeebus = window.cloudeebus = {version: "0.3.1"};
32
33 cloudeebus.reset = function() {
34         cloudeebus.sessionBus = null;
35         cloudeebus.systemBus = null;
36         cloudeebus.wampSession = null;
37         cloudeebus.uri = null;
38 };
39
40
41 cloudeebus.log = function(msg) { 
42 };
43
44
45 cloudeebus.connect = function(uri, manifest, successCB, errorCB) {
46         cloudeebus.reset();
47         cloudeebus.uri = uri;
48         
49         function onCloudeebusVersionCheckCB(version) {
50                 if (cloudeebus.version == version) {
51                         cloudeebus.log("Connected to " + cloudeebus.uri);
52                         if (successCB)
53                                 successCB();
54                 } else {
55                         var errorMsg = "Cloudeebus server version " + version + " and client version " + cloudeebus.version + " mismatch";
56                         cloudeebus.log(errorMsg);
57                         if (errorCB)
58                                 errorCB(errorMsg);
59                 }
60         }
61         
62         function onWAMPSessionAuthErrorCB(error) {
63                 cloudeebus.log("Authentication error: " + error.desc);
64                 if (errorCB)
65                         errorCB(error.desc);
66         }
67         
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);
72         }
73         
74         function onWAMPSessionChallengedCB(challenge) {
75                 var signature = cloudeebus.wampSession.authsign(challenge, manifest.key);
76                 cloudeebus.wampSession.auth(signature).then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
77         }
78         
79         function onWAMPSessionConnectedCB(session) {
80                 cloudeebus.wampSession = session;
81                 if (manifest)
82                         cloudeebus.wampSession.authreq(
83                                         manifest.name, 
84                                         {permissions: JSON.stringify(manifest.permissions)}
85                                 ).then(onWAMPSessionChallengedCB, onWAMPSessionAuthErrorCB);
86                 else
87                         cloudeebus.wampSession.authreq().then(function() {
88                                 cloudeebus.wampSession.auth().then(onWAMPSessionAuthenticatedCB, onWAMPSessionAuthErrorCB);
89                                 }, onWAMPSessionAuthErrorCB);
90         }
91
92         function onWAMPSessionErrorCB(code, reason) {
93                 if (code == ab.CONNECTION_UNSUPPORTED) {
94                         cloudeebus.log("Browser is not supported");
95                 }
96                 else {
97                         cloudeebus.log("Failed to open session, code = " + code + ", reason = " + reason);
98                 }
99                 if (errorCB)
100                         errorCB(reason);
101         }
102
103         return ab.connect(cloudeebus.uri, onWAMPSessionConnectedCB, onWAMPSessionErrorCB);
104 };
105
106
107 cloudeebus.SessionBus = function() {
108         return cloudeebus.sessionBus;
109 };
110
111
112 cloudeebus.SystemBus = function() {
113         return cloudeebus.systemBus;
114 };
115
116
117
118 /*****************************************************************************/
119
120 cloudeebus.BusConnection = function(name, session) {
121         this.name = name;
122         this.wampSession = session;
123         return this;
124 };
125
126
127 cloudeebus.BusConnection.prototype.getObject = function(busName, objectPath, introspectCB, errorCB) {
128         var proxy = new cloudeebus.ProxyObject(this.wampSession, this, busName, objectPath);
129         if (introspectCB)
130                 proxy._introspect(introspectCB, errorCB);
131         return proxy;
132 };
133
134
135
136 /*****************************************************************************/
137
138 cloudeebus.Request = function(proxy, onsuccess, onerror) {
139         this.proxy = proxy; 
140         this.error = null;
141         this.result = null;
142         this.onsuccess = onsuccess;
143         this.onerror = onerror;
144     return this;
145 };
146
147 cloudeebus.Request.prototype.then = function(onsuccess, onerror) {
148         this.onsuccess = onsuccess;
149         this.onerror = onerror;
150         return this;
151 };
152
153
154
155 /*****************************************************************************/
156
157 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
158         this.wampSession = session; 
159         this.busConnection = busConnection; 
160         this.busName = busName; 
161         this.objectPath = objectPath; 
162         this.interfaceProxies = {};
163         return this;
164 };
165
166
167 cloudeebus.ProxyObject.prototype.getInterface = function(ifName) {
168         return this.interfaceProxies[ifName];
169 };
170
171
172 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
173         
174         var self = this; 
175
176         function getAllPropertiesSuccessCB(props) {
177                 var ifProxy = self.interfaceProxies[self.propInterfaces[self.propInterfaces.length-1]];
178                 for (var prop in props)
179                         ifProxy[prop] = self[prop] = props[prop];
180                 getAllPropertiesNextInterfaceCB();
181         }
182         
183         function getAllPropertiesNextInterfaceCB() {
184                 self.propInterfaces.pop();
185                 if (self.propInterfaces.length > 0) 
186                         self.callMethod("org.freedesktop.DBus.Properties", 
187                                 "GetAll", 
188                                 [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB, 
189                                 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
190                 else {
191                         self.propInterfaces = null;
192                         if (successCB)
193                                 successCB(self);
194                 }
195         }
196         
197         function introspectSuccessCB(str) {
198                 var parser = new DOMParser();
199                 var xmlDoc = parser.parseFromString(str, "text/xml");
200                 var interfaces = xmlDoc.getElementsByTagName("interface");
201                 self.propInterfaces = [];
202                 var supportDBusProperties = false;
203                 for (var i=0; i < interfaces.length; i++) {
204                         var ifName = interfaces[i].attributes.getNamedItem("name").value;
205                         self.interfaceProxies[ifName] = new cloudeebus.ProxyObject(self.wampSession, self.busConnection, self.busName, self.objectPath);
206                         if (ifName == "org.freedesktop.DBus.Properties")
207                                 supportDBusProperties = true;
208                         var hasProperties = false;
209                         var ifChild = interfaces[i].firstChild;
210                         while (ifChild) {
211                                 if (ifChild.nodeName == "method") {
212                                         var nArgs = 0;
213                                         var metChild = ifChild.firstChild;
214                                         while (metChild) {
215                                                 if (metChild.nodeName == "arg" &&
216                                                         metChild.attributes.getNamedItem("direction").value == "in")
217                                                                 nArgs++;
218                                                 metChild = metChild.nextSibling;
219                                         }
220                                         var metName = ifChild.attributes.getNamedItem("name").value;
221                                         if (!self[metName])
222                                                 self._addMethod(ifName, metName, nArgs);
223                                         self.interfaceProxies[ifName]._addMethod(ifName, metName, nArgs);
224                                 }
225                                 else if (ifChild.nodeName == "property") {
226                                         if (!hasProperties)
227                                                 self.propInterfaces.push(ifName);
228                                         hasProperties = true;
229                                 }
230                                 ifChild = ifChild.nextSibling;
231                         }
232                 }
233                 if (supportDBusProperties && self.propInterfaces.length > 0) {
234                         self.callMethod("org.freedesktop.DBus.Properties", 
235                                 "GetAll", 
236                                 [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB, 
237                                 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
238                 }
239                 else {
240                         self.propInterfaces = null;
241                         if (successCB)
242                                 successCB(self);
243                 }
244         }
245
246         // call Introspect on self
247         self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", []).then(introspectSuccessCB, errorCB);
248 };
249
250
251 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
252
253         var self = this;
254         
255         self[method] = function() {
256                 var args = [];
257                 for (var i=0; i < nArgs; i++ )
258                         args.push(arguments[i]);
259                 return self.callMethod(ifName, method, args);
260         };      
261 };
262
263 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args) {
264         
265         var self = this;
266         var request = new cloudeebus.Request(this);
267         
268         function callMethodSuccessCB(str) {
269                 request.result = eval(str);
270                 if (request.onsuccess) {
271                         try { // calling dbus hook object function for un-translated types
272                                 request.onsuccess.apply(request, request.result);
273                         }
274                         catch (e) {
275                                 cloudeebus.log("Method callback exception: " + e);
276                                 request.error = e;
277                                 if (request.onerror)
278                                         request.onerror.apply(request, e);
279                         }
280                 }
281         }
282
283         function callMethodErrorCB(error) {
284                 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
285                 request.error = error.desc;
286                 if (request.onerror)
287                         request.onerror.apply(request, request.error);
288         }
289
290         var arglist = [
291                 self.busConnection.name,
292                 self.busName,
293                 self.objectPath,
294                 ifName,
295                 method,
296                 JSON.stringify(args)
297         ];
298
299         // call dbusSend with bus type, destination, object, message and arguments
300         self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
301         return request;
302 };
303
304
305 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
306         
307         var self = this; 
308
309         function signalHandler(id, data) {
310                 if (successCB) {
311                         try { // calling dbus hook object function for un-translated types
312                                 successCB.apply(self, eval(data));
313                         }
314                         catch (e) {
315                                 cloudeebus.log("Signal handler exception: " + e);
316                                 if (errorCB)
317                                         errorCB(e);
318                         }
319                 }
320         }
321         
322         function connectToSignalSuccessCB(str) {
323                 try {
324                         self.wampSession.subscribe(str, signalHandler);
325                 }
326                 catch (e) {
327                         cloudeebus.log("Subscribe error: " + e);
328                 }
329         }
330
331         function connectToSignalErrorCB(error) {
332                 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
333                 if (errorCB)
334                         errorCB(error.desc);
335         }
336
337         var arglist = [
338                 self.busConnection.name,
339                 self.busName,
340                 self.objectPath,
341                 ifName,
342                 signal
343         ];
344
345         // call dbusSend with bus type, destination, object, message and arguments
346         self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
347 };
348
349
350 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
351         try {
352                 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
353         }
354         catch (e) {
355                 cloudeebus.log("Unsubscribe error: " + e);
356         }
357 };