Esempio n. 1
0
    def _send_reply(self, conn, reply=None, failure=None,
                    ending=False, log_failure=True):
        if (self.reply_q and
            not self._obsolete_reply_queues.reply_q_valid(self.reply_q,
                                                          self.msg_id)):
            return

        if failure:
            failure = rpc_common.serialize_remote_exception(failure,
                                                            log_failure)

        msg = {'result': reply, 'failure': failure}
        if ending:
            msg['ending'] = True

        rpc_amqp._add_unique_id(msg)

        # If a reply_q exists, add the msg_id to the reply and pass the
        # reply_q to direct_send() to use it as the response queue.
        # Otherwise use the msg_id for backward compatibility.
        if self.reply_q:
            msg['_msg_id'] = self.msg_id
            try:
                conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg))
            except rpc_amqp.AMQPDestinationNotFound:
                self._obsolete_reply_queues.add(self.reply_q, self.msg_id)
        else:
            # TODO(sileht): look at which version of oslo-incubator rpc
            # send need this, but I guess this is older than icehouse
            # if this is icehouse, we can drop this at Mitaka
            # if this is havana, we can drop this now.
            conn.direct_send(self.msg_id, rpc_common.serialize_msg(msg))
Esempio n. 2
0
    def _send_reply(self,
                    conn,
                    reply=None,
                    failure=None,
                    ending=False,
                    log_failure=True):
        if (self.reply_q and not self._obsolete_reply_queues.reply_q_valid(
                self.reply_q, self.msg_id)):
            return

        if failure:
            failure = rpc_common.serialize_remote_exception(
                failure, log_failure)

        msg = {'result': reply, 'failure': failure}
        if ending:
            msg['ending'] = True

        rpc_amqp._add_unique_id(msg)

        # If a reply_q exists, add the msg_id to the reply and pass the
        # reply_q to direct_send() to use it as the response queue.
        # Otherwise use the msg_id for backward compatibility.
        if self.reply_q:
            msg['_msg_id'] = self.msg_id
            try:
                conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg))
            except rpc_amqp.AMQPDestinationNotFound:
                self._obsolete_reply_queues.add(self.reply_q, self.msg_id)
        else:
            # TODO(sileht): look at which version of oslo-incubator rpc
            # send need this, but I guess this is older than icehouse
            # if this is icehouse, we can drop this at M
            # if this is havana, we can drop this now.
            conn.direct_send(self.msg_id, rpc_common.serialize_msg(msg))
Esempio n. 3
0
    def _send_reply(self, conn, reply=None, failure=None, ending=False, log_failure=True):
        if self.reply_q and not self._obsolete_reply_queues.reply_q_valid(self.reply_q, self.msg_id):
            return

        if failure:
            failure = rpc_common.serialize_remote_exception(failure, log_failure)

        msg = {"result": reply, "failure": failure}
        if ending:
            msg["ending"] = True

        rpc_amqp._add_unique_id(msg)
        unique_id = msg[rpc_amqp.UNIQUE_ID]

        # If a reply_q exists, add the msg_id to the reply and pass the
        # reply_q to direct_send() to use it as the response queue.
        # Otherwise use the msg_id for backward compatibility.
        if self.reply_q:
            msg["_msg_id"] = self.msg_id
            try:
                if ending:
                    LOG.debug(
                        "sending reply msg_id: %(msg_id)s "
                        "reply queue: %(reply_q)s"
                        % {"msg_id": self.msg_id, "unique_id": unique_id, "reply_q": self.reply_q}
                    )
                conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg))
            except rpc_amqp.AMQPDestinationNotFound:
                self._obsolete_reply_queues.add(self.reply_q, self.msg_id)
        else:
            # TODO(sileht): look at which version of oslo-incubator rpc
            # send need this, but I guess this is older than icehouse
            # if this is icehouse, we can drop this at Mitaka
            # if this is havana, we can drop this now.
            conn.direct_send(self.msg_id, rpc_common.serialize_msg(msg))
