ffi-rzmq 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.bnsignore +22 -0
- data/History.txt +11 -0
- data/README.rdoc +27 -5
- data/examples/async_req_rep.rb +42 -0
- data/examples/t +5228 -0
- data/examples/xreqxrep_poll.rb +82 -0
- data/ffi-rzmq.gemspec +7 -7
- data/lib/ffi-rzmq.rb +2 -1
- data/lib/ffi-rzmq/context.rb +2 -2
- data/lib/ffi-rzmq/message.rb +2 -2
- data/lib/ffi-rzmq/poll.rb +8 -8
- data/lib/ffi-rzmq/socket.rb +9 -9
- data/lib/ffi-rzmq/wrapper.rb +2 -1
- data/lib/ffi-rzmq/zmq.rb +2 -2
- data/spec/context_spec.rb +1 -0
- data/spec/socket_spec.rb +5 -4
- data/version.txt +1 -1
- metadata +10 -6
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'ffi-rzmq'
|
3
|
+
|
4
|
+
|
5
|
+
link = "tcp://127.0.0.1:5555"
|
6
|
+
|
7
|
+
ctx = ZMQ::Context.new 1
|
8
|
+
s1 = ctx.socket ZMQ::XREQ
|
9
|
+
s1.identity = 'socket1.xreq'
|
10
|
+
s2 = ctx.socket ZMQ::XREP
|
11
|
+
s2.identity = 'socket2.xrep'
|
12
|
+
s3 = ctx.socket ZMQ::XREP
|
13
|
+
s3.identity = 'socket3.xrep'
|
14
|
+
|
15
|
+
s1.bind link
|
16
|
+
s2.connect link
|
17
|
+
#s3.connect link
|
18
|
+
|
19
|
+
poller = ZMQ::Poller.new
|
20
|
+
#poller.register_readable s3
|
21
|
+
poller.register_readable s2
|
22
|
+
poller.register_writable s1
|
23
|
+
|
24
|
+
start_time = Time.now
|
25
|
+
@unsent = true
|
26
|
+
|
27
|
+
until @done do
|
28
|
+
begin
|
29
|
+
poller.poll_nonblock
|
30
|
+
rescue ZMQ::PollError => e
|
31
|
+
puts "efault? [#{e.efault?}]"
|
32
|
+
raise
|
33
|
+
end
|
34
|
+
|
35
|
+
# send the message after 5 seconds
|
36
|
+
if Time.now - start_time > 5 && @unsent
|
37
|
+
puts "sending payload nonblocking"
|
38
|
+
|
39
|
+
5.times do |i|
|
40
|
+
payload = "#{ i.to_s * 40 }"
|
41
|
+
s1.send_string payload, ZMQ::NOBLOCK
|
42
|
+
end
|
43
|
+
@unsent = false
|
44
|
+
end
|
45
|
+
|
46
|
+
# check for messages after 1 second
|
47
|
+
if Time.now - start_time > 1
|
48
|
+
poller.readables.each do |sock|
|
49
|
+
#p poller.readables
|
50
|
+
puts
|
51
|
+
#p poller.writables
|
52
|
+
if sock.identity =~ /xrep/
|
53
|
+
routing_info = sock.recv_string ZMQ::NOBLOCK
|
54
|
+
puts "routing_info received [#{routing_info}] on socket.identity [#{sock.identity}]"
|
55
|
+
else
|
56
|
+
routing_info = nil
|
57
|
+
received_msg = sock.recv_string ZMQ::NOBLOCK
|
58
|
+
|
59
|
+
# skip to the next iteration if received_msg is nil; that means we got an EAGAIN
|
60
|
+
next unless received_msg
|
61
|
+
puts "message received [#{received_msg}] on socket.identity [#{sock.identity}]"
|
62
|
+
end
|
63
|
+
|
64
|
+
while sock.more_parts? do
|
65
|
+
received_msg = sock.recv_string ZMQ::NOBLOCK
|
66
|
+
|
67
|
+
puts "message received [#{received_msg}]"
|
68
|
+
end
|
69
|
+
|
70
|
+
puts "kick back a reply"
|
71
|
+
sock.send_string routing_info, ZMQ::SNDMORE | ZMQ::NOBLOCK if routing_info
|
72
|
+
time = Time.now.strftime "%Y-%m-%dT%H:%M:%S.#{Time.now.usec}"
|
73
|
+
reply = "reply " + sock.identity.upcase + " #{time}"
|
74
|
+
puts "sent reply [#{reply}], #{time}"
|
75
|
+
sock.send_string reply
|
76
|
+
#@done = true
|
77
|
+
poller.register_readable s1
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
puts "executed in [#{Time.now - start_time}] seconds"
|
data/ffi-rzmq.gemspec
CHANGED
@@ -2,15 +2,15 @@
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{ffi-rzmq}
|
5
|
-
s.version = "0.5.
|
5
|
+
s.version = "0.5.1"
|
6
6
|
|
7
7
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
8
8
|
s.authors = ["Chuck Remes"]
|
9
|
-
s.date = %q{2010-
|
9
|
+
s.date = %q{2010-08-30}
|
10
10
|
s.description = %q{This gem wraps the ZeroMQ networking library using the ruby FFI (foreign
|
11
11
|
function interface). It's a pure ruby wrapper so this gem can be loaded
|
12
12
|
and run by any ruby runtime that supports FFI. Right now that means
|
13
|
-
MRI 1.
|
13
|
+
MRI 1.9.x and JRuby.
|
14
14
|
|
15
15
|
The impetus behind this library was to provide support for ZeroMQ in
|
16
16
|
JRuby which has native threads. Unlike MRI, MacRuby, IronRuby and
|
@@ -20,7 +20,7 @@ other runtimes remove their GIL, JRuby will likely be the best
|
|
20
20
|
environment to run this library.}
|
21
21
|
s.email = %q{cremes@mac.com}
|
22
22
|
s.extra_rdoc_files = ["History.txt", "README.rdoc", "version.txt"]
|
23
|
-
s.files = ["History.txt", "README.rdoc", "Rakefile", "examples/local_lat.rb", "examples/local_lat_zerocopy.rb", "examples/publish_subscribe.rb", "examples/remote_lat.rb", "examples/remote_lat_zerocopy.rb", "examples/reqrep_poll.rb", "examples/request_response.rb", "ffi-rzmq.gemspec", "lib/ffi-rzmq.rb", "lib/ffi-rzmq/context.rb", "lib/ffi-rzmq/exceptions.rb", "lib/ffi-rzmq/message.rb", "lib/ffi-rzmq/poll.rb", "lib/ffi-rzmq/poll_items.rb", "lib/ffi-rzmq/socket.rb", "lib/ffi-rzmq/wrapper.rb", "lib/ffi-rzmq/zmq.rb", "spec/context_spec.rb", "spec/reqrep_spec.rb", "spec/socket_spec.rb", "spec/spec_helper.rb", "version.txt"]
|
23
|
+
s.files = [".bnsignore", "History.txt", "README.rdoc", "Rakefile", "examples/async_req_rep.rb", "examples/local_lat.rb", "examples/local_lat_zerocopy.rb", "examples/publish_subscribe.rb", "examples/remote_lat.rb", "examples/remote_lat_zerocopy.rb", "examples/reqrep_poll.rb", "examples/request_response.rb", "examples/t", "examples/xreqxrep_poll.rb", "ffi-rzmq.gemspec", "lib/ffi-rzmq.rb", "lib/ffi-rzmq/context.rb", "lib/ffi-rzmq/exceptions.rb", "lib/ffi-rzmq/message.rb", "lib/ffi-rzmq/poll.rb", "lib/ffi-rzmq/poll_items.rb", "lib/ffi-rzmq/socket.rb", "lib/ffi-rzmq/wrapper.rb", "lib/ffi-rzmq/zmq.rb", "spec/context_spec.rb", "spec/reqrep_spec.rb", "spec/socket_spec.rb", "spec/spec_helper.rb", "version.txt"]
|
24
24
|
s.homepage = %q{http://github.com/chuckremes/ffi-rzmq}
|
25
25
|
s.rdoc_options = ["--main", "README.rdoc"]
|
26
26
|
s.require_paths = ["lib"]
|
@@ -33,11 +33,11 @@ environment to run this library.}
|
|
33
33
|
s.specification_version = 3
|
34
34
|
|
35
35
|
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
36
|
-
s.add_development_dependency(%q<bones>, [">= 3.4.
|
36
|
+
s.add_development_dependency(%q<bones>, [">= 3.4.7"])
|
37
37
|
else
|
38
|
-
s.add_dependency(%q<bones>, [">= 3.4.
|
38
|
+
s.add_dependency(%q<bones>, [">= 3.4.7"])
|
39
39
|
end
|
40
40
|
else
|
41
|
-
s.add_dependency(%q<bones>, [">= 3.4.
|
41
|
+
s.add_dependency(%q<bones>, [">= 3.4.7"])
|
42
42
|
end
|
43
43
|
end
|
data/lib/ffi-rzmq.rb
CHANGED
@@ -63,7 +63,8 @@ end # module ZMQ
|
|
63
63
|
|
64
64
|
# some code is conditionalized based upon what ruby engine we are
|
65
65
|
# executing
|
66
|
-
|
66
|
+
|
67
|
+
RBX = defined?(RUBY_ENGINE) && RUBY_ENGINE =~ /rbx/ ? true : false
|
67
68
|
|
68
69
|
# the order of files is important
|
69
70
|
%w(wrapper zmq exceptions context message socket poll_items poll).each do |file|
|
data/lib/ffi-rzmq/context.rb
CHANGED
data/lib/ffi-rzmq/message.rb
CHANGED
@@ -95,7 +95,7 @@ module ZMQ
|
|
95
95
|
# deallocation of the native memory buffer.
|
96
96
|
#
|
97
97
|
def copy_in_string string
|
98
|
-
copy_in_bytes string, string.size
|
98
|
+
copy_in_bytes string, string.size if string
|
99
99
|
end
|
100
100
|
|
101
101
|
# Makes a copy of +len+ bytes from the ruby string +bytes+. Library
|
@@ -104,7 +104,7 @@ module ZMQ
|
|
104
104
|
def copy_in_bytes bytes, len
|
105
105
|
# release any associated buffers if this Message object is being
|
106
106
|
# reused
|
107
|
-
close unless uninitialized?
|
107
|
+
close unless uninitialized? # FIXME: this is a bug waiting to happen
|
108
108
|
|
109
109
|
data_buffer = LibC.malloc len
|
110
110
|
# writes the exact number of bytes, no null byte to terminate string
|
data/lib/ffi-rzmq/poll.rb
CHANGED
@@ -67,7 +67,6 @@ module ZMQ
|
|
67
67
|
unless item
|
68
68
|
@sockets << sock
|
69
69
|
item = LibZMQ::PollItem.new
|
70
|
-
@raw_to_socket[item.socket] = sock
|
71
70
|
|
72
71
|
case sock
|
73
72
|
when ZMQ::Socket, Socket
|
@@ -77,11 +76,12 @@ module ZMQ
|
|
77
76
|
item[:socket] = 0
|
78
77
|
item[:fd] = fd
|
79
78
|
end
|
80
|
-
end
|
81
79
|
|
80
|
+
@raw_to_socket[item[:socket].address] = sock
|
81
|
+
@items << item
|
82
|
+
end
|
83
|
+
|
82
84
|
item[:events] |= events
|
83
|
-
|
84
|
-
@items << item
|
85
85
|
end
|
86
86
|
|
87
87
|
# Deregister the +sock+ for +events+.
|
@@ -97,7 +97,7 @@ module ZMQ
|
|
97
97
|
# change the value in place
|
98
98
|
item[:events] ^= events
|
99
99
|
|
100
|
-
delete sock if
|
100
|
+
delete sock if item[:events].zero?
|
101
101
|
end
|
102
102
|
end
|
103
103
|
|
@@ -150,7 +150,7 @@ module ZMQ
|
|
150
150
|
hsh = {}
|
151
151
|
|
152
152
|
@items.each do |poll_item|
|
153
|
-
hsh[@raw_to_socket[poll_item
|
153
|
+
hsh[@raw_to_socket[poll_item[:socket].address]] = poll_item
|
154
154
|
end
|
155
155
|
|
156
156
|
hsh
|
@@ -161,8 +161,8 @@ module ZMQ
|
|
161
161
|
@writables.clear
|
162
162
|
|
163
163
|
@items.each do |poll_item|
|
164
|
-
@readables << @raw_to_socket[poll_item
|
165
|
-
@writables << @raw_to_socket[poll_item
|
164
|
+
@readables << @raw_to_socket[poll_item[:socket].address] if poll_item.readable?
|
165
|
+
@writables << @raw_to_socket[poll_item[:socket].address] if poll_item.writable?
|
166
166
|
end
|
167
167
|
end
|
168
168
|
|
data/lib/ffi-rzmq/socket.rb
CHANGED
@@ -19,7 +19,7 @@ module ZMQ
|
|
19
19
|
# memory management.
|
20
20
|
#
|
21
21
|
# +type+ can be one of ZMQ::REQ, ZMQ::REP, ZMQ::PUB,
|
22
|
-
# ZMQ::SUB, ZMQ::PAIR, ZMQ::
|
22
|
+
# ZMQ::SUB, ZMQ::PAIR, ZMQ::PULL, ZMQ::PUSH,
|
23
23
|
# ZMQ::XREQ or ZMQ::XREP.
|
24
24
|
#
|
25
25
|
# Can raise two kinds of exceptions depending on the error.
|
@@ -40,7 +40,7 @@ module ZMQ
|
|
40
40
|
raise ContextError.new ZMQ_SOCKET_STR, 0, ETERM, "Context pointer was null"
|
41
41
|
end
|
42
42
|
|
43
|
-
define_finalizer
|
43
|
+
#define_finalizer
|
44
44
|
end
|
45
45
|
|
46
46
|
# Set the queue options on this socket.
|
@@ -119,13 +119,13 @@ module ZMQ
|
|
119
119
|
# will force a raise
|
120
120
|
error_check ZMQ_SETSOCKOPT_STR, -1
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
123
|
option_value, option_length = alloc_temp_sockopt_buffers option_name
|
124
124
|
|
125
125
|
result_code = LibZMQ.zmq_getsockopt @socket, option_name, option_value, option_length
|
126
126
|
error_check ZMQ_GETSOCKOPT_STR, result_code
|
127
127
|
ret = 0
|
128
|
-
|
128
|
+
|
129
129
|
case option_name
|
130
130
|
when RCVMORE, MCAST_LOOP
|
131
131
|
# boolean return
|
@@ -135,11 +135,11 @@ module ZMQ
|
|
135
135
|
when IDENTITY
|
136
136
|
ret = option_value.read_string(option_length.read_long_long)
|
137
137
|
end
|
138
|
-
|
138
|
+
|
139
139
|
ret
|
140
140
|
end
|
141
141
|
end
|
142
|
-
|
142
|
+
|
143
143
|
# Convenience method for checking on additional message parts.
|
144
144
|
#
|
145
145
|
# Equivalent to Socket#getsockopt ZMQ::RCVMORE
|
@@ -147,13 +147,13 @@ module ZMQ
|
|
147
147
|
def more_parts?
|
148
148
|
getsockopt ZMQ::RCVMORE
|
149
149
|
end
|
150
|
-
|
150
|
+
|
151
151
|
# Convenience method for getting the value of the socket IDENTITY.
|
152
152
|
#
|
153
153
|
def identity
|
154
154
|
getsockopt ZMQ::IDENTITY
|
155
155
|
end
|
156
|
-
|
156
|
+
|
157
157
|
# Convenience method for setting the value of the socket IDENTITY.
|
158
158
|
#
|
159
159
|
def identity= value
|
@@ -312,7 +312,7 @@ module ZMQ
|
|
312
312
|
|
313
313
|
flags != NOBLOCK ? error_check(ZMQ_RECV_STR, result_code) : error_check_nonblock(result_code)
|
314
314
|
end
|
315
|
-
|
315
|
+
|
316
316
|
def alloc_temp_sockopt_buffers option_name
|
317
317
|
length = FFI::MemoryPointer.new :int64
|
318
318
|
|
data/lib/ffi-rzmq/wrapper.rb
CHANGED
@@ -23,7 +23,8 @@ module LibZMQ
|
|
23
23
|
LINUX = ["libzmq", "/usr/local/lib/libzmq", "/opt/local/lib/libzmq"]
|
24
24
|
OSX = ["libzmq", "/usr/local/lib/libzmq", "/opt/local/lib/libzmq"]
|
25
25
|
WINDOWS = []
|
26
|
-
|
26
|
+
ffi_lib(LINUX + OSX + WINDOWS)
|
27
|
+
|
27
28
|
|
28
29
|
# Misc
|
29
30
|
attach_function :zmq_version, [:pointer, :pointer, :pointer], :void
|
data/lib/ffi-rzmq/zmq.rb
CHANGED
data/spec/context_spec.rb
CHANGED
data/spec/socket_spec.rb
CHANGED
@@ -42,12 +42,12 @@ module ZMQ
|
|
42
42
|
lambda { Socket.new(ctx.pointer, ZMQ::XREP) }.should_not raise_error
|
43
43
|
end
|
44
44
|
|
45
|
-
it "should not raise an error for a ZMQ::
|
46
|
-
lambda { Socket.new(ctx.pointer, ZMQ::
|
45
|
+
it "should not raise an error for a ZMQ::PUSH socket type" do
|
46
|
+
lambda { Socket.new(ctx.pointer, ZMQ::PUSH) }.should_not raise_error
|
47
47
|
end
|
48
48
|
|
49
|
-
it "should not raise an error for a ZMQ::
|
50
|
-
lambda { Socket.new(ctx.pointer, ZMQ::
|
49
|
+
it "should not raise an error for a ZMQ::PULL socket type" do
|
50
|
+
lambda { Socket.new(ctx.pointer, ZMQ::PULL) }.should_not raise_error
|
51
51
|
end
|
52
52
|
|
53
53
|
it "should raise an error for an unknown socket type" do
|
@@ -60,6 +60,7 @@ module ZMQ
|
|
60
60
|
end
|
61
61
|
|
62
62
|
it "should define a finalizer on this object" do
|
63
|
+
pending # need to wait for 0mq 2.1 or later to fix this
|
63
64
|
ObjectSpace.should_receive(:define_finalizer)
|
64
65
|
ctx = Context.new 1
|
65
66
|
end
|
data/version.txt
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.1
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 5
|
8
|
-
-
|
9
|
-
version: 0.5.
|
8
|
+
- 1
|
9
|
+
version: 0.5.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Chuck Remes
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-08-30 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -27,15 +27,15 @@ dependencies:
|
|
27
27
|
segments:
|
28
28
|
- 3
|
29
29
|
- 4
|
30
|
-
-
|
31
|
-
version: 3.4.
|
30
|
+
- 7
|
31
|
+
version: 3.4.7
|
32
32
|
type: :development
|
33
33
|
version_requirements: *id001
|
34
34
|
description: |-
|
35
35
|
This gem wraps the ZeroMQ networking library using the ruby FFI (foreign
|
36
36
|
function interface). It's a pure ruby wrapper so this gem can be loaded
|
37
37
|
and run by any ruby runtime that supports FFI. Right now that means
|
38
|
-
MRI 1.
|
38
|
+
MRI 1.9.x and JRuby.
|
39
39
|
|
40
40
|
The impetus behind this library was to provide support for ZeroMQ in
|
41
41
|
JRuby which has native threads. Unlike MRI, MacRuby, IronRuby and
|
@@ -53,9 +53,11 @@ extra_rdoc_files:
|
|
53
53
|
- README.rdoc
|
54
54
|
- version.txt
|
55
55
|
files:
|
56
|
+
- .bnsignore
|
56
57
|
- History.txt
|
57
58
|
- README.rdoc
|
58
59
|
- Rakefile
|
60
|
+
- examples/async_req_rep.rb
|
59
61
|
- examples/local_lat.rb
|
60
62
|
- examples/local_lat_zerocopy.rb
|
61
63
|
- examples/publish_subscribe.rb
|
@@ -63,6 +65,8 @@ files:
|
|
63
65
|
- examples/remote_lat_zerocopy.rb
|
64
66
|
- examples/reqrep_poll.rb
|
65
67
|
- examples/request_response.rb
|
68
|
+
- examples/t
|
69
|
+
- examples/xreqxrep_poll.rb
|
66
70
|
- ffi-rzmq.gemspec
|
67
71
|
- lib/ffi-rzmq.rb
|
68
72
|
- lib/ffi-rzmq/context.rb
|