+ class QueueRing
+ # A QueueRing is implemented as an array with elements in the form
+ # [chan, [message1, message2, ...]
+ # Note that the channel +chan+ has no actual bearing with the channels
+ # to which messages will be sent
+
+ def initialize
+ @storage = Array.new
+ @last_idx = -1
+ end
+
+ def clear
+ @storage.clear
+ @last_idx = -1
+ end
+
+ def length
+ len = 0
+ @storage.each {|c|
+ len += c[1].size
+ }
+ return len
+ end
+ alias :size :length
+
+ def empty?
+ @storage.empty?
+ end
+
+ def push(mess, chan)
+ cmess = @storage.assoc(chan)
+ if cmess
+ idx = @storage.index(cmess)
+ cmess[1] << mess
+ @storage[idx] = cmess
+ else
+ @storage << [chan, [mess]]
+ end
+ end
+
+ def next
+ if empty?
+ warning "trying to access empty ring"
+ return nil
+ end
+ save_idx = @last_idx
+ @last_idx = (@last_idx + 1) % @storage.size
+ mess = @storage[@last_idx][1].first
+ @last_idx = save_idx
+ return mess
+ end
+
+ def shift
+ if empty?
+ warning "trying to access empty ring"
+ return nil
+ end
+ @last_idx = (@last_idx + 1) % @storage.size
+ mess = @storage[@last_idx][1].shift
+ @storage.delete(@storage[@last_idx]) if @storage[@last_idx][1] == []
+ return mess
+ end
+
+ end
+
+ class MessageQueue
+ 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
+ end
+
+ def clear
+ @rings.each { |r|
+ r.clear
+ }
+ @last_ring = 0
+ end
+
+ def push(mess, chan=nil, cring=0)
+ ring = cring
+ 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
+ end
+
+ def empty?
+ @rings.each { |r|
+ return false unless r.empty?
+ }
+ return true
+ end
+
+ def length
+ len = 0
+ @rings.each { |r|
+ len += r.size
+ }
+ len
+ end
+ alias :size :length
+
+ def next
+ if empty?
+ warning "trying to access empty ring"
+ return nil
+ end
+ mess = nil
+ if !@rings[0].empty?
+ mess = @rings[0].first
+ else
+ save_ring = @last_ring
+ (@rings.size - 1).times {
+ @last_ring = (@last_ring % (@rings.size - 1)) + 1
+ if !@rings[@last_ring].empty?
+ mess = @rings[@last_ring].next
+ break
+ end
+ }
+ @last_ring = save_ring
+ end
+ error "nil message" if mess.nil?
+ return mess
+ end
+
+ def shift
+ if empty?
+ warning "trying to access empty ring"
+ return nil
+ end
+ mess = nil
+ if !@rings[0].empty?
+ return @rings[0].shift
+ end
+ (@rings.size - 1).times {
+ @last_ring = (@last_ring % (@rings.size - 1)) + 1
+ if !@rings[@last_ring].empty?
+ return @rings[@last_ring].shift
+ end
+ }
+ error "nil message" if mess.nil?
+ return mess
+ end
+
+ end
+