Esempio n. 4
0
def marshal_request(request, context, envelope):
    msg = proton.Message()
    if envelope:
        request = common.serialize_msg(request)
    data = {"request": request, "context": context}
    msg.body = jsonutils.dumps(data)
    return msg
Esempio n. 5
0
    def _send(self, target, ctxt, message,
              wait_for_reply=None, timeout=None, call_monitor_timeout=None,
              envelope=True, notify=False, retry=None, transport_options=None):

        msg = message

        if wait_for_reply:
            msg_id = uuid.uuid4().hex
            msg.update({'_msg_id': msg_id})
            msg.update({'_reply_q': self._get_reply_q()})
            msg.update({'_timeout': call_monitor_timeout})

        rpc_amqp._add_unique_id(msg)
        unique_id = msg[rpc_amqp.UNIQUE_ID]

        rpc_amqp.pack_context(msg, ctxt)

        if envelope:
            msg = rpc_common.serialize_msg(msg)

        if wait_for_reply:
            self._waiter.listen(msg_id)
            log_msg = "CALL msg_id: %s " % msg_id
        else:
            log_msg = "CAST unique_id: %s " % unique_id

        try:
            with self._get_connection(rpc_common.PURPOSE_SEND) as conn:
                if notify:
                    exchange = self._get_exchange(target)
                    LOG.debug(log_msg + "NOTIFY exchange '%(exchange)s'"
                              " topic '%(topic)s'", {'exchange': exchange,
                                                     'topic': target.topic})
                    conn.notify_send(exchange, target.topic, msg, retry=retry)
                elif target.fanout:
                    log_msg += "FANOUT topic '%(topic)s'" % {
                        'topic': target.topic}
                    LOG.debug(log_msg)
                    conn.fanout_send(target.topic, msg, retry=retry)
                else:
                    topic = target.topic
                    exchange = self._get_exchange(target)
                    if target.server:
                        topic = '%s.%s' % (target.topic, target.server)
                    LOG.debug(log_msg + "exchange '%(exchange)s'"
                              " topic '%(topic)s'", {'exchange': exchange,
                                                     'topic': topic})
                    conn.topic_send(exchange_name=exchange, topic=topic,
                                    msg=msg, timeout=timeout, retry=retry,
                                    transport_options=transport_options)

            if wait_for_reply:
                result = self._waiter.wait(msg_id, timeout,
                                           call_monitor_timeout)
                if isinstance(result, Exception):
                    raise result
                return result
        finally:
            if wait_for_reply:
                self._waiter.unlisten(msg_id)
Esempio n. 6
0
    def _send_reply(self, conn, reply=None, failure=None, ending=True):
        if not self._obsolete_reply_queues.reply_q_valid(
                self.reply_q, self.msg_id):
            return

        if failure:
            failure = rpc_common.serialize_remote_exception(failure)
        # NOTE(sileht): ending can be removed in N*, see Listener.wait()
        # for more detail.
        msg = {
            'result': reply,
            'failure': failure,
            'ending': ending,
            '_msg_id': self.msg_id
        }
        rpc_amqp._add_unique_id(msg)
        unique_id = msg[rpc_amqp.UNIQUE_ID]

        LOG.debug(
            "sending reply msg_id: %(msg_id)s "
            "reply queue: %(reply_q)s "
            "time elapsed: %(elapsed)ss", {
                'msg_id': self.msg_id,
                'unique_id': unique_id,
                'reply_q': self.reply_q,
                'elapsed': self.stopwatch.elapsed()
            })
        conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg))
