0mq 0.1.2 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (5) hide show
  1. checksums.yaml +7 -7
  2. data/lib/0mq.rb +1 -0
  3. data/lib/0mq/context.rb +3 -1
  4. data/lib/0mq/poll.rb +149 -0
  5. metadata +72 -105
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 473c613f99809b02a04f9dd571c4346fb4a21dc6
4
- data.tar.gz: 7715e9eccbb78b63574c49bbb8dde74b399be0b9
5
- SHA512:
6
- metadata.gz: f2634ad6efa95f1fefb126a3d73f846905ea31d3e7796839d35d74146622666b5cbe1e48869dafca39e4cfbf673af839d6a32511ddf8dc056b6241b22a8896fe
7
- data.tar.gz: ecc8934f09fcacaaecbc1a4d9a6eb51673495bd1fba521972c0a4b9f0938246d32bba54f91a0ee068f90bd924d7cf4d56c718e7d1e8d9f2d2ab156394f65c076
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9804002690a57145812c94d81b0ac10f6b034401
4
+ data.tar.gz: 369e72c53a0e9571f9d8cc105cdb9e61d8329e2c
5
+ SHA512:
6
+ metadata.gz: d3ab814c5981fd4aa48fc760b5f2ce40d0c02dc0534853d2400349f9812b06907728d11e47d0167c49d97600d1a44970bf92272547cb490d4f3fcaf3501ed6f4
7
+ data.tar.gz: 58c50e1b3288430f6188af59f5ef5aa53387db01b0d7a90285f6a4cce45fbe45373736860511b2ca63c66abb4fb8943d3d5d88bbdba1f9e68887b9cb254ea59a
data/lib/0mq.rb CHANGED
@@ -5,3 +5,4 @@ require_relative '0mq/error_map'
5
5
  require_relative '0mq/context'
6
6
  require_relative '0mq/socket'
7
7
  require_relative '0mq/proxy'
8
+ require_relative '0mq/poll'
data/lib/0mq/context.rb CHANGED
@@ -11,7 +11,9 @@ module ZMQ
11
11
  # Destroy the ØMQ context.
12
12
  def terminate
13
13
  if @ptr
14
- rc = LibZMQ.zmq_ctx_term @ptr
14
+ rc = LibZMQ.version4? ?
15
+ LibZMQ.zmq_ctx_term(@ptr) :
16
+ LibZMQ.zmq_term(@ptr)
15
17
  ZMQ.error_check true if rc == -1
16
18
 
17
19
  @ptr = nil
