Class: CZTop::Poller::ZPoller
- Inherits:
-
Object
- Object
- CZTop::Poller::ZPoller
- Extended by:
- HasFFIDelegate::ClassMethods
- Includes:
- CZMQ::FFI, HasFFIDelegate
- Defined in:
- lib/cztop/poller/zpoller.rb
Overview
This is the trivial poller based on zpoller. It only supports polling for readability, but it also supports doing that on CLIENT/SERVER sockets, which is useful for CZTop::Poller.
Instance Attribute Summary
Attributes included from HasFFIDelegate
Instance Method Summary collapse
-
#add(reader) ⇒ void
Adds another reader socket to the poller.
-
#ignore_interrupts ⇒ void
Tells the zpoller to ignore interrupts.
-
#initialize(reader, *readers) ⇒ ZPoller
constructor
Initializes the Poller.
-
#nonstop=(flag) ⇒ Object
By default the poller stops if the process receives a SIGINT or SIGTERM signal.
-
#remove(reader) ⇒ void
Removes a reader socket from the poller.
-
#wait(timeout = -1)) ⇒ Socket, ...
Waits and returns the first socket that becomes readable.
Methods included from HasFFIDelegate::ClassMethods
ffi_delegate, from_ffi_delegate
Methods included from HasFFIDelegate
#attach_ffi_delegate, #from_ffi_delegate, raise_zmq_err, #to_ptr
Constructor Details
#initialize(reader, *readers) ⇒ ZPoller
Initializes the Poller. At least one reader has to be given.
18 19 20 21 22 23 24 25 26 |
# File 'lib/cztop/poller/zpoller.rb', line 18 def initialize(reader, *readers) @sockets = {} # to keep references and return same instances ptr = Zpoller.new(reader, *readers.flat_map { |r| [:pointer, r] }, :pointer, nil) attach_ffi_delegate(ptr) remember_socket(reader) readers.each { |r| remember_socket(r) } end |
Instance Method Details
#add(reader) ⇒ void
This method returns an undefined value.
Adds another reader socket to the poller.
33 34 35 36 37 |
# File 'lib/cztop/poller/zpoller.rb', line 33 def add(reader) rc = ffi_delegate.add(reader) raise_zmq_err(format('unable to add socket %p', reader)) if rc == -1 remember_socket(reader) end |
#ignore_interrupts ⇒ void
This method returns an undefined value.
Tells the zpoller to ignore interrupts. By default, #wait will return immediately if it detects an interrupt (when zsys_interrupted
is set to something other than zero). Calling this method will supress this behavior.
75 76 77 |
# File 'lib/cztop/poller/zpoller.rb', line 75 def ignore_interrupts ffi_delegate.ignore_interrupts end |
#nonstop=(flag) ⇒ Object
By default the poller 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).
Setting this will cause #wait to never raise.
88 89 90 |
# File 'lib/cztop/poller/zpoller.rb', line 88 def nonstop=(flag) ffi_delegate.set_nonstop(flag) end |
#remove(reader) ⇒ void
This method returns an undefined value.
Removes a reader socket from the poller.
46 47 48 49 50 |
# File 'lib/cztop/poller/zpoller.rb', line 46 def remove(reader) rc = ffi_delegate.remove(reader) raise_zmq_err(format('unable to remove socket %p', reader)) if rc == -1 forget_socket(reader) end |
#wait(timeout = -1)) ⇒ Socket, ...
Waits and returns the first socket that becomes readable.
59 60 61 62 63 64 65 66 67 |
# File 'lib/cztop/poller/zpoller.rb', line 59 def wait(timeout = -1) ptr = ffi_delegate.wait(timeout) if ptr.null? raise Interrupt if ffi_delegate.terminated return nil end socket_by_ptr(ptr) end |