Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

loopback.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. # -*- test-case-name: twisted.test.test_loopback -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Testing support for protocols -- loopback between client and server.
  6. """
  7. # system imports
  8. import tempfile
  9. from zope.interface import implementer
  10. from twisted.internet import defer, interfaces, main, protocol
  11. from twisted.internet.interfaces import IAddress
  12. from twisted.internet.task import deferLater
  13. # Twisted Imports
  14. from twisted.protocols import policies
  15. from twisted.python import failure
  16. class _LoopbackQueue:
  17. """
  18. Trivial wrapper around a list to give it an interface like a queue, which
  19. the addition of also sending notifications by way of a Deferred whenever
  20. the list has something added to it.
  21. """
  22. _notificationDeferred = None
  23. disconnect = False
  24. def __init__(self):
  25. self._queue = []
  26. def put(self, v):
  27. self._queue.append(v)
  28. if self._notificationDeferred is not None:
  29. d, self._notificationDeferred = self._notificationDeferred, None
  30. d.callback(None)
  31. def __nonzero__(self):
  32. return bool(self._queue)
  33. __bool__ = __nonzero__
  34. def get(self):
  35. return self._queue.pop(0)
  36. @implementer(IAddress)
  37. class _LoopbackAddress:
  38. pass
  39. @implementer(interfaces.ITransport, interfaces.IConsumer)
  40. class _LoopbackTransport:
  41. disconnecting = False
  42. producer = None
  43. # ITransport
  44. def __init__(self, q):
  45. self.q = q
  46. def write(self, data):
  47. if not isinstance(data, bytes):
  48. raise TypeError("Can only write bytes to ITransport")
  49. self.q.put(data)
  50. def writeSequence(self, iovec):
  51. self.q.put(b"".join(iovec))
  52. def loseConnection(self):
  53. self.q.disconnect = True
  54. self.q.put(None)
  55. def abortConnection(self):
  56. """
  57. Abort the connection. Same as L{loseConnection}.
  58. """
  59. self.loseConnection()
  60. def getPeer(self):
  61. return _LoopbackAddress()
  62. def getHost(self):
  63. return _LoopbackAddress()
  64. # IConsumer
  65. def registerProducer(self, producer, streaming):
  66. assert self.producer is None
  67. self.producer = producer
  68. self.streamingProducer = streaming
  69. self._pollProducer()
  70. def unregisterProducer(self):
  71. assert self.producer is not None
  72. self.producer = None
  73. def _pollProducer(self):
  74. if self.producer is not None and not self.streamingProducer:
  75. self.producer.resumeProducing()
  76. def identityPumpPolicy(queue, target):
  77. """
  78. L{identityPumpPolicy} is a policy which delivers each chunk of data written
  79. to the given queue as-is to the target.
  80. This isn't a particularly realistic policy.
  81. @see: L{loopbackAsync}
  82. """
  83. while queue:
  84. bytes = queue.get()
  85. if bytes is None:
  86. break
  87. target.dataReceived(bytes)
  88. def collapsingPumpPolicy(queue, target):
  89. """
  90. L{collapsingPumpPolicy} is a policy which collapses all outstanding chunks
  91. into a single string and delivers it to the target.
  92. @see: L{loopbackAsync}
  93. """
  94. bytes = []
  95. while queue:
  96. chunk = queue.get()
  97. if chunk is None:
  98. break
  99. bytes.append(chunk)
  100. if bytes:
  101. target.dataReceived(b"".join(bytes))
  102. def loopbackAsync(server, client, pumpPolicy=identityPumpPolicy):
  103. """
  104. Establish a connection between C{server} and C{client} then transfer data
  105. between them until the connection is closed. This is often useful for
  106. testing a protocol.
  107. @param server: The protocol instance representing the server-side of this
  108. connection.
  109. @param client: The protocol instance representing the client-side of this
  110. connection.
  111. @param pumpPolicy: When either C{server} or C{client} writes to its
  112. transport, the string passed in is added to a queue of data for the
  113. other protocol. Eventually, C{pumpPolicy} will be called with one such
  114. queue and the corresponding protocol object. The pump policy callable
  115. is responsible for emptying the queue and passing the strings it
  116. contains to the given protocol's C{dataReceived} method. The signature
  117. of C{pumpPolicy} is C{(queue, protocol)}. C{queue} is an object with a
  118. C{get} method which will return the next string written to the
  119. transport, or L{None} if the transport has been disconnected, and which
  120. evaluates to C{True} if and only if there are more items to be
  121. retrieved via C{get}.
  122. @return: A L{Deferred} which fires when the connection has been closed and
  123. both sides have received notification of this.
  124. """
  125. serverToClient = _LoopbackQueue()
  126. clientToServer = _LoopbackQueue()
  127. server.makeConnection(_LoopbackTransport(serverToClient))
  128. client.makeConnection(_LoopbackTransport(clientToServer))
  129. return _loopbackAsyncBody(
  130. server, serverToClient, client, clientToServer, pumpPolicy
  131. )
  132. def _loopbackAsyncBody(server, serverToClient, client, clientToServer, pumpPolicy):
  133. """
  134. Transfer bytes from the output queue of each protocol to the input of the other.
  135. @param server: The protocol instance representing the server-side of this
  136. connection.
  137. @param serverToClient: The L{_LoopbackQueue} holding the server's output.
  138. @param client: The protocol instance representing the client-side of this
  139. connection.
  140. @param clientToServer: The L{_LoopbackQueue} holding the client's output.
  141. @param pumpPolicy: See L{loopbackAsync}.
  142. @return: A L{Deferred} which fires when the connection has been closed and
  143. both sides have received notification of this.
  144. """
  145. def pump(source, q, target):
  146. sent = False
  147. if q:
  148. pumpPolicy(q, target)
  149. sent = True
  150. if sent and not q:
  151. # A write buffer has now been emptied. Give any producer on that
  152. # side an opportunity to produce more data.
  153. source.transport._pollProducer()
  154. return sent
  155. while 1:
  156. disconnect = clientSent = serverSent = False
  157. # Deliver the data which has been written.
  158. serverSent = pump(server, serverToClient, client)
  159. clientSent = pump(client, clientToServer, server)
  160. if not clientSent and not serverSent:
  161. # Neither side wrote any data. Wait for some new data to be added
  162. # before trying to do anything further.
  163. d = defer.Deferred()
  164. clientToServer._notificationDeferred = d
  165. serverToClient._notificationDeferred = d
  166. d.addCallback(
  167. _loopbackAsyncContinue,
  168. server,
  169. serverToClient,
  170. client,
  171. clientToServer,
  172. pumpPolicy,
  173. )
  174. return d
  175. if serverToClient.disconnect:
  176. # The server wants to drop the connection. Flush any remaining
  177. # data it has.
  178. disconnect = True
  179. pump(server, serverToClient, client)
  180. elif clientToServer.disconnect:
  181. # The client wants to drop the connection. Flush any remaining
  182. # data it has.
  183. disconnect = True
  184. pump(client, clientToServer, server)
  185. if disconnect:
  186. # Someone wanted to disconnect, so okay, the connection is gone.
  187. server.connectionLost(failure.Failure(main.CONNECTION_DONE))
  188. client.connectionLost(failure.Failure(main.CONNECTION_DONE))
  189. return defer.succeed(None)
  190. def _loopbackAsyncContinue(
  191. ignored, server, serverToClient, client, clientToServer, pumpPolicy
  192. ):
  193. # Clear the Deferred from each message queue, since it has already fired
  194. # and cannot be used again.
  195. clientToServer._notificationDeferred = None
  196. serverToClient._notificationDeferred = None
  197. # Schedule some more byte-pushing to happen. This isn't done
  198. # synchronously because no actual transport can re-enter dataReceived as
  199. # a result of calling write, and doing this synchronously could result
  200. # in that.
  201. from twisted.internet import reactor
  202. return deferLater(
  203. reactor,
  204. 0,
  205. _loopbackAsyncBody,
  206. server,
  207. serverToClient,
  208. client,
  209. clientToServer,
  210. pumpPolicy,
  211. )
  212. @implementer(interfaces.ITransport, interfaces.IConsumer)
  213. class LoopbackRelay:
  214. buffer = b""
  215. shouldLose = 0
  216. disconnecting = 0
  217. producer = None
  218. def __init__(self, target, logFile=None):
  219. self.target = target
  220. self.logFile = logFile
  221. def write(self, data):
  222. self.buffer = self.buffer + data
  223. if self.logFile:
  224. self.logFile.write("loopback writing %s\n" % repr(data))
  225. def writeSequence(self, iovec):
  226. self.write(b"".join(iovec))
  227. def clearBuffer(self):
  228. if self.shouldLose == -1:
  229. return
  230. if self.producer:
  231. self.producer.resumeProducing()
  232. if self.buffer:
  233. if self.logFile:
  234. self.logFile.write("loopback receiving %s\n" % repr(self.buffer))
  235. buffer = self.buffer
  236. self.buffer = b""
  237. self.target.dataReceived(buffer)
  238. if self.shouldLose == 1:
  239. self.shouldLose = -1
  240. self.target.connectionLost(failure.Failure(main.CONNECTION_DONE))
  241. def loseConnection(self):
  242. if self.shouldLose != -1:
  243. self.shouldLose = 1
  244. def getHost(self):
  245. return "loopback"
  246. def getPeer(self):
  247. return "loopback"
  248. def registerProducer(self, producer, streaming):
  249. self.producer = producer
  250. def unregisterProducer(self):
  251. self.producer = None
  252. def logPrefix(self):
  253. return f"Loopback({self.target.__class__.__name__!r})"
  254. class LoopbackClientFactory(protocol.ClientFactory):
  255. def __init__(self, protocol):
  256. self.disconnected = 0
  257. self.deferred = defer.Deferred()
  258. self.protocol = protocol
  259. def buildProtocol(self, addr):
  260. return self.protocol
  261. def clientConnectionLost(self, connector, reason):
  262. self.disconnected = 1
  263. self.deferred.callback(None)
  264. class _FireOnClose(policies.ProtocolWrapper):
  265. def __init__(self, protocol, factory):
  266. policies.ProtocolWrapper.__init__(self, protocol, factory)
  267. self.deferred = defer.Deferred()
  268. def connectionLost(self, reason):
  269. policies.ProtocolWrapper.connectionLost(self, reason)
  270. self.deferred.callback(None)
  271. def loopbackTCP(server, client, port=0, noisy=True):
  272. """Run session between server and client protocol instances over TCP."""
  273. from twisted.internet import reactor
  274. f = policies.WrappingFactory(protocol.Factory())
  275. serverWrapper = _FireOnClose(f, server)
  276. f.noisy = noisy
  277. f.buildProtocol = lambda addr: serverWrapper
  278. serverPort = reactor.listenTCP(port, f, interface="127.0.0.1")
  279. clientF = LoopbackClientFactory(client)
  280. clientF.noisy = noisy
  281. reactor.connectTCP("127.0.0.1", serverPort.getHost().port, clientF)
  282. d = clientF.deferred
  283. d.addCallback(lambda x: serverWrapper.deferred)
  284. d.addCallback(lambda x: serverPort.stopListening())
  285. return d
  286. def loopbackUNIX(server, client, noisy=True):
  287. """Run session between server and client protocol instances over UNIX socket."""
  288. path = tempfile.mktemp()
  289. from twisted.internet import reactor
  290. f = policies.WrappingFactory(protocol.Factory())
  291. serverWrapper = _FireOnClose(f, server)
  292. f.noisy = noisy
  293. f.buildProtocol = lambda addr: serverWrapper
  294. serverPort = reactor.listenUNIX(path, f)
  295. clientF = LoopbackClientFactory(client)
  296. clientF.noisy = noisy
  297. reactor.connectUNIX(path, clientF)
  298. d = clientF.deferred
  299. d.addCallback(lambda x: serverWrapper.deferred)
  300. d.addCallback(lambda x: serverPort.stopListening())
  301. return d