Esempio n. 7
0
    def _send(self, target, ctxt, message,
              wait_for_reply=None, timeout=None, call_monitor_timeout=None,
              envelope=True, notify=False, retry=None):

        msg = message

        if wait_for_reply:
            msg_id = uuid.uuid4().hex
            msg.update({'_msg_id': msg_id})
            msg.update({'_reply_q': self._get_reply_q()})
            msg.update({'_timeout': call_monitor_timeout})

        rpc_amqp._add_unique_id(msg)
        unique_id = msg[rpc_amqp.UNIQUE_ID]

        rpc_amqp.pack_context(msg, ctxt)

        if envelope:
            msg = rpc_common.serialize_msg(msg)

        if wait_for_reply:
            self._waiter.listen(msg_id)
            log_msg = "CALL msg_id: %s " % msg_id
        else:
            log_msg = "CAST unique_id: %s " % unique_id

        try:
            with self._get_connection(rpc_common.PURPOSE_SEND) as conn:
                if notify:
                    exchange = self._get_exchange(target)
                    LOG.debug(log_msg + "NOTIFY exchange '%(exchange)s'"
                              " topic '%(topic)s'", {'exchange': exchange,
                                                     'topic': target.topic})
                    conn.notify_send(exchange, target.topic, msg, retry=retry)
                elif target.fanout:
                    log_msg += "FANOUT topic '%(topic)s'" % {
                        'topic': target.topic}
                    LOG.debug(log_msg)
                    conn.fanout_send(target.topic, msg, retry=retry)
                else:
                    topic = target.topic
                    exchange = self._get_exchange(target)
                    if target.server:
                        topic = '%s.%s' % (target.topic, target.server)
                    LOG.debug(log_msg + "exchange '%(exchange)s'"
                              " topic '%(topic)s'", {'exchange': exchange,
                                                     'topic': topic})
                    conn.topic_send(exchange_name=exchange, topic=topic,
                                    msg=msg, timeout=timeout, retry=retry)

            if wait_for_reply:
                result = self._waiter.wait(msg_id, timeout,
                                           call_monitor_timeout)
                if isinstance(result, Exception):
                    raise result
                return result
        finally:
            if wait_for_reply:
                self._waiter.unlisten(msg_id)
Esempio n. 8
0
def marshal_request(request, context, envelope):
    # NOTE(flaper87): Set inferred to True since rabbitmq-amqp-1.0 doesn't
    # have support for vbin8.
    msg = proton.Message(inferred=True)
    if envelope:
        request = common.serialize_msg(request)
    data = {"request": request, "context": context}
    msg.body = jsonutils.dumps(data)
    return msg
Esempio n. 9
0
def marshal_request(request, context, envelope):
    # NOTE(flaper87): Set inferred to True since rabbitmq-amqp-1.0 doesn't
    # have support for vbin8.
    msg = proton.Message(inferred=True)
    if envelope:
        request = common.serialize_msg(request)
    data = {"request": request, "context": context}
    msg.body = jsonutils.dumps(data)
    return msg
Esempio n. 10
0
def marshal_request(request, context, envelope):
    msg = proton.Message()
    if envelope:
        request = common.serialize_msg(request)
    data = {
        "request": request,
        "context": context
    }
    msg.body = jsonutils.dumps(data)
    return msg
Esempio n. 11
0
    def _send_reply(self, conn, reply=None, failure=None,
                    ending=False, log_failure=True):
        if failure:
            failure = rpc_common.serialize_remote_exception(failure,
                                                            log_failure)

        msg = {'result': reply, 'failure': failure}
        if ending:
            msg['ending'] = True

        rpc_amqp._add_unique_id(msg)

        # If a reply_q exists, add the msg_id to the reply and pass the
        # reply_q to direct_send() to use it as the response queue.
        # Otherwise use the msg_id for backward compatibility.
        if self.reply_q:
            msg['_msg_id'] = self.msg_id
            conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg))
        else:
            conn.direct_send(self.msg_id, rpc_common.serialize_msg(msg))
Esempio n. 12
0
def pack_message(ctxt, msg):
    """Pack context into msg."""
    if isinstance(ctxt, dict):
        context_d = ctxt
    else:
        context_d = ctxt.to_dict()
    msg['_context'] = context_d

    msg = driver_common.serialize_msg(msg)

    return msg
Esempio n. 13
0
def pack_message(ctxt, msg):
    """Pack context into msg."""
    if isinstance(ctxt, dict):
        context_d = ctxt
    else:
        context_d = ctxt.to_dict()
    msg['_context'] = context_d

    msg = driver_common.serialize_msg(msg)

    return msg
