Skip to content
This repository was archived by the owner on Jun 30, 2023. It is now read-only.

Commit

Permalink
Debugged ZLoop.
Browse files Browse the repository at this point in the history
  • Loading branch information
mtortonesi committed Mar 11, 2014
1 parent c4a7125 commit 16965e1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
40 changes: 25 additions & 15 deletions lib/czmq/zloop.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
module CZMQ
class ZLoop
def initialize
@zloop = LibCZMQ.zloop_new
def initialize(zloop=nil)
@zloop = zloop || LibCZMQ.zloop_new

setup_finalizer
# LibCZMQ.zloop_set_verbose(@zloop, true)

unless zloop
setup_finalizer
end
end

def destroy
if @zloop
# Since we explicitly close the zloop, we have to remove the finalizer.
remove_finalizer
LibCZMQ.zloop_destroy(@zloop)
rc = LibCZMQ.zloop_destroy(@zloop)
raise 'Error!' unless @zloop.nil?
rc
end
end

Expand All @@ -24,13 +29,17 @@ 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

# need to preserve this callback from the garbage collector
@callback = block_given? ?
LibCZMQ.create_zloop_callback(Proc.new(block)) :
LibCZMQ.create_zloop_callback(func)
the_proc = block_given? ? block.to_proc : func

puts "@callback: #{@callback.inspect}"
puts "poll_item: #{poll_item.inspect}"
# 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
Expand All @@ -39,15 +48,16 @@ def poller(poll_item, func=nil, &block)

def poller_end(poll_item)
LibCZMQ.zloop_poller_end(@zloop, poll_item)
@callback = nil
end

alias_method :remove_poller, :poller_end


# def stop
# raise 'Trying to stop an unitialized ZLoop!' if @zloop.nil?
# LibCZMQ.zloop_stop(@zloop)
# end
def add_timer(delay, times, &block)
if block
LibCZMQ.zloop_timer(@zloop, delay, times, block.to_proc, nil)
end
end


private
Expand Down
2 changes: 1 addition & 1 deletion lib/czmq/zsocket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def __get_zsocket_pointer__
def to_pollitem(polling_type=CZMQ::POLLIN)
raise "Can't convert an uninitialized/closed ZSocket to a pollitem!" unless @zsocket
# TODO: check what to do in case we have a pollitem with a different poll type
LibCZMQ.create_pollitem(socket: @zsocket, revents: polling_type)
LibCZMQ.create_pollitem(socket: @zsocket, events: polling_type)
end


Expand Down

0 comments on commit 16965e1

Please sign in to comment.