em-zeromq 0.2.3 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -12,3 +12,5 @@ pkg
12
12
  Gemfile.lock
13
13
  pkg/*
14
14
  .rvmrc
15
+ coverage/
16
+ *test*/
data/README.md CHANGED
@@ -12,6 +12,7 @@ EventMachine support for ZeroMQ
12
12
  ## Usage: ##
13
13
 
14
14
  Supported on:
15
+
15
16
  - MRI 1.9+
16
17
  - Rubinius
17
18
  - JRuby
@@ -29,7 +30,8 @@ to keep a reference to it in scope, this is what you don't want to do (that's ho
29
30
  ```ruby
30
31
  EM.run do
31
32
  context = EM::ZeroMQ::Context.new(1)
32
- dealer_socket = context.connect(...)
33
+ dealer_socket = context.sockt(...)
34
+ dealer_socket.connect(...)
33
35
  dealer_socket.send_msg('', "ping")
34
36
  end
35
37
  ```
@@ -42,42 +44,52 @@ It should not be a major problem anyway since code like above is only written fo
42
44
  but I just pulled my hair trying to figure out why my test code was not working so now you
43
45
  have been warned !
44
46
 
47
+ ## Breaking changes: 0.2.x => 0.3.x ##
48
+
49
+ Until the gem hit the 1.0 mark you should not use the "~>" operator in your Gemfile,
50
+ lock yourself to the exact version you want. That said I will use the second digit to
51
+ flag api changes but be aware that small changes can still occur between releases.
52
+
45
53
 
46
54
  ## Example ##
