em-zeromq 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Binary file
@@ -1,4 +1,3 @@
1
-
2
1
  module EventMachine
3
2
  module ZeroMQ
4
3
  class Connection < EventMachine::Connection
@@ -12,14 +11,24 @@ module EventMachine
12
11
  @address = address
13
12
  end
14
13
 
14
+ def readable?
15
+ (@socket.getsockopt(ZMQ::EVENTS) & ZMQ::POLLIN) == ZMQ::POLLIN
16
+ end
17
+
18
+ def writable?
19
+ (@socket.getsockopt(ZMQ::EVENTS) & ZMQ::POLLOUT) == ZMQ::POLLOUT
20
+ end
21
+
15
22
  def notify_readable
16
- return unless read_capable?
17
- #return unless (@socket.getsockopt(ZMQ::EVENTS) & ZMQ::POLLOUT) == ZMQ::POLLOUT
23
+ # Not sure if this is actually necessary. I suppose it prevents us
24
+ # from having to to instantiate a ZMQ::Message unnecessarily.
25
+ # I'm leaving this is because its in the docs, but it could probably
26
+ # be taken out.
27
+ return unless readable?
18
28
 
19
- msg_parts = []
20
-
21
29
  loop do
22
- msg = get_message
30
+ msg_parts = []
31
+ msg = get_message
23
32
  if msg
24
33
  msg_parts << msg
25
34
  while @socket.more_parts?
@@ -44,29 +53,40 @@ module EventMachine
44
53
  msg_recvd ? msg : nil
45
54
  end
46
55
 
56
+ def notify_writable
57
+ if writable?
58
+ @handler.on_writable(@socket)
59
+ end
60
+ end
61
+
62
+ # Stop triggering on_writable when socket is writable
47
63
  def deregister_writable
48
64
  self.notify_writable = false
49
65
  end
50
-
66
+
67
+ # Make this socket available for reads
68
+ def register_readable
69
+ # Since ZMQ is event triggered I think this is necessary
70
+ if (@socket.getsockopt(ZMQ::EVENTS) & ZMQ::POLLIN) == ZMQ::POLLIN
71
+ notify_readable
72
+ end
73
+ self.notify_readable = true
74
+ end
75
+
76
+ # Trigger on_readable when socket is readable
51
77
  def register_writable
78
+ # Since ZMQ is event triggered I think this is necessary
52
79
  if (@socket.getsockopt(ZMQ::EVENTS) & ZMQ::POLLOUT) == ZMQ::POLLOUT
53
80
  notify_writable
54
81
  end
55
82
  self.notify_writable = true
56
83
  end
57
84
 
58
- def notify_writable
59
- @handler.on_writable(@socket)
60
- end
61
-
62
- private
63
-
64
- def read_capable?
65
- @read_capable ||= EM::ZeroMQ::READABLE_TYPES.include? @socket_type
66
- end
67
-
68
- def write_capable?
69
- @write_capable ||= EM::ZeroMQ::WRITABLE_TYPES.include? @socket_type
85
+ # Detaches the socket from the EM loop,
86
+ # then closes the socket
87
+ def detach_and_close
88
+ detach
89
+ @socket.close
70
90
  end
71
91
  end
72
92
  end
@@ -1,8 +1,5 @@
1
1
  module EventMachine
2
2
  module ZeroMQ
3
- READABLE_TYPES = [ZMQ::SUB, ZMQ::PULL, ZMQ::REQ, ZMQ::REP, ZMQ::XREQ, ZMQ::XREP, ZMQ::PAIR]
4
- WRITABLE_TYPES = [ZMQ::PUB, ZMQ::PUSH, ZMQ::REQ, ZMQ::REP, ZMQ::XREQ, ZMQ::XREP, ZMQ::PAIR]
5
-
6
3
  def self.create(context, socket_type, bind_or_connect, address, handler)
7
4
  socket = context.socket socket_type
8
5
 
@@ -17,13 +14,6 @@ module EventMachine
17
14
  end
18
15
 
19
16
  conn = EM.watch(socket.getsockopt(ZMQ::FD), EventMachine::ZeroMQ::Connection, socket, socket_type, address, handler)
20
- conn.notify_readable = true if EM::ZeroMQ::READABLE_TYPES.include? socket_type
21
-
22
- #Given the nature of ZMQ this isn't that useful, and will generally
23
- #cause perf problems as it repeatedly triggers. If people really want to
24
- #use it, they should do so explicitly
25
- conn.notify_writable = false
26
- conn
27
17
  end