Esempio n. 14
0
    def _send(self, target, ctxt, message,
              wait_for_reply=None, timeout=None,
              envelope=True, notify=False, retry=None):

        # FIXME(markmc): remove this temporary hack
        class Context(object):
            def __init__(self, d):
                self.d = d

            def to_dict(self):
                return self.d

        context = Context(ctxt)
        msg = message

        if wait_for_reply:
            msg_id = uuid.uuid4().hex
            msg.update({'_msg_id': msg_id})
            LOG.debug('MSG_ID is %s', msg_id)
            msg.update({'_reply_q': self._get_reply_q()})

        rpc_amqp._add_unique_id(msg)
        rpc_amqp.pack_context(msg, context)

        if envelope:
            msg = rpc_common.serialize_msg(msg)

        if wait_for_reply:
            self._waiter.listen(msg_id)

        try:
            with self._get_connection(rpc_amqp.PURPOSE_SEND) as conn:
                if notify:
                    conn.notify_send(self._get_exchange(target),
                                     target.topic, msg, retry=retry)
                elif target.fanout:
                    conn.fanout_send(target.topic, msg, retry=retry)
                else:
                    topic = target.topic
                    if target.server:
                        topic = '%s.%s' % (target.topic, target.server)
                    conn.topic_send(exchange_name=self._get_exchange(target),
                                    topic=topic, msg=msg, timeout=timeout,
                                    retry=retry)

            if wait_for_reply:
                result = self._waiter.wait(msg_id, timeout)
                if isinstance(result, Exception):
                    raise result
                return result
        finally:
            if wait_for_reply:
                self._waiter.unlisten(msg_id)
Esempio n. 15
0
def serialize(ctxt, publisher_id, priority, event_type, payload):
    message = {'message_id': six.text_type(uuid.uuid4()),
               'publisher_id': publisher_id,
               'timestamp': timeutils.utcnow(),
               'priority': priority,
               'event_type': event_type,
               'payload': payload,
              }
    rpc_amqp._add_unique_id(message)
    rpc_amqp.pack_context(message, ctxt)
    message = rpc_common.serialize_msg(message)
    return message
Esempio n. 16
0
def marshal_request(request,
                    context,
                    envelope=False,
                    call_monitor_timeout=None):
    # NOTE(flaper87): Set inferred to True since rabbitmq-amqp-1.0 doesn't
    # have support for vbin8.
    msg = proton.Message(inferred=True)
    if envelope:
        request = common.serialize_msg(request)
    data = {"request": request, "context": context}
    if call_monitor_timeout is not None:
        data["call_monitor_timeout"] = call_monitor_timeout
    msg.body = jsonutils.dumps(data)
    return msg
Esempio n. 17
0
    def _send_reply(self,
                    conn,
                    reply=None,
                    failure=None,
                    ending=False,
                    log_failure=True):
        if failure:
            failure = rpc_common.serialize_remote_exception(
                failure, log_failure)

        msg = {'result': reply, 'failure': failure}
        if ending:
            msg['ending'] = True

        rpc_amqp._add_unique_id(msg)

        # If a reply_q exists, add the msg_id to the reply and pass the
        # reply_q to direct_send() to use it as the response queue.
        # Otherwise use the msg_id for backward compatibility.
        if self.reply_q:
            msg['_msg_id'] = self.msg_id
            conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg))
        else:
            conn.direct_send(self.msg_id, rpc_common.serialize_msg(msg))
Esempio n. 18
0
    def cast(self, msg_id, topic, data, envelope):
        msg_id = msg_id or 0

        if not envelope:
            data = _serialize(data)
            if six.PY3:
                data = data.encode('utf-8')
            data = (msg_id, topic, b'cast', data)
            self.outq.send([bytes(item) for item in data])
            return

        rpc_envelope = rpc_common.serialize_msg(data[1])
        zmq_msg = moves.reduce(lambda x, y: x + y, rpc_envelope.items())
        data = (msg_id, topic, b'impl_zmq_v2', data[0]) + zmq_msg
        self.outq.send([bytes(item) for item in data])
