Class: CZMQ::FFI::Zloop
- Inherits:
-
Object
- Object
- CZMQ::FFI::Zloop
- Defined in:
- lib/czmq-ffi-gen/czmq/ffi/zloop.rb
Overview
This class is 100% generated using zproject.
event-driven reactor
Defined Under Namespace
Classes: DestroyedError
Class Method Summary collapse
- .__new ⇒ Object
- .create_finalizer_for(ptr) ⇒ Proc
-
.fn ⇒ Object
Create a new callback of the following type: Callback function for reactor events (low-level) typedef int (zloop_fn) ( zloop_t *loop, zmq_pollitem_t *item, void *arg);.
-
.new ⇒ CZMQ::Zloop
Create a new zloop reactor.
-
.reader_fn ⇒ Object
Create a new callback of the following type: Callback function for reactor socket activity typedef int (zloop_reader_fn) ( zloop_t *loop, zsock_t *reader, void *arg);.
-
.test(verbose) ⇒ void
Self test of this class.
-
.timer_fn ⇒ Object
Create a new callback of the following type: Callback for reactor timer events typedef int (zloop_timer_fn) ( zloop_t *loop, int timer_id, void *arg);.
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 a reactor.
-
#initialize(ptr, finalize = true) ⇒ Zloop
constructor
Attaches the pointer ptr to this instance and defines a finalizer for it if necessary.
- #null? ⇒ Boolean
-
#poller(item, handler, arg) ⇒ Integer
Register low-level libzmq pollitem with the reactor.
-
#poller_end(item) ⇒ void
Cancel a pollitem from the reactor, specified by socket or FD.
-
#poller_set_tolerant(item) ⇒ void
Configure a registered poller to ignore errors.
-
#reader(sock, handler, arg) ⇒ Integer
Register socket reader with the reactor.
-
#reader_end(sock) ⇒ void
Cancel a socket reader from the reactor.
-
#reader_set_tolerant(sock) ⇒ void
Configure a registered reader to ignore errors.
-
#set_max_timers(max_timers) ⇒ void
Set hard limit on number of timers allowed.
-
#set_nonstop(nonstop) ⇒ void
By default the reactor stops if the process receives a SIGINT or SIGTERM signal.
-
#set_ticket_delay(ticket_delay) ⇒ void
Set the ticket delay, which applies to all tickets.
-
#set_verbose(verbose) ⇒ void
Set verbose tracing of reactor on/off.
-
#start ⇒ Integer
Start the reactor.
-
#ticket(handler, arg) ⇒ ::FFI::Pointer
Register a ticket timer.
-
#ticket_delete(handle) ⇒ void
Delete a ticket timer.
-
#ticket_reset(handle) ⇒ void
Reset a ticket timer, which moves it to the end of the ticket list and resets its execution time.
-
#timer(delay, times, handler, arg) ⇒ Integer
Register a timer that expires after some delay and repeats some number of times.
-
#timer_end(timer_id) ⇒ Integer
Cancel a specific timer identified by a specific timer_id (as returned by zloop_timer).
Constructor Details
#initialize(ptr, finalize = true) ⇒ Zloop
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/zloop.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/zloop.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/zloop.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.zloop_destroy ptr_ptr 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: Callback function for reactor events (low-level)
typedef int (zloop_fn) (
zloop_t *loop, zmq_pollitem_t *item, void *arg);
105 106 107 108 109 110 111 112 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 105 def self.fn ::FFI::Function.new :int, [:pointer, :pointer, :pointer], blocking: true do |loop, item, arg| loop = Zloop.__new loop, false result = yield loop, item, arg result = Integer(result) result end end |
.new ⇒ CZMQ::Zloop
Create a new zloop reactor
134 135 136 137 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 134 def self.new() ptr = ::CZMQ::FFI.zloop_new() __new ptr end |
.reader_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: Callback function for reactor socket activity
typedef int (zloop_reader_fn) (
zloop_t *loop, zsock_t *reader, void *arg);
86 87 88 89 90 91 92 93 94 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 86 def self.reader_fn ::FFI::Function.new :int, [:pointer, :pointer, :pointer], blocking: true do |loop, reader, arg| loop = Zloop.__new loop, false reader = Zsock.__new reader, false result = yield loop, reader, arg result = Integer(result) result end end |
.test(verbose) ⇒ void
This method returns an undefined value.
Self test of this class.
385 386 387 388 389 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 385 def self.test(verbose) verbose = !(0==verbose||!verbose) # boolean result = ::CZMQ::FFI.zloop_test(verbose) result end |
.timer_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: Callback for reactor timer events
typedef int (zloop_timer_fn) (
zloop_t *loop, int timer_id, void *arg);
123 124 125 126 127 128 129 130 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 123 def self.timer_fn ::FFI::Function.new :int, [:pointer, :int, :pointer], blocking: true do |loop, timer_id, arg| loop = Zloop.__new loop, false result = yield loop, timer_id, arg result = Integer(result) result end 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/zloop.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/zloop.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/zloop.rb', line 72 def __undef_finalizer ObjectSpace.undefine_finalizer self @finalizer = nil end |
#destroy ⇒ void
This method returns an undefined value.
Destroy a reactor
142 143 144 145 146 147 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 142 def destroy() return unless @ptr self_p = __ptr_give_ref result = ::CZMQ::FFI.zloop_destroy(self_p) result end |
#null? ⇒ Boolean
44 45 46 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 44 def null? !@ptr or @ptr.null? end |
#poller(item, handler, arg) ⇒ Integer
Register low-level libzmq pollitem with the reactor. When the pollitem is ready, will call the handler, passing the arg. Returns 0 if OK, -1 if there was an error. If you register the pollitem more than once, each instance will invoke its corresponding handler. A pollitem with socket=NULL and fd=0 means ‘poll on FD zero’.
202 203 204 205 206 207 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 202 def poller(item, handler, arg) raise DestroyedError unless @ptr self_p = @ptr result = ::CZMQ::FFI.zloop_poller(self_p, item, handler, arg) result end |
#poller_end(item) ⇒ void
This method returns an undefined value.
Cancel a pollitem from the reactor, specified by socket or FD. If both are specified, uses only socket. If multiple poll items exist for same socket/FD, cancels ALL of them.
215 216 217 218 219 220 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 215 def poller_end(item) raise DestroyedError unless @ptr self_p = @ptr result = ::CZMQ::FFI.zloop_poller_end(self_p, item) result end |
#poller_set_tolerant(item) ⇒ void
This method returns an undefined value.
Configure a registered poller to ignore errors. If you do not set this, then poller that have errors are removed from the reactor silently.
227 228 229 230 231 232 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 227 def poller_set_tolerant(item) raise DestroyedError unless @ptr self_p = @ptr result = ::CZMQ::FFI.zloop_poller_set_tolerant(self_p, item) result end |
#reader(sock, handler, arg) ⇒ Integer
Register socket reader with the reactor. When the reader has messages, the reactor will call the handler, passing the arg. Returns 0 if OK, -1 if there was an error. If you register the same socket more than once, each instance will invoke its corresponding handler.
158 159 160 161 162 163 164 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 158 def reader(sock, handler, arg) raise DestroyedError unless @ptr self_p = @ptr sock = sock.__ptr if sock result = ::CZMQ::FFI.zloop_reader(self_p, sock, handler, arg) result end |
#reader_end(sock) ⇒ void
This method returns an undefined value.
Cancel a socket reader from the reactor. If multiple readers exist for same socket, cancels ALL of them.
171 172 173 174 175 176 177 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 171 def reader_end(sock) raise DestroyedError unless @ptr self_p = @ptr sock = sock.__ptr if sock result = ::CZMQ::FFI.zloop_reader_end(self_p, sock) result end |
#reader_set_tolerant(sock) ⇒ void
This method returns an undefined value.
Configure a registered reader to ignore errors. If you do not set this, then readers that have errors are removed from the reactor silently.
184 185 186 187 188 189 190 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 184 def reader_set_tolerant(sock) raise DestroyedError unless @ptr self_p = @ptr sock = sock.__ptr if sock result = ::CZMQ::FFI.zloop_reader_set_tolerant(self_p, sock) result end |
#set_max_timers(max_timers) ⇒ void
This method returns an undefined value.
Set hard limit on number of timers allowed. Setting more than a small number of timers (10-100) can have a dramatic impact on the performance of the reactor. For high-volume cases, use ticket timers. If the hard limit is reached, the reactor stops creating new timers and logs an error.
332 333 334 335 336 337 338 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 332 def set_max_timers(max_timers) raise DestroyedError unless @ptr self_p = @ptr max_timers = Integer(max_timers) result = ::CZMQ::FFI.zloop_set_max_timers(self_p, max_timers) result end |
#set_nonstop(nonstop) ⇒ void
This method returns an undefined value.
By default the reactor stops if the process receives a SIGINT or SIGTERM signal. This makes it impossible to shut-down message based architectures like zactors. This method lets you switch off break handling. The default nonstop setting is off (false).
360 361 362 363 364 365 366 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 360 def set_nonstop(nonstop) raise DestroyedError unless @ptr self_p = @ptr nonstop = !(0==nonstop||!nonstop) # boolean result = ::CZMQ::FFI.zloop_set_nonstop(self_p, nonstop) result end |
#set_ticket_delay(ticket_delay) ⇒ void
This method returns an undefined value.
Set the ticket delay, which applies to all tickets. If you lower the delay and there are already tickets created, the results are undefined.
316 317 318 319 320 321 322 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 316 def set_ticket_delay(ticket_delay) raise DestroyedError unless @ptr self_p = @ptr ticket_delay = Integer(ticket_delay) result = ::CZMQ::FFI.zloop_set_ticket_delay(self_p, ticket_delay) result end |
#set_verbose(verbose) ⇒ void
This method returns an undefined value.
Set verbose tracing of reactor on/off. The default verbose setting is off (false).
345 346 347 348 349 350 351 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 345 def set_verbose(verbose) raise DestroyedError unless @ptr self_p = @ptr verbose = !(0==verbose||!verbose) # boolean result = ::CZMQ::FFI.zloop_set_verbose(self_p, verbose) result end |
#start ⇒ Integer
Start the reactor. Takes control of the thread and returns when the 0MQ context is terminated or the process is interrupted, or any event handler returns -1. Event handlers may register new sockets and timers, and cancel sockets. Returns 0 if interrupted, -1 if canceled by a handler.
374 375 376 377 378 379 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 374 def start() raise DestroyedError unless @ptr self_p = @ptr result = ::CZMQ::FFI.zloop_start(self_p) result end |
#ticket(handler, arg) ⇒ ::FFI::Pointer
Register a ticket timer. Ticket timers are very fast in the case where you use a lot of timers (thousands), and frequently remove and add them. The main use case is expiry timers for servers that handle many clients, and which reset the expiry timer for each message received from a client. Whereas normal timers perform poorly as the number of clients grows, the cost of ticket timers is constant, no matter the number of clients. You must set the ticket delay using zloop_set_ticket_delay before creating a ticket. Returns a handle to the timer that you should use in zloop_ticket_reset and zloop_ticket_delete.
279 280 281 282 283 284 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 279 def ticket(handler, arg) raise DestroyedError unless @ptr self_p = @ptr result = ::CZMQ::FFI.zloop_ticket(self_p, handler, arg) result end |
#ticket_delete(handle) ⇒ void
This method returns an undefined value.
Delete a ticket timer. We do not actually delete the ticket here, as other code may still refer to the ticket. We mark as deleted, and remove later and safely.
304 305 306 307 308 309 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 304 def ticket_delete(handle) raise DestroyedError unless @ptr self_p = @ptr result = ::CZMQ::FFI.zloop_ticket_delete(self_p, handle) result end |
#ticket_reset(handle) ⇒ void
This method returns an undefined value.
Reset a ticket timer, which moves it to the end of the ticket list and resets its execution time. This is a very fast operation.
291 292 293 294 295 296 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 291 def ticket_reset(handle) raise DestroyedError unless @ptr self_p = @ptr result = ::CZMQ::FFI.zloop_ticket_reset(self_p, handle) result end |
#timer(delay, times, handler, arg) ⇒ Integer
Register a timer that expires after some delay and repeats some number of times. At each expiry, will call the handler, passing the arg. To run a timer forever, use 0 times. Returns a timer_id that is used to cancel the timer in the future. Returns -1 if there was an error.
244 245 246 247 248 249 250 251 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 244 def timer(delay, times, handler, arg) raise DestroyedError unless @ptr self_p = @ptr delay = Integer(delay) times = Integer(times) result = ::CZMQ::FFI.zloop_timer(self_p, delay, times, handler, arg) result end |
#timer_end(timer_id) ⇒ Integer
Cancel a specific timer identified by a specific timer_id (as returned by zloop_timer).
258 259 260 261 262 263 264 |
# File 'lib/czmq-ffi-gen/czmq/ffi/zloop.rb', line 258 def timer_end(timer_id) raise DestroyedError unless @ptr self_p = @ptr timer_id = Integer(timer_id) result = ::CZMQ::FFI.zloop_timer_end(self_p, timer_id) result end |