Class: CZMQ::FFI::Zactor
- Inherits:
-
Object
- Object
- CZMQ::FFI::Zactor
- Defined in:
- lib/czmq-ffi-gen/czmq/ffi/zactor.rb
Overview
This class is 100% generated using zproject.
provides a simple actor framework
Defined Under Namespace
Classes: DestroyedError
Class Method Summary collapse
- .__new ⇒ Object
- .create_finalizer_for(ptr) ⇒ Proc
-
.destructor_fn ⇒ Object
Create a new callback of the following type: Function to be called on zactor_destroy.
-
.fn ⇒ Object
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);.
-
.is(self_) ⇒ Boolean
Probe the supplied object, and report if it looks like a zactor_t.
-
.new(task, args) ⇒ CZMQ::Zactor
Create a new actor passing arbitrary arguments reference.
-
.resolve(self_) ⇒ ::FFI::Pointer
Probe the supplied reference.
-
.test(verbose) ⇒ void
Self test of this class.
Instance Method Summary collapse
-
#__ptr ⇒ ::FFI::Pointer
(also: #to_ptr)
Return internal pointer.
-
#__ptr_give_ref ⇒ ::FFI::MemoryPointer
Nullify internal pointer and return pointer pointer.
-
#__undef_finalizer ⇒ void
Undefines the finalizer for this object.
-
#destroy ⇒ void
Destroy an actor.
-
#initialize(ptr, finalize = true) ⇒ Zactor
constructor
Attaches the pointer ptr to this instance and defines a finalizer for it if necessary.
- #null? ⇒ Boolean
-
#recv ⇒ Zmsg
Receive a zmsg message from the actor.
-
#send(msg_p) ⇒ Integer
Send a zmsg message to the actor, take ownership of the message and destroy when it has been sent.
-
#set_destructor(destructor) ⇒ void
Change default destructor by custom function.
-
#sock ⇒ Zsock
Return the actor’s zsock handle.
Constructor Details
#initialize(ptr, finalize = true) ⇒ Zactor
Attaches the pointer ptr to this instance and defines a finalizer for it if necessary.
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
.__new ⇒ Object
18 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 18 alias :__new :new |
.create_finalizer_for(ptr) ⇒ 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_fn ⇒ Object
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 |
.fn ⇒ Object
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.
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.
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.
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 |
Instance Method Details
#__ptr ⇒ ::FFI::Pointer Also known as: to_ptr
Return internal pointer
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
This detaches the current instance from the native object and thus makes it unusable.
Nullify internal pointer and return pointer pointer.
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_finalizer ⇒ void
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 |
#destroy ⇒ void
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
44 45 46 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zactor.rb', line 44 def null? !@ptr or @ptr.null? end |
#recv ⇒ Zmsg
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.
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.
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.
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 |
#sock ⇒ Zsock
Return the actor’s zsock handle. Use this when you absolutely need to work with the zsock instance rather than the actor.
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 |