def test_ctor(self): e = message.Result(123456) msg = e.marshal() self.assertEqual(len(msg), 3) self.assertEqual(msg[0], message.Result.MESSAGE_TYPE) self.assertEqual(msg[1], 123456) self.assertEqual(msg[2], {}) e = message.Result(123456, args=[1, 2, 3], kwargs={ u'foo': 23, u'bar': u'hello' }) msg = e.marshal() self.assertEqual(len(msg), 5) self.assertEqual(msg[0], message.Result.MESSAGE_TYPE) self.assertEqual(msg[1], 123456) self.assertEqual(msg[2], {}) self.assertEqual(msg[3], [1, 2, 3]) self.assertEqual(msg[4], {u'foo': 23, u'bar': u'hello'}) e = message.Result(123456, progress=True) msg = e.marshal() self.assertEqual(len(msg), 3) self.assertEqual(msg[0], message.Result.MESSAGE_TYPE) self.assertEqual(msg[1], 123456) self.assertEqual(msg[2], {u'progress': True})
def processYield(self, session, yield_): """ Implements :func:`crossbar.router.interfaces.IDealer.processYield` """ # assert(session in self._session_to_registrations) if yield_.request in self._invocations: # get the invocation request tracked for the caller # invocation_request = self._invocations[yield_.request] is_valid = True if yield_.payload is None: # validate normal args/kwargs payload try: self._router.validate('call_result', invocation_request.call.procedure, yield_.args, yield_.kwargs) except Exception as e: is_valid = False reply = message.Error(message.Call.MESSAGE_TYPE, invocation_request.call.request, ApplicationError.INVALID_ARGUMENT, [u"call result from procedure '{0}' with invalid application payload: {1}".format(invocation_request.call.procedure, e)]) else: reply = message.Result(invocation_request.call.request, args=yield_.args, kwargs=yield_.kwargs, progress=yield_.progress) else: reply = message.Result(invocation_request.call.request, payload=yield_.payload, progress=yield_.progress, enc_algo=yield_.enc_algo, enc_key=yield_.enc_key, enc_serializer=yield_.enc_serializer) # the calling session might have been lost in the meantime .. # if invocation_request.caller._transport: self._router.send(invocation_request.caller, reply) # the call is done if it's a regular call (non-progressive) or if the payload was invalid # if not yield_.progress or not is_valid: callee_extra = invocation_request.registration.observers_extra.get(session, None) if callee_extra: callee_extra.concurrency_current -= 1 self._remove_invoke_request(invocation_request) # check for any calls queued on the registration for which an # invocation just returned, and hence there is likely concurrency # free again to actually forward calls previously queued calls # that were queued because no callee endpoint concurrency was free if self._call_store: queued_call = self._call_store.get_queued_call(invocation_request.registration) if queued_call: invocation_sent = self._call(queued_call.session, queued_call.call, queued_call.registration, queued_call.authorization, True) # only actually pop the queued call when we really were # able to forward the call now if invocation_sent: self._call_store.pop_queued_call(invocation_request.registration) else: raise ProtocolError(u"Dealer.onYield(): YIELD received for non-pending request ID {0}".format(yield_.request))
def send(self, msg): if self._log: payload, isbinary = self._serializer.serialize(msg) print("Send: {}".format(payload)) reply = None if isinstance(msg, message.Publish): if msg.topic.startswith(u'com.myapp'): if msg.acknowledge: reply = message.Published(msg.request, util.id()) elif len(msg.topic) == 0: reply = message.Error(message.Publish.MESSAGE_TYPE, msg.request, u'wamp.error.invalid_uri') else: reply = message.Error(message.Publish.MESSAGE_TYPE, msg.request, u'wamp.error.not_authorized') elif isinstance(msg, message.Call): if msg.procedure == u'com.myapp.procedure1': reply = message.Result(msg.request, args = [100]) elif msg.procedure == u'com.myapp.procedure2': reply = message.Result(msg.request, args = [1, 2, 3]) elif msg.procedure == u'com.myapp.procedure3': reply = message.Result(msg.request, args = [1, 2, 3], kwargs = {u'foo': u'bar', u'baz': 23}) elif msg.procedure.startswith(u'com.myapp.myproc'): registration = self._registrations[msg.procedure] request = util.id() self._invocations[request] = msg.request reply = message.Invocation(request, registration, args = msg.args, kwargs = msg.kwargs) else: reply = message.Error(message.Call.MESSAGE_TYPE, msg.request, u'wamp.error.no_such_procedure') elif isinstance(msg, message.Yield): if self._invocations.has_key(msg.request): request = self._invocations[msg.request] reply = message.Result(request, args = msg.args, kwargs = msg.kwargs) elif isinstance(msg, message.Subscribe): reply = message.Subscribed(msg.request, util.id()) elif isinstance(msg, message.Unsubscribe): reply = message.Unsubscribed(msg.request) elif isinstance(msg, message.Register): registration = util.id() self._registrations[msg.procedure] = registration reply = message.Registered(msg.request, registration) elif isinstance(msg, message.Unregister): reply = message.Unregistered(msg.request) if reply: if self._log: payload, isbinary = self._serializer.serialize(reply) print("Receive: {}".format(payload)) self._handler.onMessage(reply)
def processYield(self, session, yield_): """ Implements :func:`crossbar.router.interfaces.IDealer.processYield` """ # assert(session in self._session_to_registrations) if yield_.request in self._invocations: # get the invocation request tracked for the caller # invocation_request = self._invocations[yield_.request] is_valid = True if yield_.payload is None: # validate normal args/kwargs payload try: self._router.validate('call_result', invocation_request.call.procedure, yield_.args, yield_.kwargs) except Exception as e: is_valid = False reply = message.Error( message.Call.MESSAGE_TYPE, invocation_request.call.request, ApplicationError.INVALID_ARGUMENT, [ u"call result from procedure '{0}' with invalid application payload: {1}" .format(invocation_request.call.procedure, e) ]) else: reply = message.Result(invocation_request.call.request, args=yield_.args, kwargs=yield_.kwargs, progress=yield_.progress) else: reply = message.Result(invocation_request.call.request, payload=yield_.payload, progress=yield_.progress, enc_algo=yield_.enc_algo, enc_key=yield_.enc_key, enc_serializer=yield_.enc_serializer) # the calling session might have been lost in the meantime .. # if invocation_request.caller._transport: self._router.send(invocation_request.caller, reply) # the call is done if it's a regular call (non-progressive) or if the payload was invalid # if not yield_.progress or not is_valid: del self._invocations[yield_.request] else: raise ProtocolError( u"Dealer.onYield(): YIELD received for non-pending request ID {0}" .format(yield_.request))
def generate_test_messages(): """ List of WAMP test message used for serializers. Expand this if you add more options or messages. This list of WAMP message does not contain any binary app payloads! """ msgs = [ message.Hello(u"realm1", {u'subscriber': role.RoleSubscriberFeatures()}), message.Goodbye(), message.Yield(123456), message.Yield(123456, args=[1, 2, 3], kwargs={u'foo': 23, u'bar': u'hello'}), message.Yield(123456, args=[u'hello']), message.Yield(123456, progress=True), message.Interrupt(123456), message.Interrupt(123456, mode=message.Interrupt.KILL), message.Invocation(123456, 789123), message.Invocation(123456, 789123, args=[1, 2, 3], kwargs={u'foo': 23, u'bar': u'hello'}), message.Invocation(123456, 789123, timeout=10000), message.Result(123456), message.Result(123456, args=[1, 2, 3], kwargs={u'foo': 23, u'bar': u'hello'}), message.Result(123456, progress=True), message.Cancel(123456), message.Cancel(123456, mode=message.Cancel.KILL), message.Call(123456, u'com.myapp.procedure1'), message.Call(123456, u'com.myapp.procedure1', args=[1, 2, 3], kwargs={u'foo': 23, u'bar': u'hello'}), message.Call(123456, u'com.myapp.procedure1', timeout=10000), message.Unregistered(123456), message.Unregister(123456, 789123), message.Registered(123456, 789123), message.Register(123456, u'com.myapp.procedure1'), message.Register(123456, u'com.myapp.procedure1', match=u'prefix'), message.Register(123456, u'com.myapp.procedure1', invoke=u'roundrobin'), message.Event(123456, 789123), message.Event(123456, 789123, args=[1, 2, 3], kwargs={u'foo': 23, u'bar': u'hello'}), message.Event(123456, 789123, publisher=300), message.Published(123456, 789123), message.Publish(123456, u'com.myapp.topic1'), message.Publish(123456, u'com.myapp.topic1', args=[1, 2, 3], kwargs={u'foo': 23, u'bar': u'hello'}), message.Publish(123456, u'com.myapp.topic1', exclude_me=False, exclude=[300], eligible=[100, 200, 300]), message.Unsubscribed(123456), message.Unsubscribe(123456, 789123), message.Subscribed(123456, 789123), message.Subscribe(123456, u'com.myapp.topic1'), message.Subscribe(123456, u'com.myapp.topic1', match=message.Subscribe.MATCH_PREFIX), message.Error(message.Call.MESSAGE_TYPE, 123456, u'com.myapp.error1'), message.Error(message.Call.MESSAGE_TYPE, 123456, u'com.myapp.error1', args=[1, 2, 3], kwargs={u'foo': 23, u'bar': u'hello'}), message.Call(123456, u'com.myapp.\u4f60\u597d\u4e16\u754c', args=[1, 2, 3]), message.Result(123456, args=[1, 2, 3], kwargs={u'en': u'Hello World', u'jp': u'\u3053\u3093\u306b\u3061\u306f\u4e16\u754c'}) ] return [(False, msg) for msg in msgs]
def processYield(self, session, yield_): """ Implements :func:`autobahn.wamp.interfaces.IDealer.processYield` """ assert(session in self._session_to_registrations) if yield_.request in self._invocations: ## get original call message and calling session ## call_msg, call_session = self._invocations[yield_.request] ## validate payload ## is_valid = True try: self._router.validate('call_result', call_msg.procedure, yield_.args, yield_.kwargs) except Exception as e: is_valid = False reply = message.Error(message.Call.MESSAGE_TYPE, call_msg.request, ApplicationError.INVALID_ARGUMENT, ["call result from procedure '{0}' with invalid application payload: {1}".format(call_msg.procedure, e)]) else: reply = message.Result(call_msg.request, args = yield_.args, kwargs = yield_.kwargs, progress = yield_.progress) ## the calling session might have been lost in the meantime .. ## if call_session._transport: call_session._transport.send(reply) ## the call is done if it's a regular call (non-progressive) or if the payload was invalid ## if not yield_.progress or not is_valid: del self._invocations[yield_.request] else: raise ProtocolError("Dealer.onYield(): YIELD received for non-pending request ID {0}".format(yield_.request))
def generate_test_messages(): return [ message.Hello(u"realm1", [role.RoleBrokerFeatures()]), message.Goodbye(), message.Heartbeat(123, 456), message.Yield(123456), message.Yield(123456, args = [1, 2, 3], kwargs = {u'foo': 23, u'bar': u'hello'}), message.Yield(123456, progress = True), message.Interrupt(123456), message.Interrupt(123456, mode = message.Interrupt.KILL), message.Invocation(123456, 789123), message.Invocation(123456, 789123, args = [1, 2, 3], kwargs = {u'foo': 23, u'bar': u'hello'}), message.Invocation(123456, 789123, timeout = 10000), message.Result(123456), message.Result(123456, args = [1, 2, 3], kwargs = {u'foo': 23, u'bar': u'hello'}), message.Result(123456, progress = True), message.Cancel(123456), message.Cancel(123456, mode = message.Cancel.KILL), message.Call(123456, u'com.myapp.procedure1'), message.Call(123456, u'com.myapp.procedure1', args = [1, 2, 3], kwargs = {u'foo': 23, u'bar': u'hello'}), message.Call(123456, u'com.myapp.procedure1', timeout = 10000), message.Unregistered(123456), message.Unregister(123456, 789123), message.Registered(123456, 789123), message.Register(123456, u'com.myapp.procedure1'), message.Register(123456, u'com.myapp.procedure1', pkeys = [10, 11, 12]), message.Event(123456, 789123), message.Event(123456, 789123, args = [1, 2, 3], kwargs = {u'foo': 23, u'bar': u'hello'}), message.Event(123456, 789123, publisher = 300), message.Published(123456, 789123), message.Publish(123456, u'com.myapp.topic1'), message.Publish(123456, u'com.myapp.topic1', args = [1, 2, 3], kwargs = {u'foo': 23, u'bar': u'hello'}), message.Publish(123456, u'com.myapp.topic1', excludeMe = False, exclude = [300], eligible = [100, 200, 300], discloseMe = True), message.Unsubscribed(123456), message.Unsubscribe(123456, 789123), message.Subscribed(123456, 789123), message.Subscribe(123456, u'com.myapp.topic1'), message.Subscribe(123456, u'com.myapp.topic1', match = message.Subscribe.MATCH_PREFIX), message.Error(message.Call.MESSAGE_TYPE, 123456, u'com.myapp.error1'), message.Error(message.Call.MESSAGE_TYPE, 123456, u'com.myapp.error1', args = [1, 2, 3], kwargs = {u'foo': 23, u'bar': u'hello'}), message.Call(123456, u'com.myapp.\u4f60\u597d\u4e16\u754c', args=[1, 2, 3]), message.Result(123456, args=[1, 2, 3], kwargs={u'en': u'Hello World', u'jp': u'\u3053\u3093\u306b\u3061\u306f\u4e16\u754c'}) ]
def processYield(self, session, yield_): """ Implements :func:`autobahn.wamp.interfaces.IDealer.processYield` """ assert (session in self._session_to_registrations) if yield_.request in self._invocations: call_msg, call_session = self._invocations[yield_.request] msg = message.Result(call_msg.request, args=yield_.args, kwargs=yield_.kwargs, progress=yield_.progress) ## the calling session might have been lost in the meantime .. if call_session._transport: call_session._transport.send(msg) if not yield_.progress: del self._invocations[yield_.request] else: raise ProtocolError( "Dealer.onYield(): YIELD received for non-pending request ID {}" .format(yield_.request))
def send(self, msg): if self._log: payload, isbinary = self._serializer.serialize(msg) print("Send: {0}".format(payload)) reply = None if isinstance(msg, message.Publish): if msg.topic.startswith(u'com.myapp'): if msg.acknowledge: reply = message.Published( msg.request, self._fake_router_session._request_id_gen.next()) elif len(msg.topic) == 0: reply = message.Error(message.Publish.MESSAGE_TYPE, msg.request, u'wamp.error.invalid_uri') else: reply = message.Error(message.Publish.MESSAGE_TYPE, msg.request, u'wamp.error.not_authorized') elif isinstance(msg, message.Call): if msg.procedure == u'com.myapp.procedure1': reply = message.Result(msg.request, args=[100]) elif msg.procedure == u'com.myapp.procedure2': reply = message.Result(msg.request, args=[1, 2, 3]) elif msg.procedure == u'com.myapp.procedure3': reply = message.Result(msg.request, args=[1, 2, 3], kwargs={ u'foo': u'bar', u'baz': 23 }) elif msg.procedure.startswith(u'com.myapp.myproc'): registration = self._registrations[msg.procedure] request = self._fake_router_session._request_id_gen.next() if request in self._invocations: raise ProtocolError("duplicate invocation") self._invocations[request] = msg.request reply = message.Invocation( request, registration, args=msg.args, kwargs=msg.kwargs, receive_progress=msg.receive_progress, ) else: reply = message.Error(message.Call.MESSAGE_TYPE, msg.request, u'wamp.error.no_such_procedure') elif isinstance(msg, message.Yield): if msg.request in self._invocations: request = self._invocations[msg.request] reply = message.Result(request, args=msg.args, kwargs=msg.kwargs, progress=msg.progress) elif isinstance(msg, message.Subscribe): topic = msg.topic if topic in self._subscription_topics: reply_id = self._subscription_topics[topic] else: reply_id = self._fake_router_session._request_id_gen.next() self._subscription_topics[topic] = reply_id reply = message.Subscribed(msg.request, reply_id) elif isinstance(msg, message.Unsubscribe): reply = message.Unsubscribed(msg.request) elif isinstance(msg, message.Register): registration = self._fake_router_session._request_id_gen.next() self._registrations[msg.procedure] = registration reply = message.Registered(msg.request, registration) elif isinstance(msg, message.Unregister): reply = message.Unregistered(msg.request) elif isinstance(msg, message.Error): # since I'm basically a Dealer, I forward on the # error, but reply to my own request/invocation request = self._invocations[msg.request] reply = message.Error( message.Call.MESSAGE_TYPE, request, msg.error, args=msg.args, kwargs=msg.kwargs, ) if reply: if self._log: payload, isbinary = self._serializer.serialize(reply) print("Receive: {0}".format(payload)) self._handler.onMessage(reply)
def generate_test_messages(): """ List of WAMP test message used for serializers. Expand this if you add more options or messages. This list of WAMP message does not contain any binary app payloads! """ some_bytes = os.urandom(32) some_unicode = '\u3053\u3093\u306b\u3061\u306f\u4e16\u754c' some_uri = 'com.myapp.foobar' some_unicode_uri = 'com.myapp.\u4f60\u597d\u4e16\u754c.baz' some_args = [1, 2, 3, 'hello', some_bytes, some_unicode, {'foo': 23, 'bar': 'hello', 'baz': some_bytes, 'moo': some_unicode}] some_kwargs = {'foo': 23, 'bar': 'hello', 'baz': some_bytes, 'moo': some_unicode, 'arr': some_args} msgs = [ message.Hello("realm1", {'subscriber': role.RoleSubscriberFeatures()}), message.Hello("realm1", {'publisher': role.RolePublisherFeatures()}), message.Hello("realm1", {'caller': role.RoleCallerFeatures()}), message.Hello("realm1", {'callee': role.RoleCalleeFeatures()}), message.Hello("realm1", { 'subscriber': role.RoleSubscriberFeatures(), 'publisher': role.RolePublisherFeatures(), 'caller': role.RoleCallerFeatures(), 'callee': role.RoleCalleeFeatures(), }), message.Goodbye(), message.Yield(123456), message.Yield(123456, args=some_args), message.Yield(123456, args=[], kwargs=some_kwargs), message.Yield(123456, args=some_args, kwargs=some_kwargs), message.Yield(123456, progress=True), message.Interrupt(123456), message.Interrupt(123456, mode=message.Interrupt.KILL), message.Invocation(123456, 789123), message.Invocation(123456, 789123, args=some_args), message.Invocation(123456, 789123, args=[], kwargs=some_kwargs), message.Invocation(123456, 789123, args=some_args, kwargs=some_kwargs), message.Invocation(123456, 789123, timeout=10000), message.Result(123456), message.Result(123456, args=some_args), message.Result(123456, args=[], kwargs=some_kwargs), message.Result(123456, args=some_args, kwargs=some_kwargs), message.Result(123456, progress=True), message.Cancel(123456), message.Cancel(123456, mode=message.Cancel.KILL), message.Call(123456, some_uri), message.Call(123456, some_uri, args=some_args), message.Call(123456, some_uri, args=[], kwargs=some_kwargs), message.Call(123456, some_uri, args=some_args, kwargs=some_kwargs), message.Call(123456, some_uri, timeout=10000), message.Call(123456, some_unicode_uri), message.Call(123456, some_unicode_uri, args=some_args), message.Call(123456, some_unicode_uri, args=[], kwargs=some_kwargs), message.Call(123456, some_unicode_uri, args=some_args, kwargs=some_kwargs), message.Call(123456, some_unicode_uri, timeout=10000), message.Unregistered(123456), message.Unregister(123456, 789123), message.Registered(123456, 789123), message.Register(123456, some_uri), message.Register(123456, some_uri, match='prefix'), message.Register(123456, some_uri, invoke='roundrobin'), message.Register(123456, some_unicode_uri), message.Register(123456, some_unicode_uri, match='prefix'), message.Register(123456, some_unicode_uri, invoke='roundrobin'), message.Event(123456, 789123), message.Event(123456, 789123, args=some_args), message.Event(123456, 789123, args=[], kwargs=some_kwargs), message.Event(123456, 789123, args=some_args, kwargs=some_kwargs), message.Event(123456, 789123, publisher=300), message.Published(123456, 789123), message.Publish(123456, some_uri), message.Publish(123456, some_uri, args=some_args), message.Publish(123456, some_uri, args=[], kwargs=some_kwargs), message.Publish(123456, some_uri, args=some_args, kwargs=some_kwargs), message.Publish(123456, some_uri, exclude_me=False, exclude=[300], eligible=[100, 200, 300]), message.Publish(123456, some_unicode_uri), message.Publish(123456, some_unicode_uri, args=some_args), message.Publish(123456, some_unicode_uri, args=[], kwargs=some_kwargs), message.Publish(123456, some_unicode_uri, args=some_args, kwargs=some_kwargs), message.Publish(123456, some_unicode_uri, exclude_me=False, exclude=[300], eligible=[100, 200, 300]), message.Unsubscribed(123456), message.Unsubscribe(123456, 789123), message.Subscribed(123456, 789123), message.Subscribe(123456, some_uri), message.Subscribe(123456, some_uri, match=message.Subscribe.MATCH_PREFIX), message.Subscribe(123456, some_unicode_uri), message.Subscribe(123456, some_unicode_uri, match=message.Subscribe.MATCH_PREFIX), message.Error(message.Call.MESSAGE_TYPE, 123456, some_uri), message.Error(message.Call.MESSAGE_TYPE, 123456, some_uri, args=some_args), message.Error(message.Call.MESSAGE_TYPE, 123456, some_uri, args=[], kwargs=some_kwargs), message.Error(message.Call.MESSAGE_TYPE, 123456, some_uri, args=some_args, kwargs=some_kwargs), message.Error(message.Call.MESSAGE_TYPE, 123456, some_unicode_uri), message.Error(message.Call.MESSAGE_TYPE, 123456, some_unicode_uri, args=some_args), message.Error(message.Call.MESSAGE_TYPE, 123456, some_unicode_uri, args=[], kwargs=some_kwargs), message.Error(message.Call.MESSAGE_TYPE, 123456, some_unicode_uri, args=some_args, kwargs=some_kwargs), message.Result(123456), message.Result(123456, args=some_args), message.Result(123456, args=some_args, kwargs=some_kwargs), ] return [(False, msg) for msg in msgs]
def send(self, msg): if self._log: print("req") print(msg) reply = None if isinstance(msg, message.Publish): if msg.topic in self._subscription_topics.keys(): pubID = util.id() def published(): self._s(message.Published(msg.request, pubID)) reg = self._subscription_topics[msg.topic] reply = message.Event(reg, pubID, args=msg.args, kwargs=msg.kwargs) if msg.acknowledge: reactor.callLater(0, published) elif len(msg.topic) == 0: reply = message.Error(message.Publish.MESSAGE_TYPE, msg.request, u'wamp.error.invalid_uri') else: reply = message.Error(message.Publish.MESSAGE_TYPE, msg.request, u'wamp.error.not_authorized') elif isinstance(msg, message.Error): # Convert an invocation error into a call error if msg.request_type == 68: msg.request_type = 48 reply = msg elif isinstance(msg, message.Call): if msg.procedure in self._registrations: request = util.id() registration = self._registrations[msg.procedure] self._invocations[msg.request] = msg.request def invoke(): self._s(message.Invocation(msg.request, registration, args=msg.args, kwargs=msg.kwargs)) reactor.callLater(0, invoke) else: reply = message.Error(message.Call.MESSAGE_TYPE, msg.request, u'wamp.error.no_such_procedure') elif isinstance(msg, message.Yield): if msg.request in self._invocations: request = self._invocations[msg.request] reply = message.Result(request, args=msg.args, kwargs=msg.kwargs) elif isinstance(msg, message.Subscribe): topic = msg.topic if topic in self._subscription_topics: reply_id = self._subscription_topics[topic] else: reply_id = util.id() self._subscription_topics[topic] = reply_id reply = message.Subscribed(msg.request, reply_id) elif isinstance(msg, message.Unsubscribe): reply = message.Unsubscribed(msg.request) elif isinstance(msg, message.Register): registration = util.id() self._registrations[msg.procedure] = registration reply = message.Registered(msg.request, registration) elif isinstance(msg, message.Unregister): reply = message.Unregistered(msg.request) if reply: self._s(reply)