Exemplo n.º 1
0
    def __init__(self, messaging, agent):
        log.Logger.__init__(self, messaging)
        self._messaging = messaging
        self._agent = IMessagingPeer(agent)

        self._bindings = []
        self._queue_name = self._agent.get_queue_name()
        self.log_name = self._queue_name
        self._queue = None
        self._disconnect = False
        self._consumeDeferred = None
Exemplo n.º 2
0
class Connection(log.Logger):

    implements(IMessagingClient)

    def __init__(self, messaging, agent):
        log.Logger.__init__(self, messaging)
        self._messaging = messaging
        self._agent = IMessagingPeer(agent)

        self._bindings = []
        self._queue_name = self._agent.get_queue_name()
        self.log_name = self._queue_name
        self._queue = None
        self._disconnect = False
        self._consumeDeferred = None

    def initiate(self):
        if self._queue_name is not None:
            d = defer.maybeDeferred(self._messaging.defineQueue,
                self._queue_name)
            d.addCallback(self._mainLoop)
        else:
            self.warning('Queue name is None, skipping creating queue '
                         'and consumer.')
            d = defer.succeed(None)
        d.addCallback(lambda _: self)
        return d

    def _mainLoop(self, queue):
        self._queue = queue

        def rebind(_):
            reactor.callLater(0, bind)

        def stop(reason):
            if reason.check(FinishConnection):
                self.log('Error handler: exiting, reason %r' % reason)
            else:
                reason.raiseException()

        def bind():
            if self._disconnect:
                return
            d = self._consumeQueue(queue)
            d.addCallbacks(rebind, stop)

        bind()

    def _consumeQueue(self, queue):

        def get_and_call_on_message(message):
            # it is important to always lookup the current message handler
            # maybe someone bound callback to it ?
            on_message = self._agent.on_message
            return on_message(message)

        self._consumeDeferred = queue.get()
        self._consumeDeferred.addCallback(get_and_call_on_message)
        return self._consumeDeferred

    def _append_binding(self, binding):
        self._bindings.append(binding)

    def _remove_binding(self, binding):
        self._bindings.remove(binding)

    # IMessagingClient implementation

    def disconnect(self):
        self._disconnect = True
        if self._consumeDeferred and not self._consumeDeferred.called:
            ex = FinishConnection("Disconnecting")
            self._consumeDeferred.errback(ex)
        self._messaging.disconnect()

    def personal_binding(self, key, shard=None):
        if not shard:
            shard = self._agent.get_shard_name()
        return PersonalBinding(self, key=key, shard=shard)

    def publish(self, key, shard, message):
        if not isinstance(message, BaseMessage):
            raise ValueError(
                'Expected third argument to be f.a.b.BaseMessage, '
                'got %r instead' % type(message))
        return self._messaging.publish(key, shard, message)

    def get_bindings(self, shard=None):
        if shard:
            return filter(lambda x: x.shard == shard, self._bindings)
        else:
            return self._bindings