Parent

Class/Module Index [+]

Quicksearch

Cinch::MessageQueue

This class manages all outgoing messages, applying rate throttling and fair distribution.

@api private

Public Class Methods

new(socket, bot) click to toggle source
# File lib/cinch/message_queue.rb, line 9
def initialize(socket, bot)
  @socket               = socket
  @queues               = {:generic => OpenEndedQueue.new}

  @queues_to_process    = Queue.new
  @queued_queues        = Set.new

  @mutex                = Mutex.new
  @time_since_last_send = nil
  @bot                  = bot

  @log = []
end

Public Instance Methods

process!() click to toggle source

@return [void]

# File lib/cinch/message_queue.rb, line 54
def process!
  while true
    wait

    queue = @queues_to_process.pop
    message = queue.pop.to_s.chomp

    if queue.empty?
      @mutex.synchronize do
        @queued_queues.delete(queue)
      end
    else
      @queues_to_process << queue
    end

    begin
      to_send = Cinch::Utilities::Encoding.encode_outgoing(message, @bot.config.encoding)
      @socket.write to_send + "\r\n"
      @log << Time.now
      @bot.loggers.outgoing(message)

      @time_since_last_send = Time.now
    rescue IOError
      @bot.loggers.error "Could not send message (connectivity problems): #{message}"
    end
  end
end
queue(message) click to toggle source

@return [void]

# File lib/cinch/message_queue.rb, line 24
def queue(message)
  command, *rest = message.split(" ")

  queue = nil
  case command
  when "PRIVMSG", "NOTICE"
    @mutex.synchronize do
      # we are assuming that each message has only one target,
      # which will be true as long as the user does not send raw
      # messages.
      #
      # this assumption is also reflected in the computation of
      # passed time and processed messages, since our score does
      # not take weights into account.
      queue = @queues[rest.first] ||= OpenEndedQueue.new
    end
  else
    queue = @queues[:generic]
  end
  queue << message

  @mutex.synchronize do
    unless @queued_queues.include?(queue)
      @queued_queues << queue
      @queues_to_process << queue
    end
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.