Class: ModSpox::MessageFactory

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

Instance Attribute Summary

Attributes inherited from Pool

#proc, #queue

Instance Method Summary collapse

Methods inherited from Pool

#destroy, #start_pool

Constructor Details

#initialize(pipeline) ⇒ MessageFactory

pipeline

Message pipeline

Create a new MessageFactory



12
13
14
15
16
17
18
19
# File 'lib/mod_spox/MessageFactory.rb', line 12

def initialize(pipeline)
    super()
    @pipeline = pipeline
    @handlers = Hash.new
    Logger.log("Created new factory queue: #{@queue}", 15)
    build_handlers
    start_pool
end

Instance Method Details

#<<(string) ⇒ Object

string

server message to be parsed

Parses messages from server. This is placed in a queue to be processed thus there is now wait for processing to be completed.



25
26
27
# File 'lib/mod_spox/MessageFactory.rb', line 25

def <<(string)
    @queue << string
end

#build_handlersObject

Builds the message handlers. This will load all Messages and Handlers found in the lib directory and then initialize all the Handlers



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mod_spox/MessageFactory.rb', line 31

def build_handlers
    # load our handlers in first
    # note: the handlers add themselves to the @handlers hash
    # during initialization
    Handlers.constants.each{|name|
        klass = Handlers.const_get(name)
        if(klass < Handlers::Handler)
            Logger.log("Building handler: #{name}")
            begin
                klass.new(@handlers)
            rescue Object => boom
                Logger.log("ERROR: Failed to build handler: #{name} -> #{boom}")
            end
        end
    }
    Logger.log("Handlers now available:", 15)
    @handlers.each_pair{|k,v| Logger.log("#{k} -> #{v}", 15)}
end

#parse_message(message) ⇒ Object

message

server message

Parses the server message and passes it to the proper handler



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

def parse_message(message)
    Logger.log("Processing message: #{message}", 15)
    begin
        if(message =~ /^:\S+ (\S+)/ || message =~ /^([A-Za-z0-9]+)\s/)
            key = $1
            key = key.to_sym unless key[0].chr =~ /\d/
            if(@handlers.has_key?(key))
                Logger.log("Message of type #{key} is now being handled by #{@handlers[key]}", 10)
                message = @handlers[key].process(message)
                @pipeline << message unless message.nil?
            end
        end
    rescue Object => boom
        Logger.log("Failed to parse message from server: #{boom}\n#{boom.backtrace.join("\n")}")
    end
end