Module: DripDrop::ZMQWritableHandler

Included in:
Mongrel2Handler, ZMQPubHandler, ZMQPushHandler, ZMQXRepHandler, ZMQXReqHandler
Defined in:
lib/dripdrop/handlers/zeromq.rb

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



55
56
57
58
59
# File 'lib/dripdrop/handlers/zeromq.rb', line 55

def initialize(*args)
  super(*args)
  @send_queue = []
  @send_queue_enabled = false
end

#on_writable(conn) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/dripdrop/handlers/zeromq.rb', line 61

def on_writable(conn)
  unless @send_queue.empty?
    message = @send_queue.shift

    conn.send_msg(*message)
  else
    conn.deregister_writable if @send_queue_enabled
  end
end

#send_message(message) ⇒ Object

Sends a message, accepting either a DripDrop::Message, a hash that looks like a DripDrop::Message (has keys :name, :head, :body), or your own custom messages. Custom messages should either be a String, or for multipart messages, an Array of String objects.



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/dripdrop/handlers/zeromq.rb', line 75

def send_message(message)
  dd_message = dd_messagify(message)
  if dd_message.is_a?(DripDrop::Message)
    @send_queue.push([dd_message.encoded])
  elsif message.class == Array
    @send_queue.push(message)
  else
    @send_queue.push([message])
  end
  
  self.write_connection.register_writable if @send_queue_enabled
     
  EM::next_tick {
    on_writable(self.write_connection)
  }
end