# This file is part of Moksha.
# Copyright (C) 2008-2014  Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Authors: <NAME> <<EMAIL>>
#          <NAME> <<EMAIL>>

try:
    # Try first to use modern stomp-1.1
    import stomper.stomp_11 as stomper
except ImportError:
    # Failing that, use whatever is available.
    try:
        import stomper
    except ImportError:
        pass

import logging

import six
from twisted.internet.protocol import ClientFactory

from moksha.hub.stomp.protocol import StompProtocol
from moksha.hub.messaging import MessagingHubExtension
from moksha.hub.reactor import reactor

log = logging.getLogger('moksha.hub')


class StompHubExtension(MessagingHubExtension, ClientFactory):
    username = None
    password = None
    proto = None
    frames = None

    def __init__(self, hub, config):
        self.config = config
        self.hub = hub
        self._topics = list(hub.topics.keys())
        self._frames = []

        uri = self.config.get('stomp_uri', None)
        if not uri:
            port = int(self.config.get('stomp_port', 61613))
            host = self.config.get('stomp_broker')
            uri = "%s:%i" % (host, port)

        # A list of addresses over which we emulate failover()
        self.addresses = [pair.split(":") for pair in uri.split(',')]
        self.address_index = 0

        # An exponential delay used to back off if we keep failing.
        self._delay = float(self.config.get('stomp_delay', '0.1'))

        self.username = self.config.get('stomp_user', 'guest')
        self.password = self.config.get('stomp_pass', '<PASSWORD>')

        self.key = self.config.get('stomp_ssl_key', None)
        self.crt = self.config.get('stomp_ssl_crt', None)

        self.client_heartbeat = int(self.config.get('stomp_heartbeat', 0))

        self.connect(self.addresses[self.address_index], self.key, self.crt)
        super(StompHubExtension, self).__init__()

    def connect(self, address, key=None, crt=None):
        host, port = address
        if key and crt:
            log.info("connecting encrypted to %r %r %r" % (
                host, int(port), self))

            from twisted.internet import ssl

            with open(key) as key_file:
                with open(crt) as cert_file:
                    client_cert = ssl.PrivateCertificate.loadPEM(
                        key_file.read() + cert_file.read())

            try:
                import service_identity
                # connect SSL/TLS with SNI support (https://twistedmatrix.com/trac/ticket/5190)
                # This requires service_identity module: https://pypi.python.org/pypi/service_identity
                ssl_context = ssl.optionsForClientTLS(six.text_type(host), clientCertificate=client_cert)
            except ImportError:
                log.warn("Connecting without SNI support due to absence of service_identity module.")
                ssl_context = client_cert.options()

            reactor.connectSSL(host, int(port), self, ssl_context)
        else:
            log.info("connecting unencrypted to %r %r %r" % (
                host, int(port), self))
            reactor.connectTCP(host, int(port), self)

    def buildProtocol(self, addr):
        self._delay = float(self.config.get('stomp_delay', '0.1'))
        log.debug("build protocol was called with %r" % addr)
        self.proto = StompProtocol(self, self.username, self.password)
        return self.proto

    def connected(self, server_heartbeat):
        if server_heartbeat and self.client_heartbeat:
            interval = max(self.client_heartbeat, server_heartbeat)
            log.debug("Heartbeat of %ims negotiated from (%i,%i); starting." % (
                interval, self.client_heartbeat, server_heartbeat))
            # According to STOMP documentation, we have negotiated a heartbeat
            # of `interval` milliseconds and heartbeats must be sent *at least*
            # that often.  Here, we'll send them twice as often to give plenty
            # of room for latency.
            # https://stomp.github.io/stomp-specification-1.2.html#Heart-beating
            fudge_factor = 0.5
            self.start_heartbeat(interval=(interval * fudge_factor))
        else:
            log.debug("Skipping heartbeat initialization")

        # Sometimes, a stomp consumer may wish to be subscribed to a queue
        # which is composed of messages from many different topics.  In this
        # case, the hub hands dispatching messages to the right consumers.
        # This extension is only concerned with the queue, and negotiating that
        # with the broker.
        stomp_queue = self.config.get('stomp_queue', None)
        if stomp_queue and self._topics and self._topics != [stomp_queue]:
            log.info('Discarding consumer-specified topics in favor of '
                     'stomp_queue=%s: %r' % (stomp_queue, self._topics))
            # Overwrite the declarations of all of our consumers.
            self._topics = [stomp_queue]

        for topic in self._topics:
            log.info('Subscribing to %s topic' % topic)
            self.subscribe(topic, callback=lambda msg: None)

        for frame in self._frames:
            log.debug('Flushing queued frame')
            self.proto.transport.write(frame.pack().encode('utf-8'))
        self._frames = []

    def clientConnectionLost(self, connector, reason):
        log.info('Lost connection.  Reason: %s' % reason)
        self.stop_heartbeat()
        self.failover()

    def clientConnectionFailed(self, connector, reason):
        log.error('Connection failed. Reason: %s' % reason)
        self.stop_heartbeat()
        self.failover()

    def failover(self):
        self.address_index = (self.address_index + 1) % len(self.addresses)
        args = (self.addresses[self.address_index], self.key, self.crt,)
        self._delay = min(60.0, self._delay * (1 + (2.0 / len(self.addresses))))
        log.info('(failover) reconnecting in %f seconds.' % self._delay)
        reactor.callLater(self._delay, self.connect, *args)

    def start_heartbeat(self, interval):
        self._heartbeat_enabled = True
        reactor.callLater(interval / 1000.0, self.heartbeat, interval)

    def heartbeat(self, interval):
        if self._heartbeat_enabled:
            self.proto.transport.write(chr(0x0A).encode('utf-8'))  # Lub-dub
            reactor.callLater(interval / 1000.0, self.heartbeat, interval)
        else:
            log.debug("(heartbeat stopped)")

    def stop_heartbeat(self):
        log.debug("stopping heartbeat")
        self._heartbeat_enabled = False

    def send_message(self, topic, message, **headers):
        # Convert any utf-8 encoded payloads back to unicode.  stomper can't handle bytestrings
        topic = topic.decode('utf-8') if isinstance(topic, six.binary_type) else topic
        message = message.decode('utf-8') if isinstance(message, six.binary_type) else message
        f = stomper.Frame()
        f.unpack(stomper.send(topic, message))
        if not self.proto:
            log.info("Queueing stomp frame for later delivery")
            self._frames.append(f)
        else:
            self.proto.transport.write(f.pack().encode('utf-8'))

        super(StompHubExtension, self).send_message(topic, message, **headers)

    def subscribe(self, topic, callback):
        # FIXME -- note, the callback is just thrown away here.
        if not self.proto:
            log.info("queuing topic for later subscription %r." % topic)
            if topic not in self._topics:
                self._topics.append(topic)
        else:
            log.debug("sending subscription to the protocol")
            self.proto.subscribe(topic)

        super(StompHubExtension, self).subscribe(topic, callback)

