Class: Arachni::Reactor::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/arachni/reactor/queue.rb

Overview

Note:

Pretty much an EventMachine::Queue rip-off.

A cross thread, Reactor scheduled, linear queue.

This class provides a simple queue abstraction on top of the scheduler.

It services two primary purposes:

  • API sugar for stateful protocols.
  • Pushing processing onto the reactor thread.

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reactor) ⇒ Queue

Returns a new instance of Queue.

Parameters:



32
33
34
35
36
# File 'lib/arachni/reactor/queue.rb', line 32

def initialize( reactor )
    @reactor = reactor
    @items   = []
    @waiting = []
end

Instance Attribute Details

#reactorReactor (readonly)

Returns:



29
30
31
# File 'lib/arachni/reactor/queue.rb', line 29

def reactor
  @reactor
end

Instance Method Details

#empty?Boolean

Note:

This is a peek, it's not thread safe, and may only tend toward accuracy.

Returns:

  • (Boolean)


68
69
70
# File 'lib/arachni/reactor/queue.rb', line 68

def empty?
    @items.empty?
end

#num_waitingInteger

Note:

Accuracy cannot be guaranteed.

Returns Number of jobs that are currently waiting on the Queue for items to appear.

Returns:

  • (Integer)

    Number of jobs that are currently waiting on the Queue for items to appear.



84
85
86
# File 'lib/arachni/reactor/queue.rb', line 84

def num_waiting
    @waiting.size
end

#pop(&block) ⇒ Object

Parameters:

  • block (Block)

    Block to be scheduled by the Arachni::Reactor and passed an item from the queue as soon as one becomes available.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/arachni/reactor/queue.rb', line 41

def pop( &block )
    @reactor.schedule do
        if @items.empty?
            @waiting << block
        else
            block.call @items.shift
        end
    end

    nil
end

#push(item) ⇒ Object Also known as: <<

Parameters:

  • item (Object)

    Schedules an item for addition to the queue.



55
56
57
58
59
60
61
62
# File 'lib/arachni/reactor/queue.rb', line 55

def push( item )
    @reactor.schedule do
        @items.push( item )
        @waiting.shift.call @items.shift until @items.empty? || @waiting.empty?
    end

    nil
end

#sizeInteger

Note:

This is a peek, it's not thread safe, and may only tend toward accuracy.

Returns Queue size.

Returns:

  • (Integer)

    Queue size.



76
77
78
# File 'lib/arachni/reactor/queue.rb', line 76

def size
    @items.size
end