rbuv-em 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 00732cd000bb89f894104171d87d8cdeb4bb78b4
4
+ data.tar.gz: 4c5fccce7e1a7f366a92a762956cc4f188f55f42
5
+ SHA512:
6
+ metadata.gz: 001f125181cce3636754c78acd0adbc1be281ff52e526fda6f404812d5ef87b75b64ed7523f4f14525e2301b54ae53d87176c16ca25565d5ae10645369686bb4
7
+ data.tar.gz: 085f11ac8d8375530e659eb03ba6efb3f53500fe8638f71d92bef51cdc16c55ef16fb6a7fba92eabd1211de4a3a6532c5121622f5efadd2b0b95e2bda9739e0b
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
19
+ .ruby-gemset
20
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rbuv-em.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Hanfei Shen
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # Rbuv::EM
2
+
3
+ Provide an EventMachine compatibile layer for rbuv.
4
+
5
+ ## What is Rbuv
6
+
7
+ Rbuv is a libuv binding for Ruby.
8
+
9
+ ## What is EventMachine
10
+
11
+ EventMachine is an event-driven I/O and lightweight concurrency library for Ruby.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'rbuv-em'
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install rbuv-em
26
+
27
+ ## Usage
28
+
29
+ TODO: Write usage instructions here
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
38
+
39
+ ## License
40
+
41
+ Rbuv is distributed under the MIT License. See LICENSE.txt for further details.
42
+ Copyright (c) 2013 Hanfei Shen
43
+
44
+ Contains code originally from 'EventMachine' available under the terms of either the GPL or Ruby's License.
45
+ Copyright: (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ lib = File.expand_path('../../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'rbuv/em'
4
+
5
+ EventMachine = EM = Rbuv::EM
6
+
7
+ class EchoServer < EM::Connection
8
+ def receive_data(data)
9
+ send_data data
10
+ end
11
+ end
12
+
13
+ class Client < EM::Connection
14
+ def connection_completed
15
+ send_data "Hello, world"
16
+ end
17
+
18
+ def receive_data(data)
19
+ close_connection
20
+ EM.stop
21
+ end
22
+ end
23
+
24
+ EM.run do
25
+ Rbuv::Signal.start(Rbuv::Signal::INT) { EM.stop }
26
+ EM.start_server '127.0.0.1', 60000, EchoServer
27
+ EM.connect '127.0.0.1', 60000, Client
28
+ end
data/lib/rbuv/em.rb ADDED
@@ -0,0 +1,237 @@
1
+ require "rbuv/em/version"
2
+ require "rbuv"
3
+ require "rbuv/em/connection"
4
+
5
+ ConnectionNotBound = Class.new(Rbuv::Error)
6
+
7
+ module Rbuv
8
+ module EM
9
+
10
+ @reactor_running = false
11
+ @tails = []
12
+
13
+ def self.run(blk=nil, tail=nil, &block)
14
+ tail && @tails.unshift(tail)
15
+
16
+ b = blk || block
17
+ if reactor_running?
18
+ b && b.call
19
+ else
20
+ @conns = {}
21
+ @acceptors = {}
22
+ @timers = {}
23
+ @tails ||= []
24
+ begin
25
+ @reactor_running = true
26
+ initialize_event_machine
27
+ b && add_timer(0, b)
28
+ run_machine
29
+ ensure
30
+ until @tails.empty?
31
+ @tails.pop.call
32
+ end
33
+
34
+ release_machine
35
+
36
+ @reactor_running = false
37
+ end
38
+ end
39
+ end
40
+
41
+ def self.stop
42
+ Rbuv.stop
43
+ end
44
+
45
+ def self.stop_event_loop
46
+ self.stop
47
+ end
48
+
49
+ def self.run_block
50
+ run do
51
+ yield
52
+ Rbuv.stop
53
+ end
54
+ end
55
+
56
+ def self.reactor_running?
57
+ @reactor_running || false
58
+ end
59
+
60
+ def self.add_timer(interval, blk=nil, &block)
61
+ if blk ||= block
62
+ s = add_oneshot_timer((interval.to_f * 1000).to_i, &blk)
63
+ @timers[s] = blk
64
+ s
65
+ end
66
+ end
67
+
68
+ def self.add_periodic_timer(interval, blk=nil, &block)
69
+ blk ||= block
70
+ EventMachine::PeriodicTimer.new(interval, blk)
71
+ end
72
+
73
+ def self.cancel_timer(timer_or_sig)
74
+ if timer_or_sig.respond_to? :cancel
75
+ timer_or_sig.cancel
76
+ else
77
+ @timers[timer_or_sig] = false if @timers.has_key?(timer_or_sig)
78
+ end
79
+ end
80
+
81
+ def self.start_server(server, port=nil, handler=nil, *args, &block)
82
+ begin
83
+ port = Integer(port)
84
+ rescue ArgumentError, TypeError
85
+ # there was no port, so server must be a unix domain socket
86
+ # the port argument is actually the handler, and the handler is one of the args
87
+ args.unshift handler if handler
88
+ handler = port
89
+ port = nil
90
+ end if port
91
+
92
+ klass = klass_from_handler(Connection, handler, *args)
93
+
94
+ s = if port
95
+ start_tcp_server server, port
96
+ else
97
+ start_unix_server server
98
+ end
99
+ @acceptors[s] = [klass, args, block]
100
+ s
101
+ end
102
+
103
+ def self.connect(server, port=nil, handler=nil, *args, &blk)
104
+ bind_connect nil, nil, server, port, handler, *args, &blk
105
+ end
106
+
107
+ def self.bind_connect bind_addr, bind_port, server, port=nil, handler=nil, *args
108
+ begin
109
+ port = Integer(port)
110
+ rescue ArgumentError, TypeError
111
+ # there was no port, so server must be a unix domain socket
112
+ # the port argument is actually the handler, and the handler is one of the args
113
+ args.unshift handler if handler
114
+ handler = port
115
+ port = nil
116
+ end if port
117
+
118
+ klass = klass_from_handler(Connection, handler, *args)
119
+
120
+ s = if port
121
+ if bind_addr
122
+ bind_connect_server bind_addr, bind_port.to_i, server, port
123
+ else
124
+ connect_server server, port
125
+ end
126
+ else
127
+ connect_unix_server server
128
+ end
129
+
130
+ c = klass.new s, *args
131
+ @conns[s] = c
132
+ block_given? and yield c
133
+ c
134
+ end
135
+
136
+ def self.send_data(tcp, data, _size)
137
+ p [tcp, data, _size]
138
+ tcp.write(data)
139
+ end
140
+
141
+ def self.close_connection(tcp, _after_writing)
142
+ tcp.close
143
+ end
144
+
145
+ private
146
+ def self.initialize_event_machine
147
+ end
148
+
149
+ def self.run_machine
150
+ Rbuv::Loop.run
151
+ end
152
+
153
+ def self.release_machine
154
+ end
155
+
156
+ def self.add_oneshot_timer(interval)
157
+ Rbuv::Timer.start(interval, 0) { yield }
158
+ end
159
+
160
+ def self.klass_from_handler(klass = Connection, handler = nil, *args)
161
+ klass = if handler && handler.is_a?(Class)
162
+ raise ArgumentError, "must provide module or subclass of #{klass.name}" unless klass >= handler
163
+ handler
164
+ elsif handler
165
+ begin
166
+ handler::EM_CONNECTION_CLASS
167
+ rescue NameError
168
+ handler::const_set(:EM_CONNECTION_CLASS, Class.new(klass) { include handler })
169
+ end
170
+ else
171
+ klass
172
+ end
173
+
174
+ arity = klass.instance_method(:initialize).arity
175
+ expected = arity >= 0 ? arity : -(arity + 1)
176
+ if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
177
+ raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
178
+ end
179
+
180
+ klass
181
+ end
182
+
183
+ def self.start_tcp_server(server, port)
184
+ s = Rbuv::Tcp.new
185
+ s.bind server, port
186
+ s.listen 128 do
187
+ self.on_accept(s)
188
+ end
189
+ s
190
+ end
191
+
192
+ def self.start_unix_server(server)
193
+ end
194
+
195
+ def self.on_accept(s)
196
+ klass,args,blk = @acceptors[s]
197
+ c_tcp = Rbuv::Tcp.new
198
+ s.accept(c_tcp)
199
+ c = klass.new(c_tcp, *args)
200
+ c_tcp.read_start do |data, error|
201
+ if error.is_a?(EOFError)
202
+ c.unbind
203
+ else
204
+ c.receive_data(data)
205
+ end
206
+ end
207
+ @conns[c_tcp] = c
208
+ blk && blk.call(c)
209
+ end
210
+
211
+ def self.connect_server(server, port)
212
+ self.bind_connect_server nil, nil, server, port
213
+ end
214
+
215
+ def self.bind_connect_server(bind_addr, bind_port, server, port)
216
+ c = Rbuv::Tcp.new
217
+ c.bind bind_addr, bind_port if bind_addr
218
+ c.connect(server, port) do
219
+ self.on_connect(c)
220
+ end
221
+ c
222
+ end
223
+
224
+ def self.on_connect(c_tcp)
225
+ c = @conns[c_tcp] or raise ConnectionNotBound, "received ConnectionCompleted for unknown signature: #{c_tcp}"
226
+ c.connection_completed
227
+ c_tcp.read_start do |data, error|
228
+ if error.is_a?(EOFError)
229
+ c.unbind
230
+ else
231
+ c.receive_data(data)
232
+ end
233
+ end
234
+ end
235
+
236
+ end
237
+ end
@@ -0,0 +1,46 @@
1
+ module Rbuv
2
+ module EM
3
+ class Connection
4
+
5
+ def self.new(sig, *args)
6
+ allocate.instance_eval do
7
+ @signature = sig
8
+
9
+ initialize(*args)
10
+
11
+ post_init
12
+
13
+ self
14
+ end
15
+ end
16
+
17
+ def initialize(*args)
18
+ end
19
+
20
+ def post_init
21
+ end
22
+
23
+ def receive_data(data)
24
+ puts "............>>>#{data.length}"
25
+ end
26
+
27
+ def unbind
28
+ end
29
+
30
+ def send_data(data)
31
+ data = data.to_s
32
+ size = data.bytesize if data.respond_to?(:bytesize)
33
+ size ||= data.size
34
+ EM.send_data @signature, data, size
35
+ end
36
+
37
+ def close_connection(after_writing=false)
38
+ EM.close_connection @signature, after_writing
39
+ end
40
+
41
+ def connection_completed
42
+ end
43
+
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,5 @@
1
+ module Rbuv
2
+ module EM
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
data/rbuv-em.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rbuv/em/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rbuv-em"
8
+ spec.version = Rbuv::EM::VERSION
9
+ spec.authors = ["Hanfei Shen"]
10
+ spec.email = ["qqshfox@gmail.com"]
11
+ spec.description = %q{EventMachine compatibility for rbuv}
12
+ spec.summary = %q{EventMachine compatibility for rbuv}
13
+ spec.homepage = "https://github.com/rbuv/rbuv-em"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "rbuv", ">= 0.0.5"
22
+
23
+ spec.add_development_dependency "bundler"
24
+ spec.add_development_dependency "rake"
25
+ end
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbuv-em
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Hanfei Shen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rbuv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: 0.0.5
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: 0.0.5
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: EventMachine compatibility for rbuv
56
+ email:
57
+ - qqshfox@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - examples/echo_server.rb
68
+ - lib/rbuv/em.rb
69
+ - lib/rbuv/em/connection.rb
70
+ - lib/rbuv/em/version.rb
71
+ - rbuv-em.gemspec
72
+ homepage: https://github.com/rbuv/rbuv-em
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.0.3
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: EventMachine compatibility for rbuv
96
+ test_files: []