class CZMQ::ZBeacon
Public Class Methods
new(ctx, port, opts={})
click to toggle source
# File lib/czmq/zbeacon.rb, line 3 def initialize(ctx, port, opts={}) @ctx = ctx @zbeacon = LibCZMQ.zbeacon_new(port) # zbeacon_new returns NULL in case of failure raise 'Could not create ZBeacon' if @zbeacon.null? # Setup no_echo option if requested if opts[:no_echo] LibCZMQ.zbeacon_no_echo(@zbeacon) end # Setup the finalizer to free the memory allocated by the CZMQ library setup_finalizer end
Private Class Methods
close_zbeacon(zbeacon)
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/zbeacon.rb, line 91 def self.close_zbeacon(zbeacon) Proc.new do LibCZMQ.zbeacon_destroy(zbeacon) zbeacon = nil # Just in case end end
Public Instance Methods
close()
click to toggle source
# File lib/czmq/zbeacon.rb, line 19 def close if @zbeacon # Since we explicitly close the ZBeacon, we have to remove the finalizer. remove_finalizer # Destroy the ZBeacon LibCZMQ.zbeacon_destroy(@zbeacon) # Unset @zbeacon @zbeacon = nil end end
hostname()
click to toggle source
# File lib/czmq/zbeacon.rb, line 32 def hostname raise "Can't get the hostname of a closed ZBeacon!" unless @zbeacon LibCZMQ.zbeacon_hostname(@zbeacon) end
publish(data)
click to toggle source
# File lib/czmq/zbeacon.rb, line 42 def publish(data) raise "Can't publish advertisements on a closed ZBeacon!" unless @zbeacon # Transform data into a bytes array bytes = to_bytearray(data) LibCZMQ.zbeacon_publish(@zbeacon, bytes) end
set_interval(interval)
click to toggle source
# File lib/czmq/zbeacon.rb, line 37 def set_interval(interval) raise "Can't set the advertisement interval of a closed ZBeacon!" unless @zbeacon LibCZMQ.zbeacon_set_interval(@zbeacon, interval) end
silence()
click to toggle source
# File lib/czmq/zbeacon.rb, line 51 def silence raise "Can't silence an uninitialized ZBeacon!" unless @zbeacon LibCZMQ.zbeacon_silence(@zbeacon) end
socket()
click to toggle source
# File lib/czmq/zbeacon.rb, line 66 def socket raise "Can't get socket of an uninitialized ZBeacon!" unless @zbeacon ZSocket.new(@ctx, LibCZMQ.zbeacon_socket(@zbeacon)) end
subscribe(match=nil)
click to toggle source
# File lib/czmq/zbeacon.rb, line 56 def subscribe(match=nil) raise "Can't subscribe to an uninitialized ZBeacon!" unless @zbeacon LibCZMQ.zbeacon_subscribe(@zbeacon, match) end
unsubscribe()
click to toggle source
# File lib/czmq/zbeacon.rb, line 61 def unsubscribe raise "Can't unsubscribe from an uninitialized ZBeacon!" unless @zbeacon LibCZMQ.zbeacon_unsubscribe(@zbeacon) end
Private Instance Methods
remove_finalizer()
click to toggle source
# File lib/czmq/zbeacon.rb, line 85 def remove_finalizer ObjectSpace.undefine_finalizer self end
setup_finalizer()
click to toggle source
After object destruction, make sure that the corresponding zbeacon 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/zbeacon.rb, line 81 def setup_finalizer ObjectSpace.define_finalizer(self, self.class.close_zbeacon(@zbeacon)) end
to_bytearray(data)
click to toggle source
# File lib/czmq/zbeacon.rb, line 98 def to_bytearray(data) bytes = nil if data.is_a? String # String to byte array conversion using the default UTF-8 encoding # bytes = data.bytes.to_a bytes = data.encode("UTF-8").bytes.to_a elsif data.respond_to? :to_a and !data.is_a? Array bytes = data else raise "Don't know how to deal with data" unless data.is_a? Array end bytes end