class CZMQ::FFI::Zactor

provides a simple actor framework @note This class is 100% generated using zproject.

Public Class Methods

__new(task, args)
Alias for: new
create_finalizer_for(ptr) click to toggle source

@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
destructor_fn() { |self_| ... } click to toggle source

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
fn() { |pipe, args| ... } click to toggle source

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
is(self_) click to toggle source

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
new(ptr, finalize = true) click to toggle source

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
new(task, args) click to toggle source

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
Also aliased as: __new
resolve(self_) click to toggle source

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
test(verbose) click to toggle source

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

__ptr() click to toggle source

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
Also aliased as: to_ptr
__ptr_give_ref() click to toggle source

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
__undef_finalizer() click to toggle source

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() click to toggle source

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
null?() click to toggle source

@return [Boolean]

# File lib/czmq-ffi-gen/czmq/ffi/zactor.rb, line 43
def null?
  !@ptr or @ptr.null?
end
recv() click to toggle source

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(msg_p) click to toggle source

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
set_destructor(destructor) click to toggle source

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
sock() click to toggle source

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
to_ptr()

So external Libraries can just pass the Object to a FFI function which expects a :pointer

Alias for: __ptr