# This file is part of Moksha.
# Copyright (C) 2008-2014  Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Authors: <NAME> <<EMAIL>>
#          <NAME> <<EMAIL>>

try:
    # Try first to use modern stomp-1.1
    import stomper.stomp_11 as stomper
except ImportError:
    # Failing that, use whatever is available.
    try:
        import stomper
    except ImportError:
        pass

import logging

import six
from twisted.internet.protocol import ClientFactory

from moksha.hub.stomp.protocol import StompProtocol
from moksha.hub.messaging import MessagingHubExtension
from moksha.hub.reactor import reactor

log = logging.getLogger('moksha.hub')


class StompHubExtension(MessagingHubExtension, ClientFactory):
    username = None
    password = None
    proto = None
    frames = None

    def __init__(self, hub, config):
        self.config = config
        self.hub = hub
        self._topics = list(hub.topics.keys())
        self._frames = []

        uri = self.config.get('stomp_uri', None)
        if not uri:
            port = int(self.config.get('stomp_port', 61613))
            host = self.config.get('stomp_broker')
            uri = "%s:%i" % (host, port)

        # A list of addresses over which we emulate failover()
        self.addresses = [pair.split(":") for pair in uri.split(',')]
        self.address_index = 0

        # An exponential delay used to back off if we keep failing.
        self._delay = float(self.config.get('stomp_delay', '0.1'))

        self.username = self.config.get('stomp_user', 'guest')
        self.password = self.config.get('stomp_pass', '<PASSWORD>')

        self.key = self.config.get('stomp_ssl_key', None)
        self.crt = self.config.get('stomp_ssl_crt', None)

        self.client_heartbeat = int(self.config.get('stomp_heartbeat', 0))

        self.connect(self.addresses[self.address_index], self.key, self.crt)
        super(StompHubExtension, self).__init__()

    def connect(self, address, key=None, crt=None):
        host, port = address
        if key and crt:
            log.info("connecting encrypted to %r %r %r" % (
                host, int(port), self))

            from twisted.internet import ssl

            with open(key) as key_file:
                with open(crt) as cert_file:
                    client_cert = ssl.PrivateCertificate.loadPEM(
                        key_file.read() + cert_file.read())

            try:
                import service_identity
                # connect SSL/TLS with SNI support (https://twistedmatrix.com/trac/ticket/5190)
                # This requires service_identity module: https://pypi.python.org/pypi/service_identity
                ssl_context = ssl.optionsForClientTLS(six.text_type(host), clientCertificate=client_cert)
            except ImportError:
                log.warn("Connecting without SNI support due to absence of service_identity module.")
                ssl_context = client_cert.options()

            reactor.connectSSL(host, int(port), self, ssl_context)
        else:
            log.info("connecting unencrypted to %r %r %r" % (
                host, int(port), self))
            reactor.connectTCP(host, int(port), self)

    def buildProtocol(self, addr):
        self._delay = float(self.config.get('stomp_delay', '0.1'))
        log.debug("build protocol was called with %r" % addr)
        self.proto = StompProtocol(self, self.username, self.password)
        return self.proto

    def connected(self, server_heartbeat):
        if server_heartbeat and self.client_heartbeat:
            interval = max(self.client_heartbeat, server_heartbeat)
            log.debug("Heartbeat of %ims negotiated from (%i,%i); starting." % (
                interval, self.client_heartbeat, server_heartbeat))
            # According to STOMP documentation, we have negotiated a heartbeat
            # of `interval` milliseconds and heartbeats must be sent *at least*
            # that often.  Here, we'll send them twice as often to give plenty
            # of room for latency.
            # https://stomp.github.io/stomp-specification-1.2.html#Heart-beating
            fudge_factor = 0.5
            self.start_heartbeat(interval=(interval * fudge_factor))
        else:
            log.debug("Skipping heartbeat initialization")

        # Sometimes, a stomp consumer may wish to be subscribed to a queue
        # which is composed of messages from many different topics.  In this
        # case, the hub hands dispatching messages to the right consumers.
        # This extension is only concerned with the queue, and negotiating that
        # with the broker.
        stomp_queue = self.config.get('stomp_queue', None)
        if stomp_queue and self._topics and self._topics != [stomp_queue]:
            log.info('Discarding consumer-specified topics in favor of '
                     'stomp_queue=%s: %r' % (stomp_queue, self._topics))
            # Overwrite the declarations of all of our consumers.
            self._topics = [stomp_queue]

        for topic in self._topics:
            log.info('Subscribing to %s topic' % topic)
            self.subscribe(topic, callback=lambda msg: None)

        for frame in self._frames:
            log.debug('Flushing queued frame')
            self.proto.transport.write(frame.pack().encode('utf-8'))
        self._frames = []

    def clientConnectionLost(self, connector, reason):
        log.info('Lost connection.  Reason: %s' % reason)
        self.stop_heartbeat()
        self.failover()

    def clientConnectionFailed(self, connector, reason):
        log.error('Connection failed. Reason: %s' % reason)
        self.stop_heartbeat()
        self.failover()

    def failover(self):
        self.address_index = (self.address_index + 1) % len(self.addresses)
        args = (self.addresses[self.address_index], self.key, self.crt,)
        self._delay = min(60.0, self._delay * (1 + (2.0 / len(self.addresses))))
        log.info('(failover) reconnecting in %f seconds.' % self._delay)
        reactor.callLater(self._delay, self.connect, *args)

    def start_heartbeat(self, interval):
        self._heartbeat_enabled = True
        reactor.callLater(interval / 1000.0, self.heartbeat, interval)

    def heartbeat(self, interval):
        if self._heartbeat_enabled:
            self.proto.transport.write(chr(0x0A).encode('utf-8'))  # Lub-dub
            reactor.callLater(interval / 1000.0, self.heartbeat, interval)
        else:
            log.debug("(heartbeat stopped)")

    def stop_heartbeat(self):
        log.debug("stopping heartbeat")
        self._heartbeat_enabled = False

    def send_message(self, topic, message, **headers):
        # Convert any utf-8 encoded payloads back to unicode.  stomper can't handle bytestrings
        topic = topic.decode('utf-8') if isinstance(topic, six.binary_type) else topic
        message = message.decode('utf-8') if isinstance(message, six.binary_type) else message
        f = stomper.Frame()
        f.unpack(stomper.send(topic, message))
        if not self.proto:
            log.info("Queueing stomp frame for later delivery")
            self._frames.append(f)
        else:
            self.proto.transport.write(f.pack().encode('utf-8'))

        super(StompHubExtension, self).send_message(topic, message, **headers)

    def subscribe(self, topic, callback):
        # FIXME -- note, the callback is just thrown away here.
        if not self.proto:
            log.info("queuing topic for later subscription %r." % topic)
            if topic not in self._topics:
                self._topics.append(topic)
        else:
            log.debug("sending subscription to the protocol")
            self.proto.subscribe(topic)

        super(StompHubExtension, self).subscribe(topic, callback)

# This file is part of Moksha. # Copyright (C) 2008-2014  Red Hat, Inc. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # #    http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # # Authors: <NAME> <<EMAIL>> #          <NAME> <<EMAIL>> # Try first to use modern stomp-1.1 # Failing that, use whatever is available. # A list of addresses over which we emulate failover() # An exponential delay used to back off if we keep failing. # connect SSL/TLS with SNI support (https://twistedmatrix.com/trac/ticket/5190) # This requires service_identity module: https://pypi.python.org/pypi/service_identity # According to STOMP documentation, we have negotiated a heartbeat # of `interval` milliseconds and heartbeats must be sent *at least* # that often.  Here, we'll send them twice as often to give plenty # of room for latency. # https://stomp.github.io/stomp-specification-1.2.html#Heart-beating # Sometimes, a stomp consumer may wish to be subscribed to a queue # which is composed of messages from many different topics.  In this # case, the hub hands dispatching messages to the right consumers. # This extension is only concerned with the queue, and negotiating that # with the broker. # Overwrite the declarations of all of our consumers. # Lub-dub # Convert any utf-8 encoded payloads back to unicode.  stomper can't handle bytestrings # FIXME -- note, the callback is just thrown away here.