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.

request.py 9.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. __all__ = (
  27. 'Publication',
  28. 'Subscription',
  29. 'Handler',
  30. 'Registration',
  31. 'Endpoint',
  32. 'PublishRequest',
  33. 'SubscribeRequest',
  34. 'UnsubscribeRequest',
  35. 'CallRequest',
  36. 'InvocationRequest',
  37. 'RegisterRequest',
  38. 'UnregisterRequest',
  39. )
  40. class Publication(object):
  41. """
  42. Object representing a publication (feedback from publishing an event when doing
  43. an acknowledged publish).
  44. """
  45. __slots__ = ('id', 'was_encrypted')
  46. def __init__(self, publication_id, was_encrypted):
  47. """
  48. :param publication_id: The publication ID of the published event.
  49. :type publication_id: int
  50. :param was_encrypted: Flag indicating whether the app payload was encrypted.
  51. :type was_encrypted: bool
  52. """
  53. self.id = publication_id
  54. self.was_encrypted = was_encrypted
  55. def __str__(self):
  56. return "Publication(id={0}, was_encrypted={1})".format(self.id, self.was_encrypted)
  57. class Subscription(object):
  58. """
  59. Object representing a handler subscription.
  60. """
  61. __slots__ = ('id', 'topic', 'active', 'session', 'handler')
  62. def __init__(self, subscription_id, topic, session, handler):
  63. """
  64. :param subscription_id: The subscription ID.
  65. :type subscription_id: int
  66. :param topic: The subscription URI or URI pattern.
  67. :type topic: str
  68. :param session: The ApplicationSession this subscription is living on.
  69. :type session: instance of ApplicationSession
  70. :param handler: The user event callback.
  71. :type handler: callable
  72. """
  73. self.id = subscription_id
  74. self.topic = topic
  75. self.active = True
  76. self.session = session
  77. self.handler = handler
  78. def unsubscribe(self):
  79. """
  80. Unsubscribe this subscription.
  81. """
  82. if self.active:
  83. return self.session._unsubscribe(self)
  84. else:
  85. raise Exception("subscription no longer active")
  86. def __str__(self):
  87. return "Subscription(id={0}, is_active={1})".format(self.id, self.active)
  88. class Handler(object):
  89. """
  90. Object representing an event handler attached to a subscription.
  91. """
  92. __slots__ = ('fn', 'obj', 'details_arg')
  93. def __init__(self, fn, obj=None, details_arg=None):
  94. """
  95. :param fn: The event handler function to be called.
  96. :type fn: callable
  97. :param obj: The (optional) object upon which to call the function.
  98. :type obj: obj or None
  99. :param details_arg: The keyword argument under which event details should be provided.
  100. :type details_arg: str or None
  101. """
  102. self.fn = fn
  103. self.obj = obj
  104. self.details_arg = details_arg
  105. class Registration(object):
  106. """
  107. Object representing a registration.
  108. """
  109. __slots__ = ('id', 'active', 'session', 'procedure', 'endpoint')
  110. def __init__(self, session, registration_id, procedure, endpoint):
  111. """
  112. :param id: The registration ID.
  113. :type id: int
  114. :param active: Flag indicating whether this registration is active.
  115. :type active: bool
  116. :param procedure: The procedure URI or URI pattern.
  117. :type procedure: callable
  118. :param endpoint: The user callback.
  119. :type endpoint: callable
  120. """
  121. self.id = registration_id
  122. self.active = True
  123. self.session = session
  124. self.procedure = procedure
  125. self.endpoint = endpoint
  126. def unregister(self):
  127. """
  128. """
  129. if self.active:
  130. return self.session._unregister(self)
  131. else:
  132. raise Exception("registration no longer active")
  133. def __str__(self):
  134. return 'Registration(id={0}, is_active={1}, procedure="{2}")'.format(self.id, self.active, self.procedure)
  135. class Endpoint(object):
  136. """
  137. Object representing an procedure endpoint attached to a registration.
  138. """
  139. __slots__ = ('fn', 'obj', 'details_arg')
  140. def __init__(self, fn, obj=None, details_arg=None):
  141. """
  142. :param fn: The endpoint procedure to be called.
  143. :type fn: callable
  144. :param obj: The (optional) object upon which to call the function.
  145. :type obj: obj or None
  146. :param details_arg: The keyword argument under which call details should be provided.
  147. :type details_arg: str or None
  148. """
  149. self.fn = fn
  150. self.obj = obj
  151. self.details_arg = details_arg
  152. class Request(object):
  153. """
  154. Object representing an outstanding request, such as for subscribe/unsubscribe,
  155. register/unregister or call/publish.
  156. """
  157. __slots__ = ('request_id', 'on_reply')
  158. def __init__(self, request_id, on_reply):
  159. """
  160. :param request_id: The WAMP request ID.
  161. :type request_id: int
  162. :param on_reply: The Deferred/Future to be fired when the request returns.
  163. :type on_reply: Deferred/Future
  164. """
  165. self.request_id = request_id
  166. self.on_reply = on_reply
  167. class PublishRequest(Request):
  168. """
  169. Object representing an outstanding request to publish (acknowledged) an event.
  170. """
  171. __slots__ = ('was_encrypted')
  172. def __init__(self, request_id, on_reply, was_encrypted):
  173. """
  174. :param request_id: The WAMP request ID.
  175. :type request_id: int
  176. :param on_reply: The Deferred/Future to be fired when the request returns.
  177. :type on_reply: Deferred/Future
  178. :param was_encrypted: Flag indicating whether the app payload was encrypted.
  179. :type was_encrypted: bool
  180. """
  181. Request.__init__(self, request_id, on_reply)
  182. self.was_encrypted = was_encrypted
  183. class SubscribeRequest(Request):
  184. """
  185. Object representing an outstanding request to subscribe to a topic.
  186. """
  187. __slots__ = ('handler', 'topic')
  188. def __init__(self, request_id, topic, on_reply, handler):
  189. """
  190. :param request_id: The WAMP request ID.
  191. :type request_id: int
  192. :param topic: The topic URI being subscribed to.
  193. :type topic: unicode
  194. :param on_reply: The Deferred/Future to be fired when the request returns.
  195. :type on_reply: Deferred/Future
  196. :param handler: WAMP call options that are in use for this call.
  197. :type handler: callable
  198. """
  199. Request.__init__(self, request_id, on_reply)
  200. self.topic = topic
  201. self.handler = handler
  202. class UnsubscribeRequest(Request):
  203. """
  204. Object representing an outstanding request to unsubscribe a subscription.
  205. """
  206. __slots__ = ('subscription_id',)
  207. def __init__(self, request_id, on_reply, subscription_id):
  208. """
  209. """
  210. Request.__init__(self, request_id, on_reply)
  211. self.subscription_id = subscription_id
  212. class CallRequest(Request):
  213. """
  214. Object representing an outstanding request to call a procedure.
  215. """
  216. __slots__ = ('procedure', 'options',)
  217. def __init__(self, request_id, procedure, on_reply, options):
  218. """
  219. :param request_id: The WAMP request ID.
  220. :type request_id: int
  221. :param on_reply: The Deferred/Future to be fired when the request returns.
  222. :type on_reply: Deferred/Future
  223. :param options: WAMP call options that are in use for this call.
  224. :type options: dict
  225. """
  226. Request.__init__(self, request_id, on_reply)
  227. self.procedure = procedure
  228. self.options = options
  229. class InvocationRequest(Request):
  230. """
  231. Object representing an outstanding request to invoke an endpoint.
  232. """
  233. class RegisterRequest(Request):
  234. """
  235. Object representing an outstanding request to register a procedure.
  236. """
  237. __slots__ = ('procedure', 'endpoint',)
  238. def __init__(self, request_id, on_reply, procedure, endpoint):
  239. """
  240. """
  241. Request.__init__(self, request_id, on_reply)
  242. self.procedure = procedure
  243. self.endpoint = endpoint
  244. class UnregisterRequest(Request):
  245. """
  246. Object representing an outstanding request to unregister a registration.
  247. """
  248. __slots__ = ('registration_id',)
  249. def __init__(self, request_id, on_reply, registration_id):
  250. """
  251. """
  252. Request.__init__(self, request_id, on_reply)
  253. self.registration_id = registration_id