class CZMQ::ZLoop

Public Class Methods

new(zloop=nil) click to toggle source
# File lib/czmq/zloop.rb, line 3
def initialize(zloop=nil)
  @zloop = zloop || LibCZMQ.zloop_new

  # LibCZMQ.zloop_set_verbose(@zloop, true)

  unless zloop
    setup_finalizer
  end
end

Private Class Methods

close_zloop(zloop) 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/zloop.rb, line 82
def self.close_zloop(zloop)
  Proc.new do
    LibCZMQ.zloop_destroy(zloop)
    zloop = nil # Just in case
  end
end

Public Instance Methods

add_poller(poll_item, func=nil, &block)
Alias for: poller
add_timer(delay, times, &block) click to toggle source
# File lib/czmq/zloop.rb, line 56
def add_timer(delay, times, &block)
  if block
    LibCZMQ.zloop_timer(@zloop, delay, times, block.to_proc, nil)
  end
end
destroy() click to toggle source
# File lib/czmq/zloop.rb, line 13
def destroy
  if @zloop
    # Since we explicitly close the zloop, we have to remove the finalizer.
    remove_finalizer
    rc = LibCZMQ.zloop_destroy(@zloop)
    raise 'Error!' unless @zloop.nil?
    rc
  end
end
poller(poll_item, func=nil, &block) click to toggle source
# File lib/czmq/zloop.rb, line 28
def poller(poll_item, func=nil, &block)
  raise 'Trying to add a poller to an unitialized ZLoop!' if @zloop.nil?
  raise ArgumentError, 'You need to provide a block or a proc/lambda!' unless block_given? or func.responds_to :call

  the_proc = block_given? ? block.to_proc : func

  # need to preserve this callback from the garbage collector
  @callback = LibCZMQ.create_zloop_callback(
    lambda do |zloopbuf, zpollitembuf, arg|
      zpollitem = LibCZMQ::ZPollItem.new(zpollitembuf)
      zlp = ZLoop.new(zloopbuf)
      zsk = ZSocket.new(nil, zpollitem[:socket])
      the_proc.call(zlp, zsk)
    end
  )

  LibCZMQ.zloop_poller(@zloop, poll_item, @callback, nil)
end
Also aliased as: add_poller
poller_end(poll_item) click to toggle source
# File lib/czmq/zloop.rb, line 49
def poller_end(poll_item)
  LibCZMQ.zloop_poller_end(@zloop, poll_item)
  @callback = nil
end
Also aliased as: remove_poller
remove_poller(poll_item)
Alias for: poller_end
start() click to toggle source
# File lib/czmq/zloop.rb, line 23
def start
  raise 'Trying to start an unitialized ZLoop!' if @zloop.nil?
  LibCZMQ.zloop_start(@zloop)
end

Private Instance Methods

remove_finalizer() click to toggle source
# File lib/czmq/zloop.rb, line 76
def remove_finalizer
  ObjectSpace.undefine_finalizer self
end
setup_finalizer() click to toggle source

After object destruction, make sure that the corresponding zloop 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/zloop.rb, line 72
def setup_finalizer
  ObjectSpace.define_finalizer(self, self.class.close_zloop(@zloop))
end