class CZMQ::FFI::Zactor
provides a simple actor framework @note This class is 100% generated using zproject.
Public Class Methods
@param ptr [::FFI::Pointer] @return [Proc]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 35 def self.create_finalizer_for(ptr) Proc.new do ptr_ptr = ::FFI::MemoryPointer.new :pointer ptr_ptr.write_pointer ptr ::CZMQ::FFI.zactor_destroy ptr_ptr end end
Create a new callback of the following type: Function to be called on zactor_destroy. Default behavior is to send zmsg_t with string “$TERM” in a first frame.
An example - to send $KTHXBAI string
if (zstr_send (self, "$KTHXBAI") == 0) zsock_wait (self); typedef void (zactor_destructor_fn) ( zactor_t *self);
@note WARNING: If your Ruby code doesn't retain a reference to the
FFI::Function object after passing it to a C function call, it may be garbage collected while C still holds the pointer, potentially resulting in a segmentation fault.
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 107 def self.destructor_fn ::FFI::Function.new :void, [:pointer], blocking: true do |self_| self_ = Zactor.__new self_, false result = yield self_ result end end
Create a new callback of the following type: Actors get a pipe and arguments from caller
typedef void (zactor_fn) ( zsock_t *pipe, void *args);
@note WARNING: If your Ruby code doesn't retain a reference to the
FFI::Function object after passing it to a C function call, it may be garbage collected while C still holds the pointer, potentially resulting in a segmentation fault.
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 85 def self.fn ::FFI::Function.new :void, [:pointer, :pointer], blocking: true do |pipe, args| pipe = Zsock.__new pipe, false result = yield pipe, args result end end
Probe the supplied object, and report if it looks like a zactor_t.
@param self_ [::FFI::Pointer, to_ptr
] @return [Boolean]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 164 def self.is(self_) result = ::CZMQ::FFI.zactor_is(self_) result end
Attaches the pointer ptr to this instance and defines a finalizer for it if necessary. @param ptr [::FFI::Pointer] @param finalize [Boolean]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 24 def initialize(ptr, finalize = true) @ptr = ptr if @ptr.null? @ptr = nil # Remove null pointers so we don't have to test for them. elsif finalize @finalizer = self.class.create_finalizer_for @ptr ObjectSpace.define_finalizer self, @finalizer end end
Create a new actor passing arbitrary arguments reference. @param task [::FFI::Pointer, to_ptr
] @param args [::FFI::Pointer, to_ptr
] @return [CZMQ::Zactor]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 119 def self.new(task, args) ptr = ::CZMQ::FFI.zactor_new(task, args) __new ptr end
Probe the supplied reference. If it looks like a zactor_t instance, return the underlying libzmq actor handle; else if it looks like a libzmq actor handle, return the supplied value.
@param self_ [::FFI::Pointer, to_ptr
] @return [::FFI::Pointer]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 175 def self.resolve(self_) result = ::CZMQ::FFI.zactor_resolve(self_) result end
Self test of this class.
@param verbose [Boolean] @return [void]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 207 def self.test(verbose) verbose = !(0==verbose||!verbose) # boolean result = ::CZMQ::FFI.zactor_test(verbose) result end
Public Instance Methods
Return internal pointer @return [::FFI::Pointer]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 48 def __ptr raise DestroyedError unless @ptr @ptr end
Nullify internal pointer and return pointer pointer. @note This detaches the current instance from the native object
and thus makes it unusable.
@return [::FFI::MemoryPointer] the pointer pointing to a pointer
pointing to the native object
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 59 def __ptr_give_ref raise DestroyedError unless @ptr ptr_ptr = ::FFI::MemoryPointer.new :pointer ptr_ptr.write_pointer @ptr __undef_finalizer if @finalizer @ptr = nil ptr_ptr end
Undefines the finalizer for this object. @note Only use this if you need to and can guarantee that the native
object will be freed by other means.
@return [void]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 71 def __undef_finalizer ObjectSpace.undefine_finalizer self @finalizer = nil end
Destroy an actor.
@return [void]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 127 def destroy() return unless @ptr self_p = __ptr_give_ref result = ::CZMQ::FFI.zactor_destroy(self_p) result end
@return [Boolean]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 43 def null? !@ptr or @ptr.null? end
Receive a zmsg message from the actor. Returns NULL if the actor was interrupted before the message could be received, or if there was a timeout on the actor.
@return [Zmsg]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 152 def recv() raise DestroyedError unless @ptr self_p = @ptr result = ::CZMQ::FFI.zactor_recv(self_p) result = Zmsg.__new result, true result end
Send a zmsg message to the actor, take ownership of the message and destroy when it has been sent.
@param msg_p [#__ptr_give_ref] @return [Integer]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 139 def send(msg_p) raise DestroyedError unless @ptr self_p = @ptr msg_p = msg_p.__ptr_give_ref result = ::CZMQ::FFI.zactor_send(self_p, msg_p) result end
Change default destructor by custom function. Actor MUST be able to handle new message instead of default $TERM.
@param destructor [::FFI::Pointer, to_ptr
] @return [void]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 196 def set_destructor(destructor) raise DestroyedError unless @ptr self_p = @ptr result = ::CZMQ::FFI.zactor_set_destructor(self_p, destructor) result end
Return the actor's zsock handle. Use this when you absolutely need to work with the zsock instance rather than the actor.
@return [Zsock]
# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 184 def sock() raise DestroyedError unless @ptr self_p = @ptr result = ::CZMQ::FFI.zactor_sock(self_p) result = Zsock.__new result, false result end