em-zeromq 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -66,6 +66,7 @@ EM.run {
66
66
 
67
67
  pull.on(:message) { |part|
68
68
  puts part.copy_out_string
69
+ part.close
69
70
  }
70
71
 
71
72
  EM.add_periodic_timer(1) {
@@ -73,6 +74,7 @@ EM.run {
73
74
  }
74
75
  }
75
76
  ```
77
+ Note that it's important to close message parts to avoid memory leaks.
76
78
 
77
79
  ## License: ##
78
80
 
data/Rakefile CHANGED
@@ -4,5 +4,6 @@ Bundler::GemHelper.install_tasks
4
4
  require 'rspec/core/rake_task'
5
5
  RSpec::Core::RakeTask.new(:spec) do |t|
6
6
  ENV['COVERAGE'] = "1"
7
+ t.rspec_opts = ['--color']
7
8
  end
8
9
  task :default => :spec
@@ -17,7 +17,7 @@ Gem::Specification.new do |s|
17
17
 
18
18
  s.add_dependency 'eventmachine', '>= 1.0.0'
19
19
  s.add_dependency 'ffi', '>= 1.0.0'
20
- s.add_dependency 'ffi-rzmq', '0.9.3'
20
+ s.add_dependency 'ffi-rzmq', '= 0.9.7'
21
21
 
22
22
  s.add_development_dependency 'rspec', '>= 2.5.0'
23
23
  s.add_development_dependency 'simplecov'
@@ -13,10 +13,13 @@ EM.run {
13
13
 
14
14
  pull.on(:message) { |part1, part2|
15
15
  p [:part1, part1.copy_out_string, :part2, part2.copy_out_string]
16
+ part1.close
17
+ part2.close
16
18
  }
17
19
 
18
20
  pull.on(:message) { |*parts|
19
21
  p [:parts, parts.map(&:copy_out_string)]
22
+ parts.each(&:close)
20
23
  }
21
24
 
22
25
  push = zmq.socket(ZMQ::PUSH)
@@ -36,6 +36,7 @@ EM.run do
36
36
  pull_socket.on(:message) { |*parts|
37
37
  parts.each do |m|
38
38
  puts m.copy_out_string
39
+ m.close
39
40
  end
40
41
  }
41
42
 
@@ -15,6 +15,7 @@ EM.run {
15
15
 
16
16
  pull.on(:message) { |part|
17
17
  puts part.copy_out_string
18
+ part.close
18
19
  }
19
20
 
20
21
  EM.add_periodic_timer(1) {
@@ -1,6 +1,13 @@
1
1
  require 'eventmachine'
2
2
  require 'ffi-rzmq'
3
3
 
4
+ # compatibilty hacks for zmq 2.x/3.x
5
+ module ZMQ
6
+ if LibZMQ.version3?
7
+ NOBLOCK = DONTWAIT
8
+ end
9
+ end
10
+
4
11
  module EmZeromq
5
12
 
6
13
  end
@@ -6,7 +6,7 @@ module EventMachine
6
6
 
7
7
  include EventEmitter
8
8
 
9
- attr_reader :socket, :socket_type
9
+ attr_reader :socket, :socket_type
10
10
 
11
11
  def initialize(socket, socket_type)
12
12
  @socket = socket
@@ -15,43 +15,53 @@ module EventMachine
15
15
  self.notify_readable = true if READABLES.include?(socket_type)
16
16
  self.notify_writable = true if WRITABLES.include?(socket_type)
17
17
  end
18
-
18
+
19
19
  def self.map_sockopt(opt, name)
20
20
  define_method(name){ getsockopt(opt) }
21
- define_method("#{name}="){|val| @socket.setsockopt(opt, val) }
21
+ define_method("#{name}="){|val| setsockopt(opt, val) }
22
+ end
23
+
24
+ if defined?(ZMQ::HWM)
25
+ map_sockopt(ZMQ::HWM, :hwm)
26
+ else
27
+ map_sockopt(ZMQ::SNDHWM, :sndhwm)
28
+ map_sockopt(ZMQ::RCVHWM, :rcvhwm)
29
+ def hwm=(val)
30
+ self.sndhwm = val
31
+ self.rcvhwm = val
32
+ end
22
33
  end
23
-
24
- map_sockopt(ZMQ::HWM, :hwm)
25
- map_sockopt(ZMQ::SWAP, :swap)
34
+
35
+ map_sockopt(ZMQ::SWAP, :swap) if defined?(ZMQ::SWAP)
26
36
  map_sockopt(ZMQ::IDENTITY, :identity)
27
37
  map_sockopt(ZMQ::AFFINITY, :affinity)
28
38
  map_sockopt(ZMQ::SNDBUF, :sndbuf)
29
39
  map_sockopt(ZMQ::RCVBUF, :rcvbuf)
30
-
40
+
31
41
  # pgm
32
42
  map_sockopt(ZMQ::RATE, :rate)
33
43
  map_sockopt(ZMQ::RECOVERY_IVL, :recovery_ivl)
34
- map_sockopt(ZMQ::MCAST_LOOP, :mcast_loop)
35
-
44
+ map_sockopt(ZMQ::MCAST_LOOP, :mcast_loop) if defined?(ZMQ::MCAST_LOOP)
45
+
36
46
  # User method
37
47
  def bind(address)
38
48
  @socket.bind(address)
39
49
  end
40
-
50
+
41
51
  def connect(address)
42
52
  @socket.connect(address)
43
53
  end
44
-
54
+
45
55
  def subscribe(what = '')
46
56
  raise "only valid on sub socket type (was #{@socket.name})" unless @socket.name == 'SUB'
47
57
  @socket.setsockopt(ZMQ::SUBSCRIBE, what)
48
58
  end
49
-
59
+
50
60
  def unsubscribe(what)
51
61
  raise "only valid on sub socket type (was #{@socket.name})" unless @socket.name == 'SUB'
52
62
  @socket.setsockopt(ZMQ::UNSUBSCRIBE, what)
53
63
  end
54
-
64
+
55
65
  # send a non blocking message
56
66
  # parts: if only one argument is given a signle part message is sent
57
67
  # if more than one arguments is given a multipart message is sent
@@ -61,34 +71,34 @@ module EventMachine
61
71
  def send_msg(*parts)
62
72
  parts = Array(parts[0]) if parts.size == 0
63
73
  sent = true
64
-
74
+
65
75
  # multipart
66
76
  parts[0...-1].each do |msg|
67
- sent = @socket.send_string(msg, ZMQ::NOBLOCK | ZMQ::SNDMORE)
68
- if sent == false
77
+ ret = @socket.send_string(msg, ZMQ::NOBLOCK | ZMQ::SNDMORE)
78
+ if ret < 0
79
+ sent = false
69
80
  break
70
81
  end
71
82
  end
72
-
83
+
73
84
  if sent
74
85
  # all the previous parts were queued, send
75
86
  # the last one
76
87
  ret = @socket.send_string(parts[-1], ZMQ::NOBLOCK)
77
88
  if ret < 0
78
- raise "Unable to send message: #{ZMQ::Util.error_string}"
89
+ sent = false
79
90
  end
80
91
  else
81
92
  # error while sending the previous parts
82
93
  # register the socket for writability
83
94
  self.notify_writable = true
84
- sent = false
85
95
  end
86
-
96
+
87
97
  EM::next_tick{ notify_readable() }
88
-
98
+
89
99
  sent
90
100
  end
91
-
101
+
92
102
  def getsockopt(opt)
93
103
  ret = []
94
104
  rc = @socket.getsockopt(opt, ret)
@@ -96,9 +106,9 @@ module EventMachine
96
106
  raise ZMQOperationFailed, "getsockopt: #{ZMQ::Util.error_string}"
97
107
  end
98
108
 
99
- (ret.size == 1) ? ret[0] : ret
109
+ (ret.size == 1) ? ret[0] : ret
100
110
  end
101
-
111
+
102
112
  def setsockopt(opt, value)
103
113
  @socket.setsockopt(opt, value)
104
114
  end
@@ -119,15 +129,15 @@ module EventMachine
119
129
  emit(:message, *message)
120
130
  end
121
131
  end
122
-
132
+
123
133
  def notify_writable
124
134
  return unless writable?
125
-
135
+
126
136
  # one a writable event is successfully received the socket
127
137
  # should be accepting messages again so stop triggering
128
138
  # write events
129
139
  self.notify_writable = false
130
-
140
+
131
141
  emit(:writable)
132
142
  end
133
143
  def readable?
@@ -139,7 +149,7 @@ module EventMachine
139
149
  # ZMQ::EVENTS has issues in ZMQ HEAD, we'll ignore this till they're fixed
140
150
  # (getsockopt(ZMQ::EVENTS) & ZMQ::POLLOUT) == ZMQ::POLLOUT
141
151
  end
142
-
152
+
143
153
  private
144
154
 
145
155
  def get_message
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module EmZeromq
3
- VERSION = "0.4.0"
3
+ VERSION = "0.4.1"
4
4
  end
@@ -4,19 +4,10 @@ describe 'Context' do
4
4
  before do
5
5
  @ctx = EM::ZeroMQ::Context.new(1)
6
6
  end
7
-
7
+
8
8
  it 'can be created with a context' do
9
9
  zmq_ctx = ZMQ::Context.new(1)
10
10
  ctx = EM::ZeroMQ::Context.new( zmq_ctx )
11
11
  ctx.instance_variable_get('@context').should == zmq_ctx
12
12
  end
13
-
14
- it 'can create socket' do
15
- EM::run do
16
- s = @ctx.socket(ZMQ::ROUTER)
17
- s.instance_variable_get('@socket').name.should == 'ROUTER'
18
- EM::stop_event_loop
19
- end
20
- end
21
13
  end
22
-
@@ -18,7 +18,7 @@ describe EventMachine::ZeroMQ do
18
18
  @received = []
19
19
  @test_message = test_message = "TMsg#{rand(999)}"
20
20
 
21
- run_reactor do
21
+ run_reactor(0.2) do
22
22
  address = rand_addr
23
23
 
24
24
  sub_conn = SPEC_CTX.socket(ZMQ::SUB)
@@ -31,9 +31,9 @@ describe EventMachine::ZeroMQ do
31
31
  pub_conn = SPEC_CTX.socket(ZMQ::PUB)
32
32
  pub_conn.connect(address)
33
33
 
34
- pub_conn.socket.send_string test_message, ZMQ::NOBLOCK
35
-
36
- EM::Timer.new(0.1) { @results[:specs_ran] = true }
34
+ EM::Timer.new(0.1) do
35
+ pub_conn.socket.send_string test_message, ZMQ::NOBLOCK
36
+ end
37
37
  end
38
38
  end
39
39
 
@@ -0,0 +1,29 @@
1
+ require File.join(File.dirname(__FILE__), %w[spec_helper])
2
+
3
+ describe 'Socket' do
4
+ before do
5
+ @ctx = EM::ZeroMQ::Context.new(1)
6
+ end
7
+
8
+ it 'can create a socket' do
9
+ EM.run do
10
+ s = @ctx.socket(ZMQ::ROUTER)
11
+ s.instance_variable_get('@socket').name.should == 'ROUTER'
12
+ EM.stop
13
+ end
14
+ end
15
+
16
+ it 'can set hwm' do
17
+ EM.run do
18
+ s = @ctx.socket(ZMQ::PUSH)
19
+ s.hwm = 100
20
+ if defined?(ZMQ::HWM)
21
+ s.hwm.should == 100
22
+ else
23
+ s.rcvhwm.should == 100
24
+ s.sndhwm.should == 100
25
+ end
26
+ EM.stop
27
+ end
28
+ end
29
+ end
@@ -11,14 +11,10 @@ require File.expand_path(
11
11
  File.join(File.dirname(__FILE__), %w[.. lib em-zeromq]))
12
12
 
13
13
  def run_reactor(time=0.1,&block)
14
- Thread.new do
15
- EM.run do
16
- yield
17
- end
14
+ EM.run do
15
+ yield
16
+ EM.add_timer(time){EM.stop}
18
17
  end
19
- sleep time
20
- EM.stop rescue nil
21
- sleep 0.1
22
18
  end
23
19
 
24
20
  USED_RAND_ADDRS = Set.new
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-zeromq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-11-15 00:00:00.000000000 Z
13
+ date: 2013-01-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: eventmachine
@@ -51,7 +51,7 @@ dependencies:
51
51
  requirements:
52
52
  - - '='
53
53
  - !ruby/object:Gem::Version
54
- version: 0.9.3
54
+ version: 0.9.7
55
55
  type: :runtime
56
56
  prerelease: false
57
57
  version_requirements: !ruby/object:Gem::Requirement
@@ -59,7 +59,7 @@ dependencies:
59
59
  requirements:
60
60
  - - '='
61
61
  - !ruby/object:Gem::Version
62
- version: 0.9.3
62
+ version: 0.9.7
63
63
  - !ruby/object:Gem::Dependency
64
64
  name: rspec
65
65
  requirement: !ruby/object:Gem::Requirement
@@ -135,6 +135,7 @@ files:
135
135
  - spec/pub_sub_spec.rb
136
136
  - spec/push_pull_spec.rb
137
137
  - spec/router_dealer_spec.rb
138
+ - spec/socket_spec.rb
138
139
  - spec/spec_helper.rb
139
140
  homepage: https://github.com/andrewvc/em-zeromq
140
141
  licenses: []
@@ -152,7 +153,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
153
  version: '0'
153
154
  segments:
154
155
  - 0
155
- hash: 4479496977537005722
156
+ hash: -528291771408101050
156
157
  required_rubygems_version: !ruby/object:Gem::Requirement
157
158
  none: false
158
159
  requirements:
@@ -161,7 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
162
  version: '0'
162
163
  segments:
163
164
  - 0
164
- hash: 4479496977537005722
165
+ hash: -528291771408101050
165
166
  requirements: []
166
167
  rubyforge_project: em-zeromq
167
168
  rubygems_version: 1.8.24
@@ -173,4 +174,5 @@ test_files:
173
174
  - spec/pub_sub_spec.rb
174
175
  - spec/push_pull_spec.rb
175
176
  - spec/router_dealer_spec.rb
177
+ - spec/socket_spec.rb
176
178
  - spec/spec_helper.rb