Class: Cinch::MessageQueue Private

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

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.

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.



7
8
9
10
11
12
13
14
# File 'lib/cinch/message_queue.rb', line 7

def initialize(socket, bot)
  @socket               = socket
  @queue                = Queue.new
  @time_since_last_send = nil
  @bot                  = bot

  @log = []
end

Instance Method Details

#process!void

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.



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
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/cinch/message_queue.rb', line 28

def process!
  while true
    mps            = @bot.config.messages_per_second
    max_queue_size = @bot.config.server_queue_size

    if @log.size > 1
      time_passed = 0

      @log.each_with_index do |one, index|
        second = @log[index+1]
        time_passed += second - one
        break if index == @log.size - 2
      end

      messages_processed = (time_passed * mps).floor
      effective_size = @log.size - messages_processed

      if effective_size <= 0
        @log.clear
      elsif effective_size >= max_queue_size
        sleep 1.0/mps
      end
    end

    message = @queue.pop.to_s.chomp

    begin
      @socket.writeline Cinch.encode_outgoing(message, @bot.config.encoding) + "\r\n"
      @log << Time.now
      @bot.logger.log(message, :outgoing) if @bot.config.verbose

      @time_since_last_send = Time.now
    rescue IOError
      @bot.debug "Could not send message (connectivity problems): #{message}"
    end
  end
end

#queue(message) ⇒ void

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.



17
18
19
20
21
22
23
24
25
# File 'lib/cinch/message_queue.rb', line 17

def queue(message)
  command = message.split(" ").first

  if command == "PONG"
    @queue.unshift(message)
  else
    @queue << message
  end
end