adding signal emission
[contrib/cloudeebus.git] / cloudeebus / cloudeebus.py
index bb14ee7..0f85747 100755 (executable)
@@ -153,7 +153,10 @@ class DbusCallHandler:
 
 
 ################################################################################       
-class exec_code:
+class ExecCode:
+    '''
+    Execute DynDBusClass generated code
+    '''
     def __init__(self, globalCtx, localCtx) :
         self.exec_string = ""
         self.exec_code = None
@@ -168,15 +171,9 @@ class exec_code:
     def append_stmt(self, stmt) :
         self.exec_code_valid = 0
         self.line += 1
-        if (stmt != "\n"):
-            for x in range(0,self.indent_level):
-                self.exec_string = self.exec_string + ' '            
-            self.exec_string = self.exec_string + stmt + "\t\t# l:" + str(self.line) + '\n'
-        else:
-            if (stmt == "\n"):
-                self.exec_string = self.exec_string + "# l:" + str(self.line) + '\n'
-            else:
-                self.exec_string = self.exec_string + stmt + "\t\t# l:" + str(self.line) + '\n'
+        for x in range(0,self.indent_level):
+            self.exec_string = self.exec_string + ' '            
+        self.exec_string = self.exec_string + stmt + '\n'
 
     def indent(self) :
         self.indent_level = self.indent_level + self.indent_increment
@@ -199,7 +196,7 @@ class exec_code:
 
 
 ################################################################################       
-class XmlCb_Parser: # The target object of the parser
+class XmlCbParser: # The target object of the parser
     maxDepth = 0
     depth = 0
     def __init__(self, dynDBusClass):
@@ -232,6 +229,8 @@ class XmlCb_Parser: # The target object of the parser
                                                 attrib['type'])
                 return
             if (self.current == 'signal'):
+                if (attrib.has_key('name') == False):
+                    attrib['name'] = 'value'
                 self.dynDBusClass.add_signature(attrib['name'], 'in',
                                                 attrib['type'])
                 return
@@ -253,16 +252,15 @@ class XmlCb_Parser: # The target object of the parser
 
        
 ################################################################################       
-class dynDBusClass():
+class DynDBusClass():
     def __init__(self, className, globalCtx, localCtx):
         self.className = className
-        self.xmlCB = XmlCb_Parser(self)
+        self.xmlCB = XmlCbParser(self)
         self.signature = {}
-        self.class_code = exec_code(globalCtx, localCtx)  
+        self.class_code = ExecCode(globalCtx, localCtx)  
         self.class_code.indent_increment = 4
         self.class_code.append_stmt("import dbus")
         self.class_code.append_stmt("\n")
-        self.class_code.append_stmt("\n")
         self.class_code.append_stmt("class " + self.className + "(dbus.service.Object):")
         self.class_code.indent()
         
@@ -272,7 +270,6 @@ class dynDBusClass():
         self.add_stmt("self.bus = bus")
         self.add_stmt("self.objName = objName")
         self.add_stmt("self.callback = callback")        
-#        self.add_stmt("dbus.service.Object.__init__(self, conn=bus, object_path=objName, bus_name=busName)")
         self.add_stmt("dbus.service.Object.__init__(self, conn=bus, bus_name=busName)")
         self.end_method()
                
@@ -352,7 +349,6 @@ class dynDBusClass():
         
     def end_method(self):
         self.class_code.append_stmt("\n")
-        self.class_code.append_stmt("\n")        
         self.class_code.dedent()
         
     def add_dbus_method(self):
@@ -382,7 +378,6 @@ class dynDBusClass():
 
     def add_body_method(self):
         if (self.methodToAdd != None):
-            self.class_code.append_stmt("print 'In " + self.methodToAdd + "()'")
             if (self.args_str != str()):
                 self.class_code.append_stmt("self.callback('" + self.methodToAdd + "', dbus_async_cb, dbus_async_err_cb, %s)" % self.args_str)
             else:        
