serialize multiple calls to a javascript agent method
authorLuc Yriarte <luc.yriarte@intel.com>
Tue, 26 Mar 2013 15:30:12 +0000 (16:30 +0100)
committerLuc Yriarte <luc.yriarte@intel.com>
Tue, 26 Mar 2013 15:30:12 +0000 (16:30 +0100)
cloudeebus/cloudeebus.js
cloudeebus/cloudeebus.py
doc/agent/client.html
doc/agent/server.html

index 3e4d469..6c01414 100644 (file)
@@ -259,9 +259,10 @@ cloudeebus.Service.prototype.registerMethod = function(methodId, methodHandler)
        this.wampSession.subscribe(methodId, methodHandler);
 };
 
-cloudeebus.Service.prototype.returnMethod = function(methodId, success, result, successCB, errorCB) {
+cloudeebus.Service.prototype.returnMethod = function(methodId, callIndex, success, result, successCB, errorCB) {
        var arglist = [
            methodId,
+           callIndex,
            success,
            result
            ];
index 936dd3e..2a75452 100755 (executable)
@@ -490,13 +490,16 @@ class CloudeebusService:
     @exportRpc
     def returnMethod(self, list):
         '''
-        arguments: methodId, success (=true, error otherwise), result (to return)
+        arguments: methodId, callIndex, success (=true, error otherwise), result (to return)
         '''
         methodId = list[0]
-        success = list[1]
-        result = list[2]
+        callIndex = list[1]
+        success = list[2]
+        result = list[3]
         if (self.servicePendingCalls.has_key(methodId)):
-            cb = self.servicePendingCalls[methodId]
+            cb = self.servicePendingCalls[methodId]['calls'][callIndex]
+            if cb is None:
+                raise Exception("No pending call " + str(callIndex) + " for methodID " + methodId)
             if (success):                
                 successCB = cb["successCB"]
                 if (result != None):
@@ -509,7 +512,10 @@ class CloudeebusService:
                     errorCB(result)
                 else:
                     errorCB()
-            del self.servicePendingCalls[methodId]
+            self.servicePendingCalls[methodId]['calls'][callIndex] = None
+            self.servicePendingCalls[methodId]['count'] = self.servicePendingCalls[methodId]['count'] - 1
+            if self.servicePendingCalls[methodId]['count'] == 0:
+                del self.servicePendingCalls[methodId]
         else:
             raise Exception("No methodID " + methodId)
 
@@ -517,8 +523,12 @@ class CloudeebusService:
         methodId = self.srvName + "#" + self.agentObjectPath + "#" + name
         cb = { 'successCB': async_succes_cb, 
                'errorCB': async_error_cb}
-        self.servicePendingCalls[methodId] = cb
-        factory.dispatch(methodId, json.dumps(args))
+        if methodId not in self.servicePendingCalls:
+            self.servicePendingCalls[methodId] = {'count': 0, 'calls': []}
+        pendingCallStr = json.dumps({'callIndex': len(self.servicePendingCalls[methodId]['calls']), 'args': args})
+        self.servicePendingCalls[methodId]['calls'].append(cb)
+        self.servicePendingCalls[methodId]['count'] = self.servicePendingCalls[methodId]['count'] + 1
+        factory.dispatch(methodId, pendingCallStr)
                     
     @exportRpc
     def serviceAdd(self, list):
index ab59d09..977e84c 100644 (file)
@@ -47,7 +47,8 @@ function gotAddResult(aSum) {
 
 function gotProxy(proxy) {
   logCB(proxy);
-  proxy.Add(32,16,gotAddResult,errorCB);
+  for (var i=-10; i<10; i++)
+    proxy.Add(i,i*2,gotAddResult,errorCB);
   sampleProxy = proxy;
 }
 
index 00c95bd..d39022f 100644 (file)
@@ -31,15 +31,19 @@ function errorCB(error) {
 }
 
 function addCalled(args) {
-var args = JSON.parse(arguments[1]);
-  cloudeebus.log("Add method called: " + args[0] + " + " + args[1]);
-  cloudeebus.SessionBus().service.returnMethod("org.cloudeebus.Sample#/org/cloudeebus/Sample#Add", true, args[0] + args[1]);
+  var methodId = arguments[0];
+  var callDict = JSON.parse(arguments[1]);
+  cloudeebus.log("Method called: " + methodId);
+  cloudeebus.log("Add: " + callDict.args[0] + " + " + callDict.args[1] + " = " + (callDict.args[0] + callDict.args[1]));
+  cloudeebus.SessionBus().service.returnMethod(methodId, callDict.callIndex, true, callDict.args[0] + callDict.args[1]);
 }
 
 function releaseCalled() {
-  cloudeebus.log("Release method called");
-  cloudeebus.SessionBus().service.returnMethod("org.cloudeebus.Sample#/org/cloudeebus/Sample#Release", true, null);
-  cloudeebus.SessionBus().service.wampSession.unsubscribe("org.cloudeebus.Sample#/org/cloudeebus/Sample#Release");  
+  var methodId = arguments[0];
+  var callDict = JSON.parse(arguments[1]);
+  cloudeebus.log("Method called: " + methodId);
+  cloudeebus.SessionBus().service.returnMethod(methodId, 0, true, null);
+  cloudeebus.SessionBus().service.wampSession.unsubscribe(methodId);  
   cloudeebus.SessionBus().service.delAgent("/org/cloudeebus/Sample", logCB, errorCB);
   cloudeebus.SessionBus().service.remove(logCB, errorCB);
 }