28
18
 
29
19
  end
Binary file
@@ -22,6 +22,7 @@ describe EventMachine::ZeroMQ do
22
22
  def make_sub(addr, b_or_c, handler=EMTestSubHandler.new)
23
23
  conn = EM::ZeroMQ.create SPEC_CTX, ZMQ::SUB, b_or_c, addr, handler
24
24
  conn.socket.setsockopt(ZMQ::SUBSCRIBE,'')
25
+ conn.register_readable
25
26
  conn
26
27
  end
27
28
 
@@ -20,11 +20,13 @@ describe EventMachine::ZeroMQ do
20
20
  end
21
21
 
22
22
  def make_pull(addr, b_or_c, handler=EMTestPullHandler.new)
23
- EM::ZeroMQ.create SPEC_CTX, ZMQ::PULL, b_or_c, addr, handler
23
+ conn = EM::ZeroMQ.create SPEC_CTX, ZMQ::PULL, b_or_c, addr, handler
24
+ conn.register_readable
25
+ conn
24
26
  end
25
27
 
26
28
  def make_push(addr, b_or_c, handler=EMTestPushHandler.new)
27
- EM::ZeroMQ.create SPEC_CTX, ZMQ::PUSH, b_or_c, addr, handler
29
+ conn = EM::ZeroMQ.create SPEC_CTX, ZMQ::PUSH, b_or_c, addr, handler
28
30
  end
29
31
 
30
32
  it "Should instantiate a connection given valid opts" do
@@ -57,15 +59,9 @@ describe EventMachine::ZeroMQ do
57
59
  @results[:specs_ran].should be_true
58
60
  end
59
61
 
60
- it "should receive one message" do
61
- @results[:pull_hndlr].received.length.should == 1
62
- end
63
-
64
- it "should receive the message as a ZMQ::Message" do
65
- @results[:pull_hndlr].received.first.should be_a(ZMQ::Message)
66
- end
67
-
68
62
  it "should receive the message intact" do
63
+ @results[:pull_hndlr].received.should_not be_empty
64
+ @results[:pull_hndlr].received.first.should be_a(ZMQ::Message)
69
65
  @results[:pull_hndlr].received.first.copy_out_string.should == @test_message
70
66
  end
71
67
  end
@@ -0,0 +1,87 @@
1
+ require File.join(File.dirname(__FILE__), %w[spec_helper])
2
+
3
+ describe EventMachine::ZeroMQ do
4
+ class EMTestXREQHandler
5
+ attr_reader :received
6
+ def initialize
7
+ @received = []
8
+ end
9
+ def on_readable(socket, messages)
10
+ @received += messages
11
+ end
12
+ end
13
+ class EMTestXREPHandler
14
+ attr_reader :received
15
+ def initialize(&block)
16
+ @received = []
17
+ @on_writable_callback = block
18
+ end
19
+ def on_writable(socket)
20
+ @on_writable_callback.call(socket) if @on_writable_callback
21
+ end
22
+ def on_readable(socket, messages)
23
+ ident, delim, message = messages.map(&:copy_out_string)
24
+ @received += [ident, delim, message].map {|s| ZMQ::Message.new(s)}
25
+ socket.send_string ident, ZMQ::SNDMORE
26
+ socket.send_string delim, ZMQ::SNDMORE
27
+ socket.send_string message
28
+ end
29
+ end
30
+
31
+ def make_xreq(addr, b_or_c, handler=EMTestXREQHandler.new)
32
+ conn = EM::ZeroMQ.create SPEC_CTX, ZMQ::XREQ, b_or_c, addr, handler
33
+ conn.register_readable
34
+ conn
35
+ end
36
+
37
+ def make_xrep(addr, b_or_c, handler=EMTestXREPHandler.new)
38
+ conn = EM::ZeroMQ.create SPEC_CTX, ZMQ::XREP, b_or_c, addr, handler
39
+ conn.register_readable
40
+ conn
41
+ end
42
+
43
+ it "Should instantiate a connection given valid opts" do
44
+ xreq_conn = nil
45
+ run_reactor(1) do
46
+ xreq_conn = make_xreq(rand_addr, :bind, EMTestXREQHandler.new)
47
+ end
48
+ xreq_conn.should be_a(EventMachine::ZeroMQ::Connection)
49
+ end
50
+
51
+ describe "sending/receiving a single message via Xreq/Xrep" do
52
+ before(:all) do
53
+ results = {}
54
+ @test_message = test_message = "TMsg#{rand(999)}"
55
+
56
+ run_reactor(1.5) do
57
+ results[:xrep_hndlr] = xrep_hndlr = EMTestXREPHandler.new
58
+ results[:xreq_hndlr] = xreq_hndlr = EMTestXREQHandler.new
59
+ xreq_conn = make_xreq rand_addr, :connect, xreq_hndlr
60
+ xrep_conn = make_xrep xreq_conn.address, :bind, xrep_hndlr
61
+
62
+ xreq_conn.socket.send_string '', ZMQ::SNDMORE #delim
63
+ xreq_conn.socket.send_string test_message
64
+
65
+ EM::Timer.new(0.1) { results[:specs_ran] = true }
66
+ end
67
+
68
+ @results = results
69
+ end
70
+
71
+ it "should run completely" do
72
+ @results[:specs_ran].should be_true
73
+ end
74
+
75
+ it "should receive the message intact on the xrep" do
76
+ @results[:xrep_hndlr].received.should_not be_empty
77
+ @results[:xrep_hndlr].received.last.should be_a(ZMQ::Message)
78
+ @results[:xrep_hndlr].received.last.copy_out_string.should == @test_message
79
+ end
80
+
81
+ it "the xreq should be echoed its original message" do
82
+ @results[:xreq_hndlr].received.should_not be_empty
83
+ @results[:xreq_hndlr].received.last.should be_a(ZMQ::Message)
84
+ @results[:xreq_hndlr].received.last.copy_out_string.should == @test_message
85
+ end
86
+ end
87
+ end
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: em-zeromq
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 31
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 1
8
- - 1
9
- version: 0.1.1
9
+ - 2
10
+ version: 0.1.2
10
11
  platform: ruby
