Class: Cinch::MessageQueue Private

Inherits:
Object
  • Object
show all
Defined in:
lib/cinch/message_queue.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

API:

  • private

Instance Method Summary collapse

Constructor Details

#initialize(socket, bot) ⇒ MessageQueue

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of MessageQueue.

API:

  • private



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cinch/message_queue.rb', line 10

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

Instance Method Details

#process!

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

API:

  • private



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
# File 'lib/cinch/message_queue.rb', line 55

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)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

API:

  • private



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/cinch/message_queue.rb', line 25

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