Esempio n. 1
0
    def test_open(self, connection, ssl_domain, find):
        url = TEST_URL
        connector = Connector(url)
        connector.ssl.ca_certificate = 'test-ca'
        connector.ssl.client_key = 'test-key'
        connector.ssl.client_certificate = 'test-crt'
        find.return_value = connector

        # test
        c = Connection(url)
        c._ssh = Mock()
        c.open()

        # validation
        ssl_domain.assert_called_once_with(connector)
        connection.assert_called_once_with(
            host=':'.join((connector.host, str(connector.port))),
            virtual_host=connector.virtual_host,
            userid=connector.userid,
            password=connector.password,
            ssl=ssl_domain.return_value,
            confirm_publish=True)

        connection.return_value.connect.assert_called_once_with()
        self.assertEqual(c._impl, connection.return_value)
Esempio n. 2
0
class Endpoint(Messenger):
    def __init__(self, url):
        super(Endpoint, self).__init__(url)
        self.connection = Connection(url)
        self.channel = None

    def open(self):
        self.connection.open()
        self.channel = self.connection.channel()

    def repair(self):
        self.connection.close()
        self.connection.open()
        self.channel = self.connection.channel()

    def close(self):
        try:
            self.channel.close()
        except Exception:
            pass
Esempio n. 3
0
class Endpoint(Messenger):

    def __init__(self, url):
        super(Endpoint, self).__init__(url)
        self.connection = Connection(url)
        self.channel = None

    def open(self):
        self.connection.open()
        self.channel = self.connection.channel()
    
    def repair(self):
        self.connection.close()
        self.connection.open()
        self.channel = self.connection.channel()

    def close(self):
        try:
            self.channel.close()
        except Exception:
            pass
Esempio n. 4
0
class Sender(BaseSender):
    """
    An AMQP message sender.
    """
    def __init__(self, url):
        """
        :param url: The broker url.
        :type url: str
        """
        BaseSender.__init__(self, url)
        self.connection = Connection(url)
        self.channel = None

    def is_open(self):
        """
        Get whether the sender has been opened.
        :return: True if open.
        :rtype bool
        """
        return self.channel is not None

    @reliable
    def open(self):
        """
        Open the reader.
        """
        if self.is_open():
            # already opened
            return
        self.connection.open()
        self.channel = self.connection.channel()

    def repair(self):
        """
        Repair the reader.
        """
        self.close()
        self.connection.close()
        self.connection.open()
        self.channel = self.connection.channel()

    def close(self):
        """
        Close the reader.
        """
        channel = self.channel
        self.channel = None
        try:
            channel.close()
        except Exception:
            pass

    @reliable
    def send(self, address, content, ttl=None):
        """
        Send a message.
        :param address: An AMQP address.
        :type address: str
        :param content: The message content
        :type content: buf
        :param ttl: Time to Live (seconds)
        :type ttl: float
        """
        parts = address.split('/')
        if len(parts) > 1:
            exchange = parts[0]
        else:
            exchange = ''
        key = parts[-1]
        message = build_message(content, ttl, self.durable)
        self.channel.basic_publish(message,
                                   mandatory=True,
                                   exchange=exchange,
                                   routing_key=key)
        log.debug('sent (%s)', address)
Esempio n. 5
0
class Reader(BaseReader):
    """
    An AMQP message reader.
    """

    def __init__(self, node, url):
        """
        :param node: The AMQP node to read.
        :type node: gofer.messaging.adapter.model.Node
        :param url: The broker url.
        :type url: str
        :see: gofer.messaging.adapter.url.URL
        """
        BaseReader.__init__(self, node, url)
        self.connection = Connection(url)
        self.channel = None
        self.receiver = None

    def is_open(self):
        """
        Get whether the messenger has been opened.
        :return: True if open.
        :rtype bool
        """
        return self.receiver is not None

    @reliable
    def open(self):
        """
        Open the reader.
        :raise: NotFound
        """
        if self.is_open():
            # already opened
            return
        self.connection.open()
        self.channel = self.connection.channel()
        receiver = Receiver(self)
        self.receiver = receiver.open()

    def repair(self):
        """
        Repair the reader.
        :raise: NotFound
        """
        self.close()
        self.connection.close()
        self.connection.open()
        self.channel = self.connection.channel()
        receiver = Receiver(self)
        self.receiver = receiver.open()

    def close(self):
        """
        Close the reader.
        """
        receiver = self.receiver
        self.receiver = None
        channel = self.channel
        self.channel = None
        try:
            receiver.close()
        except Exception:
            pass
        try:
            channel.close()
        except Exception:
            pass


    @reliable
    def get(self, timeout=None):
        """
        Get the next message from the queue.
        :param timeout: The read timeout in seconds.
        :type timeout: int
        :return: The next message or None.
        :rtype: Message
        """
        try:
            impl = self.receiver.fetch(timeout or NO_DELAY)
            return Message(self, impl, impl.body)
        except Empty:
            pass

    @reliable
    def ack(self, message):
        """
        Ack the specified message.
        :param message: The message to acknowledge.
        :type message: amqp.Message
        """
        self.channel.basic_ack(message.delivery_info[DELIVERY_TAG])

    @reliable
    def reject(self, message, requeue=True):
        """
        Reject the specified message.
        :param message: The message to reject.
        :type message: amqp.Message
        :param requeue: Requeue the message or discard it.
        :type requeue: bool
        """
        self.channel.basic_reject(message.delivery_info[DELIVERY_TAG], requeue)
