Пример #1
0
   def test_ctor(self):
      e = message.Invocation(123456, 789123)
      msg = e.marshal()
      self.assertEqual(len(msg), 4)
      self.assertEqual(msg[0], message.Invocation.MESSAGE_TYPE)
      self.assertEqual(msg[1], 123456)
      self.assertEqual(msg[2], 789123)
      self.assertEqual(msg[3], {})

      e = message.Invocation(123456, 789123, args = [1, 2, 3], kwargs = {u'foo': 23,  u'bar':  u'hello'})
      msg = e.marshal()
      self.assertEqual(len(msg), 6)
      self.assertEqual(msg[0], message.Invocation.MESSAGE_TYPE)
      self.assertEqual(msg[1], 123456)
      self.assertEqual(msg[2], 789123)
      self.assertEqual(msg[3], {})
      self.assertEqual(msg[4], [1, 2, 3])
      self.assertEqual(msg[5], {u'foo': 23,  u'bar':  u'hello'})

      e = message.Invocation(123456, 789123, timeout = 10000)
      msg = e.marshal()
      self.assertEqual(len(msg), 4)
      self.assertEqual(msg[0], message.Invocation.MESSAGE_TYPE)
      self.assertEqual(msg[1], 123456)
      self.assertEqual(msg[2], 789123)
      self.assertEqual(msg[3], {u'timeout': 10000})
Пример #2
0
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]
Пример #3
0
    def processCall(self, session, call):
        """
      Implements :func:`autobahn.wamp.interfaces.IDealer.processCall`
      """
        assert (session in self._session_to_registrations)

        ## check procedure URI
        ##
        if (not self._option_uri_strict and not  _URI_PAT_LOOSE_NON_EMPTY.match(call.procedure)) or \
           (    self._option_uri_strict and not _URI_PAT_STRICT_NON_EMPTY.match(call.procedure)):

            reply = message.Error(
                message.Register.MESSAGE_TYPE, call.request,
                ApplicationError.INVALID_URI, [
                    "call with invalid procedure URI '{}'".format(
                        call.procedure)
                ])
            session._transport.send(reply)

        else:

            if call.procedure in self._procs_to_regs:
                registration_id, endpoint_session, discloseCaller = self._procs_to_regs[
                    call.procedure]

                request_id = util.id()

                if discloseCaller or call.discloseMe:
                    caller = session._session_id
                    authid = session._authid
                    authrole = session._authrole
                    authmethod = session._authmethod
                else:
                    caller = None
                    authid = None
                    authrole = None
                    authmethod = None

                invocation = message.Invocation(
                    request_id,
                    registration_id,
                    args=call.args,
                    kwargs=call.kwargs,
                    timeout=call.timeout,
                    receive_progress=call.receive_progress,
                    caller=caller,
                    authid=authid,
                    authrole=authrole,
                    authmethod=authmethod)

                self._invocations[request_id] = (call, session)
                endpoint_session._transport.send(invocation)
            else:
                reply = message.Error(
                    message.Call.MESSAGE_TYPE, call.request,
                    ApplicationError.NO_SUCH_PROCEDURE,
                    ["no procedure '{}' registered".format(call.procedure)])
                session._transport.send(reply)
Пример #4
0
      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)
Пример #5
0
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'})
   ]
Пример #6
0
                def on_authorize_success(authorized):
                    if authorized:
                        registration_id, endpoint_session, discloseCaller, discloseCallerTransport = self._procs_to_regs[
                            call.procedure]

                        request_id = util.id()

                        if discloseCaller or call.discloseMe:
                            caller = session._session_id
                            caller_transport = None
                            authid = session._authid
                            authrole = session._authrole
                            authmethod = session._authmethod
                            if discloseCallerTransport and hasattr(
                                    session._transport, '_transport_info'):
                                caller_transport = session._transport._transport_info
                        else:
                            caller = None
                            caller_transport = None
                            authid = None
                            authrole = None
                            authmethod = None

                        invocation = message.Invocation(
                            request_id,
                            registration_id,
                            args=call.args,
                            kwargs=call.kwargs,
                            timeout=call.timeout,
                            receive_progress=call.receive_progress,
                            caller=caller,
                            caller_transport=caller_transport,
                            authid=authid,
                            authrole=authrole,
                            authmethod=authmethod)

                        self._invocations[request_id] = (call, session)
                        endpoint_session._transport.send(invocation)
                    else:
                        reply = message.Error(
                            message.Call.MESSAGE_TYPE, call.request,
                            ApplicationError.NOT_AUTHORIZED, [
                                "session is not authorized to call procedure '{0}'"
                                .format(call.procedure)
                            ])
                        session._transport.send(reply)
