DoInterestManager.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. """
  2. The DoInterestManager keeps track of which parent/zones that we currently
  3. have interest in. When you want to "look" into a zone you add an interest
  4. to that zone. When you want to get rid of, or ignore, the objects in that
  5. zone, remove interest in that zone.
  6. p.s. A great deal of this code is just code moved from ClientRepository.py.
  7. """
  8. from pandac.PandaModules import *
  9. from MsgTypes import *
  10. from direct.directnotify import DirectNotifyGlobal
  11. from direct.showbase.PythonUtil import *
  12. from direct.showbase import DirectObject
  13. from PyDatagram import PyDatagram
  14. #from PyDatagramIterator import PyDatagramIterator
  15. WantInterestPrintout = base.config.GetInt("want-interest-printout", 0)
  16. class DoInterestManager(DirectObject.DirectObject):
  17. """
  18. Top level Interest Manager
  19. """
  20. if __debug__:
  21. notify = DirectNotifyGlobal.directNotify.newCategory("DoInterestManager")
  22. _interestIdAssign = 1;
  23. _interestIdScopes = 100;
  24. _interests = {}
  25. def __init__(self):
  26. assert self.notify.debugCall()
  27. DirectObject.DirectObject.__init__(self)
  28. def addInterest(self, parentId, zoneIdList, description, event=None):
  29. """
  30. Look into a zone.
  31. """
  32. assert self.notify.debugCall()
  33. DoInterestManager._interestIdAssign += 1
  34. DoInterestManager._interestIdScopes += 1
  35. contextId = DoInterestManager._interestIdAssign
  36. scopeId = DoInterestManager._interestIdScopes
  37. DoInterestManager._interests[contextId] = [description, scopeId, event, "Active"]
  38. self._sendAddInterest(contextId, scopeId, parentId, zoneIdList)
  39. assert self.printInterests()
  40. return contextId
  41. def removeInterest(self, contextId, event=None):
  42. """
  43. Stop looking in a zone
  44. """
  45. assert self.notify.debugCall()
  46. answer = 0
  47. if DoInterestManager._interests.has_key(contextId):
  48. if event is not None:
  49. DoInterestManager._interestIdScopes += 1
  50. DoInterestManager._interests[contextId][3] = "PendingDel"
  51. DoInterestManager._interests[contextId][2] = event
  52. DoInterestManager._interests[contextId][1] = DoInterestManager._interestIdScopes
  53. self._sendRemoveInterest(contextId)
  54. else:
  55. DoInterestManager._interests[contextId][2] = None
  56. DoInterestManager._interests[contextId][1] = 0
  57. self._sendRemoveInterest(contextId)
  58. del DoInterestManager._interests[contextId]
  59. answer = 1
  60. else:
  61. self.notify.warning("removeInterest: contextId not found: %s" % (contextId))
  62. assert self.printInterests()
  63. return answer
  64. def alterInterest(self, contextId, parentId, zoneIdList, description=None, event=None):
  65. """
  66. Removes old interests and adds new interests.
  67. """
  68. assert self.notify.debugCall()
  69. answer = 0
  70. if DoInterestManager._interests.has_key(contextId):
  71. DoInterestManager._interestIdScopes += 1
  72. if description is not None:
  73. DoInterestManager._interests[contextId][0] = description
  74. DoInterestManager._interests[contextId][1] = DoInterestManager._interestIdScopes;
  75. DoInterestManager._interests[contextId][2] = event;
  76. self._sendAddInterest(contextId, DoInterestManager._interestIdScopes, parentId, zoneIdList)
  77. answer = 1
  78. assert self.printInterests()
  79. else:
  80. self.notify.warning("alterInterest: contextId not found: %s" % (contextId))
  81. return answer
  82. def getInterestScopeId(self, contextId):
  83. """
  84. Part of the new otp-server code.
  85. Return a ScopeId Id for an Interest
  86. """
  87. assert self.notify.debugCall()
  88. answer = 0
  89. if DoInterestManager._interests.has_key(contextId):
  90. answer = DoInterestManager._interests[contextId][1];
  91. else:
  92. self.notify.warning("GetInterestScopeID: contextId not found: %s" % (contextId))
  93. return answer
  94. def getInterestScopeEvent(self, contextId):
  95. """
  96. returns an event for an interest.
  97. """
  98. assert self.notify.debugCall()
  99. answer = None
  100. if DoInterestManager._interests.has_key(contextId):
  101. answer = DoInterestManager._interests[contextId][2];
  102. else:
  103. self.notify.warning("GetInterestScopeEvent: contextId not found: %s" % (contextId))
  104. return answer
  105. def _ponderRemoveFlaggedInterest(self, handle):
  106. """
  107. Consider whether we should cull the interest set.
  108. """
  109. assert self.notify.debugCall()
  110. if DoInterestManager._interests.has_key(handle):
  111. if DoInterestManager._interests[handle][3] == "PendingDel":
  112. del DoInterestManager._interests[handle]
  113. if __debug__ and WantInterestPrintout:
  114. def printInterests(self):
  115. """
  116. Part of the new otp-server code.
  117. """
  118. print "*********************** Interest Sets **************"
  119. for i in DoInterestManager._interests.keys():
  120. print "Interest ID:%s, Description=%s Scope=%s Event=%s Mode=%s"%(
  121. i,
  122. DoInterestManager._interests[i][0],
  123. DoInterestManager._interests[i][1],
  124. DoInterestManager._interests[i][2],
  125. DoInterestManager._interests[i][3])
  126. print "****************************************************"
  127. return 1 # for assert()
  128. def _sendAddInterest(self, contextId, scopeId, parentId, zoneIdList):
  129. """
  130. Part of the new otp-server code.
  131. contextId is a client-side created number that refers to
  132. a set of interests. The same contextId number doesn't
  133. necessarily have any relationship to the same contextId
  134. on another client.
  135. """
  136. assert self.notify.debugCall()
  137. datagram = PyDatagram()
  138. # Add message type
  139. datagram.addUint16(CLIENT_ADD_INTEREST)
  140. datagram.addUint16(contextId)
  141. datagram.addUint32(scopeId)
  142. datagram.addUint32(parentId)
  143. if isinstance(zoneIdList, types.ListType):
  144. vzl = list(zoneIdList)
  145. vzl.sort()
  146. PythonUtil.uniqueElements(vzl)
  147. for zone in vzl:
  148. datagram.addUint32(zone)
  149. else:
  150. datagram.addUint32(zoneIdList)
  151. self.send(datagram)
  152. def _sendRemoveInterest(self, contextId):
  153. """
  154. contextId is a client-side created number that refers to
  155. a set of interests. The same contextId number doesn't
  156. necessarily have any relationship to the same contextId
  157. on another client.
  158. """
  159. assert self.notify.debugCall()
  160. datagram = PyDatagram()
  161. # Add message type
  162. datagram.addUint16(CLIENT_REMOVE_INTEREST)
  163. datagram.addUint16(contextId)
  164. self.send(datagram)
  165. def handleInterestDoneMessage(self, di):
  166. """
  167. This handles the interest done messages and may dispatch a
  168. action based on the ID, Context
  169. """
  170. assert self.notify.debugCall()
  171. id = di.getUint16()
  172. scope = di.getUint32()
  173. expect_scope = self.getInterestScopeId(id)
  174. print "handleInterestDoneMessage--> Received ID:%s Scope:%s"%(id,scope);
  175. if expect_scope == scope:
  176. print "handleInterestDoneMessage--> Scope Match:%s Scope:%s"%(id,scope);
  177. event = self.getInterestScopeEvent(id)
  178. if event is not None:
  179. print "handleInterestDoneMessage--> Send Event : %s"%(event);
  180. messenger.send(event)
  181. else:
  182. print "handleInterestDoneMessage--> No Event ";
  183. self._ponderRemoveFlaggedInterest(id)
  184. else:
  185. print "handleInterestDoneMessage--> Scope MisMatch :%s :%s"%(expect_scope,scope);
  186. assert self.printInterests()