celluloid-zmq 0.8.0 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.md CHANGED
@@ -1,3 +1,10 @@
1
+ 0.9.0
2
+ -----
3
+ * New 0MQ APIs which wrap ffi-rzmq's
4
+ * Terminate the 0MQ context at shutdown
5
+ * Use Celluloid::IO 0.9.0's reactor injection support so we no longer have to
6
+ subclass Celluloid::IO::Mailbox
7
+
1
8
  0.8.0
2
9
  -----
3
10
  * Update to match internals of celluloid-io
@@ -11,13 +11,13 @@ Gem::Specification.new do |gem|
11
11
  gem.name = "celluloid-zmq"
12
12
  gem.version = Celluloid::ZMQ::VERSION
13
13
 
14
- gem.add_dependency "celluloid", "~> 0.8.0"
15
- gem.add_dependency "celluloid-io", "~> 0.8.0"
14
+ gem.add_dependency "celluloid", "~> 0.9.0"
15
+ gem.add_dependency "celluloid-io", "~> 0.9.0"
16
16
  gem.add_dependency "ffi"
17
17
  gem.add_dependency "ffi-rzmq"
18
18
 
19
19
  gem.add_development_dependency "rake"
20
- gem.add_development_dependency "rspec", ">= 2.7.0"
20
+ gem.add_development_dependency "rspec"
21
21
 
22
22
  # Files
