Class: ModSpox::PriorityQueue

Inherits:
Object
  • Object
show all
Defined in:
lib/mod_spox/PriorityQueue.rb

Overview

This class provides some simple logic for message output. It is basically a priority based queue with some round robin thrown in, just to keep things interesting. This queue provides protection on message output from extreme lag to one target when another target is expecting large quantities out output NOTE: Design help from the great Ryan “pizza_” Flynn

Instance Method Summary collapse

Constructor Details

#initializePriorityQueue

Create a priority queue



11
12
13
14
15
# File 'lib/mod_spox/PriorityQueue.rb', line 11

def initialize
    @target_queues = {}
    @queues = {:PRIORITY => Queue.new, :NEW => Queue.new, :NORMAL => Queue.new, :WHOCARES => Queue.new}
    @lock = Mutex.new
end

Instance Method Details

#direct_queue(message) ⇒ Object

message

message to send

This will add messages to the PRIORITY queue which gets sent before all other messages.



43
44
45
46
47
48
49
# File 'lib/mod_spox/PriorityQueue.rb', line 43

def direct_queue(message)
    @lock.synchronize do
        @target_queues[:general] = Queue.new unless @target_queues[:general]
        @target_queues[:general] << message
        @queues[:PRIORITY] << @target_queues[:general]
    end
end

#popObject

Returns the next message to send. This method decides what message to send based on the priority of the message. It will throw an Exceptions::EmptyQueue when there are no messages left.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mod_spox/PriorityQueue.rb', line 55

def pop
    m = nil
    @lock.synchronize do
        [:PRIORITY, :NEW, :NORMAL, :WHOCARES].each do |k|
            unless(@queues[k].empty?)
                m = @queues[k].pop.pop
                break
            end
        end
    end
    raise Exceptions::EmptyQueue.new if m.nil?
    return m
end

#priority_queue(target, message) ⇒ Object

target

message target (targets starting with * will be labelled WHOCARES

message

message to send

This prioritizes output to help reduce lag when lots of output is being sent to another target. This will automatically decide how to queue the message based on the target



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mod_spox/PriorityQueue.rb', line 22

def priority_queue(target, message)
    @lock.synchronize do
        target.downcase!
        @target_queues[target] = Queue.new unless @target_queues[target]
        if(target[0].chr == '*')
            @target_queues[target] << message
            @queues[:WHOCARES] << @target_queues[target]
        else
            @target_queues[target] << message
            if(@target_queues[target].size < 2)
                @queues[:NEW] << @target_queues[target]
            else
                @queues[:NORMAL] << @target_queues[target]
            end
        end
    end
end