Esempio n. 6
0
class Sender(BaseSender):
    """
    An AMQP message sender.
    """

    def __init__(self, url):
        """
        :param url: The broker url.
        :type url: str
        """
        BaseSender.__init__(self, url)
        self.connection = Connection(url)
        self.channel = None

    def is_open(self):
        """
        Get whether the sender has been opened.
        :return: True if open.
        :rtype bool
        """
        return self.channel is not None

    @reliable
    def open(self):
        """
        Open the reader.
        """
        if self.is_open():
            # already opened
            return
        self.connection.open()
        self.channel = self.connection.channel()

    def repair(self):
        """
        Repair the reader.
        """
        self.close()
        self.connection.close()
        self.connection.open()
        self.channel = self.connection.channel()

    def close(self):
        """
        Close the reader.
        """
        channel = self.channel
        self.channel = None
        try:
            channel.close()
        except Exception:
            pass

    @reliable
    def send(self, address, content, ttl=None):
        """
        Send a message.
        :param address: An AMQP address.
        :type address: str
        :param content: The message content
        :type content: buf
        :param ttl: Time to Live (seconds)
        :type ttl: float
        """
        parts = address.split('/')
        if len(parts) > 1:
            exchange = parts[0]
        else:
            exchange = ''
        key = parts[-1]
        message = build_message(content, ttl, self.durable)
        self.channel.basic_publish(message, mandatory=True, exchange=exchange, routing_key=key)
        log.debug('sent (%s)', address)
Esempio n. 7
0
 def test_open_already(self):
     url = TEST_URL
     c = Connection(url)
     c._impl = Mock()
     c.open()
     self.assertFalse(c._impl.open.called)
Esempio n. 8
0
class Reader(BaseReader):
    """
    An AMQP message reader.
    """

    def __init__(self, node, url):
        """
        :param node: The AMQP node to read.
        :type node: gofer.messaging.adapter.model.Node
        :param url: The broker url.
        :type url: str
        :see: gofer.messaging.adapter.url.URL
        """
        BaseReader.__init__(self, node, url)
        self.connection = Connection(url)
        self.channel = None
        self.receiver = None

    def is_open(self):
        """
        Get whether the messenger has been opened.
        :return: True if open.
        :rtype bool
        """
        return self.receiver is not None

    @reliable
    def open(self):
        """
        Open the reader.
        :raise: NotFound
        """
        if self.is_open():
            # already opened
            return
        self.connection.open()
        self.channel = self.connection.channel()
        receiver = Receiver(self)
        self.receiver = receiver.open()

    def repair(self):
        """
        Repair the reader.
        :raise: NotFound
        """
        self.close()
        self.connection.close()
        self.connection.open()
        self.channel = self.connection.channel()
        receiver = Receiver(self)
        self.receiver = receiver.open()

    def close(self):
        """
        Close the reader.
        """
        receiver = self.receiver
        self.receiver = None
        channel = self.channel
        self.channel = None
        try:
            receiver.close()
        except Exception:
            pass
        try:
            channel.close()
        except Exception:
            pass

    @reliable
    def get(self, timeout=None):
        """
        Get the next message from the queue.
        :param timeout: The read timeout in seconds.
        :type timeout: int
        :return: The next message or None.
        :rtype: Message
        """
        try:
            impl = self.receiver.fetch(timeout or NO_DELAY)
            return Message(self, impl, impl.body)
        except Empty:
            pass

    @reliable
    def ack(self, message):
        """
        Ack the specified message.
        :param message: The message to acknowledge.
        :type message: amqp.Message
        """
        self.channel.basic_ack(message.delivery_info[DELIVERY_TAG])

    @reliable
    def reject(self, message, requeue=True):
        """
        Reject the specified message.
        :param message: The message to reject.
        :type message: amqp.Message
        :param requeue: Requeue the message or discard it.
        :type requeue: bool
        """
        self.channel.basic_reject(message.delivery_info[DELIVERY_TAG], requeue)