Esempio n. 19
0
def marshal_request(request, context, envelope=False,
                    call_monitor_timeout=None):
    # NOTE(flaper87): Set inferred to True since rabbitmq-amqp-1.0 doesn't
    # have support for vbin8.
    msg = proton.Message(inferred=True)
    if envelope:
        request = common.serialize_msg(request)
    data = {
        "request": request,
        "context": context
    }
    if call_monitor_timeout is not None:
        data["call_monitor_timeout"] = call_monitor_timeout
    msg.body = jsonutils.dumps(data)
    return msg
Esempio n. 20
0
    def cast(self, msg_id, topic, data, envelope):
        msg_id = msg_id or 0

        if not envelope:
            data = _serialize(data)
            if six.PY3:
                data = data.encode('utf-8')
            data = (msg_id, topic, b'cast', data)
            self.outq.send([bytes(item) for item in data])
            return

        rpc_envelope = rpc_common.serialize_msg(data[1])
        zmq_msg = moves.reduce(lambda x, y: x + y, rpc_envelope.items())
        data = (msg_id, topic, b'impl_zmq_v2', data[0]) + zmq_msg
        self.outq.send([bytes(item) for item in data])
    def _send_reply(self, conn, reply=None, failure=None, log_failure=True):
        if not self._obsolete_reply_queues.reply_q_valid(self.reply_q, self.msg_id):
            return

        if failure:
            failure = rpc_common.serialize_remote_exception(failure, log_failure)
        # NOTE(sileht): ending can be removed in N*, see Listener.wait()
        # for more detail.
        msg = {"result": reply, "failure": failure, "ending": True, "_msg_id": self.msg_id}
        rpc_amqp._add_unique_id(msg)
        unique_id = msg[rpc_amqp.UNIQUE_ID]

        LOG.debug(
            "sending reply msg_id: %(msg_id)s " "reply queue: %(reply_q)s",
            {"msg_id": self.msg_id, "unique_id": unique_id, "reply_q": self.reply_q},
        )
        conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg))
Esempio n. 22
0
    def _send_reply(self, conn, reply=None, failure=None, ending=True):
        if not self._obsolete_reply_queues.reply_q_valid(self.reply_q,
                                                         self.msg_id):
            return

        if failure:
            failure = rpc_common.serialize_remote_exception(failure)
        # NOTE(sileht): ending can be removed in N*, see Listener.wait()
        # for more detail.
        msg = {'result': reply, 'failure': failure, 'ending': ending,
               '_msg_id': self.msg_id}
        rpc_amqp._add_unique_id(msg)
        unique_id = msg[rpc_amqp.UNIQUE_ID]

        LOG.debug("sending reply msg_id: %(msg_id)s "
                  "reply queue: %(reply_q)s "
                  "time elapsed: %(elapsed)ss", {
                      'msg_id': self.msg_id,
                      'unique_id': unique_id,
                      'reply_q': self.reply_q,
                      'elapsed': self.stopwatch.elapsed()})
        conn.direct_send(self.reply_q, rpc_common.serialize_msg(msg))