Пример #7
0
        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)
Пример #8
0
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]
Пример #9
0
 def invoke():
     self._s(message.Invocation(msg.request, registration, args=msg.args, kwargs=msg.kwargs))
Пример #10
0
    def _call(self,
              session,
              call,
              registration,
              authorization,
              is_queued_call=False):
        # will hold the callee (the concrete endpoint) that we will forward the call to ..
        #
        callee = None
        callee_extra = None

        # determine callee according to invocation policy
        #
        if registration.extra.invoke in [
                message.Register.INVOKE_SINGLE, message.Register.INVOKE_FIRST,
                message.Register.INVOKE_LAST
        ]:

            # a single endpoint is considered for forwarding the call ..

            if registration.extra.invoke == message.Register.INVOKE_SINGLE:
                callee = registration.observers[0]

            elif registration.extra.invoke == message.Register.INVOKE_FIRST:
                callee = registration.observers[0]

            elif registration.extra.invoke == message.Register.INVOKE_LAST:
                callee = registration.observers[len(registration.observers) -
                                                1]

            else:
                # should not arrive here
                raise Exception(u"logic error")

            # check maximum concurrency of the (single) endpoint
            callee_extra = registration.observers_extra.get(callee, None)
            if callee_extra:
                if callee_extra.concurrency and callee_extra.concurrency_current >= callee_extra.concurrency:
                    if is_queued_call or (
                            self._call_store
                            and self._call_store.maybe_queue_call(
                                session, call, registration, authorization)):
                        return False
                    else:
                        reply = message.Error(
                            message.Call.MESSAGE_TYPE, call.request,
                            u'crossbar.error.max_concurrency_reached', [
                                u'maximum concurrency {} of callee/endpoint reached (on non-shared/single registration)'
                                .format(callee_extra.concurrency)
                            ])
                        self._router.send(session, reply)
                        return False
                else:
                    callee_extra.concurrency_current += 1

        elif registration.extra.invoke == message.Register.INVOKE_ROUNDROBIN:

            # remember where we started to search for a suitable callee/endpoint in the round-robin list of callee endpoints
            roundrobin_start_index = registration.extra.roundrobin_current % len(
                registration.observers)

            # now search fo a suitable callee/endpoint
            while True:
                callee = registration.observers[
                    registration.extra.roundrobin_current %
                    len(registration.observers)]
                callee_extra = registration.observers_extra.get(callee, None)

                registration.extra.roundrobin_current += 1

                if callee_extra and callee_extra.concurrency:

                    if callee_extra.concurrency_current >= callee_extra.concurrency:

                        # this callee has set a maximum concurrency that has already been reached.
                        # we need to search further .. but only if we haven't reached the beginning
                        # of our round-robin list
                        if registration.extra.roundrobin_current % len(
                                registration.observers
                        ) == roundrobin_start_index:
                            # we've looked through the whole round-robin list, and didn't find a suitable
                            # callee (one that hasn't it's maximum concurrency already reached).
                            if is_queued_call or (
                                    self._call_store
                                    and self._call_store.maybe_queue_call(
                                        session, call, registration,
                                        authorization)):
                                return False
                            else:
                                reply = message.Error(
                                    message.Call.MESSAGE_TYPE, call.request,
                                    u'crossbar.error.max_concurrency_reached',
                                    [
                                        u'maximum concurrency of all callee/endpoints reached (on round-robin registration)'
                                        .format(callee_extra.concurrency)
                                    ])
                                self._router.send(session, reply)
                                return False
                        else:
                            # .. search on ..
                            pass
                    else:
                        # ok, we've found a callee that has set a maximum concurrency, but where the
                        # maximum has not yet been reached
                        break
                else:
                    # ok, we've found a callee which hasn't set a maximum concurrency, and hence is always
                    # eligible for having a call forwarded to
                    break

            if callee_extra:
                callee_extra.concurrency_current += 1

        elif registration.extra.invoke == message.Register.INVOKE_RANDOM:

            # FIXME: implement max. concurrency and call queueing
            callee = registration.observers[random.randint(
                0,
                len(registration.observers) - 1)]

        else:
            # should not arrive here
            raise Exception(u"logic error")

        # new ID for the invocation
        #
        invocation_request_id = self._request_id_gen.next()

        # caller disclosure
        #
        if authorization[u'disclose']:
            disclose = True
        elif (call.procedure.startswith(u"wamp.")
              or call.procedure.startswith(u"crossbar.")):
            disclose = True
        else:
            disclose = False

        if disclose:
            caller = session._session_id
            caller_authid = session._authid
            caller_authrole = session._authrole
        else:
            caller = None
            caller_authid = None
            caller_authrole = None

        # for pattern-based registrations, the INVOCATION must contain
        # the actual procedure being called
        #
        if registration.match != message.Register.MATCH_EXACT:
            procedure = call.procedure
        else:
            procedure = None

        if call.payload:
            invocation = message.Invocation(
                invocation_request_id,
                registration.id,
                payload=call.payload,
                timeout=call.timeout,
                receive_progress=call.receive_progress,
                caller=caller,
                caller_authid=caller_authid,
                caller_authrole=caller_authrole,
                procedure=procedure,
                enc_algo=call.enc_algo,
                enc_key=call.enc_key,
                enc_serializer=call.enc_serializer)
        else:
            invocation = message.Invocation(
                invocation_request_id,
                registration.id,
                args=call.args,
                kwargs=call.kwargs,
                timeout=call.timeout,
                receive_progress=call.receive_progress,
                caller=caller,
                caller_authid=caller_authid,
                caller_authrole=caller_authrole,
                procedure=procedure)

        self._add_invoke_request(invocation_request_id, registration, session,
                                 call, callee)
        self._router.send(callee, invocation)
        return True
