From 6687734a6a4999fc3589f2718068be686839df87 Mon Sep 17 00:00:00 2001 From: Luc Yriarte Date: Tue, 26 Mar 2013 16:30:12 +0100 Subject: [PATCH] serialize multiple calls to a javascript agent method --- cloudeebus/cloudeebus.js | 3 ++- cloudeebus/cloudeebus.py | 24 +++++++++++++++++------- doc/agent/client.html | 3 ++- doc/agent/server.html | 16 ++++++++++------ 4 files changed, 31 insertions(+), 15 deletions(-) diff --git a/cloudeebus/cloudeebus.js b/cloudeebus/cloudeebus.js index 3e4d469..6c01414 100644 --- a/cloudeebus/cloudeebus.js +++ b/cloudeebus/cloudeebus.js @@ -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 ]; diff --git a/cloudeebus/cloudeebus.py b/cloudeebus/cloudeebus.py index 936dd3e..2a75452 100755 --- a/cloudeebus/cloudeebus.py +++ b/cloudeebus/cloudeebus.py @@ -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): diff --git a/doc/agent/client.html b/doc/agent/client.html index ab59d09..977e84c 100644 --- a/doc/agent/client.html +++ b/doc/agent/client.html @@ -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; } diff --git a/doc/agent/server.html b/doc/agent/server.html index 00c95bd..d39022f 100644 --- a/doc/agent/server.html +++ b/doc/agent/server.html @@ -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); } -- 2.7.4