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.

forwarder.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) typedef int GmbH
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. #
  25. ###############################################################################
  26. import txaio
  27. txaio.use_twisted()
  28. from twisted.python import usage
  29. from twisted.internet.defer import inlineCallbacks
  30. from twisted.internet.protocol import Factory, Protocol
  31. from twisted.internet.endpoints import clientFromString, serverFromString
  32. from twisted.application import service
  33. class DestEndpointForwardingProtocol(Protocol):
  34. log = txaio.make_logger()
  35. def connectionMade(self):
  36. self.log.debug("DestEndpointForwardingProtocol.connectionMade")
  37. pass
  38. def dataReceived(self, data):
  39. self.log.debug(
  40. "DestEndpointForwardingProtocol.dataReceived: {data}",
  41. data=data,
  42. )
  43. if self.factory._sourceProtocol:
  44. self.factory._sourceProtocol.transport.write(data)
  45. def connectionLost(self, reason):
  46. self.log.debug("DestEndpointForwardingProtocol.connectionLost")
  47. if self.factory._sourceProtocol:
  48. self.factory._sourceProtocol.transport.loseConnection()
  49. class DestEndpointForwardingFactory(Factory):
  50. def __init__(self, sourceProtocol):
  51. self._sourceProtocol = sourceProtocol
  52. self._proto = None
  53. def buildProtocol(self, addr):
  54. self._proto = DestEndpointForwardingProtocol()
  55. self._proto.factory = self
  56. return self._proto
  57. class EndpointForwardingProtocol(Protocol):
  58. log = txaio.make_logger()
  59. @inlineCallbacks
  60. def connectionMade(self):
  61. self.log.debug("EndpointForwardingProtocol.connectionMade")
  62. self._destFactory = DestEndpointForwardingFactory(self)
  63. self._destEndpoint = clientFromString(self.factory.service._reactor,
  64. self.factory.service._destEndpointDescriptor)
  65. self._destEndpointPort = yield self._destEndpoint.connect(self._destFactory)
  66. def dataReceived(self, data):
  67. self.log.debug(
  68. "EndpointForwardingProtocol.dataReceived: {data}",
  69. data=data,
  70. )
  71. if self._destFactory._proto:
  72. self._destFactory._proto.transport.write(data)
  73. def connectionLost(self, reason):
  74. self.log.debug("EndpointForwardingProtocol.connectionLost")
  75. if self._destFactory._proto:
  76. self._destFactory._proto.transport.loseConnection()
  77. class EndpointForwardingService(service.Service):
  78. def __init__(self, endpointDescriptor, destEndpointDescriptor, reactor=None):
  79. if reactor is None:
  80. from twisted.internet import reactor
  81. self._reactor = reactor
  82. self._endpointDescriptor = endpointDescriptor
  83. self._destEndpointDescriptor = destEndpointDescriptor
  84. @inlineCallbacks
  85. def startService(self):
  86. factory = Factory.forProtocol(EndpointForwardingProtocol)
  87. factory.service = self
  88. self._endpoint = serverFromString(self._reactor, self._endpointDescriptor)
  89. self._endpointPort = yield self._endpoint.listen(factory)
  90. def stopService(self):
  91. return self._endpointPort.stopListening()
  92. class Options(usage.Options):
  93. synopsis = "[options]"
  94. longdesc = 'Endpoint Forwarder.'
  95. optParameters = [
  96. ["endpoint", "e", None, "Source endpoint."],
  97. ["dest_endpoint", "d", None, "Destination endpoint."]
  98. ]
  99. def makeService(config):
  100. service = EndpointForwardingService(config['endpoint'], config['dest_endpoint'])
  101. return service