class CZMQ::ZMessage
Public Class Methods
new(zmsg=nil)
click to toggle source
# File lib/czmq/zmsg.rb, line 3 def initialize(zmsg=nil) @zmsg = zmsg || LibCZMQ.zmsg_new setup_finalizer end
Private Class Methods
close_zmsg(zmsg)
click to toggle source
Need to make this a class method, or the deallocation won’t take place. See: www.mikeperham.com/2010/02/24/the-trouble-with-ruby-finalizers/
# File lib/czmq/zmsg.rb, line 105 def self.close_zmsg(zmsg) Proc.new do LibCZMQ.zmsg_destroy(zmsg) zmsg = nil # Just in case end end
Public Instance Methods
__send_over(zsocket)
click to toggle source
# File lib/czmq/zmsg.rb, line 26 def __send_over(zsocket) raise 'Trying to transmit an unitialized ZMessage!' if @zmsg.nil? # Since by sending it over a zsocket we explicitly close the zmsg, we # have to remove the finalizer. remove_finalizer rc = LibCZMQ.zmsg_send(@zmsg, zsocket) raise 'Error!' unless @zmsg.nil? rc end
append(obj)
click to toggle source
Append new frame at the end of message
# File lib/czmq/zmsg.rb, line 71 def append(obj) raise 'Trying to append to an unitialized ZMessage!' if @zmsg.nil? if obj.is_a? String LibCZMQ.zmsg_addstr(@zmsg, obj) elsif obj.is_a? ZFrame LibCZMQ.zmsg_append(@zmsg, obj.__extract__) else raise ArgumentError, 'Unknown object type!' end end
Also aliased as: <<
content_size()
click to toggle source
# File lib/czmq/zmsg.rb, line 40 def content_size LibCZMQ.zmsg_content_size(@zmsg) end
destroy()
click to toggle source
# File lib/czmq/zmsg.rb, line 17 def destroy if @zmsg # Since we explicitly close the zmsg, we have to remove the finalizer. remove_finalizer LibCZMQ.zmsg_destroy(@zmsg) raise 'Error!' unless @zmsg.nil? end end
initialize_copy(orig)
click to toggle source
Need to provide a copy constructor that implements deep copy
Calls superclass method
# File lib/czmq/zmsg.rb, line 10 def initialize_copy(orig) super unless @zmsg.nil? @zmsg = LibCZMQ.zmsg_dup(@zmsg) end end
pop(type=:zframe)
click to toggle source
# File lib/czmq/zmsg.rb, line 44 def pop(type=:zframe) raise 'Trying to pop from an unitialized ZMessage!' if @zmsg.nil? case type when :zframe ZFrame.new(LibCZMQ.zmsg_pop(@zmsg)) when :string LibCZMQ.zmsg_popstr(@zmsg) end end
push(obj)
click to toggle source
Push new frame in front of message
# File lib/czmq/zmsg.rb, line 56 def push(obj) raise 'Trying to push in front of an unitialized ZMessage!' if @zmsg.nil? if obj.is_a? String LibCZMQ.zmsg_pushstr(@zmsg, obj) elsif obj.is_a? Array LibCZMQ.zmsg_pushmem(@zmsg, obj) elsif obj.is_a? ZFrame LibCZMQ.zmsg_push(@zmsg, obj.__extract__) else raise ArgumentError, 'Unknown object type!' end end
size()
click to toggle source
# File lib/czmq/zmsg.rb, line 36 def size LibCZMQ.zmsg_size(@zmsg) end
Private Instance Methods
remove_finalizer()
click to toggle source
# File lib/czmq/zmsg.rb, line 99 def remove_finalizer ObjectSpace.undefine_finalizer self end
setup_finalizer()
click to toggle source
After object destruction, make sure that the corresponding zmsg is destroyed as well.
NOTE: We don’t care about ensuring, as ffi-rmzq does, that the resource deallocation request comes from the process that allocated it in the first place. In fact, the CZMQ
documentation at zeromq.org/area:faq explicitly states “It is not safe to share a context or sockets between a parent and its child.”
# File lib/czmq/zmsg.rb, line 95 def setup_finalizer ObjectSpace.define_finalizer(self, self.class.close_zmsg(@zmsg)) end