Class: Smirc::Queue

Inherits:
Object
  • Object
show all
Includes:
Eventful
Defined in:
lib/smirc/queue.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeQueue

Returns a new instance of Queue.



6
7
8
9
10
11
# File 'lib/smirc/queue.rb', line 6

def initialize
  @locked = false
  @messages = []
  @transferred = 0
  @limit = 1400
end

Instance Attribute Details

#limitObject

Returns the value of attribute limit.



4
5
6
# File 'lib/smirc/queue.rb', line 4

def limit
  @limit
end

#lockedObject

Returns the value of attribute locked.



4
5
6
# File 'lib/smirc/queue.rb', line 4

def locked
  @locked
end

#messagesObject

Returns the value of attribute messages.



4
5
6
# File 'lib/smirc/queue.rb', line 4

def messages
  @messages
end

#transferredObject

Returns the value of attribute transferred.



4
5
6
# File 'lib/smirc/queue.rb', line 4

def transferred
  @transferred
end

Instance Method Details

#<<(message) ⇒ Object



29
30
31
32
# File 'lib/smirc/queue.rb', line 29

def <<(message)
  @messages << message
  invoke
end

#invokeObject



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/smirc/queue.rb', line 43

def invoke
  while(should_invoke?) do
    unless limit_exceeded?(messages.first.size)
      message = messages.shift
      send(message) unless limit_exceeded?
      @transferred += message.size
      lock if limit_exceeded? 
    else
      lock
    end
  end
end

#limit_exceeded?(additional = 0) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/smirc/queue.rb', line 56

def limit_exceeded?(additional = 0)
  @transferred + additional > @limit
end

#lockObject



13
14
15
16
# File 'lib/smirc/queue.rb', line 13

def lock
  @locked = true
  fire(:lock)
end

#locked?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/smirc/queue.rb', line 25

def locked?
  @locked
end

#send(message) ⇒ Object



60
61
62
# File 'lib/smirc/queue.rb', line 60

def send(message)
  fire(:send, message)
end

#should_invoke?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/smirc/queue.rb', line 38

def should_invoke?
  !locked? && size > 0
end

#sizeObject



34
35
36
# File 'lib/smirc/queue.rb', line 34

def size
  @messages.size
end

#unlockObject



18
19
20
21
22
23
# File 'lib/smirc/queue.rb', line 18

def unlock
  @locked = false
  @transferred = 0
  fire(:unlock)
  invoke
end