Class: ModSpox::Pipeline

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

Instance Attribute Summary

Attributes inherited from Pool

#proc, #queue

Instance Method Summary collapse

Methods inherited from Pool

#destroy, #start_pool

Constructor Details

#initializePipeline

Create a new Pipeline



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/mod_spox/Pipeline.rb', line 6

def initialize
    super()
    @timeout = 20 # Anything over 20 seconds and we assume a plugin locked up the thread
    Logger.log("Created queue #{@queue} in pipeline", 10)
    @hooks = Hash.new
    @plugins = Hash.new
    @admin = Models::Group.filter(:name => 'admin').first
    populate_triggers
    populate_signatures
    hook(self, :populate_triggers, :Internal_TriggersUpdate)
    hook(self, :populate_signatures, :Internal_SignaturesUpdate)
    start_pool
end

Instance Method Details

#<<(message) ⇒ Object

message

Message to send down pipeline

Queues a message to send down pipeline



22
23
24
25
# File 'lib/mod_spox/Pipeline.rb', line 22

def <<(message)
    Logger.log("Message added to pipeline queue: #{message}", 5)
    @queue << message
end

#clearObject

Clears all hooks from the pipeline (Commonly used when reloading plugins)



77
78
79
80
81
# File 'lib/mod_spox/Pipeline.rb', line 77

def clear
    Logger.log("All hooks have been cleared from pipeline", 10)
    @hooks.clear
    @plugins.clear
end

#hook(object, method, type) ⇒ Object

plugin

Plugin to hook to pipeline

method

Plugin method pipeline should call to process message

type

Type of message the plugin wants to process

Hooks a plugin into the pipeline for a specific type of message



50
51
52
53
54
55
56
57
58
# File 'lib/mod_spox/Pipeline.rb', line 50

def hook(object, method, type)
    Logger.log("Object #{object.class.to_s} hooking into messages of type: #{type}", 10)
    type = type.gsub(/::/, '_').to_sym unless type.is_a?(Symbol)
    method = method.to_sym unless method.is_a?(Symbol)
    name = object.class.to_s.gsub(/^.+:/, '')
    @hooks[type] = Hash.new unless @hooks.has_key?(type)
    @hooks[type][name.to_sym] = Array.new unless @hooks[type][name.to_sym].is_a?(Array)
    @hooks[type][name.to_sym] << {:object => object, :method => method} 
end

#hook_plugin(plugin) ⇒ Object

plugin

Plugin to hook to pipeline

Hooks a plugin into the pipeline so it can be called directly when it matches a trigger



30
31
32
33
# File 'lib/mod_spox/Pipeline.rb', line 30

def hook_plugin(plugin)
    Logger.log("Plugin #{plugin.name} hooking into pipeline", 10)
    @plugins[plugin.name.to_sym] = plugin
end

#populate_signatures(m = nil) ⇒ Object

Repopulate the active signatures list



90
91
92
93
# File 'lib/mod_spox/Pipeline.rb', line 90

def populate_signatures(m=nil)
    @signatures = []
    Models::Signature.all.each{|s|@signatures << s}
end

#populate_triggers(m = nil) ⇒ Object

Repopulate the active trigger list



84
85
86
87
# File 'lib/mod_spox/Pipeline.rb', line 84

def populate_triggers(m=nil)
    @triggers = []
    Models::Trigger.filter(:active => true).each{|t|@triggers << t.trigger}
end

#unhook(object, method, type) ⇒ Object

plugin

Plugin to unhook from pipeline

type

Type of message the plugin no longer wants to process

This will remove the hook a plugin has for a specific message type



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/mod_spox/Pipeline.rb', line 63

def unhook(object, method, type)
    Logger.log("Object #{object.class.to_s} unhooking from messages of type: #{type}", 10)
    type = type.gsub(/::/, '_').to_sym unless type.is_a?(Symbol)
    name = object.class.to_s.gsub(/^.+:/, '').to_sym
    raise Exceptions::InvalidValue.new("Unknown hook type given: #{type.to_s}") unless @hooks.has_key?(type)
    raise Exceptions::InvalidValue.new("Unknown object hooked: #{name.to_s}") unless @hooks[type].has_key?(name)
    @hooks[type][name].each{|hook|
        @hooks[type][name].delete(hook) if hook[:method] == method
    }
    @hooks[type].delete(name) if @hooks[type][name].empty
    @hooks.delete(type) if @hooks[type].empty?
end

#unhook_plugin(plugin) ⇒ Object

plugin

Plugin to unhook from pipeline

Unhooks a plugin from the pipeline (This does not unhook it from the standard hooks)



38
39
40
41
42
43
44
# File 'lib/mod_spox/Pipeline.rb', line 38

def unhook_plugin(plugin)
    Logger.log("Plugin #{plugin.name} unhooking from plugin", 10)
    @plugins.delete(plugin.name.to_sym)
    @hooks.each_pair do |type, things|
        things.delete(plugin.name.to_sym) if things.has_key?(plugin.name.to_sym)
    end
end