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.

autobahn_endpoints.py 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) Crossbar.io Technologies 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. from __future__ import absolute_import
  27. from zope.interface import implementer
  28. from twisted.plugin import IPlugin
  29. from twisted.internet.interfaces import IStreamServerEndpointStringParser, \
  30. IStreamServerEndpoint, \
  31. IStreamClientEndpoint
  32. try:
  33. from twisted.internet.interfaces import IStreamClientEndpointStringParserWithReactor
  34. _HAS_REACTOR_ARG = True
  35. except ImportError:
  36. _HAS_REACTOR_ARG = False
  37. from twisted.internet.interfaces import IStreamClientEndpointStringParser as \
  38. IStreamClientEndpointStringParserWithReactor
  39. from twisted.internet.endpoints import serverFromString, clientFromString
  40. from autobahn.twisted.websocket import WrappingWebSocketServerFactory, \
  41. WrappingWebSocketClientFactory
  42. def _parseOptions(options):
  43. opts = {}
  44. if 'url' not in options:
  45. raise Exception("URL needed")
  46. else:
  47. opts['url'] = options['url']
  48. if 'compression' in options:
  49. value = options['compression'].lower().strip()
  50. if value == 'true':
  51. opts['enableCompression'] = True
  52. elif value == 'false':
  53. opts['enableCompression'] = False
  54. else:
  55. raise Exception("invalid value '{0}' for compression".format(value))
  56. if 'autofrag' in options:
  57. try:
  58. value = int(options['autofrag'])
  59. except:
  60. raise Exception("invalid value '{0}' for autofrag".format(options['autofrag']))
  61. if value < 0:
  62. raise Exception("negative value '{0}' for autofrag".format(value))
  63. opts['autoFragmentSize'] = value
  64. if 'subprotocol' in options:
  65. value = options['subprotocol'].lower().strip()
  66. opts['subprotocol'] = value
  67. if 'debug' in options:
  68. value = options['debug'].lower().strip()
  69. if value == 'true':
  70. opts['debug'] = True
  71. elif value == 'false':
  72. opts['debug'] = False
  73. else:
  74. raise Exception("invalid value '{0}' for debug".format(value))
  75. return opts
  76. @implementer(IPlugin)
  77. @implementer(IStreamServerEndpointStringParser)
  78. class AutobahnServerParser(object):
  79. prefix = "autobahn"
  80. def parseStreamServer(self, reactor, description, **options):
  81. # The present endpoint plugin is intended to be used as in the
  82. # following for running a streaming protocol over WebSocket over
  83. # an underlying stream transport.
  84. #
  85. # endpoint = serverFromString(reactor,
  86. # "autobahn:tcp\:9000\:interface\=0.0.0.0:url=ws\://localhost\:9000:compress=false"
  87. #
  88. # This will result in `parseStreamServer` to be called will
  89. #
  90. # description == tcp:9000:interface=0.0.0.0
  91. #
  92. # and
  93. #
  94. # options == {'url': 'ws://localhost:9000', 'compress': 'false'}
  95. #
  96. # Essentially, we are using the `\:` escape to coerce the endpoint descriptor
  97. # of the underlying stream transport into one (first) positional argument.
  98. #
  99. # Note that the `\:` within "url" is another form of escaping!
  100. #
  101. opts = _parseOptions(options)
  102. endpoint = serverFromString(reactor, description)
  103. return AutobahnServerEndpoint(reactor, endpoint, opts)
  104. @implementer(IPlugin)
  105. @implementer(IStreamServerEndpoint)
  106. class AutobahnServerEndpoint(object):
  107. def __init__(self, reactor, endpoint, options):
  108. self._reactor = reactor
  109. self._endpoint = endpoint
  110. self._options = options
  111. def listen(self, protocolFactory):
  112. return self._endpoint.listen(WrappingWebSocketServerFactory(protocolFactory, reactor=self._reactor, **self._options))
  113. # note that for older Twisted before the WithReactor variant, we
  114. # import it under that name so we can share (most of) this
  115. # implementation...
  116. @implementer(IPlugin)
  117. @implementer(IStreamClientEndpointStringParserWithReactor)
  118. class AutobahnClientParser(object):
  119. prefix = "autobahn"
  120. def parseStreamClient(self, *args, **options):
  121. if _HAS_REACTOR_ARG:
  122. reactor = args[0]
  123. if len(args) != 2:
  124. raise RuntimeError("autobahn: client plugin takes exactly one positional argument")
  125. description = args[1]
  126. else:
  127. from twisted.internet import reactor
  128. if len(args) != 1:
  129. raise RuntimeError("autobahn: client plugin takes exactly one positional argument")
  130. description = args[0]
  131. opts = _parseOptions(options)
  132. endpoint = clientFromString(reactor, description)
  133. return AutobahnClientEndpoint(reactor, endpoint, opts)
  134. @implementer(IPlugin)
  135. @implementer(IStreamClientEndpoint)
  136. class AutobahnClientEndpoint(object):
  137. def __init__(self, reactor, endpoint, options):
  138. self._reactor = reactor
  139. self._endpoint = endpoint
  140. self._options = options
  141. def connect(self, protocolFactory):
  142. return self._endpoint.connect(WrappingWebSocketClientFactory(protocolFactory, reactor=self._reactor, **self._options))
  143. autobahnServerParser = AutobahnServerParser()
  144. autobahnClientParser = AutobahnClientParser()