Class: Isaac::Queue

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(socket) ⇒ Queue

Returns a new instance of Queue.



210
211
212
213
214
215
216
# File 'lib/isaac.rb', line 210

def initialize(socket)
  @socket     = socket
  @queue      = []
  @transfered = 0
  @lock       = true
  transmit
end

Instance Attribute Details

#lockObject

Returns the value of attribute lock.



209
210
211
# File 'lib/isaac.rb', line 209

def lock
  @lock
end

Instance Method Details

#<<(msg) ⇒ Object

I luvz Rubyz



219
220
221
222
# File 'lib/isaac.rb', line 219

def << (msg)
  # .flatten! returns nill if no modifications were made, thus we do this. 
  @queue = (@queue << msg).flatten
end

#transmitObject

To prevent excess flood no more than 1472 bytes will be sent to the server. When that limit is reached, @lock = true and the server will be PINGed. @lock will be true until a PONG is received (Application#handle).



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/isaac.rb', line 227

def transmit
  Thread.start { loop {
    unless @lock || @queue.empty?
      msg = @queue.first
      if (@transfered + msg.size) > 1472
        # No honestly, :excess. The RFC is not too clear on this subject TODO
        @socket.puts "PING :excess"
        @lock = true
        @transfered = 0
      else
        @socket.puts msg
        @transfered += msg.size
        @queue.shift
      end
    end
    sleep 0.1
  }}
end