Class: IRC::Client::MessageHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/irc/client/message_handler.rb

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ MessageHandler

Returns a new instance of MessageHandler.



40
41
42
43
44
# File 'lib/irc/client/message_handler.rb', line 40

def initialize(context)
  @log = Log4r::Logger.new('IRC::Client::MessageHandler')
  @context = context  
  @terminate = false
end

Instance Method Details

#startObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/irc/client/message_handler.rb', line 46

def start
  
  # Don't start the command handler if already started.
  raise MessageHandlerError.new("Can't start message handler. Already started.") if @thread != nil
  
  # Terminate the message reading loop if true.
  @terminate = false
  
  # Start the message handler thread.
  @thread = Thread.new do
    
    while @terminate == false do
  
      begin
  
        raw_message = nil
        
        # Read the raw message from the socket.              
        Timeout::timeout(0.5) do
          raw_message = socket.gets("\r\n")
        end                
        
        # Log the raw message.
        @log.debug("[#{@context.network.to_s.upcase}] <<< #{raw_message.chomp}")
                   
        # Create the message object & ask the message object to handle it.
        message = message = IRC::Messages::Factory.create(raw_message)   
        message.handle(@context)
        
      rescue IOError => e        
        @terminate = true
        @thread = nil
        
      rescue Timeout::Error => e
        # Ok
      end              

    end
    
  end
  
  @log.debug("[#{@context.network.to_s.upcase}] Message handler successfully started.")
  
end

#stopObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/irc/client/message_handler.rb', line 91

def stop
  
  # Don't stop the command handler if already stopped.
  raise MessageHandlerError.new("Can't stop message handler. Already stopped.") if @thread == nil
  
  # Terminate the message reading loop.
  @terminate = true
  
  # Kill the thread.
#        Thread.kill(@thread)

  @thread = nil
  @log.debug("[#{@context.network.to_s.upcase}] Message handler successfully stopped.")
  
end