Ejemplo n.º 1
0
  def delete(self, queue, if_unused=False, if_empty=False, nowait=True, 
    ticket=None, cb=None):
    '''
    queue delete.
    '''
    nowait = nowait and self.allow_nowait() and not cb

    args = Writer()
    args.write_short(ticket or self.default_ticket).\
      write_shortstr(queue).\
      write_bits(if_unused, if_empty, nowait)
    self.send_frame( MethodFrame(self.channel_id, 50, 40, args) )

    if not nowait:
      self._delete_cb.append( cb )
      return self.channel.add_synchronous_cb( self._recv_delete_ok )
Ejemplo n.º 2
0
    def purge(self, queue, nowait=True, ticket=None, cb=None):
        '''
    Purge all messages in a queue.
    '''
        # If a callback is defined, then we have to use synchronous transactions.
        if cb: nowait = False

        args = Writer()
        args.write_short(ticket or self.default_ticket).\
          write_shortstr(queue).\
          write_bit(nowait)
        self.send_frame(MethodFrame(self.channel_id, 50, 30, args))

        if not nowait:
            self.channel.add_synchronous_cb(self._recv_purge_ok)
            self._purge_cb.append(cb)
Ejemplo n.º 3
0
  def delete(self, exchange, if_unused=False, nowait=True, ticket=None, cb=None):
    '''
    Delete an exchange.
    '''
    # If a callback is defined, then we have to use synchronous transactions.
    if cb: nowait = False

    args = Writer()
    args.write_short(ticket or self.default_ticket).\
      write_shortstr(exchange).\
      write_bits(if_unused, nowait)
    self.send_frame( MethodFrame(self.channel_id, 40, 20, args) )
    
    if not nowait:
      self.channel.add_synchronous_cb( self._recv_delete_ok )
      self._delete_cb.append( cb )
Ejemplo n.º 4
0
    def select(self, nowait=True, cb=None):
        '''
    Set this channel to use publisher confirmations.
    '''
        nowait = nowait and self.allow_nowait() and not cb

        if not self._enabled:
            self._enabled = True
            self.channel.basic._msg_id = 0
            self.channel.basic._last_ack_id = 0
            args = Writer()
            args.write_bit(nowait)

            self.send_frame(MethodFrame(self.channel_id, 85, 10, args))

            if not nowait:
                self._select_cb.append(cb)
                self.channel.add_synchronous_cb(self._recv_select_ok)
Ejemplo n.º 5
0
  def bind(self, queue, exchange, routing_key='', nowait=True, arguments={}, 
      ticket=None, cb=None):
    '''
    bind to a queue.
    '''
    nowait = nowait and self.allow_nowait() and not cb

    args = Writer()
    args.write_short(ticket or self.default_ticket).\
      write_shortstr(queue).\
      write_shortstr(exchange).\
      write_shortstr(routing_key).\
      write_bit(nowait).\
      write_table(arguments)
    self.send_frame( MethodFrame(self.channel_id, 50, 20, args) )
    
    if not nowait:
      self._bind_cb.append( cb )
      self.channel.add_synchronous_cb( self._recv_bind_ok )
Ejemplo n.º 6
0
    def unbind(self,
               queue,
               exchange,
               routing_key='',
               arguments={},
               ticket=None,
               cb=None):
        '''
    Unbind a queue from an exchange.  This is always synchronous.
    '''
        args = Writer()
        args.write_short(ticket or self.default_ticket).\
          write_shortstr(queue).\
          write_shortstr(exchange).\
          write_shortstr(routing_key).\
          write_table(arguments)
        self.send_frame(MethodFrame(self.channel_id, 50, 50, args))

        self._unbind_cb.append(cb)
        self.channel.add_synchronous_cb(self._recv_unbind_ok)
Ejemplo n.º 7
0
  def declare(self, queue='', passive=False, durable=False,
      exclusive=False, auto_delete=True, nowait=True,
      arguments={}, ticket=None, cb=None):
    '''
    Declare a queue.  By default is asynchronoous but will be synchronous if nowait=False
    or a callback is defined. In synchronous mode, returns (message_count, consumer_count)

    queue - The name of the queue
    cb - An optional method which will be called with (queue_name, msg_count, consumer_count)
         if nowait=False
    '''
    nowait = nowait and self.allow_nowait() and not cb

    args = Writer()
    args.write_short(ticket or self.default_ticket).\
      write_shortstr(queue).\
      write_bits(passive, durable, exclusive, auto_delete, nowait).\
      write_table(arguments)
    self.send_frame( MethodFrame(self.channel_id, 50, 10, args) )

    if not nowait:
      self._declare_cb.append( cb )
      return self.channel.add_synchronous_cb( self._recv_declare_ok )
Ejemplo n.º 8
0
  def declare(self, exchange, type, passive=False, durable=False,\
      auto_delete=True, internal=False, nowait=True, arguments=None, \
      ticket=None, cb=None):
    """
    Declare the exchange.

    exchange - The name of the exchange to declare
    type - One of 
    """
    # If a callback is defined, then we have to use synchronous transactions.
    if cb: nowait = False

    args = Writer()
    args.write_short(ticket or self.default_ticket).\
      write_shortstr(exchange).\
      write_shortstr(type).\
      write_bits(passive, durable, auto_delete, internal, nowait).\
      write_table(arguments or {})
    self.send_frame( MethodFrame(self.channel_id, 40, 10, args) )

    if not nowait:
      self.channel.add_synchronous_cb( self._recv_declare_ok )
      self._declare_cb.append( cb )
Ejemplo n.º 9
0
  def consume(self, queue, consumer, consumer_tag='', no_local=False,
        no_ack=True, exclusive=False, nowait=True, ticket=None, cb=None):
    '''
    Start a queue consumer. If `cb` is supplied, will be called when
    broker confirms that consumer is registered.
    '''
    nowait = nowait and self.allow_nowait() and not cb

    if nowait and consumer_tag=='':
      consumer_tag = self._generate_consumer_tag()

    args = Writer()
    args.write_short(ticket or self.default_ticket).\
      write_shortstr(queue).\
      write_shortstr(consumer_tag).\
      write_bits( no_local, no_ack, exclusive, nowait ).\
      write_table({}) # unused according to spec
    self.send_frame( MethodFrame(self.channel_id, 60, 20, args) )

    if not nowait:
      self._pending_consumers.append( (consumer,cb) )
      self.channel.add_synchronous_cb( self._recv_consume_ok )
    else:
      self._consumer_cb[ consumer_tag ] = consumer
Ejemplo n.º 10
0
    def publish(self,
                msg,
                exchange,
                routing_key,
                mandatory=False,
                immediate=False,
                ticket=None):
        '''
    publish a message.
    '''
        args = Writer()
        args.write_short(ticket or self.default_ticket).\
          write_shortstr(exchange).\
          write_shortstr(routing_key).\
          write_bits(mandatory, immediate)

        self.send_frame(MethodFrame(self.channel_id, 60, 40, args))
        self.send_frame(
            HeaderFrame(self.channel_id, 60, 0, len(msg), msg.properties))

        frame_max = self.channel.connection.frame_max
        for frame in ContentFrame.create_frames(self.channel_id, msg.body,
                                                frame_max):
            self.send_frame(frame)