47
55
  ```ruby
48
56
  require 'rubygems'
49
57
  require 'em-zeromq'
50
-
51
- Thread.abort_on_exception = true
52
58
 
53
59
  class EMTestPullHandler
54
60
  attr_reader :received
55
- def on_readable(socket, messages)
56
- messages.each do |m|
61
+ def on_readable(socket, parts)
62
+ parts.each do |m|
57
63
  puts m.copy_out_string
58
64
  end
59
65
  end
60
66
  end
61
67
 
68
+ trap('INT') do
69
+ EM::stop()
70
+ end
62
71
 
63
72
  ctx = EM::ZeroMQ::Context.new(1)
64
73
  EM.run do
65
74
  # setup push sockets
66
- push_socket1 = ctx.bind( ZMQ::PUSH, 'tcp://127.0.0.1:2091')
67
- push_socket2 = ctx.bind( ZMQ::PUSH, 'ipc:///tmp/a')
68
- push_socket3 = ctx.bind( ZMQ::PUSH, 'inproc://simple_test')
75
+ push_socket1 = ctx.socket(ZMQ::PUSH)
76
+ push_socket1.bind('tcp://127.0.0.1:2091')
69
77
 
70
- # setup one pull sockets listening to both push sockets
71
- pull_socket = ctx.connect( ZMQ::PULL, 'tcp://127.0.0.1:2091', EMTestPullHandler.new)
78
+ push_socket2 = ctx.socket(ZMQ::PUSH) do |s|
79
+ s.bind('ipc:///tmp/a')
80
+ end
81
+
82
+ push_socket3 = ctx.socket(ZMQ::PUSH)
83
+ push_socket3.bind('inproc://simple_test')
84
+
85
+ # setup one pull sockets listening to all push sockets
86
+ pull_socket = ctx.socket(ZMQ::PULL, EMTestPullHandler.new)
87
+ pull_socket.connect('tcp://127.0.0.1:2091')
72
88
  pull_socket.connect('ipc:///tmp/a')
73
89
  pull_socket.connect('inproc://simple_test')
74
90
 
75
91
  n = 0
76
92
 
77
- # push_socket.hwm = 40
78
- # puts push_socket.hwm
79
- # puts pull_socket.hwm
80
-
81
93
  EM::PeriodicTimer.new(0.1) do
82
94
  puts '.'
83
95
  push_socket1.send_msg("t#{n += 1}_")
@@ -91,7 +103,7 @@ end
91
103
 
92
104
  (The MIT License)
93
105
 
94
- Copyright (c) 2011
106
+ Copyright (c) 2011 - 2012
95
107
 
96
108
  Permission is hereby granted, free of charge, to any person obtaining
97
109
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -3,5 +3,6 @@ Bundler::GemHelper.install_tasks
3
3
 
4
4
  require 'rspec/core/rake_task'
5
5
  RSpec::Core::RakeTask.new(:spec) do |t|
6
+ ENV['COVERAGE'] = "1"
6
7
  end
7
8
  task :default => :spec
@@ -20,6 +20,7 @@ Gem::Specification.new do |s|
20
20
  s.add_dependency 'ffi-rzmq', '0.9.3'
21
21
 
22
22
  s.add_development_dependency 'rspec', '>= 2.5.0'
23
+ s.add_development_dependency 'simplecov'
23
24
  s.add_development_dependency 'rake'
24
25
 
25
26
  s.files = `git ls-files`.split("\n")
@@ -6,8 +6,8 @@ Thread.abort_on_exception = true
6
6
 
7
7
  class EMTestPullHandler
8
8
  attr_reader :received
9
- def on_readable(socket, messages)
10
- messages.each do |m|
9
+ def on_readable(socket, parts)
10
+ parts.each do |m|
11
11
  puts m.copy_out_string
12
12
  end
13
13
  end
@@ -23,20 +23,28 @@ puts "Started (with zmq #{ZMQ::Util.version.join('.')})."
23
23
  ctx = EM::ZeroMQ::Context.new(1)
24
24
  EM.run do
25
25
  # setup push sockets
26
- push_socket1 = ctx.bind( ZMQ::PUSH, 'tcp://127.0.0.1:2091')
27
- push_socket2 = ctx.bind( ZMQ::PUSH, 'ipc:///tmp/a')
28
- push_socket3 = ctx.bind( ZMQ::PUSH, 'inproc://simple_test')
26
+ push_socket1 = ctx.socket(ZMQ::PUSH)
27
+
28
+ push_socket1.hwm = 40
29
+ puts "HWM: #{push_socket1.hwm}"
30
+
31
+ push_socket1.bind('tcp://127.0.0.1:2091')
32
+
33
+ push_socket2 = ctx.socket(ZMQ::PUSH) do |s|
34
+ s.bind('ipc:///tmp/a')
35
+ end
36
+
37
+ push_socket3 = ctx.socket(ZMQ::PUSH)
38
+ push_socket3.bind('inproc://simple_test')
29
39
 
30
40
  # setup one pull sockets listening to all push sockets
31
- pull_socket = ctx.connect( ZMQ::PULL, 'tcp://127.0.0.1:2091', EMTestPullHandler.new)
41
+ pull_socket = ctx.socket(ZMQ::PULL, EMTestPullHandler.new)
42
+ pull_socket.connect('tcp://127.0.0.1:2091')
32
43
  pull_socket.connect('ipc:///tmp/a')
33
44
  pull_socket.connect('inproc://simple_test')
34
45
 
35
46
  n = 0
36
47
 
37
- push_socket1.hwm = 40
38
- puts "HWM: #{push_socket1.hwm}"
39
-
40
48
  EM::PeriodicTimer.new(0.1) do
41
49
  puts '.'
42
50
  push_socket1.send_msg("t#{n += 1}_")
@@ -17,62 +17,34 @@ module EventMachine
17
17
  @context = ZMQ::Context.new(threads_or_context)
18
18
  end
19
19
  end
20
-
21
- def bind(socket_type, address, handler = nil, opts = {})
22
- create(socket_type, :bind, address, handler, opts)
23
- end
24
-
25
- def connect(socket_type, address, handler = nil, opts = {})
26
- create(socket_type, :connect, address, handler, opts)
27
- end
28
20
 
29
- def create(socket_type, bind_or_connect, address, handler, opts = {})
30
- socket_type = find_type(socket_type)
31
- socket = @context.socket(socket_type)
32
-
33
- ident = opts.delete(:identity)
34
- if ident
35
- socket.setsockopt(ZMQ::IDENTITY, ident)
36
- end
37
-
38
- unless opts.empty?
39
- raise "unknown keys: #{opts.keys.join(', ')}"
40
- end
41
-
42
- if bind_or_connect == :bind
43
- socket.bind(address)
44
- else
45
- socket.connect(address)
46
- end
21
+ ##
22
+ # Create a socket in this context.
23
+ #
24
+ # @param [Integer] socket_type One of ZMQ::REQ, ZMQ::REP, ZMQ::PULL, ZMQ::PUSH,
25
+ # ZMQ::ROUTER, ZMQ::DEALER
26
+ #
27
+ # @param [Object] handler an object which respond to on_readable(socket, parts)
28
+ # and can respond to on_writeable(socket)
29
+ #
30
+ def socket(socket_type, handler = nil)
31
+ zmq_socket = @context.socket(socket_type)
47
32
 
48
33
  fd = []
49
- if socket.getsockopt(ZMQ::FD, fd) < 0
34
+ if zmq_socket.getsockopt(ZMQ::FD, fd) < 0
50
35
  raise "Unable to get socket FD: #{ZMQ::Util.error_string}"
51
36
  end
52
37
 
53
- conn = EM.watch(fd[0], EventMachine::ZeroMQ::Connection, socket, socket_type, address, handler)
54
-
55
- if READABLES.include?(socket_type)
56
- conn.register_readable
57
- end
58
38
 
59
- if WRITABLES.include?(socket_type)
60
- conn.register_writable
61
- end
62
-
63
- conn
64
- end
65
-
66
- private
67
- def find_type(type)
68
- if type.is_a?(Symbol) or type.is_a?(String)
69
- ZMQ.const_get(type.to_s.upcase)
70
- else
71
- type
39
+ EM.watch(fd[0], EventMachine::ZeroMQ::Socket, zmq_socket, socket_type, handler).tap do |s|
40
+ s.register_readable if READABLES.include?(socket_type)
41
+ s.register_writable if WRITABLES.include?(socket_type)
42
+
43
+ yield(s) if block_given?
72
44
  end
73
45
  end
74
46
 
75
47
  end
76
-
48
+
77
49
  end
78
50
  end
@@ -1,14 +1,13 @@
1
1
  module EventMachine
2
2
  module ZeroMQ
3
- class Connection < EventMachine::Connection
3
+ class Socket < EventMachine::Connection
4
4
  attr_accessor :on_readable, :on_writable, :handler
5
- attr_reader :socket, :socket_type, :address
5
+ attr_reader :socket, :socket_type
6
6
 
7
- def initialize(socket, socket_type, address, handler)
7
+ def initialize(socket, socket_type, handler)
8
8
  @socket = socket
9
9
  @socket_type = socket_type
10
10
  @handler = handler
11
- @address = address
12
11
  end
13
12
 
14
13
  def self.map_sockopt(opt, name)
@@ -176,7 +175,7 @@ module EventMachine
176
175
  def get_message
177
176
  msg = ZMQ::Message.new
178
177
  msg_recvd = @socket.recv(msg, ZMQ::NOBLOCK)
179
- msg_recvd ? msg : nil
178
+ msg_recvd != -1 ? msg : nil
180
179
  end
181
180
 
182
181
  # Detaches the socket from the EM loop,
@@ -1,4 +1,4 @@
1
1
  # encoding: utf-8
2
2
  module EmZeromq
3
- VERSION = "0.2.3"
3
+ VERSION = "0.3.0"
4
4
  end
@@ -13,11 +13,8 @@ describe 'Context' do
13
13
 
14
14
  it 'can create socket' do
15
15
  EM::run do
16
- s1 = @ctx.bind(:router, 'tcp://127.0.0.1:5555')
17
- s2 = @ctx.bind('router', 'tcp://127.0.0.1:5556')
18
- s3 = @ctx.bind(ZMQ::ROUTER, 'tcp://127.0.0.1:5557')
19
-
20
- s1.instance_variable_get('@socket').name.should == 'ROUTER'
16
+ s = @ctx.socket(ZMQ::ROUTER)
17
+ s.instance_variable_get('@socket').name.should == 'ROUTER'
21
18
  EM::stop_event_loop
22
19
  end
23
20
  end
@@ -13,10 +13,13 @@ describe EventMachine::ZeroMQ do
13
13
 
14
14
  it "Should instantiate a connection given valid opts" do
15
15
  sub_conn = nil
16
+ address = rand_addr
17
+
16
18
  run_reactor(1) do
17
- sub_conn = SPEC_CTX.bind(ZMQ::PUB, rand_addr, EMTestSubHandler.new)
19
+ sub_conn = SPEC_CTX.socket(ZMQ::PUB, EMTestSubHandler.new)
20
+ sub_conn.bind(address)
18
21
  end
19
- sub_conn.should be_a(EventMachine::ZeroMQ::Connection)
22
+ sub_conn.should be_a(EventMachine::ZeroMQ::Socket)
20
23
  end
21
24
 
22
25
  describe "sending/receiving a single message via PUB/SUB" do
@@ -25,11 +28,15 @@ describe EventMachine::ZeroMQ do
25
28
  @test_message = test_message = "TMsg#{rand(999)}"
26
29
 
27
30
  run_reactor(0.5) do
31
+ address = rand_addr
32
+
28
33
  results[:sub_hndlr] = pull_hndlr = EMTestSubHandler.new
29
- sub_conn = SPEC_CTX.bind(ZMQ::SUB, rand_addr, pull_hndlr)
34
+ sub_conn = SPEC_CTX.socket(ZMQ::SUB, pull_hndlr)
35
+ sub_conn.bind(address)
30
36
  sub_conn.subscribe('')
31
37
 
32
- pub_conn = SPEC_CTX.connect(ZMQ::PUB, sub_conn.address, EMTestSubHandler.new)
38
+ pub_conn = SPEC_CTX.socket(ZMQ::PUB, EMTestSubHandler.new)
39
+ pub_conn.connect(address)
33
40
 
34
41
  pub_conn.socket.send_string test_message, ZMQ::NOBLOCK
35
42
 
@@ -14,9 +14,10 @@ describe EventMachine::ZeroMQ do
14
14
  it "Should instantiate a connection given valid opts" do
15
15
  pull_conn = nil
16
16
  run_reactor do
17
- pull_conn = SPEC_CTX.bind(ZMQ::PULL, rand_addr, EMTestPullHandler.new)
17
+ pull_conn = SPEC_CTX.socket(ZMQ::PULL, EMTestPullHandler.new)
18
+ pull_conn.bind(rand_addr)
18
19
  end
19
- pull_conn.should be_a(EventMachine::ZeroMQ::Connection)
20
+ pull_conn.should be_a(EventMachine::ZeroMQ::Socket)
20
21
  end
21
22
 
22
23
  describe "sending/receiving a single message via PUB/SUB" do
@@ -25,9 +26,15 @@ describe EventMachine::ZeroMQ do
25
26
  @test_message = test_message = "TMsg#{rand(999)}"
26
27
 
27
28
  run_reactor(0.5) do
29
+
30
+ address = rand_addr
31
+
28
32
  results[:pull_hndlr] = pull_hndlr = EMTestPullHandler.new
29
- pull_conn = SPEC_CTX.bind(ZMQ::PULL, rand_addr, pull_hndlr)
30
- push_conn = SPEC_CTX.connect(ZMQ::PUSH, pull_conn.address)
33
+ pull_conn = SPEC_CTX.socket(ZMQ::PULL, pull_hndlr)
34
+ pull_conn.bind(address)
35
+
36
+ push_conn = SPEC_CTX.socket(ZMQ::PUSH)
37
+ push_conn.connect(address)
31
38
 
32
39
  push_conn.socket.send_string test_message, ZMQ::NOBLOCK
33
40
 
@@ -33,9 +33,10 @@ describe EventMachine::ZeroMQ do
33
33
  it "Should instantiate a connection given valid opts for Router/Dealer" do
34
34
  router_conn = nil
35
35
  run_reactor(1) do
36
- router_conn = SPEC_CTX.bind(ZMQ::ROUTER, rand_addr, EMTestRouterHandler.new)
36
+ router_conn = SPEC_CTX.socket(ZMQ::ROUTER, EMTestRouterHandler.new)
37
+ router_conn.bind(rand_addr)
37
38
  end
38
- router_conn.should be_a(EventMachine::ZeroMQ::Connection)
39
+ router_conn.should be_a(EventMachine::ZeroMQ::Socket)
39
40
  end
40
41
 
41
42
  describe "sending/receiving a single message via Router/Dealer" do
@@ -48,8 +49,13 @@ describe EventMachine::ZeroMQ do
48
49
  results[:router_hndlr] = router_hndlr = EMTestRouterHandler.new
49
50
 
50
51
  addr = rand_addr
51
- dealer_conn = SPEC_CTX.bind(ZMQ::DEALER, addr, dealer_hndlr, :identity => "dealer1")
52
- router_conn = SPEC_CTX.connect(ZMQ::ROUTER, addr, router_hndlr, :identity => "router1")
52
+ dealer_conn = SPEC_CTX.socket(ZMQ::DEALER, dealer_hndlr)
53
+ dealer_conn.identity = "dealer1"
54
+ dealer_conn.bind(addr)
55
+
56
+ router_conn = SPEC_CTX.socket(ZMQ::ROUTER, router_hndlr)
57
+ router_conn.identity = "router1"
58
+ router_conn.connect(addr)
53
59
 
54
60
  EM::add_timer(0.1) do
55
61
  router_conn.send_msg('dealer1','', test_message)
@@ -2,6 +2,11 @@ require 'rspec'
2
2
  require 'set'
3
3
  Thread.abort_on_exception = true
4
4
 
5
+ if ENV['COVERAGE']
6
+ require 'simplecov'
7
+ SimpleCov.start
8
+ end
9
+
5
10
  require File.expand_path(
6
11
  File.join(File.dirname(__FILE__), %w[.. lib em-zeromq]))
7
12
 
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.2.3
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,11 +10,11 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-02-23 00:00:00.000000000 Z
13
+ date: 2012-03-18 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: eventmachine
17
- requirement: &70154900691620 !ruby/object:Gem::Requirement
17
+ requirement: &70098716501040 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - =
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.0.0.beta.4
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *70154900691620
25
+ version_requirements: *70098716501040
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: ffi
28
- requirement: &70154900707520 !ruby/object:Gem::Requirement
28
+ requirement: &70098716500480 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 1.0.0
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *70154900707520
36
+ version_requirements: *70098716500480
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: ffi-rzmq
39
- requirement: &70154900707060 !ruby/object:Gem::Requirement
39
+ requirement: &70098716499780 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - =
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 0.9.3
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *70154900707060
47
+ version_requirements: *70098716499780
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rspec
50
- requirement: &70154900706600 !ruby/object:Gem::Requirement
50
+ requirement: &70098716498920 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,10 +55,21 @@ dependencies:
55
55
  version: 2.5.0
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *70154900706600
58
+ version_requirements: *70098716498920
59
+ - !ruby/object:Gem::Dependency
60
+ name: simplecov
61
+ requirement: &70098716498140 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ! '>='
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *70098716498140
59
70
  - !ruby/object:Gem::Dependency
60
71
  name: rake
61
- requirement: &70154900706220 !ruby/object:Gem::Requirement
72
+ requirement: &70098716373420 !ruby/object:Gem::Requirement
62
73
  none: false
63
74
  requirements:
64
75
  - - ! '>='
@@ -66,7 +77,7 @@ dependencies:
66
77
  version: '0'
67
78
  type: :development
68
79
  prerelease: false
69
- version_requirements: *70154900706220
80
+ version_requirements: *70098716373420
70
81
  description: Low level event machine support for ZeroMQ
71
82
  email:
72
83
  - schmurfy@gmail.com
@@ -83,9 +94,8 @@ files:
83
94
  - em-zeromq.gemspec
84
95
  - example/simple.rb
85
96
  - lib/em-zeromq.rb
86
- - lib/em-zeromq/connection.rb
87
97
  - lib/em-zeromq/context.rb
88
- - lib/em-zeromq/rzmq_compat.rb
98
+ - lib/em-zeromq/socket.rb
89
99
  - lib/em-zeromq/version.rb
90
100
  - spec/context_spec.rb
91
101
  - spec/pub_sub_spec.rb
@@ -108,7 +118,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
108
118
  version: '0'
109
119
  segments:
110
120
  - 0
111
- hash: -3798582032913593030
121
+ hash: 4514270127933019528
112
122
  required_rubygems_version: !ruby/object:Gem::Requirement
113
123
  none: false
114
124
  requirements:
@@ -117,10 +127,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
117
127
  version: '0'
118
128
  segments:
119
129
  - 0
120
- hash: -3798582032913593030
130
+ hash: 4514270127933019528
121
131
  requirements: []
122
132
  rubyforge_project: em-zeromq
123
- rubygems_version: 1.8.15
133
+ rubygems_version: 1.8.11
124
134
  signing_key:
125
135
  specification_version: 3
126
136
  summary: Low level event machine support for ZeroMQ
@@ -1,26 +0,0 @@
1
- module RZMQCompat
2
- class ZMQError < RuntimeError; end
3
- class ZMQOperationFailed < ZMQError; end
4
-
5
- def self.included(klass)
6
- klass.instance_eval do
7
- %w(recv).each do |m|
8
- alias_method :"#{m}_without_raise", m.to_sym
9
- alias_method m.to_sym, :"#{m}_with_raise"
10
- end
11
- end
12
- end
13
-
14
- def recv_with_raise(msg, flags = 0)
15
- ret = recv_without_raise(msg, flags)
16
- if (ret == true) || (ret == 0)
17
- true
18
- else
19
- false
20
- end
21
- end
22
-
23
-
24
- end
25
-
26
- ZMQ::Socket.send(:include, RZMQCompat)