@@ -495,15 +490,33 @@ class CloudeebusService:
 
 
     @exportRpc
+    def emitSignal(self, list):
+        '''
+        arguments: agentObjectPath, signalName, result (to emit)
+        '''
+        objectPath = list[0]
+        className = re.sub('/', '_', objectPath[1:])
+        signalName = list[1]
+        result = list[2]
+        if (self.serviceAgents.has_key(className) == True):
+            exe_str = "self.serviceAgents['"+ className +"']."+ signalName + "(" + str(result) + ")"
+            eval(exe_str, self.globalCtx, self.localCtx)
+        else:
+            raise Exception("No object path " + objectPath)
+
+    @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):
@@ -516,74 +529,23 @@ class CloudeebusService:
                     errorCB(result)
                 else:
                     errorCB()
-            self.servicePendingCalls[methodId] = None
+            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:
-            print "No methodID %s  !!" % (methodId)  
-
-    def jsonEncodeTupleKeyDict(self, data):
-        ndict = dict()
-        # creates new dictionary with the original tuple converted to json string
-        dataLen = len(data)
-        string = ""
-        for index in range(dataLen):
-            for key in data[index]:
-                value = data[index][key]
-                print "key=" + key
-                print "value=" + str(value)
-                nkey = str(key)
-                nvalue = ""
-                print "JSON key=" + nkey
-                if (isinstance(value, dbus.Array)):
-                    # Searching dbus byte in array...
-                    ValueLen = len(value)
-                    nvalue = []
-                    for indexValue in range(ValueLen):
-                        a = value[indexValue]
-                        if (isinstance(a, dbus.Byte)):
-                            a = int(value[indexValue])
-                            nvalue.append(a)
-                        else:
-                            nvalue = str(value[indexValue])
-                            
-                print "JSON value=" + str(nvalue)                
-                ndict[nkey] =  nvalue
-
-        return ndict
+            raise Exception("No methodID " + methodId)
 
     def srvCB(self, name, async_succes_cb, async_error_cb, *args):
         methodId = self.srvName + "#" + self.agentObjectPath + "#" + name
         cb = { 'successCB': async_succes_cb, 
                'errorCB': async_error_cb}
-        self.servicePendingCalls[methodId] = cb
-
-        if (len(args) > 0):
-            print "Received args=%s" % (str(args))
-        else:                     
-            print "No args received"
-            
-        try:               
-            print "factory.dispatch(methodId=%s, args=%s)" % (methodId, json.dumps(args))                     
-            factory.dispatch(methodId, json.dumps(args))
-            return
-        except Exception, e :
-            print "Error=%s" % (str(e))
-            
-        print "Trying to decode dbus.Dictionnary..."
-        try:
-            params = self.jsonEncodeTupleKeyDict(args)                
-            print "factory.dispatch(methodId=%s, args=%s)" % (methodId, params)                     
-            factory.dispatch(methodId, params)
-            return
-        except Exception, e :
-            print "Error=%s" % (str(e))
-                    
-        print "Trying to pass args as string..."
-        try:               
-            print "factory.dispatch(methodId=%s, args=%s)" % (methodId, str(args))                     
-            factory.dispatch(methodId, str(args))
-            return
-        except Exception, e :
-            print "Error=%s" % (str(e))
+        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):
@@ -618,7 +580,7 @@ class CloudeebusService:
         xmlTemplate = list[1]
         self.className = re.sub('/', '_', self.agentObjectPath[1:])
         if (self.dynDBusClasses.has_key(self.className) == False):
-            self.dynDBusClasses[self.className] = dynDBusClass(self.className, self.globalCtx, self.localCtx)
+            self.dynDBusClasses[self.className] = DynDBusClass(self.className, self.globalCtx, self.localCtx)
             self.dynDBusClasses[self.className].createDBusServiceFromXML(xmlTemplate)
             self.dynDBusClasses[self.className].declare()