data/lib/0mq/poll.rb ADDED
@@ -0,0 +1,149 @@
1
+
2
+ module ZMQ
3
+
4
+ # A mechanism for applications to multiplex input/output events
5
+ # in a level-triggered fashion over a set of sockets.
6
+ class Poll
7
+ # Timeout is specified in seconds.
8
+ # A value of 0 will return immediately (non-blocking), and
9
+ # a value of -1 will block indefinitely until an event has
10
+ # occurred. Fractions of a second are allowed.
11
+ # Timeout defaults to block indefinitely (-1).
12
+ attr_accessor :timeout
13
+
14
+ # Construct a Poll object and start polling.
15
+ # See #initialize for parameters.
16
+ # See #run for block and return value.
17
+ def self.poll(*sockets, &block)
18
+ new(*sockets).tap { |poll| poll.run(&block) }
19
+ end
20
+
21
+ # Non-blocking version of poll.
22
+ def self.poll_nonblock(*sockets, &block)
23
+ self.poll *sockets, timeout: 0, &block
24
+ end
25
+
26
+ # Accepts a list of sockets to poll for events
27
+ # (ZMQ::POLLIN, ZMQ::POLLOUT, ZMQ::POLLERR).
28
+ # Default is to poll for input (ZMQ::POLLIN).
29
+ # To poll for a different kind of event, specify the socket
30
+ # and event type as a key/value pair
31
+ # (my_socket => ZMQ::POLLOUT).
32
+ # Event flags can be binary OR'd together if necessary
33
+ # (my_socket => ZMQ::POLLIN | ZMQ::POLLOUT).
34
+ #
35
+ # Timeout can be specified in seconds as a keyword arg.
36
+ # See the :timeout accessor.
37
+ #
38
+ # Does not poll until #run is called.
39
+ #
40
+ # Example:
41
+ # ZMQ::Poll.new socket1, socket2, socket3 => ZMQ::POLLOUT, timeout: 1
42
+ def initialize(*sockets)
43
+ opts = sockets.last.is_a?(Hash) ? sockets.pop : {} # For Ruby 1.9
44
+
45
+ @timeout = opts.fetch :timeout, -1
46
+
47
+ @poll_items = []
48
+ @socks = {}
49
+
50
+ sockets.each { |socket| @socks[socket] = ZMQ::POLLIN }
51
+
52
+ # Pull remaining sockets out of options hash and package into poll items.
53
+ # Skip any option symbols in the hash; they aren't sockets.
54
+ # Rejecting symbols allows duck-typed sockets to be included.
55
+ @socks.merge! opts.reject {|socket, events| socket.is_a? Symbol}
56
+
57
+ # Build table to reference ZMQ::Socket to its pointer's address.
58
+ # This is an easy way to reconnect PollItem to ZMQ::Socket without
59
+ # having to store multiple dimensions in the socks hash.
60
+ @socket_lookup = {}
61
+ @socks.each { |socket, event| @socket_lookup[socket.ptr.address] = socket }
62
+
63
+ # Allocate space for C PollItem (zmq_pollitem_t) structs.
64
+ @poll_structs = FFI::MemoryPointer.new LibZMQ::PollItem, @socks.count, true
65
+
66
+ # Create the PollItem objects.
67
+ # Initializing them within the FFI::MemoryPointer prevents having to copy
68
+ # the struct data to the MemoryPointer when polling, then back again to
69
+ # retrieve the revents flags.
70
+ i = 0
71
+ @socks.each do |socket, events|
72
+ @poll_items.push LibZMQ::PollItem.new(@poll_structs[i]).tap { |pi|
73
+ pi.socket = socket
74
+ pi.events = events
75
+ }
76
+
77
+ i += 1
78
+ end
79
+ end
80
+
81
+ # Start polling.
82
+ #
83
+ # Returns a hash of ZMQ::Socket => revents (triggered event flags).
84
+ # Each item of the hash is passed to the block, if provided.
85
+ def run(&block)
86
+ return {} if @poll_items.empty?
87
+
88
+ # Convert seconds to miliseconds.
89
+ timeout = @timeout > 0 ? (@timeout * 1000).to_i : @timeout
90
+
91
+ # Poll
92
+ rc = LibZMQ::zmq_poll @poll_structs, @poll_items.count, timeout
93
+ ZMQ.error_check true if rc==-1
94
+
95
+ # Create a hash of the items with triggered events.
96
+ # (ZMQ::Socket => revents)
97
+ triggered_items = @poll_items.select { |pi| pi.revents > 0 }
98
+ .map { |pi| [@socket_lookup[pi.socket.address], pi.revents] }
99
+
100
+ triggered_items = Hash[triggered_items]
101
+
102
+ # Pass triggered sockets to block.
103
+ triggered_items.each { |socket, revents| block.call socket, revents } if block
104
+
105
+ triggered_items
106
+ end
107
+
108
+ # Non-blocking version of run.
109
+ def run_nonblock(&block)
110
+ @timeout = 0
111
+ run &block
112
+ end
113
+
114
+ end
115
+ end
116
+
117
+
118
+ module LibZMQ
119
+ class PollItem
120
+
121
+ # Get the event flags:
122
+ # ZMQ::POLLIN, ZMQ::POLLOUT, ZMQ::POLLERR.
123
+ # Event flags are bitmasked.
124
+ def events
125
+ self[:events]
126
+ end
127
+
128
+ # Set the event flags:
129
+ # ZMQ::POLLIN, ZMQ::POLLOUT, ZMQ::POLLERR.
130
+ # Event flags are bitmasked.
131
+ def events=(flags)
132
+ self[:events] = flags
133
+ end
134
+
135
+ # Get requested events that triggered:
136
+ # ZMQ::POLLIN, ZMQ::POLLOUT, ZMQ::POLLERR.
137
+ # Event flags are bitmasked.
138
+ def revents
139
+ self[:revents]
140
+ end
141
+
142
+ # Set the socket to poll for events on.
143
+ # Accepts a ZMQ::Socket or a pointer.
144
+ def socket=(sock)
145
+ self[:socket] = sock.is_a?(FFI::Pointer) ? sock : sock.ptr
146
+ end
147
+
148
+ end
149
+ end
metadata CHANGED
@@ -1,149 +1,116 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: 0mq
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.2
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
5
  platform: ruby
6
- authors:
6
+ authors:
7
7
  - Joe McIlvain
8
8
  - Alex McLain
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-03 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
12
+
13
+ date: 2014-03-04 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
15
16
  name: ffi-rzmq-core
16
- requirement: !ruby/object:Gem::Requirement
17
- requirements:
18
- - - "~>"
19
- - !ruby/object:Gem::Version
20
- version: '1.0'
21
- type: :runtime
22
17
  prerelease: false
23
- version_requirements: !ruby/object:Gem::Requirement
24
- requirements:
25
- - - "~>"
26
- - !ruby/object:Gem::Version
27
- version: '1.0'
28
- - !ruby/object:Gem::Dependency
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: "1.0"
23
+ type: :runtime
24
+ version_requirements: *id001
25
+ - !ruby/object:Gem::Dependency
29
26
  name: bundler
30
- requirement: !ruby/object:Gem::Requirement
31
- requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: '0'
35
- type: :development
36
27
  prerelease: false