Esempio n. 23
0
    def _send(self, target, ctxt, message, wait_for_reply=None, timeout=None, envelope=True, notify=False, retry=None):

        # FIXME(markmc): remove this temporary hack
        class Context(object):
            def __init__(self, d):
                self.d = d

            def to_dict(self):
                return self.d

        context = Context(ctxt)
        msg = message

        if wait_for_reply:
            msg_id = uuid.uuid4().hex
            msg.update({"_msg_id": msg_id})
            msg.update({"_reply_q": self._get_reply_q()})

        rpc_amqp._add_unique_id(msg)
        unique_id = msg[rpc_amqp.UNIQUE_ID]

        rpc_amqp.pack_context(msg, context)

        if envelope:
            msg = rpc_common.serialize_msg(msg)

        if wait_for_reply:
            self._waiter.listen(msg_id)
            log_msg = "CALL msg_id: %s " % msg_id
        else:
            log_msg = "CAST unique_id: %s " % unique_id

        try:
            with self._get_connection(rpc_amqp.PURPOSE_SEND) as conn:
                if notify:
                    exchange = self._get_exchange(target)
                    log_msg += "NOTIFY exchange '%(exchange)s'" " topic '%(topic)s'" % {
                        "exchange": exchange,
                        "topic": target.topic,
                    }
                    LOG.debug(log_msg)
                    conn.notify_send(exchange, target.topic, msg, retry=retry)
                elif target.fanout:
                    log_msg += "FANOUT topic '%(topic)s'" % {"topic": target.topic}
                    LOG.debug(log_msg)
                    conn.fanout_send(target.topic, msg, retry=retry)
                else:
                    topic = target.topic
                    exchange = self._get_exchange(target)
                    if target.server:
                        topic = "%s.%s" % (target.topic, target.server)
                    log_msg += "exchange '%(exchange)s'" " topic '%(topic)s'" % {
                        "exchange": exchange,
                        "topic": target.topic,
                    }
                    LOG.debug(log_msg)
                    conn.topic_send(exchange_name=exchange, topic=topic, msg=msg, timeout=timeout, retry=retry)

            if wait_for_reply:
                result = self._waiter.wait(msg_id, timeout)
                if isinstance(result, Exception):
                    raise result
                return result
        finally:
            if wait_for_reply:
                self._waiter.unlisten(msg_id)
Esempio n. 24
0
    def _send(self,
              target,
              ctxt,
              message,
              wait_for_reply=None,
              timeout=None,
              envelope=True,
              notify=False,
              retry=None):

        msg = message
        #如果等待reply,则生成msg._msg_id/msg._reply_q;并生成self._waiter
        if wait_for_reply:
            msg_id = uuid.uuid4().hex
            msg.update({'_msg_id': msg_id})
            msg.update({'_reply_q':
                        self._get_reply_q()})  #wait已经生成关联消费者到reply_q队列
        #msg._unique_id = uuid
        rpc_amqp._add_unique_id(msg)
        unique_id = msg[rpc_amqp.UNIQUE_ID]

        rpc_amqp.pack_context(msg, ctxt)  #ctxt信息放入msg

        if envelope:
            msg = rpc_common.serialize_msg(msg)

        if wait_for_reply:
            self._waiter.listen(
                msg_id)  #wait在ReplyWaiters中创建msg_id队列,用于接受reply
            log_msg = "CALL msg_id: %s " % msg_id
        else:
            log_msg = "CAST unique_id: %s " % unique_id

        try:
            with self._get_connection(rpc_common.PURPOSE_SEND) as conn:
                if notify:
                    exchange = self._get_exchange(target)
                    log_msg += "NOTIFY exchange '%(exchange)s'" \
                               " topic '%(topic)s'" % {
                                   'exchange': exchange,
                                   'topic': target.topic}
                    LOG.debug(log_msg)
                    conn.notify_send(exchange, target.topic, msg, retry=retry)
                elif target.fanout:
                    log_msg += "FANOUT topic '%(topic)s'" % {
                        'topic': target.topic
                    }
                    LOG.debug(log_msg)
                    conn.fanout_send(target.topic, msg, retry=retry)
                else:
                    topic = target.topic
                    exchange = self._get_exchange(target)
                    if target.server:
                        topic = '%s.%s' % (target.topic, target.server)
                    log_msg += "exchange '%(exchange)s'" \
                               " topic '%(topic)s'" % {
                                   'exchange': exchange,
                                   'topic': topic}
                    LOG.debug(log_msg)
                    conn.topic_send(exchange_name=exchange,
                                    topic=topic,
                                    msg=msg,
                                    timeout=timeout,
                                    retry=retry)

            if wait_for_reply:
                result = self._waiter.wait(
                    msg_id,
                    timeout)  #wait最多等待timeout,在ReplyWaiters的msg_id队列获取reply数据
                if isinstance(result, Exception):
                    raise result
                return result
        finally:
            if wait_for_reply:
                self._waiter.unlisten(msg_id)
