ClientDistClass.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. """ClientDistClass module: contains the ClientDistClass class"""
  2. from PandaModules import *
  3. import DirectNotifyGlobal
  4. import ClientDistUpdate
  5. import sys
  6. # These are stored here so that the distributed classes we load on the fly
  7. # can be exec'ed in the module namespace as if we imported them normally.
  8. # This is important for redefine to work, and is a good idea anyways.
  9. moduleGlobals = globals()
  10. moduleLocals = locals()
  11. class ClientDistClass:
  12. notify = DirectNotifyGlobal.directNotify.newCategory("ClientDistClass")
  13. def __init__(self, dcClass):
  14. self.number = dcClass.getNumber()
  15. self.name = dcClass.getName()
  16. self.allFields = self.parseFields(dcClass)
  17. self.allCDU = self.createAllCDU(self.allFields)
  18. self.number2CDU = self.createNumber2CDUDict(self.allCDU)
  19. self.name2CDU = self.createName2CDUDict(self.allCDU)
  20. self.broadcastRequiredCDU = self.listBroadcastRequiredCDU(self.allCDU)
  21. self.allRequiredCDU = self.listRequiredCDU(self.allCDU)
  22. # Import the class, and store the constructor
  23. try:
  24. exec("import " + self.name, moduleGlobals, moduleLocals)
  25. except ImportError, e:
  26. self.notify.warning("Unable to import %s.py: %s" % (self.name, e))
  27. self.constructor = None
  28. return
  29. try:
  30. self.constructor = eval(self.name + "." + self.name)
  31. except (NameError, AttributeError), e:
  32. self.notify.warning("%s.%s does not exist: %s" % (self.name, self.name, e))
  33. self.constructor = None
  34. # If this assertion fails, you probably had an import error in
  35. # a file named in your toon.dc file, or in some file included
  36. # in a file named in your toon.dc file.
  37. assert(self.constructor != None)
  38. return
  39. def parseFields(self, dcClass):
  40. fields=[]
  41. for i in range(0,dcClass.getNumInheritedFields()):
  42. fields.append(dcClass.getInheritedField(i))
  43. return fields
  44. def createAllCDU(self, allFields):
  45. allCDU = []
  46. for i in allFields:
  47. allCDU.append(ClientDistUpdate.ClientDistUpdate(self, i))
  48. return allCDU
  49. def createNumber2CDUDict(self, allCDU):
  50. dict={}
  51. for i in allCDU:
  52. dict[i.number] = i
  53. return dict
  54. def createName2CDUDict(self, allCDU):
  55. dict={}
  56. for i in allCDU:
  57. dict[i.name] = i
  58. return dict
  59. def listBroadcastRequiredCDU(self, allCDU):
  60. requiredCDU = []
  61. for i in allCDU:
  62. atom = i.field.asAtomicField()
  63. if atom:
  64. if (atom.isRequired() and atom.isBroadcast()):
  65. requiredCDU.append(i)
  66. return requiredCDU
  67. def listRequiredCDU(self, allCDU):
  68. requiredCDU = []
  69. for i in allCDU:
  70. atom = i.field.asAtomicField()
  71. if atom:
  72. if (atom.isRequired()):
  73. requiredCDU.append(i)
  74. return requiredCDU
  75. def updateField(self, do, di):
  76. # Get the update field id
  77. fieldId = di.getArg(STUint16)
  78. # look up the CDU
  79. assert(self.number2CDU.has_key(fieldId))
  80. cdu = self.number2CDU[fieldId]
  81. # Let the cdu finish the job
  82. cdu.updateField(self, do, di)
  83. return None
  84. def sendUpdate(self, cr, do, fieldName, args, sendToId = None):
  85. # Look up the cdu
  86. assert(self.name2CDU.has_key(fieldName))
  87. cdu = self.name2CDU[fieldName]
  88. # Let the cdu finish the job
  89. cdu.sendUpdate(cr, do, args, sendToId)