em-zmq 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source :rubygems
2
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,18 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ em-zmq (0.0.1)
5
+ eventmachine
6
+ ffi-rzmq (>= 0.7.2)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ eventmachine (0.12.10)
12
+ ffi-rzmq (0.8.0)
13
+
14
+ PLATFORMS
15
+ ruby
16
+
17
+ DEPENDENCIES
18
+ em-zmq!
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
data/em-zmq.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
2
+ require 'eventmachine/zmq/version'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'em-zmq'
6
+ s.version = EventMachine::ZMQ::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.author = 'Chris Lloyd'
9
+ s.email = 'christopher.lloyd@gmail.com'
10
+ s.homepage = 'https://github.com/chrislloyd/em-zmq'
11
+
12
+ s.summary = 'ZMQ bindings for EventMachine.'
13
+ s.description = s.summary
14
+
15
+ s.add_dependency 'eventmachine'
16
+ s.add_dependency 'ffi-rzmq', '>= 0.7.2'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
21
+ s.require_paths = ['lib']
22
+ end
data/example/simple.rb ADDED
@@ -0,0 +1,26 @@
1
+ $LOAD_PATH << File.expand_path('../../lib', __FILE__)
2
+ require 'eventmachine'
3
+ require 'eventmachine/zmq'
4
+
5
+ module Handler
6
+ def post_init
7
+ @i = 0
8
+ end
9
+
10
+ def receive_data(data)
11
+ puts "Recieved message ##{@i}: #{data}"
12
+ @i += 1
13
+ end
14
+ end
15
+
16
+ EM.run do
17
+ ctx = EM::ZMQ::Context.new
18
+
19
+ push = ctx.bind :push, 'inproc://test'
20
+ pull = ctx.connect :pull, 'inproc://test', Handler
21
+
22
+ EM::PeriodicTimer.new(1) do
23
+ push.send_data 'Hello World'
24
+ end
25
+
26
+ end
@@ -0,0 +1,71 @@
1
+ module EventMachine
2
+ module ZMQ
3
+ class Connection < EventMachine::Connection
4
+ attr_reader :socket
5
+
6
+ def bind addr
7
+ socket.bind addr
8
+ end
9
+
10
+ def connect addr
11
+ socket.bind addr
12
+ end
13
+
14
+ def subscribe channel
15
+ socket.setsockopt ZMQ::SUBSCRIBE, channel
16
+ end
17
+
18
+ def unsubscribe channel
19
+ socket.setsockopt ZMQ::UNSUBSCRIBE, channel
20
+ end
21
+
22
+ def send_data(data, more=false)
23
+ sndmore = more ? ::ZMQ::SNDMORE : 0
24
+
25
+ success = socket.send_string(data.to_s, ::ZMQ::NOBLOCK | sndmore)
26
+ self.notify_writable = true unless success
27
+ success
28
+ end
29
+
30
+ def readable?
31
+ (socket.getsockopt(::ZMQ::EVENTS) & ::ZMQ::POLLIN) == ::ZMQ::POLLIN
32
+ end
33
+
34
+ def notify_readable
35
+ return unless readable?
36
+
37
+ loop do
38
+ if msg = get_message
39
+ receive_data msg.copy_out_string
40
+ while socket.more_parts?
41
+ if msg = get_message
42
+ receive_data msg.copy_out_string
43
+ else
44
+ break
45
+ end
46
+ end
47
+ else
48
+ break
49
+ end
50
+ end
51
+ end
52
+
53
+ def notify_writable
54
+ self.notify_writable = false
55
+ end
56
+
57
+ private
58
+
59
+ def get_message
60
+ msg = ::ZMQ::Message.new
61
+ socket.recv(msg, ::ZMQ::NOBLOCK) ? msg : nil
62
+ end
63
+
64
+ def detach
65
+ super
66
+ socket.close
67
+ end
68
+
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,47 @@
1
+ module EventMachine
2
+ module ZMQ
3
+ class Context < ::ZMQ::Context
4
+
5
+ READ = [:sub, :req, :rep, :xreq, :xrep, :pull]
6
+ WRITE = [:pub, :req, :rep, :xreq, :xrep, :push]
7
+
8
+ def bind type, address, handler=nil, *args, &blk
9
+ sock = socket type_const_from_args(type)
10
+ [*address].each {|addr| sock.bind addr}
11
+
12
+ create(sock, handler, *args, &blk)
13
+ end
14
+
15
+ def connect type, address, handler=nil, *args, &blk
16
+ sock = socket type_const_from_args(type)
17
+ [*address].each {|addr| sock.connect addr}
18
+
19
+ create(sock, handler, *args, &blk)
20
+ end
21
+
22
+ private
23
+
24
+ def create sock, handler=nil, *args, &blk
25
+ klass = EM.send(:klass_from_handler, Connection, handler, *args)
26
+
27
+ EM.watch(sock.getsockopt(::ZMQ::FD), klass, *args, &blk).tap {|conn|
28
+ conn.instance_variable_set(:@socket, sock)
29
+
30
+ if READ.include? sock.name.downcase.to_sym
31
+ cnn.notify_readable if conn.readable?
32
+ conn.notify_readable = true
33
+ end
34
+
35
+ if WRITE.include? sock.name.downcase.to_sym
36
+ conn.notify_writable = true
37
+ end
38
+ }
39
+ end
40
+
41
+ def type_const_from_args arg
42
+ arg.is_a?(Symbol) ? ::ZMQ.const_get(arg.to_s.upcase) : arg
43
+ end
44
+
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,5 @@
1
+ module EventMachine
2
+ module ZMQ
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
@@ -0,0 +1,6 @@
1
+ require 'eventmachine'
2
+ require 'ffi-rzmq'
3
+
4
+ require 'eventmachine/zmq/connection'
5
+ require 'eventmachine/zmq/context'
6
+ require 'eventmachine/zmq/version'
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: em-zmq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Lloyd
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-05-19 00:00:00.000000000 +10:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: eventmachine
17
+ requirement: &2161613800 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *2161613800
26
+ - !ruby/object:Gem::Dependency
27
+ name: ffi-rzmq
28
+ requirement: &2161613300 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: *2161613300
37
+ description: ZMQ bindings for EventMachine.
38
+ email: christopher.lloyd@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - Gemfile.lock
46
+ - Rakefile
47
+ - em-zmq.gemspec
48
+ - example/simple.rb
49
+ - lib/eventmachine/zmq.rb
50
+ - lib/eventmachine/zmq/connection.rb
51
+ - lib/eventmachine/zmq/context.rb
52
+ - lib/eventmachine/zmq/version.rb
53
+ has_rdoc: true
54
+ homepage: https://github.com/chrislloyd/em-zmq
55
+ licenses: []
56
+ post_install_message:
57
+ rdoc_options: []
58
+ require_paths:
59
+ - lib
60
+ required_ruby_version: !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ! '>='
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ none: false
68
+ requirements:
69
+ - - ! '>='
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubyforge_project:
74
+ rubygems_version: 1.6.2
75
+ signing_key:
76
+ specification_version: 3
77
+ summary: ZMQ bindings for EventMachine.
78
+ test_files: []