Class: ZMQ::Poller

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/ffi-rzmq/poll.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#errno, #error_string, #version

Constructor Details

#initializePoller

Returns a new instance of Poller.



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

def initialize
  @items = ZMQ::PollItems.new
  @raw_to_socket = {}
  @sockets = []
  @readables = []
  @writables = []
end

Instance Attribute Details

#readablesObject (readonly)

Returns the value of attribute readables.



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

def readables
  @readables
end

#writablesObject (readonly)

Returns the value of attribute writables.



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

def writables
  @writables
end

Instance Method Details

#delete(sock) ⇒ Object

Deletes the sock for all subscribed events.



130
131
132
133
134
135
136
# File 'lib/ffi-rzmq/poll.rb', line 130

def delete sock
  if index = @sockets.index(sock)
    @items.delete_at index
    @sockets.delete sock
    @raw_to_socket.delete sock.socket
  end
end

#deregister(sock, events, fd = 0) ⇒ Object

Deregister the sock for events.

Does not raise any exceptions.



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ffi-rzmq/poll.rb', line 91

def deregister sock, events, fd = 0
  return unless sock || !fd.zero?

  item = @items.get(@sockets.index(sock))

  if item
    # change the value in place
    item[:events] ^= events

    delete sock if item[:events].zero?
  end
end

#deregister_readable(sock) ⇒ Object

A helper method to deregister a sock for readable events.



118
119
120
# File 'lib/ffi-rzmq/poll.rb', line 118

def deregister_readable sock
  deregister sock, ZMQ::POLLIN, 0
end

#deregister_writable(sock) ⇒ Object

A helper method to deregister a sock for writable events.



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

def deregister_writable sock
  deregister sock, ZMQ::POLLOUT, 0
end

#inspectObject



140
141
142
# File 'lib/ffi-rzmq/poll.rb', line 140

def inspect
  @items.inspect
end

#poll(timeout = :blocking) ⇒ Object

Checks each poll item 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.

May raise a ZMQ::PollError exception. This occurs when one of the registered sockets belongs to an application thread in another Context.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ffi-rzmq/poll.rb', line 31

def poll timeout = :blocking
  unless @items.empty?
    timeout = adjust timeout
    items_triggered = LibZMQ.zmq_poll @items.address, @items.size, timeout
    error_check ZMQ_POLL_STR, items_triggered >= 0 ? 0 : items_triggered
    update_selectables
    items_hash
  else
    {}
  end
end

#poll_nonblockObject

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

May raise a ZMQ::PollError exception. This occurs when one of the registered sockets belongs to an application thread in another Context.



50
51
52
# File 'lib/ffi-rzmq/poll.rb', line 50

def poll_nonblock
  poll 0
end

#register(sock, events = ZMQ::POLLIN | ZMQ::POLLOUT, fd = 0) ⇒ Object

Register the sock 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.

Does not raise any exceptions.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ffi-rzmq/poll.rb', line 61

def register sock, events = ZMQ::POLLIN | ZMQ::POLLOUT, fd = 0
  return unless sock || !fd.zero? || !events.zero?

  @poll_items_dirty = true
  item = @items.get(@sockets.index(sock))

  unless item
    @sockets << sock
    item = LibZMQ::PollItem.new

    case sock
    when ZMQ::Socket, Socket
      item[:socket] = sock.socket
      item[:fd] = 0
    else
      item[:socket] = 0
      item[:fd] = fd
    end

    @raw_to_socket[item[:socket].address] = sock
    @items << item
  end
  
  item[:events] |= events
end

#register_readable(sock) ⇒ Object

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



106
107
108
# File 'lib/ffi-rzmq/poll.rb', line 106

def register_readable sock
  register sock, ZMQ::POLLIN, 0
end

#register_writable(sock) ⇒ Object

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



112
113
114
# File 'lib/ffi-rzmq/poll.rb', line 112

def register_writable sock
  register sock, ZMQ::POLLOUT, 0
end

#sizeObject



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

def size(); @items.size; end

#to_sObject



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

def to_s(); inspect; end