Esempio n. 25
0
    def _send(self,
              target,
              ctxt,
              message,
              wait_for_reply=None,
              timeout=None,
              envelope=True,
              notify=False,
              retry=None):

        # FIXME(markmc): remove this temporary hack
        class Context(object):
            def __init__(self, d):
                self.d = d

            def to_dict(self):
                return self.d

        context = Context(ctxt)
        msg = message

        if wait_for_reply:
            msg_id = uuid.uuid4().hex
            msg.update({'_msg_id': msg_id})
            msg.update({'_reply_q': self._get_reply_q()})

        rpc_amqp._add_unique_id(msg)
        unique_id = msg[rpc_amqp.UNIQUE_ID]

        rpc_amqp.pack_context(msg, context)

        if envelope:
            msg = rpc_common.serialize_msg(msg)

        if wait_for_reply:
            self._waiter.listen(msg_id)
            log_msg = "CALL msg_id: %s " % msg_id
        else:
            log_msg = "CAST unique_id: %s " % unique_id

        try:
            with self._get_connection(rpc_common.PURPOSE_SEND) as conn:
                if notify:
                    exchange = self._get_exchange(target)
                    log_msg += "NOTIFY exchange '%(exchange)s'" \
                               " topic '%(topic)s'" % {
                                   'exchange': exchange,
                                   'topic': target.topic}
                    LOG.debug(log_msg)
                    conn.notify_send(exchange, target.topic, msg, retry=retry)
                elif target.fanout:
                    log_msg += "FANOUT topic '%(topic)s'" % {
                        'topic': target.topic
                    }
                    LOG.debug(log_msg)
                    conn.fanout_send(target.topic, msg, retry=retry)
                else:
                    topic = target.topic
                    exchange = self._get_exchange(target)
                    if target.server:
                        topic = '%s.%s' % (target.topic, target.server)
                    log_msg += "exchange '%(exchange)s'" \
                               " topic '%(topic)s'" % {
                                   'exchange': exchange,
                                   'topic': target.topic}
                    LOG.debug(log_msg)
                    conn.topic_send(exchange_name=exchange,
                                    topic=topic,
                                    msg=msg,
                                    timeout=timeout,
                                    retry=retry)

            if wait_for_reply:
                result = self._waiter.wait(msg_id, timeout)
                if isinstance(result, Exception):
                    raise result
                return result
        finally:
            if wait_for_reply:
                self._waiter.unlisten(msg_id)
Esempio n. 26
0
    def _send(self,
              target,
              ctxt,
              message,
              wait_for_reply=None,
              timeout=None,
              envelope=True,
              notify=False,
              retry=None):

        # FIXME(markmc): remove this temporary hack
        class Context(object):
            def __init__(self, d):
                self.d = d

            def to_dict(self):
                return self.d

        context = Context(ctxt)
        msg = message

        if wait_for_reply:
            msg_id = uuid.uuid4().hex
            msg.update({'_msg_id': msg_id})
            LOG.debug('MSG_ID is %s', msg_id)
            msg.update({'_reply_q': self._get_reply_q()})

        rpc_amqp._add_unique_id(msg)
        rpc_amqp.pack_context(msg, context)

        if envelope:
            msg = rpc_common.serialize_msg(msg)

        if wait_for_reply:
            self._waiter.listen(msg_id)

        try:
            with self._get_connection(rpc_amqp.PURPOSE_SEND) as conn:
                if notify:
                    conn.notify_send(self._get_exchange(target),
                                     target.topic,
                                     msg,
                                     retry=retry)
                elif target.fanout:
                    conn.fanout_send(target.topic, msg, retry=retry)
                else:
                    topic = target.topic
                    if target.server:
                        topic = '%s.%s' % (target.topic, target.server)
                    conn.topic_send(exchange_name=self._get_exchange(target),
                                    topic=topic,
                                    msg=msg,
                                    timeout=timeout,
                                    retry=retry)

            if wait_for_reply:
                result = self._waiter.wait(msg_id, timeout)
                if isinstance(result, Exception):
                    raise result
                return result
        finally:
            if wait_for_reply:
                self._waiter.unlisten(msg_id)