Class: Irc::MessageQueue

Inherits:
Object show all
Defined in:
lib/rbot/ircsocket.rb

Instance Method Summary collapse

Constructor Details

#initializeMessageQueue

Returns a new instance of MessageQueue.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rbot/ircsocket.rb', line 139

def initialize
  # a MessageQueue is an array of QueueRings
  # rings have decreasing priority, so messages in ring 0
  # are more important than messages in ring 1, and so on
  @rings = Array.new(3) { |i|
    if i > 0
      QueueRing.new
    else
      # ring 0 is special in that if it's not empty, it will
      # be popped. IOW, ring 0 can starve the other rings
      # ring 0 is strictly FIFO and is therefore implemented
      # as an array
      Array.new
    end
  }
  # the other rings are satisfied round-robin
  @last_ring = 0
  self.extend(MonitorMixin)
  @non_empty = self.new_cond
end

Instance Method Details

#clearObject



160
161
162
163
164
165
# File 'lib/rbot/ircsocket.rb', line 160

def clear
  self.synchronize do
    @rings.each { |r| r.clear }
    @last_ring = 0
  end
end

#push(mess, chan = nil, cring = 0) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/rbot/ircsocket.rb', line 167

def push(mess, chan=nil, cring=0)
  ring = cring
  self.synchronize do
    if ring == 0
      warning "message #{mess} at ring 0 has channel #{chan}: channel will be ignored" if !chan.nil?
      @rings[0] << mess
    else
      error "message #{mess} at ring #{ring} must have a channel" if chan.nil?
      @rings[ring].push mess, chan
    end
    @non_empty.signal
  end
end

#shift(tmout = nil) ⇒ Object



181
182
183
184
185
186
# File 'lib/rbot/ircsocket.rb', line 181

def shift(tmout = nil)
  self.synchronize do
    @non_empty.wait(tmout) if self.empty?
    return unsafe_shift
  end
end