Module: DripDrop::ZMQWritableHandler

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

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



37
38
39
40
# File 'lib/dripdrop/handlers/zeromq.rb', line 37

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

#on_writable(socket) ⇒ Object



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/dripdrop/handlers/zeromq.rb', line 42

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

    num_parts = message.length
    message.each_with_index do |part,i|
      # Set the multi-part flag unless this is the last message
      multipart_flag = i + 1 < num_parts ? ZMQ::SNDMORE : 0

      if part.class == ZMQ::Message
        socket.send(part, multipart_flag)
      else
        if part.class == String
          socket.send_string(part, multipart_flag)
        else
          $stderr.write "Can only send Strings, not #{part.class}: #{part}" if @debug
        end
      end
    end
  else
    @connection.deregister_writable
  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.



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dripdrop/handlers/zeromq.rb', line 70

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
  @connection.register_writable
end