Class: Arachni::Reactor::Queue
- Inherits:
-
Object
- Object
- Arachni::Reactor::Queue
- 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.
Instance Attribute Summary collapse
- #reactor ⇒ Reactor readonly
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(reactor) ⇒ Queue
constructor
A new instance of Queue.
-
#num_waiting ⇒ Integer
Number of jobs that are currently waiting on the Queue for items to appear.
- #pop(&block) ⇒ Object
- #push(item) ⇒ Object (also: #<<)
-
#size ⇒ Integer
Queue size.
Constructor Details
#initialize(reactor) ⇒ Queue
Returns a new instance of Queue.
32 33 34 35 36 |
# File 'lib/arachni/reactor/queue.rb', line 32 def initialize( reactor ) @reactor = reactor @items = [] @waiting = [] end |
Instance Attribute Details
#reactor ⇒ Reactor (readonly)
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.
68 69 70 |
# File 'lib/arachni/reactor/queue.rb', line 68 def empty? @items.empty? end |
#num_waiting ⇒ Integer
Note:
Accuracy cannot be guaranteed.
Returns 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
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: <<
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 |
#size ⇒ Integer
Note:
This is a peek, it's not thread safe, and may only tend toward accuracy.
Returns Queue size.
76 77 78 |
# File 'lib/arachni/reactor/queue.rb', line 76 def size @items.size end |