11
12
  authors:
12
13
  - Andrew Cholakian
@@ -14,7 +15,7 @@ autorequire:
14
15
  bindir: bin
15
16
  cert_chain: []
16
17
 
17
- date: 2011-02-01 00:00:00 -08:00
18
+ date: 2011-02-02 00:00:00 -08:00
18
19
  default_executable:
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
@@ -25,11 +26,12 @@ dependencies:
25
26
  requirements:
26
27
  - - ">="
27
28
  - !ruby/object:Gem::Version
29
+ hash: 27
28
30
  segments:
29
31
  - 3
30
- - 6
31
- - 2
32
- version: 3.6.2
32
+ - 5
33
+ - 4
34
+ version: 3.5.4
33
35
  type: :development
34
36
  version_requirements: *id001
35
37
  description: Low level event machine support for ZeroMQ
@@ -41,10 +43,12 @@ extensions: []
41
43
  extra_rdoc_files:
42
44
  - History.txt
43
45
  - bin/em-zeromq
46
+ - lib/em-zeromq/.connection.rb.swp
47
+ - lib/em-zeromq/.zeromq.rb.swp
48
+ - version.txt
44
49
  files:
45
50
  - .Rakefile.swo
46
51
  - .bnsignore
47
- - .gitignore
48
52
  - History.txt
49
53
  - README.md
50
54
  - Rakefile
@@ -52,11 +56,16 @@ files:
52
56
  - em-zeromq.gemspec
53
57
  - example/simple.rb
54
58
  - lib/em-zeromq.rb
59
+ - lib/em-zeromq/.connection.rb.swp
60
+ - lib/em-zeromq/.zeromq.rb.swp
55
61
  - lib/em-zeromq/connection.rb
56
62
  - lib/em-zeromq/zeromq.rb
63
+ - spec/.spec_helper.rb.swp
64
+ - spec/.xreq_xrep_spec.rb.swp
57
65
  - spec/pub_sub_spec.rb
58
66
  - spec/push_pull_spec.rb
59
67
  - spec/spec_helper.rb
68
+ - spec/xreq_xrep_spec.rb
60
69
  - test/test_em-zeromq.rb
61
70
  - version.txt
62
71
  has_rdoc: true
@@ -74,6 +83,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
74
83
  requirements:
75
84
  - - ">="
76
85
  - !ruby/object:Gem::Version
86
+ hash: 3
77
87
  segments:
78
88
  - 0
79
89
  version: "0"
@@ -82,6 +92,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
82
92
  requirements:
83
93
  - - ">="
84
94
  - !ruby/object:Gem::Version
95
+ hash: 3
85
96
  segments:
86
97
  - 0
87
98
  version: "0"
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- *.swp
2
- *.swo
3
- *.swn
4
-
5
- *.rbc
6
- *~
7
-
8
- pkg