dbus service: Manage 'service release'
[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.2.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         this.service = null;
124         return this;
125 };
126
127
128 cloudeebus.BusConnection.prototype.getObject = function(busName, objectPath, introspectCB, errorCB) {
129         var proxy = new cloudeebus.ProxyObject(this.wampSession, this, busName, objectPath);
130         if (introspectCB)
131                 proxy._introspect(introspectCB, errorCB);
132         return proxy;
133 };
134
135
136 cloudeebus.BusConnection.prototype.addService = function(serviceName, successCB, errorCB) {
137         var self = this;
138         
139         cloudeebusService = new cloudeebus.Service(this.wampSession, this, serviceName);
140         
141         function busServiceAddedSuccessCB(serviceName) {
142                 self.service = cloudeebusService;
143                 if (successCB)
144                         successCB(serviceName);
145         }
146         
147         cloudeebusService.add(busServiceAddedSuccessCB, errorCB);
148 };
149
150 cloudeebus.BusConnection.prototype.removeService = function(serviceName, successCB, errorCB) {
151         var self = this;
152         
153         function busServiceRemovedSuccessCB(serviceName) {
154                 // Be sure we are removing the service requested...
155                 if (serviceName == self.service.name) {
156                         self.service = null;
157                         if (successCB)
158                                 successCB(serviceName);
159                 }
160         }
161         
162         cloudeebusService.remove(busServiceRemovedSuccessCB, errorCB);
163 };
164
165
166 /*****************************************************************************/
167
168 cloudeebus.Service = function(session, busConnection, name) {
169         this.wampSession = session;
170         this.busConnection = busConnection; 
171         this.name = name;
172         this.isCreated = false;
173         return this;
174 };
175
176 cloudeebus.Service.prototype.add = function(successCB, errorCB) {
177         function ServiceAddedSuccessCB(serviceName) {
178                 if (successCB) {
179                         try { // calling dbus hook object function for un-translated types
180                                 successCB(serviceName);
181                         }
182                         catch (e) {
183                                 alert(arguments.callee.name + "-> Method callback exception: " + e);
184                         }
185                 }
186         }
187         
188         var arglist = [
189             this.busConnection,
190             this.name
191             ];
192
193         // call dbusSend with bus type, destination, object, message and arguments
194         this.wampSession.call("serviceAdd", arglist).then(ServiceAddedSuccessCB, errorCB);
195 };
196
197 cloudeebus.Service.prototype.remove = function(successCB, errorCB) {
198         function ServiceRemovedSuccessCB(serviceName) {
199                 if (successCB) {
200                         try { // calling dbus hook object function for un-translated types
201                                 successCB(serviceName);
202                         }
203                         catch (e) {
204                                 alert(arguments.callee.name + "-> Method callback exception: " + e);
205                         }
206                 }
207         }
208         
209         var arglist = [
210             this.name
211             ];
212
213         // call dbusSend with bus type, destination, object, message and arguments
214         this.wampSession.call("serviceRelease", arglist).then(ServiceRemovedSuccessCB, errorCB);
215 };
216
217 cloudeebus.Service.prototype.addAgent = function(objectPath, xmlTemplate, successCB, errorCB) {
218         function ServiceAddAgentSuccessCB(objPath) {
219                 if (successCB) {
220                         try { // calling dbus hook object function for un-translated types
221                                 successCB(objPath);
222                         }
223                         catch (e) {
224                                 alert(arguments.callee.name + "-> Method callback exception: " + e);
225                         }
226                 }
227         }
228         
229         var arglist = [
230             objectPath,
231             xmlTemplate
232             ];
233
234         // call dbusSend with bus type, destination, object, message and arguments
235         this.wampSession.call("serviceAddAgent", arglist).then(ServiceAddAgentSuccessCB, errorCB);
236 };
237
238 cloudeebus.Service.prototype.delAgent = function(objectPath, successCB, errorCB) {
239         function ServiceDelAgentSuccessCB(agent) {
240                 if (successCB) {
241                         try { // calling dbus hook object function for un-translated types
242                                 successCB(agent);
243                         }
244                         catch (e) {
245                                 alert(arguments.callee.name + "-> Method callback exception: " + e);
246                         }
247                 }
248         }
249         
250         var arglist = [
251             objectPath
252             ];
253
254         // call dbusSend with bus type, destination, object, message and arguments
255         this.wampSession.call("serviceDelAgent", arglist).then(ServiceDelAgentSuccessCB, errorCB);
256 };
257
258 cloudeebus.Service.prototype.registerMethod = function(methodId, methodHandler) {
259         this.wampSession.subscribe(methodId, methodHandler);
260 };
261
262 cloudeebus.Service.prototype.returnMethod = function(methodId, success, result, successCB, errorCB) {
263         var arglist = [
264             methodId,
265             success,
266             result
267             ];
268
269         this.wampSession.call("returnMethod", arglist).then(successCB, errorCB);
270 };
271
272
273 /*****************************************************************************/
274
275 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
276         this.wampSession = session; 
277         this.busConnection = busConnection; 
278         this.busName = busName; 
279         this.objectPath = objectPath; 
280         return this;
281 };
282
283
284 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
285         
286         var self = this; 
287
288         function getAllPropertiesSuccessCB(props) {
289                 for (var prop in props)
290                         self[prop] = props[prop];
291                 getAllPropertiesNextInterfaceCB();
292         }
293         
294         function getAllPropertiesNextInterfaceCB() {
295                 if (self.propInterfaces.length > 0) 
296                         self.callMethod("org.freedesktop.DBus.Properties", 
297                                 "GetAll", 
298                                 [self.propInterfaces.pop()], 
299                                 getAllPropertiesSuccessCB, 
300                                 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
301                 else {
302                         self.propInterfaces = null;
303                         if (successCB)
304                                 successCB(self);
305                 }
306         }
307         
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;
320                         while (ifChild) {
321                                 if (ifChild.nodeName == "method") {
322                                         var nArgs = 0;
323                                         var metChild = ifChild.firstChild;
324                                         while (metChild) {
325                                                 if (metChild.nodeName == "arg" &&
326                                                         metChild.attributes.getNamedItem("direction").value == "in")
327                                                                 nArgs++;
328                                                 metChild = metChild.nextSibling;
329                                         }
330                                         self._addMethod(ifName, 
331                                                         ifChild.attributes.getNamedItem("name").value, 
332                                                         nArgs);
333                                 }
334                                 else if (ifChild.nodeName == "property") {
335                                         if (!hasProperties)
336                                                 self.propInterfaces.push(ifName);
337                                         hasProperties = true;
338                                 }
339                                 ifChild = ifChild.nextSibling;
340                         }
341                 }
342                 if (supportDBusProperties && self.propInterfaces.length > 0) {
343                         self.callMethod("org.freedesktop.DBus.Properties", 
344                                 "GetAll", 
345                                 [self.propInterfaces.pop()], 
346                                 getAllPropertiesSuccessCB, 
347                                 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
348                 }
349                 else {
350                         self.propInterfaces = null;
351                         if (successCB)
352                                 successCB(self);
353                 }
354         }
355
356         // call Introspect on self
357         self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", [], introspectSuccessCB, errorCB);
358 };
359
360
361 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
362
363         var self = this;
364         
365         self[method] = function() {
366                 if (arguments.length < nArgs || arguments.length > nArgs + 2)
367                         throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
368                 var args = [];
369                 var successCB = null;
370                 var errorCB = 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);
378         };
379         
380 };
381
382
383 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, successCB, errorCB) {
384         
385         var self = this; 
386
387         function callMethodSuccessCB(str) {
388                 if (successCB) {
389                         try { // calling dbus hook object function for un-translated types
390                                 successCB.apply(self, eval(str));
391                         }
392                         catch (e) {
393                                 cloudeebus.log("Method callback exception: " + e);
394                                 if (errorCB)
395                                         errorCB(e);
396                         }
397                 }
398         }
399
400         function callMethodErrorCB(error) {
401                 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
402                 if (errorCB)
403                         errorCB(error.desc);
404         }
405
406         var arglist = [
407                 self.busConnection.name,
408                 self.busName,
409                 self.objectPath,
410                 ifName,
411                 method,
412                 JSON.stringify(args)
413         ];
414
415         // call dbusSend with bus type, destination, object, message and arguments
416         self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
417 };
418
419
420 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
421         
422         var self = this; 
423
424         function signalHandler(id, data) {
425                 if (successCB) {
426                         try { // calling dbus hook object function for un-translated types
427                                 successCB.apply(self, eval(data));
428                         }
429                         catch (e) {
430                                 cloudeebus.log("Signal handler exception: " + e);
431                                 if (errorCB)
432                                         errorCB(e);
433                         }
434                 }
435         }
436         
437         function connectToSignalSuccessCB(str) {
438                 try {
439                         self.wampSession.subscribe(str, signalHandler);
440                 }
441                 catch (e) {
442                         cloudeebus.log("Subscribe error: " + e);
443                 }
444         }
445
446         function connectToSignalErrorCB(error) {
447                 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
448                 if (errorCB)
449                         errorCB(error.desc);
450         }
451
452         var arglist = [
453                 self.busConnection.name,
454                 self.busName,
455                 self.objectPath,
456                 ifName,
457                 signal
458         ];
459
460         // call dbusSend with bus type, destination, object, message and arguments
461         self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
462 };
463
464
465 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
466         try {
467                 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
468         }
469         catch (e) {
470                 cloudeebus.log("Unsubscribe error: " + e);
471         }
472 };