Class: IRC::Client::CommandHandler
- Inherits:
-
Object
- Object
- IRC::Client::CommandHandler
- Defined in:
- lib/irc/client/command_handler.rb
Constant Summary collapse
- DELAY_IN_MS =
1000 / 1000
Instance Attribute Summary collapse
-
#delay ⇒ Object
The delay in milliseconds between two commands that are sent to the server.
Instance Method Summary collapse
-
#<<(command) ⇒ Object
Sends the command through a queue to the server.
-
#initialize(context) ⇒ CommandHandler
constructor
A new instance of CommandHandler.
-
#send_command(command) ⇒ Object
Sends the command directly to the server bypassing the queue.
-
#send_command_via_queue(command) ⇒ Object
Sends the command through a queue to the server.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(context) ⇒ CommandHandler
Returns a new instance of CommandHandler.
43 44 45 46 47 48 |
# File 'lib/irc/client/command_handler.rb', line 43 def initialize(context) @log = Log4r::Logger.new('IRC::Client::CommandHandler') @context = context @queue = Queue.new @delay = DELAY_IN_MS end |
Instance Attribute Details
#delay ⇒ Object
The delay in milliseconds between two commands that are sent to the server.
41 42 43 |
# File 'lib/irc/client/command_handler.rb', line 41 def delay @delay end |
Instance Method Details
#<<(command) ⇒ Object
Sends the command through a queue to the server. Sending commands through a queue prevents from flooding the IRC server.
52 53 54 |
# File 'lib/irc/client/command_handler.rb', line 52 def <<(command) send_command_via_queue(command) end |
#send_command(command) ⇒ Object
Sends the command directly to the server bypassing the queue. Sending commands too fast to the server can result in a disconnect from the server.
58 59 60 61 |
# File 'lib/irc/client/command_handler.rb', line 58 def send_command(command) @context.output_socket << "#{command.to_s}\r\n" @log.debug("[#{@context.network.to_s.upcase}] >>> #{command.to_s}") end |
#send_command_via_queue(command) ⇒ Object
Sends the command through a queue to the server. Sending commands through a queue prevents from flooding the IRC server.
65 66 67 |
# File 'lib/irc/client/command_handler.rb', line 65 def send_command_via_queue(command) @queue << command end |
#start ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/irc/client/command_handler.rb', line 69 def start # Don't start the command handler if already started. raise CommandHandlerError.new("Can't start command handler. Already started.") if @thread != nil # Start the command handler thread. @thread = Thread.new do loop do # Get the next command from the command queue. command = @queue.pop # Send the command to the server. send_command(command) # Wait some milliseconds to prevent server flodding. sleep(delay) end end @log.debug("[#{@context.network.to_s.upcase}] Command handler successfully started.") end |
#stop ⇒ Object
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/irc/client/command_handler.rb', line 96 def stop # Don't stop the command handler if already stopped. raise CommandHandlerError.new("Can't stop command handler. Already stopped.") if @thread == nil # Kill the thread & clear the command queue. Thread.kill(@thread) @queue.clear @log.debug("[#{@context.network.to_s.upcase}] Command handler successfully stopped.") end |