Пример #11
0
            def on_authorize_success(authorization):

                # the call to authorize the action _itself_ succeeded. now go on depending on whether
                # the action was actually authorized or not ..
                #
                if not authorization[u'allow']:
                    reply = message.Error(
                        message.Call.MESSAGE_TYPE, call.request,
                        ApplicationError.NOT_AUTHORIZED, [
                            u"session is not authorized to call procedure '{0}'"
                            .format(call.procedure)
                        ])
                    self._router.send(session, reply)

                else:

                    # determine callee according to invocation policy
                    #
                    if registration.extra.invoke == message.Register.INVOKE_SINGLE:
                        callee = registration.observers[0]

                    elif registration.extra.invoke == message.Register.INVOKE_FIRST:
                        callee = registration.observers[0]

                    elif registration.extra.invoke == message.Register.INVOKE_LAST:
                        callee = registration.observers[
                            len(registration.observers) - 1]

                    elif registration.extra.invoke == message.Register.INVOKE_ROUNDROBIN:
                        callee = registration.observers[
                            registration.extra.roundrobin_current %
                            len(registration.observers)]
                        registration.extra.roundrobin_current += 1

                    elif registration.extra.invoke == message.Register.INVOKE_RANDOM:
                        callee = registration.observers[random.randint(
                            0,
                            len(registration.observers) - 1)]

                    else:
                        # should not arrive here
                        raise Exception(u"logic error")

                    # new ID for the invocation
                    #
                    invocation_request_id = self._request_id_gen.next()

                    # caller disclosure
                    #
                    if authorization[u'disclose']:
                        caller = session._session_id
                        caller_authid = session._authid
                        caller_authrole = session._authrole
                    else:
                        caller = None
                        caller_authid = None
                        caller_authrole = None

                    # for pattern-based registrations, the INVOCATION must contain
                    # the actual procedure being called
                    #
                    if registration.match != message.Register.MATCH_EXACT:
                        procedure = call.procedure
                    else:
                        procedure = None

                    if call.payload:
                        invocation = message.Invocation(
                            invocation_request_id,
                            registration.id,
                            payload=call.payload,
                            timeout=call.timeout,
                            receive_progress=call.receive_progress,
                            caller=caller,
                            caller_authid=caller_authid,
                            caller_authrole=caller_authrole,
                            procedure=procedure,
                            enc_algo=call.enc_algo,
                            enc_key=call.enc_key,
                            enc_serializer=call.enc_serializer)
                    else:
                        invocation = message.Invocation(
                            invocation_request_id,
                            registration.id,
                            args=call.args,
                            kwargs=call.kwargs,
                            timeout=call.timeout,
                            receive_progress=call.receive_progress,
                            caller=caller,
                            caller_authid=caller_authid,
                            caller_authrole=caller_authrole,
                            procedure=procedure)

                    self._invocations[
                        invocation_request_id] = InvocationRequest(
                            invocation_request_id, session, call)
                    self._router.send(callee, invocation)