Class: ZMQ::Poller

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/ffi-rzmq/poll.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePoller

Returns a new instance of Poller.



10
11
12
13
14
# File 'lib/ffi-rzmq/poll.rb', line 10

def initialize
  @poll_items = ZMQ::PollItems.new
  @readables = []
  @writables = []
end

Instance Attribute Details

#readablesObject (readonly)

Returns the value of attribute readables.



8
9
10
# File 'lib/ffi-rzmq/poll.rb', line 8

def readables
  @readables
end

#writablesObject (readonly)

Returns the value of attribute writables.



8
9
10
# File 'lib/ffi-rzmq/poll.rb', line 8

def writables
  @writables
end

Instance Method Details

#delete(pollable) ⇒ Object

Deletes the pollable for all subscribed events. Called internally when a socket has been deregistered and has no more events registered anywhere.

Can also be called directly to remove the socket from the polling array.



125
126
127
128
# File 'lib/ffi-rzmq/poll.rb', line 125

def delete pollable
  return false if @poll_items.empty?
  @poll_items.delete(pollable)
end

#deregister(pollable, events) ⇒ Object

Deregister the pollable for events. When there are no events left or socket has been closed this also deletes the socket from the poll items.



81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ffi-rzmq/poll.rb', line 81

def deregister pollable, events
  return unless pollable

  item = @poll_items[pollable]
  if item && (item.events & events) > 0
    item.events ^= events
    delete(pollable) if item.events.zero? || item.closed?
    true
  else
    false
  end
end

#deregister_readable(pollable) ⇒ Object

A helper method to deregister a pollable for readable events.



108
109
110
# File 'lib/ffi-rzmq/poll.rb', line 108

def deregister_readable pollable
  deregister pollable, ZMQ::POLLIN
end

#deregister_writable(pollable) ⇒ Object

A helper method to deregister a pollable for writable events.



114
115
116
# File 'lib/ffi-rzmq/poll.rb', line 114

def deregister_writable pollable
  deregister pollable, ZMQ::POLLOUT
end

#poll(timeout = :blocking) ⇒ Object

Checks each registered socket for selectability based on the poll items’ registered events. Will block for up to timeout milliseconds. A millisecond is 1/1000 of a second, so to block for 1 second pass the value “1000” to #poll.

Pass “-1” or :blocking for timeout for this call to block indefinitely.

This method will return immediately when there are no registered sockets. In that case, the timeout parameter is not honored. To prevent a CPU busy-loop, the caller of this method should detect this possible condition (via #size) and throttle the call frequency.

Returns 0 when there are no registered sockets that are readable or writable.

Return 1 (or greater) to indicate the number of readable or writable sockets. These sockets should be processed using the #readables and #writables accessors.

Returns -1 when there is an error. Use ZMQ::Util.errno to get the related error number.



40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ffi-rzmq/poll.rb', line 40

def poll timeout = :blocking
  unless @poll_items.empty?
    timeout = adjust timeout
    items_triggered = LibZMQ.zmq_poll @poll_items.address, @poll_items.size, timeout

    update_selectables if Util.resultcode_ok?(items_triggered)
    items_triggered
  else
    0
  end
end

#poll_nonblockObject

The non-blocking version of #poll. See the #poll description for potential exceptions.

May return -1 when an error is encounted. Check ZMQ::Util.errno to determine the underlying cause.



58
59
60
# File 'lib/ffi-rzmq/poll.rb', line 58

def poll_nonblock
  poll 0
end

#register(pollable, events = ZMQ::POLLIN | ZMQ::POLLOUT) ⇒ Object

Register the pollable for events. This method is idempotent meaning it can be called multiple times with the same data and the socket will only get registered at most once. Calling multiple times with different values for events will OR the event information together.



67
68
69
70
71
72
73
74
75
76
# File 'lib/ffi-rzmq/poll.rb', line 67

def register pollable, events = ZMQ::POLLIN | ZMQ::POLLOUT
  return if pollable.nil? || events.zero?

  unless item = @poll_items[pollable]
    item = PollItem.from_pollable(pollable)
    @poll_items << item
  end

  item.events |= events
end

#register_readable(pollable) ⇒ Object

A helper method to register a pollable as readable events only.



96
97
98
# File 'lib/ffi-rzmq/poll.rb', line 96

def register_readable pollable
  register pollable, ZMQ::POLLIN
end

#register_writable(pollable) ⇒ Object

A helper method to register a pollable for writable events only.



102
103
104
# File 'lib/ffi-rzmq/poll.rb', line 102

def register_writable pollable
  register pollable, ZMQ::POLLOUT
end

#to_sObject



130
# File 'lib/ffi-rzmq/poll.rb', line 130

def to_s; inspect; end