WIP
authorFrederic PAUT <frederic.paut@linux.intel.com>
Wed, 3 Apr 2013 12:42:37 +0000 (14:42 +0200)
committerFrederic PAUT <frederic.paut@linux.intel.com>
Wed, 3 Apr 2013 12:42:37 +0000 (14:42 +0200)
cloudeebus/cloudeebus.js
doc/agent/server.html

index ab8e5cb..9df5420 100644 (file)
@@ -217,7 +217,73 @@ cloudeebus.Service.prototype.remove = function(successCB, errorCB) {
        this.wampSession.call("serviceRelease", arglist).then(ServiceRemovedSuccessCB, errorCB);
 };
 
-cloudeebus.Service.prototype.addAgent = function(objectPath, xmlTemplate, successCB, errorCB) {
+cloudeebus.Service.prototype._addMethod = function(objectPath, objectJS, method, nArgs) {
+
+       var self = this;
+       var methodId = self.name + "#" + objectPath + "#" + method;
+       var object = objectJS;
+       
+       objectJS.wrapperFunc[method] = function() {
+               var result;
+               if (arguments.length < nArgs || arguments.length > nArgs + 2)
+                       throw "Error: method " + method + " takes " + nArgs + " parameters, got " + arguments.length + ".";
+               var args = [];
+               var successCB = null;
+               var errorCB = null;
+               var methodId = arguments[0];
+               var callDict = JSON.parse(arguments[1]);
+               for (var i=0; i < nArgs; i++ )
+                       args.push(arguments[i]);
+               if (arguments.length > nArgs)
+                       successCB = arguments[nArgs];
+               if (arguments.length > nArgs + 1)
+                       errorCB = arguments[nArgs + 1];
+               
+               var str_exec = "object.prototype."+method+"()";
+               try {
+                       result = eval(str_exec);
+                       self._returnMethod(methodId, callDict.callIndex, true, result);         
+               }
+               catch (e) {
+                       alert(arguments.callee.name + "-> Method callback exception: " + e);
+                       self._returnMethod(methodId, callDict.callIndex, false, e);             
+               }
+       };
+       
+       self._registerMethod(methodId, objectJS.wrapperFunc[method]);
+
+       
+};
+
+cloudeebus.Service.prototype._createWrapper = function(xmlTemplate, objectPath, objectJS) {
+       var self = this;
+       var parser = new DOMParser();
+       var xmlDoc = parser.parseFromString(xmlTemplate, "text/xml");
+       var interfaces = xmlDoc.getElementsByTagName("interface");
+       objectJS.wrapperFunc = []
+       for (var i=0; i < interfaces.length; i++) {
+//             var ifName = interfaces[i].attributes.getNamedItem("name").value;
+               var ifChild = interfaces[i].firstChild;
+               while (ifChild) {
+                       if (ifChild.nodeName == "method") {
+                               var nArgs = 0;
+                               var metChild = ifChild.firstChild;
+                               while (metChild) {
+                                       if (metChild.nodeName == "arg" &&
+                                               metChild.attributes.getNamedItem("direction") != null && 
+                                               metChild.attributes.getNamedItem("direction").value == "in")
+                                                       nArgs++;
+                                       metChild = metChild.nextSibling;
+                               }
+                               var metName = ifChild.attributes.getNamedItem("name").value;
+                               self._addMethod(objectPath, objectJS, metName, nArgs);
+                       }
+                       ifChild = ifChild.nextSibling;
+               }
+       }
+};
+
+cloudeebus.Service.prototype.addAgent = function(objectPath, xmlTemplate, objectJS, successCB, errorCB) {
        function ServiceAddAgentSuccessCB(objPath) {
                if (successCB) {
                        try { // calling dbus hook object function for un-translated types
@@ -229,6 +295,15 @@ cloudeebus.Service.prototype.addAgent = function(objectPath, xmlTemplate, succes
                }
        }
        
+       try { // calling dbus hook object function for un-translated types
+               this._createWrapper(xmlTemplate, objectPath, objectJS);
+       }
+       catch (e) {
+               alert(arguments.callee.name + "-> Method callback exception: " + e);
+               errorCB(e.desc);
+               return;
+       }
+       
        var arglist = [
            objectPath,
            xmlTemplate
@@ -258,11 +333,11 @@ cloudeebus.Service.prototype.delAgent = function(objectPath, successCB, errorCB)
        this.wampSession.call("serviceDelAgent", arglist).then(ServiceDelAgentSuccessCB, errorCB);
 };
 
-cloudeebus.Service.prototype.registerMethod = function(methodId, methodHandler) {
+cloudeebus.Service.prototype._registerMethod = function(methodId, methodHandler) {
        this.wampSession.subscribe(methodId, methodHandler);
 };
 
-cloudeebus.Service.prototype.returnMethod = function(methodId, callIndex, success, result, successCB, errorCB) {
+cloudeebus.Service.prototype._returnMethod = function(methodId, callIndex, success, result, successCB, errorCB) {
        var arglist = [
            methodId,
            callIndex,
index f7720c5..83713de 100644 (file)
@@ -30,6 +30,23 @@ function errorCB(error) {
   cloudeebus.log(error.desc ? error.desc : error);
 }
 
+sampleObjectHandler = {
+  Add: function(a,b) {
+    return a+b;
+  }, 
+  Release: function() {
+    return;
+  },
+  interfaceProxies = {
+    "org.free.props": {
+      Release: function() {
+        return;
+         }
+    }
+  }
+};
+
+
 function addCalled(args) {
   var methodId = arguments[0];
   var objPath = methodId.split("#")[1];
@@ -54,9 +71,9 @@ function releaseCalled() {
 
 function serviceAdded(service) {
   var agentName = "/org/cloudeebus/Sample"; // = DBUS object name
-  cloudeebus.SessionBus().service.registerMethod(service.name + "#" + agentName + "#Add",addCalled);
-  cloudeebus.SessionBus().service.registerMethod(service.name + "#" + agentName + "#Release",releaseCalled);
-  cloudeebus.SessionBus().service.addAgent(agentName, sampleXml, logCB, errorCB);
+//   cloudeebus.SessionBus().service.registerMethod(service.name + "#" + agentName + "#Add",addCalled);
+//  cloudeebus.SessionBus().service.registerMethod(service.name + "#" + agentName + "#Release",releaseCalled);
+   cloudeebus.SessionBus().service.addAgent(agentName, sampleXml, sampleObjectHandler, logCB, errorCB);
 }
 
 function connectSuccess() {