request onerror callback: args for apply is an array
[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 signature = "";
214                                         var metChild = ifChild.firstChild;
215                                         while (metChild) {
216                                                 if (metChild.nodeName == "arg" &&
217                                                         metChild.attributes.getNamedItem("direction").value == "in") {
218                                                                 signature += metChild.attributes.getNamedItem("type").value;
219                                                                 nArgs++;
220                                                 }
221                                                 metChild = metChild.nextSibling;
222                                         }
223                                         var metName = ifChild.attributes.getNamedItem("name").value;
224                                         if (!self[metName])
225                                                 self._addMethod(ifName, metName, nArgs, signature);
226                                         self.interfaceProxies[ifName]._addMethod(ifName, metName, nArgs, signature);
227                                 }
228                                 else if (ifChild.nodeName == "property") {
229                                         if (!hasProperties)
230                                                 self.propInterfaces.push(ifName);
231                                         hasProperties = true;
232                                 }
233                                 ifChild = ifChild.nextSibling;
234                         }
235                 }
236                 if (supportDBusProperties && self.propInterfaces.length > 0) {
237                         self.callMethod("org.freedesktop.DBus.Properties", 
238                                 "GetAll", 
239                                 [self.propInterfaces[self.propInterfaces.length-1]]).then(getAllPropertiesSuccessCB, 
240                                 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
241                 }
242                 else {
243                         self.propInterfaces = null;
244                         if (successCB)
245                                 successCB(self);
246                 }
247         }
248
249         // call Introspect on self
250         self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", []).then(introspectSuccessCB, errorCB);
251 };
252
253
254 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs, signature) {
255
256         var self = this;
257         
258         self[method] = function() {
259                 var args = [];
260                 for (var i=0; i < nArgs; i++ )
261                         args.push(arguments[i]);
262                 return self.callMethod(ifName, method, args, signature);
263         };      
264 };
265
266 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, signature) {
267         
268         var self = this;
269         var request = new cloudeebus.Request(this);
270         
271         function callMethodSuccessCB(str) {
272                 try { // calling dbus hook object function for un-translated types
273                         request.result = eval(str);
274                         if (request.onsuccess)
275                                 request.onsuccess.apply(request, request.result);
276                 }
277                 catch (e) {
278                         cloudeebus.log("Method callback exception: " + e);
279                         request.error = e;
280                         if (request.onerror)
281                                 request.onerror.apply(request, [request.error]);
282                 }
283         }
284
285         function callMethodErrorCB(error) {
286                 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
287                 request.error = error.desc;
288                 if (request.onerror)
289                         request.onerror.apply(request, [request.error]);
290         }
291
292         var arglist = [
293                 self.busConnection.name,
294                 self.busName,
295                 self.objectPath,
296                 ifName,
297                 method,
298                 JSON.stringify(args)
299         ];
300
301         // call dbusSend with bus type, destination, object, message and arguments
302         self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
303         return request;
304 };
305
306
307 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
308         
309         var self = this; 
310
311         function signalHandler(id, data) {
312                 if (successCB) {
313                         try { // calling dbus hook object function for un-translated types
314                                 successCB.apply(self, eval(data));
315                         }
316                         catch (e) {
317                                 cloudeebus.log("Signal handler exception: " + e);
318                                 if (errorCB)
319                                         errorCB(e);
320                         }
321                 }
322         }
323         
324         function connectToSignalSuccessCB(str) {
325                 try {
326                         self.wampSession.subscribe(str, signalHandler);
327                 }
328                 catch (e) {
329                         cloudeebus.log("Subscribe error: " + e);
330                 }
331         }
332
333         function connectToSignalErrorCB(error) {
334                 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
335                 if (errorCB)
336                         errorCB(error.desc);
337         }
338
339         var arglist = [
340                 self.busConnection.name,
341                 self.busName,
342                 self.objectPath,
343                 ifName,
344                 signal
345         ];
346
347         // call dbusSend with bus type, destination, object, message and arguments
348         self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
349 };
350
351
352 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
353         try {
354                 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
355         }
356         catch (e) {
357                 cloudeebus.log("Unsubscribe error: " + e);
358         }
359 };