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.

websocket.py 13KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 copy
  27. import traceback
  28. from typing import Optional, Dict, Tuple
  29. from autobahn.util import hlval
  30. from autobahn.websocket import protocol
  31. from autobahn.websocket.types import ConnectionDeny, ConnectionRequest, ConnectionResponse
  32. from autobahn.wamp.types import TransportDetails
  33. from autobahn.wamp.interfaces import ITransport, ISession
  34. from autobahn.wamp.exception import ProtocolError, SerializationError, TransportLost
  35. __all__ = ('WampWebSocketServerProtocol',
  36. 'WampWebSocketClientProtocol',
  37. 'WampWebSocketServerFactory',
  38. 'WampWebSocketClientFactory')
  39. class WampWebSocketProtocol(object):
  40. """
  41. Base class for WAMP-over-WebSocket transport mixins.
  42. """
  43. _session: Optional[ISession] = None # default; self.session is set in onOpen
  44. _transport_details: Optional[TransportDetails] = None
  45. def _bailout(self, code: int, reason: Optional[str] = None):
  46. self.log.debug('Failing WAMP-over-WebSocket transport: code={code}, reason="{reason}"', code=code,
  47. reason=reason)
  48. self._fail_connection(code, reason)
  49. def onOpen(self):
  50. """
  51. Callback from :func:`autobahn.websocket.interfaces.IWebSocketChannel.onOpen`
  52. """
  53. # WebSocket connection established. Now let the user WAMP session factory
  54. # create a new WAMP session and fire off session open callback.
  55. try:
  56. self._session = self.factory._factory()
  57. self._session._transport = self
  58. self._session.onOpen(self)
  59. except Exception as e:
  60. self.log.critical("{tb}", tb=traceback.format_exc())
  61. reason = 'WAMP Internal Error ({0})'.format(e)
  62. self._bailout(protocol.WebSocketProtocol.CLOSE_STATUS_CODE_INTERNAL_ERROR, reason=reason)
  63. def onClose(self, wasClean: bool, code: int, reason: Optional[str]):
  64. """
  65. Callback from :func:`autobahn.websocket.interfaces.IWebSocketChannel.onClose`
  66. """
  67. # WAMP session might never have been established in the first place .. guard this!
  68. self._onclose_reason = reason
  69. if self._session is not None:
  70. # WebSocket connection lost - fire off the WAMP
  71. # session close callback
  72. # noinspection PyBroadException
  73. try:
  74. self.log.debug(
  75. 'WAMP-over-WebSocket transport lost: wasClean={wasClean}, code={code}, reason="{reason}"',
  76. wasClean=wasClean, code=code, reason=reason)
  77. self._session.onClose(wasClean)
  78. except Exception:
  79. self.log.critical("{tb}", tb=traceback.format_exc())
  80. self._session = None
  81. def onMessage(self, payload: bytes, isBinary: bool):
  82. """
  83. Callback from :func:`autobahn.websocket.interfaces.IWebSocketChannel.onMessage`
  84. """
  85. try:
  86. for msg in self._serializer.unserialize(payload, isBinary):
  87. self.log.trace('\n{action1}{session}, {authid}{action2}\n {message}\n{action3}',
  88. action1=hlval('WAMP-Receive(', color='green', bold=True),
  89. authid=hlval(self._session._authid, color='green', bold=False) if self._session._authid else '-',
  90. session=hlval(self._session._session_id, color='green', bold=False) if self._session._session_id else '-',
  91. action2=hlval(') <<', color='green', bold=True),
  92. action3=hlval('<<', color='green', bold=True),
  93. message=msg)
  94. self._session.onMessage(msg)
  95. except ProtocolError as e:
  96. self.log.critical("{tb}", tb=traceback.format_exc())
  97. reason = 'WAMP Protocol Error ({0})'.format(e)
  98. self._bailout(protocol.WebSocketProtocol.CLOSE_STATUS_CODE_PROTOCOL_ERROR, reason=reason)
  99. except Exception as e:
  100. self.log.critical("{tb}", tb=traceback.format_exc())
  101. reason = 'WAMP Internal Error ({0})'.format(e)
  102. self._bailout(protocol.WebSocketProtocol.CLOSE_STATUS_CODE_INTERNAL_ERROR, reason=reason)
  103. def send(self, msg):
  104. """
  105. Implements :func:`autobahn.wamp.interfaces.ITransport.send`
  106. """
  107. if self.isOpen():
  108. try:
  109. self.log.trace('\n{action1}{session}, {authid}{action2}\n {message}\n{action3}',
  110. action1=hlval('WAMP-Transmit(', color='red', bold=True),
  111. authid=hlval(self._session._authid, color='red', bold=False) if self._session._authid else '-',
  112. session=hlval(self._session._session_id, color='red', bold=False) if self._session._session_id else '-',
  113. action2=hlval(') >>', color='red', bold=True),
  114. action3=hlval('>>', color='red', bold=True),
  115. message=msg)
  116. payload, isBinary = self._serializer.serialize(msg)
  117. except Exception as e:
  118. self.log.error("WAMP message serialization error: {}".format(e))
  119. # all exceptions raised from above should be serialization errors ..
  120. raise SerializationError("WAMP message serialization error: {0}".format(e))
  121. else:
  122. self.sendMessage(payload, isBinary)
  123. else:
  124. raise TransportLost()
  125. def isOpen(self):
  126. """
  127. Implements :func:`autobahn.wamp.interfaces.ITransport.isOpen`
  128. """
  129. return self._session is not None
  130. @property
  131. def transport_details(self) -> Optional[TransportDetails]:
  132. """
  133. Implements :func:`autobahn.wamp.interfaces.ITransport.transport_details`
  134. """
  135. return self._transport_details
  136. def close(self):
  137. """
  138. Implements :func:`autobahn.wamp.interfaces.ITransport.close`
  139. """
  140. if self.isOpen():
  141. self.sendClose(protocol.WebSocketProtocol.CLOSE_STATUS_CODE_NORMAL)
  142. else:
  143. raise TransportLost()
  144. def abort(self):
  145. """
  146. Implements :func:`autobahn.wamp.interfaces.ITransport.abort`
  147. """
  148. if self.isOpen():
  149. self._bailout(protocol.WebSocketProtocol.CLOSE_STATUS_CODE_GOING_AWAY)
  150. else:
  151. raise TransportLost()
  152. ITransport.register(WampWebSocketProtocol)
  153. def parseSubprotocolIdentifier(subprotocol: str) -> Tuple[Optional[int], Optional[str]]:
  154. try:
  155. s = subprotocol.split('.')
  156. if s[0] != 'wamp':
  157. raise Exception('WAMP WebSocket subprotocol identifier must start with "wamp", not "{}"'.format(s[0]))
  158. version = int(s[1])
  159. serializer_id = '.'.join(s[2:])
  160. return version, serializer_id
  161. except:
  162. return None, None
  163. class WampWebSocketServerProtocol(WampWebSocketProtocol):
  164. """
  165. Mixin for WAMP-over-WebSocket server transports.
  166. """
  167. STRICT_PROTOCOL_NEGOTIATION = True
  168. def onConnect(self, request: ConnectionRequest) -> Tuple[Optional[str], Dict[str, str]]:
  169. """
  170. Callback from :func:`autobahn.websocket.interfaces.IWebSocketChannel.onConnect`
  171. """
  172. headers = {}
  173. for subprotocol in request.protocols:
  174. version, serializerId = parseSubprotocolIdentifier(subprotocol)
  175. if version == 2 and serializerId in self.factory._serializers.keys():
  176. # copy over serializer form factory, so that we keep per-session serializer stats
  177. self._serializer = copy.copy(self.factory._serializers[serializerId])
  178. return subprotocol, headers
  179. if self.STRICT_PROTOCOL_NEGOTIATION:
  180. raise ConnectionDeny(ConnectionDeny.BAD_REQUEST, 'This server only speaks WebSocket subprotocols {}'.format(
  181. ', '.join(self.factory.protocols)))
  182. else:
  183. # assume wamp.2.json (but do not announce/select it)
  184. self._serializer = copy.copy(self.factory._serializers['json'])
  185. return None, headers
  186. class WampWebSocketClientProtocol(WampWebSocketProtocol):
  187. """
  188. Mixin for WAMP-over-WebSocket client transports.
  189. """
  190. STRICT_PROTOCOL_NEGOTIATION = True
  191. def onConnect(self, response: ConnectionResponse):
  192. """
  193. Callback from :func:`autobahn.websocket.interfaces.IWebSocketChannel.onConnect`
  194. """
  195. if response.protocol not in self.factory.protocols:
  196. if self.STRICT_PROTOCOL_NEGOTIATION:
  197. raise Exception('The server does not speak any of the WebSocket subprotocols {} we requested.'.format(
  198. ', '.join(self.factory.protocols)))
  199. else:
  200. # assume wamp.2.json
  201. serializer_id = 'json'
  202. else:
  203. version, serializer_id = parseSubprotocolIdentifier(response.protocol)
  204. # copy over serializer form factory, so that we keep per-session serializer stats
  205. self._serializer = copy.copy(self.factory._serializers[serializer_id])
  206. class WampWebSocketFactory(object):
  207. """
  208. Base class for WAMP-over-WebSocket transport factory mixins.
  209. """
  210. def __init__(self, factory, serializers=None):
  211. """
  212. :param factory: A callable that produces instances that implement
  213. :class:`autobahn.wamp.interfaces.ITransportHandler`
  214. :type factory: callable
  215. :param serializers: A list of WAMP serializers to use (or None for default
  216. serializers). Serializers must implement
  217. :class:`autobahn.wamp.interfaces.ISerializer`.
  218. :type serializers: list
  219. """
  220. if callable(factory):
  221. self._factory = factory
  222. else:
  223. self._factory = lambda: factory
  224. if serializers is None:
  225. serializers = []
  226. # try CBOR WAMP serializer
  227. try:
  228. from autobahn.wamp.serializer import CBORSerializer
  229. serializers.append(CBORSerializer(batched=True))
  230. serializers.append(CBORSerializer())
  231. except ImportError:
  232. pass
  233. # try MsgPack WAMP serializer
  234. try:
  235. from autobahn.wamp.serializer import MsgPackSerializer
  236. serializers.append(MsgPackSerializer(batched=True))
  237. serializers.append(MsgPackSerializer())
  238. except ImportError:
  239. pass
  240. # try UBJSON WAMP serializer
  241. try:
  242. from autobahn.wamp.serializer import UBJSONSerializer
  243. serializers.append(UBJSONSerializer(batched=True))
  244. serializers.append(UBJSONSerializer())
  245. except ImportError:
  246. pass
  247. # try JSON WAMP serializer
  248. try:
  249. from autobahn.wamp.serializer import JsonSerializer
  250. serializers.append(JsonSerializer(batched=True))
  251. serializers.append(JsonSerializer())
  252. except ImportError:
  253. pass
  254. if not serializers:
  255. raise Exception('Could not import any WAMP serializer')
  256. self._serializers = {}
  257. for ser in serializers:
  258. self._serializers[ser.SERIALIZER_ID] = ser
  259. self._protocols = ['wamp.2.{}'.format(ser.SERIALIZER_ID) for ser in serializers]
  260. class WampWebSocketServerFactory(WampWebSocketFactory):
  261. """
  262. Mixin for WAMP-over-WebSocket server transport factories.
  263. """
  264. class WampWebSocketClientFactory(WampWebSocketFactory):
  265. """
  266. Mixin for WAMP-over-WebSocket client transport factories.
  267. """