Class: CZMQ::FFI::Zactor

Inherits:
Object
  • Object
show all
Defined in:
lib/czmq-ffi-gen/czmq/ffi/zactor.rb

Overview

Note:

This class is 100% generated using zproject.

provides a simple actor framework

Defined Under Namespace

Classes: DestroyedError

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ptr, finalize = true) ⇒ Zactor

Attaches the pointer ptr to this instance and defines a finalizer for it if necessary.

Parameters:

  • ptr (::FFI::Pointer)
  • finalize (Boolean) (defaults to: true)


24
25
26
27
28
29
30
31
32
# 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

Class Method Details

.__newObject



18
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 18

alias :__new :new

.create_finalizer_for(ptr) ⇒ Proc

Parameters:

  • ptr (::FFI::Pointer)

Returns:

  • (Proc)


35
36
37
38
39
40
41
42
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 35

def self.create_finalizer_for(ptr)
  ptr_ptr = ::FFI::MemoryPointer.new :pointer

  Proc.new do
    ptr_ptr.write_pointer ptr
    ::CZMQ::FFI.zactor_destroy ptr_ptr
  end
end

.destructor_fnObject

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.

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);


108
109
110
111
112
113
114
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 108

def self.destructor_fn
  ::FFI::Function.new :void, [:pointer], blocking: true do |self_|
    self_ = Zactor.__new self_, false
    result = yield self_
    result
  end
end

.fnObject

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.

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);


86
87
88
89
90
91
92
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 86

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_) ⇒ Boolean

Probe the supplied object, and report if it looks like a zactor_t.

Parameters:

  • self_ (::FFI::Pointer, #to_ptr)

Returns:

  • (Boolean)


165
166
167
168
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 165

def self.is(self_)
  result = ::CZMQ::FFI.zactor_is(self_)
  result
end

.new(task, args) ⇒ CZMQ::Zactor

Create a new actor passing arbitrary arguments reference.

Parameters:

  • task (::FFI::Pointer, #to_ptr)
  • args (::FFI::Pointer, #to_ptr)

Returns:

  • (CZMQ::Zactor)


120
121
122
123
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 120

def self.new(task, args)
  ptr = ::CZMQ::FFI.zactor_new(task, args)
  __new ptr
end

.resolve(self_) ⇒ ::FFI::Pointer

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.

Parameters:

  • self_ (::FFI::Pointer, #to_ptr)

Returns:

  • (::FFI::Pointer)


176
177
178
179
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 176

def self.resolve(self_)
  result = ::CZMQ::FFI.zactor_resolve(self_)
  result
end

.test(verbose) ⇒ void

This method returns an undefined value.

Self test of this class.

Parameters:

  • verbose (Boolean)


208
209
210
211
212
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 208

def self.test(verbose)
  verbose = !(0==verbose||!verbose) # boolean
  result = ::CZMQ::FFI.zactor_test(verbose)
  result
end

Instance Method Details

#__ptr::FFI::Pointer Also known as: to_ptr

Return internal pointer

Returns:

  • (::FFI::Pointer)

Raises:



49
50
51
52
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 49

def __ptr
  raise DestroyedError unless @ptr
  @ptr
end

#__ptr_give_ref::FFI::MemoryPointer

Note:

This detaches the current instance from the native object and thus makes it unusable.

Nullify internal pointer and return pointer pointer.

Returns:

  • (::FFI::MemoryPointer)

    the pointer pointing to a pointer pointing to the native object

Raises:



60
61
62
63
64
65
66
67
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 60

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_finalizervoid

Note:

Only use this if you need to and can guarantee that the native object will be freed by other means.

This method returns an undefined value.

Undefines the finalizer for this object.



72
73
74
75
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 72

def __undef_finalizer
  ObjectSpace.undefine_finalizer self
  @finalizer = nil
end

#destroyvoid

This method returns an undefined value.

Destroy an actor.



128
129
130
131
132
133
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 128

def destroy()
  return unless @ptr
  self_p = __ptr_give_ref
  result = ::CZMQ::FFI.zactor_destroy(self_p)
  result
end

#null?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 44

def null?
  !@ptr or @ptr.null?
end

#recvZmsg

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.

Returns:

Raises:



153
154
155
156
157
158
159
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 153

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) ⇒ Integer

Send a zmsg message to the actor, take ownership of the message and destroy when it has been sent.

Parameters:

Returns:

  • (Integer)

Raises:



140
141
142
143
144
145
146
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 140

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) ⇒ void

This method returns an undefined value.

Change default destructor by custom function. Actor MUST be able to handle new message instead of default $TERM.

Parameters:

  • destructor (::FFI::Pointer, #to_ptr)

Raises:



197
198
199
200
201
202
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 197

def set_destructor(destructor)
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zactor_set_destructor(self_p, destructor)
  result
end

#sockZsock

Return the actor’s zsock handle. Use this when you absolutely need to work with the zsock instance rather than the actor.

Returns:

Raises:



185
186
187
188
189
190
191
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 185

def sock()
  raise DestroyedError unless @ptr
  self_p = @ptr
  result = ::CZMQ::FFI.zactor_sock(self_p)
  result = Zsock.__new result, false
  result
end