37
- version_requirements: !ruby/object:Gem::Requirement
38
- requirements:
39
- - - ">="
40
- - !ruby/object:Gem::Version
41
- version: '0'
42
- - !ruby/object:Gem::Dependency
43
- name: rake
44
- requirement: !ruby/object:Gem::Requirement
45
- requirements:
46
- - - ">="
47
- - !ruby/object:Gem::Version
48
- version: '0'
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ requirements:
30
+ - &id003
31
+ - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
49
34
  type: :development
35
+ version_requirements: *id002
36
+ - !ruby/object:Gem::Dependency
37
+ name: rake
50
38
  prerelease: false
51
- version_requirements: !ruby/object:Gem::Requirement
52
- requirements:
53
- - - ">="
54
- - !ruby/object:Gem::Version
55
- version: '0'
56
- - !ruby/object:Gem::Dependency
57
- name: pry
58
- requirement: !ruby/object:Gem::Requirement
59
- requirements:
60
- - - ">="
61
- - !ruby/object:Gem::Version
62
- version: '0'
39
+ requirement: &id004 !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - *id003
63
42
  type: :development
43
+ version_requirements: *id004
44
+ - !ruby/object:Gem::Dependency
45
+ name: pry
64
46
  prerelease: false
65
- version_requirements: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - ">="
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- - !ruby/object:Gem::Dependency
71
- name: pry-rescue
72
- requirement: !ruby/object:Gem::Requirement
73
- requirements:
74
- - - ">="
75
- - !ruby/object:Gem::Version
76
- version: '0'
47
+ requirement: &id005 !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - *id003
77
50
  type: :development
51
+ version_requirements: *id005
52
+ - !ruby/object:Gem::Dependency
53
+ name: pry-rescue
78
54
  prerelease: false
79
- version_requirements: !ruby/object:Gem::Requirement
80
- requirements:
81
- - - ">="
82
- - !ruby/object:Gem::Version
83
- version: '0'
84
- - !ruby/object:Gem::Dependency
85
- name: rspec
86
- requirement: !ruby/object:Gem::Requirement
87
- requirements:
88
- - - ">="
89
- - !ruby/object:Gem::Version
90
- version: '0'
55
+ requirement: &id006 !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - *id003
91
58
  type: :development
59
+ version_requirements: *id006
60
+ - !ruby/object:Gem::Dependency
61
+ name: rspec
92
62
  prerelease: false
93
- version_requirements: !ruby/object:Gem::Requirement
94
- requirements:
95
- - - ">="
96
- - !ruby/object:Gem::Version
97
- version: '0'
98
- - !ruby/object:Gem::Dependency
99
- name: fivemat
100
- requirement: !ruby/object:Gem::Requirement
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- version: '0'
63
+ requirement: &id007 !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - *id003
105
66
  type: :development
67
+ version_requirements: *id007
68
+ - !ruby/object:Gem::Dependency
69
+ name: fivemat
106
70
  prerelease: false
107
- version_requirements: !ruby/object:Gem::Requirement
108
- requirements:
109
- - - ">="
110
- - !ruby/object:Gem::Version
111
- version: '0'
71
+ requirement: &id008 !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - *id003
74
+ type: :development
75
+ version_requirements: *id008
112
76
  description: A Ruby-like wrapper for ffi-rzmq-core (ZeroMQ)
113
77
  email: joe.eli.mac@gmail.com
114
78
  executables: []
79
+
115
80
  extensions: []
81
+
116
82
  extra_rdoc_files: []
117
- files:
83
+
84
+ files:
118
85
  - lib/0mq.rb
119
86
  - lib/0mq/context.rb
120
87
  - lib/0mq/error_map.rb
88
+ - lib/0mq/poll.rb
121
89
  - lib/0mq/proxy.rb
122
90
  - lib/0mq/socket.rb
123
91
  - lib/0mq/socket/options.rb
124
92
  homepage: https://github.com/jemc/0mq/
125
- licenses:
93
+ licenses:
126
94
  - MIT
127
95
  metadata: {}
96
+
128
97
  post_install_message:
129
98
  rdoc_options: []
130
- require_paths:
99
+
100
+ require_paths:
131
101
  - lib
132
- required_ruby_version: !ruby/object:Gem::Requirement
133
- requirements:
134
- - - ">="
135
- - !ruby/object:Gem::Version
136
- version: '0'
137
- required_rubygems_version: !ruby/object:Gem::Requirement
138
- requirements:
139
- - - ">="
140
- - !ruby/object:Gem::Version
141
- version: '0'
102
+ required_ruby_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - *id003
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - *id003
142
108
  requirements: []
109
+
143
110
  rubyforge_project:
144
- rubygems_version: 2.2.0
111
+ rubygems_version: 2.2.2
145
112
  signing_key:
146
113
  specification_version: 4
147
114
  summary: 0mq
148
115
  test_files: []
149
- has_rdoc:
116
+