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.

strports.py 2.5KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # -*- test-case-name: twisted.test.test_strports -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Construct listening port services from a simple string description.
  6. @see: L{twisted.internet.endpoints.serverFromString}
  7. @see: L{twisted.internet.endpoints.clientFromString}
  8. """
  9. from typing import Optional, cast
  10. from twisted.application.internet import StreamServerEndpointService
  11. from twisted.internet import endpoints, interfaces
  12. def _getReactor() -> interfaces.IReactorCore:
  13. from twisted.internet import reactor
  14. return cast(interfaces.IReactorCore, reactor)
  15. def service(
  16. description: str,
  17. factory: interfaces.IProtocolFactory,
  18. reactor: Optional[interfaces.IReactorCore] = None,
  19. ) -> StreamServerEndpointService:
  20. """
  21. Return the service corresponding to a description.
  22. @param description: The description of the listening port, in the syntax
  23. described by L{twisted.internet.endpoints.serverFromString}.
  24. @type description: C{str}
  25. @param factory: The protocol factory which will build protocols for
  26. connections to this service.
  27. @type factory: L{twisted.internet.interfaces.IProtocolFactory}
  28. @rtype: C{twisted.application.service.IService}
  29. @return: the service corresponding to a description of a reliable stream
  30. server.
  31. @see: L{twisted.internet.endpoints.serverFromString}
  32. """
  33. if reactor is None:
  34. reactor = _getReactor()
  35. svc = StreamServerEndpointService(
  36. endpoints.serverFromString(reactor, description), factory
  37. )
  38. svc._raiseSynchronously = True
  39. return svc
  40. def listen(
  41. description: str, factory: interfaces.IProtocolFactory
  42. ) -> interfaces.IListeningPort:
  43. """
  44. Listen on a port corresponding to a description.
  45. @param description: The description of the connecting port, in the syntax
  46. described by L{twisted.internet.endpoints.serverFromString}.
  47. @type description: L{str}
  48. @param factory: The protocol factory which will build protocols on
  49. connection.
  50. @type factory: L{twisted.internet.interfaces.IProtocolFactory}
  51. @rtype: L{twisted.internet.interfaces.IListeningPort}
  52. @return: the port corresponding to a description of a reliable virtual
  53. circuit server.
  54. @see: L{twisted.internet.endpoints.serverFromString}
  55. """
  56. from twisted.internet import reactor
  57. name, args, kw = endpoints._parseServer(description, factory)
  58. return cast(
  59. interfaces.IListeningPort, getattr(reactor, "listen" + name)(*args, **kw)
  60. )
  61. __all__ = ["service", "listen"]