AsyncRequest.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from otp.ai.AIBaseGlobal import *
  2. from direct.directnotify import DirectNotifyGlobal
  3. from direct.showbase.DirectObject import DirectObject
  4. if __debug__:
  5. BreakOnTimeout = config.GetBool("break-on-timeout", 0)
  6. class AsyncRequest(DirectObject):
  7. """
  8. This class is used to make asynchronos reads and creates to a database.
  9. You can create a list of self.neededObjects and then ask for each to be
  10. read or created, or if you only have one object that you need you can
  11. skip the self.neededObjects because calling askForObject or createObject
  12. will set the self.neededObjects value for you.
  13. Once all the objects have been read or created, the self.finish() method
  14. will be called. You may override this function to run your code in a
  15. derived class.
  16. If you wish to queue up several items that you all need before the finish
  17. method is called, you can put items in self.neededObjects and then call
  18. askForObject or createObject afterwards. That way the _checkCompletion
  19. will not call finish until after all the requests have been done.
  20. If you need to chain serveral object reads or creates, just add more
  21. entries to the self.neededObjects dictionary in the self.finish function
  22. and return without calling AsyncRequest.finish(). Your finish method
  23. will be called again when the new self.neededObjects is complete. You
  24. may repeat this as necessary.
  25. """
  26. if __debug__:
  27. notify = DirectNotifyGlobal.directNotify.newCategory('AsyncRequest')
  28. def __init__(self, air, replyToChannelId=None, timeout=4.0):
  29. """
  30. air is the AI Respository.
  31. replyToChannelId may be an avatarId, an accountId, or a channelId.
  32. timeout is how many seconds to wait before aborting the request.
  33. """
  34. assert self.notify.debugCall()
  35. assert isinstance(air, ConnectionRepository) # The api to AsyncRequest has changed.
  36. #DirectObject.DirectObject.__init__(self)
  37. self.air=air
  38. self.replyToChannelId=replyToChannelId
  39. self.neededObjects={}
  40. self.timeoutTask=taskMgr.doMethodLater(
  41. timeout, self.timeout, "AsyncRequestTimer-%s"%(id(self,)))
  42. def delete(self):
  43. assert self.notify.debugCall()
  44. self.timeoutTask.remove()
  45. del self.timeoutTask
  46. self.ignoreAll()
  47. if 0:
  48. for i in self.neededObjects.values():
  49. if i is not None:
  50. #self.air.unRegisterForChannel(o.account.doId)
  51. #self.air.removeDOFromTables(o.account)
  52. #if 0:
  53. # o.account.delete()
  54. # self.air.deleteObject(o.account.getDoId())
  55. self.air.removeDOFromTables(i)
  56. i.delete()
  57. del self.neededObjects
  58. del self.air
  59. del self.replyToChannelId
  60. #DirectObject.DirectObject.delete(self)
  61. def timeout(self, task):
  62. """
  63. If this is called we have not gotten the needed objects in the timeout
  64. period. Derived classes should inform the user or whomever and then
  65. call this base method to cleanup.
  66. """
  67. assert self.notify.debugCall("neededObjects: %s"%(self.neededObjects,))
  68. if __debug__:
  69. global BreakOnTimeout
  70. if BreakOnTimeout:
  71. print "\n\nself.neededObjects =", self.neededObjects
  72. print "\ntimed out after %s seconds."%(task.delayTime,)
  73. import pdb; pdb.set_trace()
  74. self.delete()
  75. def _checkCompletion(self, name, context, distObj):
  76. """
  77. This checks whether we have all the needed objects and calls
  78. finish() if we do.
  79. """
  80. assert self.notify.debugCall()
  81. if name is not None:
  82. self.neededObjects[name]=distObj
  83. else:
  84. self.neededObjects[distObj.doId]=distObj
  85. for i in self.neededObjects.values():
  86. if i is None:
  87. return
  88. self.finish()
  89. def askForObject(self, doId, context=None):
  90. """
  91. Request an already created object, i.e. read from database.
  92. """
  93. assert self.notify.debugCall()
  94. assert doId
  95. object = self.air.doId2do.get(doId)
  96. self.neededObjects[doId]=object
  97. if object is not None:
  98. self._checkCompletion(None, context, object)
  99. else:
  100. if context is None:
  101. context=self.air.allocateContext()
  102. self.accept(
  103. "doRequestResponse-%s"%(context,),
  104. self._checkCompletion, [None])
  105. self.air.queryObjectSnapshot(doId, context)
  106. def createObject(self, name, className, context=None, values=None):
  107. """
  108. Create a new database object. You can get the doId from within
  109. your self.finish() function.
  110. """
  111. assert self.notify.debugCall()
  112. assert name
  113. assert className
  114. self.neededObjects[name]=None
  115. if context is None:
  116. context=self.air.allocateContext()
  117. self.accept(
  118. "doRequestResponse-%s"%(context,), self._checkCompletion, [name])
  119. if values is None:
  120. self.air.requestDatabaseGenerate(className, context)
  121. else:
  122. self.air.requestDatabaseGenerate2(className, context, values=values)
  123. def finish(self):
  124. """
  125. This is the function that gets called when all of the needed objects
  126. are in (i.e. all the askForObject and createObject requests have
  127. been satisfied).
  128. If the other requests timeout, finish will not be called.
  129. """
  130. assert self.notify.debugCall()
  131. self.delete()