Пример #1
0
class StompHub(MessagingHub, ReconnectingClientFactory):
    username = None
    password = None
    proto = None
    frames = None

    def __init__(self, host, port, username, password, topics=None):
        self.username = username
        self.password = password
        self._topics = topics or []
        self._frames = []

        reactor.connectTCP(host, int(port), self)

    def buildProtocol(self, addr):
        self.proto = StompProtocol(self, self.username, self.password)
        return self.proto

    def connected(self):
        for topic in self._topics:
            log.debug('Subscribing to %s topic' % topic)
            self.subscribe(topic)
        self._topics = []
        for frame in self._frames:
            log.debug('Flushing queued frame')
            self.proto.transport.write(frame.pack())
        self._frames = []

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

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

    def send_message(self, topic, message):
        f = stomper.Frame()
        f.unpack(stomper.send(topic, message))
        if not self.proto:
            log.debug("Queueing stomp frame for later delivery")
            self._frames.append(f)
        else:
            self.proto.transport.write(f.pack())

    def subscribe(self, topic):
        if not self.proto:
            if topic not in self._topics:
                self._topics.append(topic)
        else:
            self.proto.subscribe(topic)
Пример #2
0
class StompHubExtension(MessagingHubExtension, ReconnectingClientFactory):
    username = None
    password = None
    proto = None
    frames = None

    def __init__(self, hub, config):
        from moksha.hub.reactor import reactor

        self.config = config
        self.hub = hub
        self._topics = hub.topics.keys()
        self._frames = []

        port = self.config.get('stomp_port', 61613)
        host = self.config.get('stomp_broker')

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

        reactor.connectTCP(host, int(port), self)

        super(StompHubExtension, self).__init__()

    def buildProtocol(self, addr):
        self.proto = StompProtocol(self, self.username, self.password)
        return self.proto

    def connected(self):
        for topic in self._topics:
            log.debug('Subscribing to %s topic' % topic)
            self.subscribe(topic, callback=lambda msg: None)
        self._topics = []
        for frame in self._frames:
            log.debug('Flushing queued frame')
            self.proto.transport.write(frame.pack())
        self._frames = []

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

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

    def send_message(self, topic, message, **headers):
        f = stomper.Frame()
        f.unpack(stomper.send(topic, message))
        if not self.proto:
            log.debug("Queueing stomp frame for later delivery")
            self._frames.append(f)
        else:
            self.proto.transport.write(f.pack())

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

    def subscribe(self, topic, callback):
        # FIXME -- note, the callback is just thrown away here.
        log.warn("STOMP callback thrown away.")
        if not self.proto:
            if topic not in self._topics:
                self._topics.append(topic)
        else:
            self.proto.subscribe(topic)

        super(StompHubExtension, self).subscribe(topic, callback)
Пример #3
0
class StompHubExtension(MessagingHubExtension, ReconnectingClientFactory):
    username = None
    password = None
    proto = None
    frames = None

    def __init__(self, hub, config):
        from moksha.hub.reactor import reactor

        self.config = config
        self.hub = hub
        self._topics = hub.topics.keys()
        self._frames = []

        port = self.config.get('stomp_port', 61613)
        host = self.config.get('stomp_broker')

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

        reactor.connectTCP(host, int(port), self)

        super(StompHubExtension, self).__init__()

    def buildProtocol(self, addr):
        self.proto = StompProtocol(self, self.username, self.password)
        return self.proto

    def connected(self):
        for topic in self._topics:
            log.debug('Subscribing to %s topic' % topic)
            self.subscribe(topic, callback=lambda msg: None)
        self._topics = []
        for frame in self._frames:
            log.debug('Flushing queued frame')
            self.proto.transport.write(frame.pack())
        self._frames = []

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

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

    def send_message(self, topic, message, **headers):
        f = stomper.Frame()
        f.unpack(stomper.send(topic, message))
        if not self.proto:
            log.debug("Queueing stomp frame for later delivery")
            self._frames.append(f)
        else:
            self.proto.transport.write(f.pack())

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

    def subscribe(self, topic, callback):
        # FIXME -- note, the callback is just thrown away here.
        log.warn("STOMP callback thrown away.")
        if not self.proto:
            if topic not in self._topics:
                self._topics.append(topic)
        else:
            self.proto.subscribe(topic)

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