Merge pull request #16 from fpaut/agent
[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         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(service) {
142                 self.service = cloudeebusService;
143                 if (successCB)
144                         successCB(cloudeebusService);
145         }
146         
147         cloudeebusService.add(busServiceAddedSuccessCB, errorCB);
148         return cloudeebusService;
149 };
150
151 cloudeebus.BusConnection.prototype.removeService = function(serviceName, successCB, errorCB) {
152         var self = this;
153         
154         function busServiceRemovedSuccessCB(serviceName) {
155                 // Be sure we are removing the service requested...
156                 if (serviceName == self.service.name) {
157                         self.service = null;
158                         if (successCB)
159                                 successCB(serviceName);
160                 }
161         }
162         
163         cloudeebusService.remove(busServiceRemovedSuccessCB, errorCB);
164 };
165
166
167 /*****************************************************************************/
168
169 cloudeebus.Service = function(session, busConnection, name) {
170         this.wampSession = session;
171         this.busConnection = busConnection; 
172         this.name = name;
173         this.isCreated = false;
174         return this;
175 };
176
177 cloudeebus.Service.prototype.add = function(successCB, errorCB) {
178         var self = this;
179         
180         function ServiceAddedSuccessCB(serviceName) {
181                 if (successCB) {
182                         try { // calling dbus hook object function for un-translated types
183                                 successCB(self);
184                         }
185                         catch (e) {
186                                 alert(arguments.callee.name + "-> Method callback exception: " + e);
187                         }
188                 }
189         }
190         
191         var arglist = [
192             this.busConnection,
193             this.name
194             ];
195
196         // call dbusSend with bus type, destination, object, message and arguments
197         this.wampSession.call("serviceAdd", arglist).then(ServiceAddedSuccessCB, errorCB);
198 };
199
200 cloudeebus.Service.prototype.remove = function(successCB, errorCB) {
201         function ServiceRemovedSuccessCB(serviceName) {
202                 if (successCB) {
203                         try { // calling dbus hook object function for un-translated types
204                                 successCB(serviceName);
205                         }
206                         catch (e) {
207                                 alert(arguments.callee.name + "-> Method callback exception: " + e);
208                         }
209                 }
210         }
211         
212         var arglist = [
213             this.name
214             ];
215
216         // call dbusSend with bus type, destination, object, message and arguments
217         this.wampSession.call("serviceRelease", arglist).then(ServiceRemovedSuccessCB, errorCB);
218 };
219
220 cloudeebus.Service.prototype._searchMethod = function(ifName, method, objectJS) {
221
222         var funcToCall = null;
223         
224         // Check if 'objectJS' has a member 'interfaceProxies' with an interface named 'ifName' 
225         // and a method named 'method'
226         if (objectJS.interfaceProxies && objectJS.interfaceProxies[ifName] &&
227                 objectJS.interfaceProxies[ifName][method]) {
228                 funcToCall = objectJS.interfaceProxies[ifName][method];
229         } else {
230                 // retrieve the method directly from 'root' of objectJs
231                 funcToCall = objectJS[method];
232         }
233
234         return funcToCall;
235 }
236
237 cloudeebus.Service.prototype._addMethod = function(objectPath, ifName, method, objectJS) {
238
239         var service = this;
240         var methodId = this.name + "#" + objectPath + "#" + ifName + "#" + method;
241         var funcToCall = this._searchMethod(ifName, method, objectJS);
242
243         if (funcToCall == null)
244                 cloudeebus.log("Method " + method + " doesn't exist in Javascript object");
245         else {
246                 objectJS.wrapperFunc[method] = function() {
247                         var result;
248                         var methodId = arguments[0];
249                         var callDict = JSON.parse(arguments[1]);
250                         try {
251                                 result = funcToCall.apply(objectJS, callDict.args);
252                                 service._returnMethod(methodId, callDict.callIndex, true, result);
253                         }
254                         catch (e) {
255                                 cloudeebus.log(arguments.callee.name + "-> Method callback exception: " + e);
256                                 service._returnMethod(methodId, callDict.callIndex, false, e.message);
257                         }
258                 };
259                 this._registerMethod(methodId, objectJS.wrapperFunc[method]);
260         }
261 };
262
263 cloudeebus.Service.prototype._addSignal = function(objectPath, ifName, signal, objectJS) {
264         var service = this;
265         var methodExist = false;
266
267         if (objectJS.interfaceProxies && objectJS.interfaceProxies[ifName])
268                 if (objectJS.interfaceProxies[ifName][signal]) {
269                         methodExist = true;
270                 } else {
271                         objectJS.interfaceProxies[ifName][signal] = function() {
272                                 var result = JSON.parse(arguments[0]);
273                                 service.emitSignal(objectPath, signal, result);
274                         };
275                 return;
276         }
277                 
278         if ((objectJS[signal] == undefined || objectJS[signal] == null) && !methodExist) 
279                 objectJS[signal] = function() {
280                         var result = JSON.parse(arguments[0]);
281                         service.emitSignal(objectPath, signal, result);
282                 };
283         else
284                 cloudeebus.log("Can not create new method to emit signal '" + signal + "' in object JS this method already exist!");
285 };
286
287 cloudeebus.Service.prototype._createWrapper = function(xmlTemplate, objectPath, objectJS) {
288         var self = this;
289         var parser = new DOMParser();
290         var xmlDoc = parser.parseFromString(xmlTemplate, "text/xml");
291         var ifXml = xmlDoc.getElementsByTagName("interface");
292         objectJS.wrapperFunc = []
293         for (var i=0; i < ifXml.length; i++) {
294                 var ifName = ifXml[i].attributes.getNamedItem("name").value;
295                 var ifChild = ifXml[i].firstChild;
296                 while (ifChild) {
297                         if (ifChild.nodeName == "method") {
298                                 var metName = ifChild.attributes.getNamedItem("name").value;
299                                 self._addMethod(objectPath, ifName, metName, objectJS);
300                         }
301                         if (ifChild.nodeName == "signal") {
302                                 var metName = ifChild.attributes.getNamedItem("name").value;
303                                 self._addSignal(objectPath, ifName, metName, objectJS);
304                         }
305                         ifChild = ifChild.nextSibling;
306                 }
307         }
308 };
309
310 cloudeebus.Service.prototype.addAgent = function(objectPath, xmlTemplate, objectJS, successCB, errorCB) {
311         function ServiceAddAgentSuccessCB(objPath) {
312                 if (successCB) {
313                         try { // calling dbus hook object function for un-translated types
314                                 successCB(objPath);
315                         }
316                         catch (e) {
317                                 alert(arguments.callee.name + "-> Method callback exception: " + e);
318                         }
319                 }
320         }
321         
322         try { // calling dbus hook object function for un-translated types
323                 this._createWrapper(xmlTemplate, objectPath, objectJS);
324         }
325         catch (e) {
326                 alert(arguments.callee.name + "-> Method callback exception: " + e);
327                 errorCB(e.desc);
328                 return;
329         }
330         
331         var arglist = [
332             objectPath,
333             xmlTemplate
334             ];
335
336         // call dbusSend with bus type, destination, object, message and arguments
337         this.wampSession.call("serviceAddAgent", arglist).then(ServiceAddAgentSuccessCB, errorCB);
338 };
339
340 cloudeebus.Service.prototype.delAgent = function(objectPath, successCB, errorCB) {
341         function ServiceDelAgentSuccessCB(agent) {
342                 if (successCB) {
343                         try { // calling dbus hook object function for un-translated types
344                                 successCB(agent);
345                         }
346                         catch (e) {
347                                 alert(arguments.callee.name + "-> Method callback exception: " + e);
348                         }
349                 }
350         }
351         
352         var arglist = [
353             objectPath
354             ];
355
356         // call dbusSend with bus type, destination, object, message and arguments
357         this.wampSession.call("serviceDelAgent", arglist).then(ServiceDelAgentSuccessCB, errorCB);
358 };
359
360 cloudeebus.Service.prototype._registerMethod = function(methodId, methodHandler) {
361         this.wampSession.subscribe(methodId, methodHandler);
362 };
363
364 cloudeebus.Service.prototype._returnMethod = function(methodId, callIndex, success, result, successCB, errorCB) {
365         var arglist = [
366             methodId,
367             callIndex,
368             success,
369             result
370             ];
371
372         this.wampSession.call("returnMethod", arglist).then(successCB, errorCB);
373 };
374
375 cloudeebus.Service.prototype.emitSignal = function(objectPath, signalName, result, successCB, errorCB) {
376         var arglist = [
377             objectPath,
378             signalName,
379             result
380             ];
381
382         this.wampSession.call("emitSignal", arglist).then(successCB, errorCB);
383 };
384
385
386 /*****************************************************************************/
387
388 cloudeebus.ProxyObject = function(session, busConnection, busName, objectPath) {
389         this.wampSession = session; 
390         this.busConnection = busConnection; 
391         this.busName = busName; 
392         this.objectPath = objectPath; 
393         this.interfaceProxies = {};
394         return this;
395 };
396
397
398 cloudeebus.ProxyObject.prototype.getInterface = function(ifName) {
399         return this.interfaceProxies[ifName];
400 };
401
402
403 cloudeebus.ProxyObject.prototype._introspect = function(successCB, errorCB) {
404         
405         var self = this; 
406
407         function getAllPropertiesSuccessCB(props) {
408                 var ifProxy = self.interfaceProxies[self.propInterfaces[self.propInterfaces.length-1]];
409                 for (var prop in props)
410                         ifProxy[prop] = self[prop] = props[prop];
411                 getAllPropertiesNextInterfaceCB();
412         }
413         
414         function getAllPropertiesNextInterfaceCB() {
415                 self.propInterfaces.pop();
416                 if (self.propInterfaces.length > 0) 
417                         self.callMethod("org.freedesktop.DBus.Properties", 
418                                 "GetAll", 
419                                 [self.propInterfaces[self.propInterfaces.length-1]], 
420                                 getAllPropertiesSuccessCB, 
421                                 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
422                 else {
423                         self.propInterfaces = null;
424                         if (successCB)
425                                 successCB(self);
426                 }
427         }
428         
429         function introspectSuccessCB(str) {
430                 var parser = new DOMParser();
431                 var xmlDoc = parser.parseFromString(str, "text/xml");
432                 var interfaces = xmlDoc.getElementsByTagName("interface");
433                 self.propInterfaces = [];
434                 var supportDBusProperties = false;
435                 for (var i=0; i < interfaces.length; i++) {
436                         var ifName = interfaces[i].attributes.getNamedItem("name").value;
437                         self.interfaceProxies[ifName] = new cloudeebus.ProxyObject(self.wampSession, self.busConnection, self.busName, self.objectPath);
438                         if (ifName == "org.freedesktop.DBus.Properties")
439                                 supportDBusProperties = true;
440                         var hasProperties = false;
441                         var ifChild = interfaces[i].firstChild;
442                         while (ifChild) {
443                                 if (ifChild.nodeName == "method") {
444                                         var nArgs = 0;
445                                         var metChild = ifChild.firstChild;
446                                         while (metChild) {
447                                                 if (metChild.nodeName == "arg" &&
448                                                         metChild.attributes.getNamedItem("direction").value == "in")
449                                                                 nArgs++;
450                                                 metChild = metChild.nextSibling;
451                                         }
452                                         var metName = ifChild.attributes.getNamedItem("name").value;
453                                         if (!self[metName])
454                                                 self._addMethod(ifName, metName, nArgs);
455                                         self.interfaceProxies[ifName]._addMethod(ifName, metName, nArgs);
456                                 }
457                                 else if (ifChild.nodeName == "property") {
458                                         if (!hasProperties)
459                                                 self.propInterfaces.push(ifName);
460                                         hasProperties = true;
461                                 }
462                                 ifChild = ifChild.nextSibling;
463                         }
464                 }
465                 if (supportDBusProperties && self.propInterfaces.length > 0) {
466                         self.callMethod("org.freedesktop.DBus.Properties", 
467                                 "GetAll", 
468                                 [self.propInterfaces[self.propInterfaces.length-1]], 
469                                 getAllPropertiesSuccessCB, 
470                                 errorCB ? errorCB : getAllPropertiesNextInterfaceCB);
471                 }
472                 else {
473                         self.propInterfaces = null;
474                         if (successCB)
475                                 successCB(self);
476                 }
477         }
478
479         // call Introspect on self
480         self.callMethod("org.freedesktop.DBus.Introspectable", "Introspect", [], introspectSuccessCB, errorCB);
481 };
482
483
484 cloudeebus.ProxyObject.prototype._addMethod = function(ifName, method, nArgs) {
485
486         var self = this;
487         
488         self[method] = function() {
489                 if (arguments.length < nArgs || arguments.length > nArgs + 2)
490                         throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
491                 var args = [];
492                 var successCB = null;
493                 var errorCB = null;
494                 for (var i=0; i < nArgs; i++ )
495                         args.push(arguments[i]);
496                 if (arguments.length > nArgs)
497                         successCB = arguments[nArgs];
498                 if (arguments.length > nArgs + 1)
499                         errorCB = arguments[nArgs + 1];
500                 self.callMethod(ifName, method, args, successCB, errorCB);
501         };
502         
503 };
504
505
506 cloudeebus.ProxyObject.prototype.callMethod = function(ifName, method, args, successCB, errorCB) {
507         
508         var self = this; 
509
510         function callMethodSuccessCB(str) {
511                 if (successCB) {
512                         try { // calling dbus hook object function for un-translated types
513                                 successCB.apply(self, eval(str));
514                         }
515                         catch (e) {
516                                 cloudeebus.log("Method callback exception: " + e);
517                                 if (errorCB)
518                                         errorCB(e);
519                         }
520                 }
521         }
522
523         function callMethodErrorCB(error) {
524                 cloudeebus.log("Error calling method: " + method + " on object: " + self.objectPath + " : " + error.desc);
525                 if (errorCB)
526                         errorCB(error.desc);
527         }
528
529         var arglist = [
530                 self.busConnection.name,
531                 self.busName,
532                 self.objectPath,
533                 ifName,
534                 method,
535                 JSON.stringify(args)
536         ];
537
538         // call dbusSend with bus type, destination, object, message and arguments
539         self.wampSession.call("dbusSend", arglist).then(callMethodSuccessCB, callMethodErrorCB);
540 };
541
542
543 cloudeebus.ProxyObject.prototype.connectToSignal = function(ifName, signal, successCB, errorCB) {
544         
545         var self = this; 
546
547         function signalHandler(id, data) {
548                 if (successCB) {
549                         try { // calling dbus hook object function for un-translated types
550                                 successCB.apply(self, eval(data));
551                         }
552                         catch (e) {
553                                 cloudeebus.log("Signal handler exception: " + e);
554                                 if (errorCB)
555                                         errorCB(e);
556                         }
557                 }
558         }
559         
560         function connectToSignalSuccessCB(str) {
561                 try {
562                         self.wampSession.subscribe(str, signalHandler);
563                 }
564                 catch (e) {
565                         cloudeebus.log("Subscribe error: " + e);
566                 }
567         }
568
569         function connectToSignalErrorCB(error) {
570                 cloudeebus.log("Error connecting to signal: " + signal + " on object: " + self.objectPath + " : " + error.desc);
571                 if (errorCB)
572                         errorCB(error.desc);
573         }
574
575         var arglist = [
576                 self.busConnection.name,
577                 self.busName,
578                 self.objectPath,
579                 ifName,
580                 signal
581         ];
582
583         // call dbusSend with bus type, destination, object, message and arguments
584         self.wampSession.call("dbusRegister", arglist).then(connectToSignalSuccessCB, connectToSignalErrorCB);
585 };
586
587
588 cloudeebus.ProxyObject.prototype.disconnectSignal = function(ifName, signal) {
589         try {
590                 this.wampSession.unsubscribe(this.busConnection.name + "#" + this.busName + "#" + this.objectPath + "#" + ifName + "#" + signal);
591         }
592         catch (e) {
593                 cloudeebus.log("Unsubscribe error: " + e);
594         }
595 };