util.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. # -*- test-case-name: twisted.test.test_pb -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Utility classes for spread.
  6. """
  7. from twisted.internet import defer
  8. from twisted.python.failure import Failure
  9. from twisted.spread import pb
  10. from twisted.protocols import basic
  11. from twisted.internet import interfaces
  12. from zope.interface import implementer
  13. class LocalMethod:
  14. def __init__(self, local, name):
  15. self.local = local
  16. self.name = name
  17. def __call__(self, *args, **kw):
  18. return self.local.callRemote(self.name, *args, **kw)
  19. class LocalAsRemote:
  20. """
  21. A class useful for emulating the effects of remote behavior locally.
  22. """
  23. reportAllTracebacks = 1
  24. def callRemote(self, name, *args, **kw):
  25. """
  26. Call a specially-designated local method.
  27. self.callRemote('x') will first try to invoke a method named
  28. sync_x and return its result (which should probably be a
  29. Deferred). Second, it will look for a method called async_x,
  30. which will be called and then have its result (or Failure)
  31. automatically wrapped in a Deferred.
  32. """
  33. if hasattr(self, 'sync_'+name):
  34. return getattr(self, 'sync_'+name)(*args, **kw)
  35. try:
  36. method = getattr(self, "async_" + name)
  37. return defer.succeed(method(*args, **kw))
  38. except:
  39. f = Failure()
  40. if self.reportAllTracebacks:
  41. f.printTraceback()
  42. return defer.fail(f)
  43. def remoteMethod(self, name):
  44. return LocalMethod(self, name)
  45. class LocalAsyncForwarder:
  46. """
  47. A class useful for forwarding a locally-defined interface.
  48. """
  49. def __init__(self, forwarded, interfaceClass, failWhenNotImplemented=0):
  50. assert interfaceClass.providedBy(forwarded)
  51. self.forwarded = forwarded
  52. self.interfaceClass = interfaceClass
  53. self.failWhenNotImplemented = failWhenNotImplemented
  54. def _callMethod(self, method, *args, **kw):
  55. return getattr(self.forwarded, method)(*args, **kw)
  56. def callRemote(self, method, *args, **kw):
  57. if self.interfaceClass.queryDescriptionFor(method):
  58. result = defer.maybeDeferred(self._callMethod, method, *args, **kw)
  59. return result
  60. elif self.failWhenNotImplemented:
  61. return defer.fail(
  62. Failure(NotImplementedError,
  63. "No Such Method in Interface: %s" % method))
  64. else:
  65. return defer.succeed(None)
  66. class Pager:
  67. """
  68. I am an object which pages out information.
  69. """
  70. def __init__(self, collector, callback=None, *args, **kw):
  71. """
  72. Create a pager with a Reference to a remote collector and
  73. an optional callable to invoke upon completion.
  74. """
  75. if callable(callback):
  76. self.callback = callback
  77. self.callbackArgs = args
  78. self.callbackKeyword = kw
  79. else:
  80. self.callback = None
  81. self._stillPaging = 1
  82. self.collector = collector
  83. collector.broker.registerPageProducer(self)
  84. def stillPaging(self):
  85. """
  86. (internal) Method called by Broker.
  87. """
  88. if not self._stillPaging:
  89. self.collector.callRemote("endedPaging", pbanswer=False)
  90. if self.callback is not None:
  91. self.callback(*self.callbackArgs, **self.callbackKeyword)
  92. return self._stillPaging
  93. def sendNextPage(self):
  94. """
  95. (internal) Method called by Broker.
  96. """
  97. self.collector.callRemote("gotPage", self.nextPage(), pbanswer=False)
  98. def nextPage(self):
  99. """
  100. Override this to return an object to be sent to my collector.
  101. """
  102. raise NotImplementedError()
  103. def stopPaging(self):
  104. """
  105. Call this when you're done paging.
  106. """
  107. self._stillPaging = 0
  108. class StringPager(Pager):
  109. """
  110. A simple pager that splits a string into chunks.
  111. """
  112. def __init__(self, collector, st, chunkSize=8192, callback=None, *args, **kw):
  113. self.string = st
  114. self.pointer = 0
  115. self.chunkSize = chunkSize
  116. Pager.__init__(self, collector, callback, *args, **kw)
  117. def nextPage(self):
  118. val = self.string[self.pointer:self.pointer+self.chunkSize]
  119. self.pointer += self.chunkSize
  120. if self.pointer >= len(self.string):
  121. self.stopPaging()
  122. return val
  123. @implementer(interfaces.IConsumer)
  124. class FilePager(Pager):
  125. """
  126. Reads a file in chunks and sends the chunks as they come.
  127. """
  128. def __init__(self, collector, fd, callback=None, *args, **kw):
  129. self.chunks = []
  130. Pager.__init__(self, collector, callback, *args, **kw)
  131. self.startProducing(fd)
  132. def startProducing(self, fd):
  133. self.deferred = basic.FileSender().beginFileTransfer(fd, self)
  134. self.deferred.addBoth(lambda x : self.stopPaging())
  135. def registerProducer(self, producer, streaming):
  136. self.producer = producer
  137. if not streaming:
  138. self.producer.resumeProducing()
  139. def unregisterProducer(self):
  140. self.producer = None
  141. def write(self, chunk):
  142. self.chunks.append(chunk)
  143. def sendNextPage(self):
  144. """
  145. Get the first chunk read and send it to collector.
  146. """
  147. if not self.chunks:
  148. return
  149. val = self.chunks.pop(0)
  150. self.producer.resumeProducing()
  151. self.collector.callRemote("gotPage", val, pbanswer=False)
  152. # Utility paging stuff.
  153. class CallbackPageCollector(pb.Referenceable):
  154. """
  155. I receive pages from the peer. You may instantiate a Pager with a
  156. remote reference to me. I will call the callback with a list of pages
  157. once they are all received.
  158. """
  159. def __init__(self, callback):
  160. self.pages = []
  161. self.callback = callback
  162. def remote_gotPage(self, page):
  163. self.pages.append(page)
  164. def remote_endedPaging(self):
  165. self.callback(self.pages)
  166. def getAllPages(referenceable, methodName, *args, **kw):
  167. """
  168. A utility method that will call a remote method which expects a
  169. PageCollector as the first argument.
  170. """
  171. d = defer.Deferred()
  172. referenceable.callRemote(methodName, CallbackPageCollector(d.callback), *args, **kw)
  173. return d