23
23
  ignores = File.read(".gitignore").split(/\r?\n/).reject{ |f| f =~ /^(#.+|\s*)$/ }.map {|f| Dir[f] }.flatten
data/lib/celluloid/zmq.rb CHANGED
@@ -1,8 +1,8 @@
1
1
  require 'ffi-rzmq'
2
2
 
3
3
  require 'celluloid/io'
4
- require 'celluloid/zmq/mailbox'
5
4
  require 'celluloid/zmq/reactor'
5
+ require 'celluloid/zmq/sockets'
6
6
  require 'celluloid/zmq/version'
7
7
  require 'celluloid/zmq/waker'
8
8
 
@@ -15,19 +15,22 @@ module Celluloid
15
15
  # Included hook to pull in Celluloid
16
16
  def included(klass)
17
17
  klass.send :include, ::Celluloid
18
- klass.use_mailbox Celluloid::ZMQ::Mailbox
18
+ klass.use_mailbox { Celluloid::IO::Mailbox.new ZMQ::Reactor.new }
19
19
  end
20
20
 
21
21
  # Obtain a 0MQ context (or lazily initialize it)
22
- def context
23
- @context ||= ::ZMQ::Context.new(1)
22
+ def context(threads = 1)
23
+ return @context if @context
24
+ @context = ::ZMQ::Context.new(threads)
25
+ at_exit { @context.terminate }
26
+ @context
24
27
  end
28
+ alias_method :init, :context
25
29
  end
26
30
 
27
31
  extend Forwardable
28
32
 
29
33
  # Wait for the given IO object to become readable/writeable
30
- def_delegators 'current_actor.mailbox.reactor',
31
- :wait_readable, :wait_writeable
34
+ def_delegators 'current_actor.mailbox.reactor', :wait_readable, :wait_writeable
32
35
  end
33
36
  end
@@ -0,0 +1,130 @@
1
+ module Celluloid
2
+ module ZMQ
3
+ class Socket
4
+ # Create a new socket
5
+ def initialize(type)
6
+ @socket = Celluloid::ZMQ.context.socket ::ZMQ.const_get(type.to_s.upcase)
7
+ end
8
+
9
+ # Connect to the given 0MQ address
10
+ # Address should be in the form: tcp://1.2.3.4:5678/
11
+ def connect(addr)
12
+ puts "zomg connecting"
13
+ unless ::ZMQ::Util.resultcode_ok? @socket.connect addr
14
+ raise IOError, "error connecting to #{addr}: #{::ZMQ::Util.error_string}"
15
+ end
16
+ true
17
+ end
18
+
19
+ # Bind to the given 0MQ address
20
+ # Address should be in the form: tcp://1.2.3.4:5678/
21
+ def bind(addr)
22
+ unless ::ZMQ::Util.resultcode_ok? @socket.setsockopt(::ZMQ::LINGER, 0)
23
+ @socket.close
24
+ raise IOError, "couldn't set ZMQ::LINGER: #{::ZMQ::Util.error_string}"
25
+ end
26
+
27
+ unless ::ZMQ::Util.resultcode_ok? @socket.bind(addr)
28
+ raise IOError, "couldn't bind to #{addr}: #{::ZMQ::Util.error_string}"
29
+ end
30
+ end
31
+
32
+ # Close the socket
33
+ def close
34
+ @socket.close
35
+ end
36
+
37
+ # Does the 0MQ socket support evented operation?
38
+ def evented?
39
+ actor = Thread.current[:actor]
40
+ return unless actor
41
+
42
+ mailbox = actor.mailbox
43
+ mailbox.is_a?(Celluloid::IO::Mailbox) && mailbox.reactor.is_a?(Celluloid::ZMQ::Reactor)
44
+ end
45
+
46
+ # Hide ffi-rzmq internals
47
+ alias_method :inspect, :to_s
48
+ end
49
+
50
+ # Readable 0MQ sockets have a read method
51
+ module ReadableSocket
52
+ # Read a message from the socket
53
+ def read(buffer = '')
54
+ Celluloid.current_actor.wait_readable(@socket) if evented?
55
+
56
+ unless ::ZMQ::Util.resultcode_ok? @socket.recv_string buffer
57
+ raise IOError, "error receiving ZMQ string: #{::ZMQ::Util.error_string}"
58
+ end
59
+ buffer
60
+ end
61
+ end
62
+
63
+ # Writable 0MQ sockets have a send method
64
+ module WritableSocket
65
+ # Send a message to the socket
66
+ def send(message)
67
+ unless ::ZMQ::Util.resultcode_ok? @socket.send_string message
68
+ raise IOError, "error sending 0MQ message: #{::ZMQ::Util.error_string}"
69
+ end
70
+
71
+ message
72
+ end
73
+ alias_method :<<, :send
74
+ end
75
+
76
+ # ReqSockets are the counterpart of RepSockets (REQ/REP)
77
+ class ReqSocket < Socket
78
+ include ReadableSocket
79
+
80
+ def initialize
81
+ super :req
82
+ end
83
+ end
84
+
85
+ # RepSockets are the counterpart of ReqSockets (REQ/REP)
86
+ class RepSocket < Socket
87
+ include WritableSocket
88
+
89
+ def initialize
90
+ super :rep
91
+ end
92
+ end
93
+
94
+ # PushSockets are the counterpart of PullSockets (PUSH/PULL)
95
+ class PushSocket < Socket
96
+ include WritableSocket
97
+
98
+ def initialize
99
+ super :push
100
+ end
101
+ end
102
+
103
+ # PullSockets are the counterpart of PushSockets (PUSH/PULL)
104
+ class PullSocket < Socket
105
+ include ReadableSocket
106
+
107
+ def initialize
108
+ super :pull
109
+ end
110
+ end
111
+
112
+ # PubSockets are the counterpart of SubSockets (PUB/SUB)
113
+ class PubSocket < Socket
114
+ include WritableSocket
115
+
116
+ def initialize
117
+ super :pub
118
+ end
119
+ end
120
+
121
+ # SubSockets are the counterpart of PubSockets (PUB/SUB)
122
+ class SubSocket < Socket
123
+ include ReadableSocket
124
+
125
+ def initialize
126
+ super :sub
127
+ end
128
+ end
129
+ end
130
+ end
@@ -1,5 +1,5 @@
1
1
  module Celluloid
2
2
  module ZMQ
3
- VERSION = "0.8.0"
3
+ VERSION = "0.9.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: celluloid-zmq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,33 +9,33 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-24 00:00:00.000000000 Z
12
+ date: 2012-02-21 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: celluloid
16
- requirement: &70168804718540 !ruby/object:Gem::Requirement
16
+ requirement: &70214687519820 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
20
20
  - !ruby/object:Gem::Version
21
- version: 0.8.0
21
+ version: 0.9.0
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70168804718540
24
+ version_requirements: *70214687519820
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: celluloid-io
27
- requirement: &70168804717900 !ruby/object:Gem::Requirement
27
+ requirement: &70214687517520 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
31
31
  - !ruby/object:Gem::Version
32
- version: 0.8.0
32
+ version: 0.9.0
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70168804717900
35
+ version_requirements: *70214687517520
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: ffi
38
- requirement: &70168804717360 !ruby/object:Gem::Requirement
38
+ requirement: &70214687516960 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70168804717360
46
+ version_requirements: *70214687516960
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: ffi-rzmq
49
- requirement: &70168804716260 !ruby/object:Gem::Requirement
49
+ requirement: &70214687516180 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70168804716260
57
+ version_requirements: *70214687516180
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: rake
60
- requirement: &70168804715840 !ruby/object:Gem::Requirement
60
+ requirement: &70214687515760 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,18 +65,18 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70168804715840
68
+ version_requirements: *70214687515760
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rspec
71
- requirement: &70168804715220 !ruby/object:Gem::Requirement
71
+ requirement: &70214687551620 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
75
75
  - !ruby/object:Gem::Version
76
- version: 2.7.0
76
+ version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70168804715220
79
+ version_requirements: *70214687551620
80
80
  description: Celluloid bindings to the ffi-rzmq library
81
81
  email:
82
82
  - tony.arcieri@gmail.com
@@ -87,17 +87,17 @@ files:
87
87
  - celluloid-zmq.gemspec
88
88
  - CHANGES.md
89
89
  - Gemfile
90
- - lib/celluloid/zmq/mailbox.rb
91
90
  - lib/celluloid/zmq/reactor.rb
91
+ - lib/celluloid/zmq/sockets.rb
92
92
  - lib/celluloid/zmq/version.rb
93
93
  - lib/celluloid/zmq/waker.rb
94
94
  - lib/celluloid/zmq.rb
95
95
  - pkg/celluloid-zmq-0.0.4.gem
96
96
  - pkg/celluloid-zmq-0.7.0.gem
97
+ - pkg/celluloid-zmq-0.8.0.gem
97
98
  - Rakefile
98
99
  - README.md
99
100
  - spec/celluloid/zmq/actor_spec.rb
100
- - spec/celluloid/zmq/mailbox_spec.rb
101
101
  - spec/spec_helper.rb
102
102
  - .gitignore
103
103
  homepage: http://github.com/tarcieri/dcell
@@ -127,7 +127,6 @@ summary: Celluloid::ZMQ provides concurrent Celluloid actors that can listen for
127
127
  events
128
128
  test_files:
129
129
  - spec/celluloid/zmq/actor_spec.rb
130
- - spec/celluloid/zmq/mailbox_spec.rb
131
130
  - spec/spec_helper.rb
132
131
  - .gitignore
133
132
  has_rdoc:
@@ -1,13 +0,0 @@
1
- module Celluloid
2
- module ZMQ
3
- # A Celluloid mailbox for Actors that wait on 0MQ sockets
4
- class Mailbox < Celluloid::IO::Mailbox
5
- def initialize
6
- # More APIs and less monkeypatching would be useful here
7
- @messages = []
8
- @lock = Mutex.new
9
- @reactor = Reactor.new
10
- end
11
- end
12
- end
13
- end
@@ -1,6 +0,0 @@
1
- require 'spec_helper'
2
- require 'celluloid/rspec'
3
-
4
- describe Celluloid::ZMQ::Mailbox do
5
- it_behaves_like "a Celluloid Mailbox"
6
- end