celluloid-zmq 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +6 -0
- data/README.md +56 -0
- data/Rakefile +7 -0
- data/celluloid-zmq.gemspec +29 -0
- data/lib/celluloid/zmq.rb +27 -0
- data/lib/celluloid/zmq/mailbox.rb +13 -0
- data/lib/celluloid/zmq/reactor.rb +74 -0
- data/lib/celluloid/zmq/version.rb +5 -0
- data/spec/celluloid/zmq/actor_spec.rb +6 -0
- data/spec/celluloid/zmq/mailbox_spec.rb +6 -0
- data/spec/spec_helper.rb +2 -0
- metadata +140 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
Celluloid::ZMQ
|
2
|
+
==============
|
3
|
+
|
4
|
+
This gem uses the ffi-rzmq library to provide Celluloid actors that can
|
5
|
+
interact with 0MQ sockets.
|
6
|
+
|
7
|
+
Celluloid::ZMQ provides two methods for multiplexing 0MQ sockets with
|
8
|
+
receiving messages over Celluloid's actor protocol:
|
9
|
+
|
10
|
+
* Celluloid::ZMQ#wait_readable(socket): wait until a message is available to
|
11
|
+
read from the given 0MQ socket
|
12
|
+
* Celluloid::ZMQ#wait_writeable(socket): waits until there's space in the
|
13
|
+
given socket's message buffer to send another message
|
14
|
+
|
15
|
+
Example Usage:
|
16
|
+
|
17
|
+
require 'celluloid-zmq'
|
18
|
+
|
19
|
+
ZMQ_CONTEXT = ZMQ::Context.new(1)
|
20
|
+
|
21
|
+
class MyZmqCell
|
22
|
+
include Celluloid::ZMQ
|
23
|
+
|
24
|
+
def initialize(addr)
|
25
|
+
@socket = ZMQ_CONTEXT.socket(ZMQ::PUSH)
|
26
|
+
|
27
|
+
unless ZMQ::Util.resultcode_ok? @socket.connect addr
|
28
|
+
@socket.close
|
29
|
+
raise "error connecting to #{addr}: #{ZMQ::Util.error_string}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def write(message)
|
34
|
+
wait_writeable @socket
|
35
|
+
unless ZMQ::Util.resultcode_ok? @socket.send_string message
|
36
|
+
raise "error sending 0MQ message: #{ZMQ::Util.error_string}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def read
|
41
|
+
wait_readable @socket
|
42
|
+
message = ''
|
43
|
+
|
44
|
+
rc = @socket.recv_string message
|
45
|
+
if ZMQ::Util.resultcode_ok? rc
|
46
|
+
handle_message message
|
47
|
+
else
|
48
|
+
raise "error receiving ZMQ string: #{ZMQ::Util.error_string}"
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
Copyright
|
54
|
+
---------
|
55
|
+
|
56
|
+
Copyright (c) 2011 Tony Arcieri. See LICENSE.txt for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/celluloid/zmq/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Tony Arcieri"]
|
6
|
+
gem.email = ["tony.arcieri@gmail.com"]
|
7
|
+
gem.description = "Celluloid bindings to the ffi-rzmq library"
|
8
|
+
gem.summary = "Celluloid::ZMQ provides concurrent Celluloid actors that can listen for 0MQ events"
|
9
|
+
gem.homepage = "http://github.com/tarcieri/dcell"
|
10
|
+
|
11
|
+
gem.name = "celluloid-zmq"
|
12
|
+
gem.version = Celluloid::ZMQ::VERSION
|
13
|
+
|
14
|
+
gem.add_dependency "celluloid", ">= 0.6.2"
|
15
|
+
gem.add_dependency "ffi"
|
16
|
+
gem.add_dependency "ffi-rzmq"
|
17
|
+
gem.add_dependency "redis"
|
18
|
+
gem.add_dependency "redis-namespace"
|
19
|
+
|
20
|
+
gem.add_development_dependency "rake"
|
21
|
+
gem.add_development_dependency "rspec", ">= 2.7.0"
|
22
|
+
|
23
|
+
# Files
|
24
|
+
ignores = File.read(".gitignore").split(/\r?\n/).reject{ |f| f =~ /^(#.+|\s*)$/ }.map {|f| Dir[f] }.flatten
|
25
|
+
gem.files = (Dir['**/*','.gitignore'] - ignores).reject {|f| !File.file?(f) }
|
26
|
+
gem.test_files = (Dir['spec/**/*','.gitignore'] - ignores).reject {|f| !File.file?(f) }
|
27
|
+
# gem.executables = Dir['bin/*'].map { |f| File.basename(f) }
|
28
|
+
gem.require_paths = ['lib']
|
29
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'ffi-rzmq'
|
2
|
+
|
3
|
+
require 'celluloid'
|
4
|
+
require 'celluloid/zmq/mailbox'
|
5
|
+
require 'celluloid/zmq/reactor'
|
6
|
+
require 'celluloid/zmq/version'
|
7
|
+
|
8
|
+
module Celluloid
|
9
|
+
# Actors which run alongside 0MQ sockets
|
10
|
+
module ZMQ
|
11
|
+
def self.included(klass)
|
12
|
+
klass.send :include, ::Celluloid
|
13
|
+
klass.use_mailbox Celluloid::ZMQ::Mailbox
|
14
|
+
end
|
15
|
+
|
16
|
+
# Wait for the given IO object to become readable
|
17
|
+
def wait_readable(socket)
|
18
|
+
# Law of demeter be damned!
|
19
|
+
current_actor.mailbox.reactor.wait_readable(socket)
|
20
|
+
end
|
21
|
+
|
22
|
+
# Wait for the given IO object to become writeable
|
23
|
+
def wait_writeable(socket)
|
24
|
+
current_actor.mailbox.reactor.wait_writeable(socket)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,13 @@
|
|
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
|
+
@messages = []
|
7
|
+
@lock = Mutex.new
|
8
|
+
@waker = Celluloid::IO::Waker.new
|
9
|
+
@reactor = Reactor.new(@waker)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module Celluloid
|
2
|
+
module ZMQ
|
3
|
+
# React to incoming 0MQ and Celluloid events. This is kinda sorta supposed
|
4
|
+
# to resemble the Reactor design pattern.
|
5
|
+
class Reactor
|
6
|
+
def initialize(waker)
|
7
|
+
@waker = waker
|
8
|
+
@poller = ::ZMQ::Poller.new
|
9
|
+
@readers = {}
|
10
|
+
@writers = {}
|
11
|
+
|
12
|
+
# FIXME: The way things are presently implemented is super ghetto
|
13
|
+
# The ZMQ::Poller should be able to wait on the waker somehow
|
14
|
+
# but I can't get it to work :(
|
15
|
+
#result = @poller.register(nil, ::ZMQ::POLLIN, @waker.io.fileno)
|
16
|
+
#
|
17
|
+
#unless ::ZMQ::Util.resultcode_ok?(result)
|
18
|
+
# raise "couldn't register waker with 0MQ poller"
|
19
|
+
#end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Wait for the given ZMQ socket to become readable
|
23
|
+
def wait_readable(socket)
|
24
|
+
monitor_zmq socket, @readers, ::ZMQ::POLLIN
|
25
|
+
end
|
26
|
+
|
27
|
+
# Wait for the given ZMQ socket to become writeable
|
28
|
+
def wait_writeable(socket)
|
29
|
+
monitor_zmq socket, @writers, ::ZMQ::POLLOUT
|
30
|
+
end
|
31
|
+
|
32
|
+
# Monitor the given ZMQ socket with the given options
|
33
|
+
def monitor_zmq(socket, set, type)
|
34
|
+
if set.has_key? socket
|
35
|
+
raise ArgumentError, "another method is already waiting on #{socket.inspect}"
|
36
|
+
else
|
37
|
+
set[socket] = Fiber.current
|
38
|
+
end
|
39
|
+
|
40
|
+
@poller.register socket, type
|
41
|
+
Fiber.yield
|
42
|
+
|
43
|
+
@poller.deregister socket, type
|
44
|
+
socket
|
45
|
+
end
|
46
|
+
|
47
|
+
# Run the reactor, waiting for events, and calling the given block if
|
48
|
+
# the reactor is awoken by the waker
|
49
|
+
def run_once(timeout = nil)
|
50
|
+
# FIXME: This approach is super ghetto. Find some way to make the
|
51
|
+
# ZMQ::Poller wait on the waker's file descriptor
|
52
|
+
if @poller.size == 0
|
53
|
+
readable, _ = select [@waker.io], [], [], timeout
|
54
|
+
yield if readable and readable.include? @waker.io
|
55
|
+
else
|
56
|
+
if ::ZMQ::Util.resultcode_ok? @poller.poll(100)
|
57
|
+
@poller.readables.each do |sock|
|
58
|
+
fiber = @readers.delete sock
|
59
|
+
fiber.resume if fiber
|
60
|
+
end
|
61
|
+
|
62
|
+
@poller.writables.each do |sock|
|
63
|
+
fiber = @writers.delete sock
|
64
|
+
fiber.resume if fiber
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
readable, _ = select [@waker.io], [], [], 0
|
69
|
+
yield if readable and readable.include? @waker.io
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: celluloid-zmq
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tony Arcieri
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: celluloid
|
16
|
+
requirement: &70334545596400 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.6.2
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70334545596400
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: ffi
|
27
|
+
requirement: &70334545595980 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70334545595980
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: ffi-rzmq
|
38
|
+
requirement: &70334545595500 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70334545595500
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: redis
|
49
|
+
requirement: &70334545595000 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70334545595000
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: redis-namespace
|
60
|
+
requirement: &70334545594480 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :runtime
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70334545594480
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: &70334545593840 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70334545593840
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: rspec
|
82
|
+
requirement: &70334545592940 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 2.7.0
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *70334545592940
|
91
|
+
description: Celluloid bindings to the ffi-rzmq library
|
92
|
+
email:
|
93
|
+
- tony.arcieri@gmail.com
|
94
|
+
executables: []
|
95
|
+
extensions: []
|
96
|
+
extra_rdoc_files: []
|
97
|
+
files:
|
98
|
+
- celluloid-zmq.gemspec
|
99
|
+
- Gemfile
|
100
|
+
- lib/celluloid/zmq/mailbox.rb
|
101
|
+
- lib/celluloid/zmq/reactor.rb
|
102
|
+
- lib/celluloid/zmq/version.rb
|
103
|
+
- lib/celluloid/zmq.rb
|
104
|
+
- Rakefile
|
105
|
+
- README.md
|
106
|
+
- spec/celluloid/zmq/actor_spec.rb
|
107
|
+
- spec/celluloid/zmq/mailbox_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
109
|
+
- .gitignore
|
110
|
+
homepage: http://github.com/tarcieri/dcell
|
111
|
+
licenses: []
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
117
|
+
none: false
|
118
|
+
requirements:
|
119
|
+
- - ! '>='
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: '0'
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ! '>='
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 1.8.10
|
131
|
+
signing_key:
|
132
|
+
specification_version: 3
|
133
|
+
summary: Celluloid::ZMQ provides concurrent Celluloid actors that can listen for 0MQ
|
134
|
+
events
|
135
|
+
test_files:
|
136
|
+
- spec/celluloid/zmq/actor_spec.rb
|
137
|
+
- spec/celluloid/zmq/mailbox_spec.rb
|
138
|
+
- spec/spec_helper.rb
|
139
|
+
- .gitignore
|
140
|
+
has_rdoc:
|