eventmachine-maglev- 0.12.10 → 1.0.0.beta.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (174) hide show
  1. data/.gitignore +7 -0
  2. data/.yardopts +7 -0
  3. data/Gemfile +3 -0
  4. data/README.md +109 -0
  5. data/Rakefile +14 -368
  6. data/docs/DocumentationGuidesIndex.md +27 -0
  7. data/docs/GettingStarted.md +521 -0
  8. data/docs/old/DEFERRABLES +246 -0
  9. data/docs/{KEYBOARD → old/KEYBOARD} +15 -11
  10. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  11. data/docs/old/SMTP +4 -0
  12. data/docs/old/SPAWNED_PROCESSES +148 -0
  13. data/eventmachine.gemspec +20 -26
  14. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  15. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  16. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  17. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  18. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  19. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  20. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  21. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  22. data/examples/{ex_channel.rb → old/ex_channel.rb} +3 -3
  23. data/examples/old/ex_tick_loop_array.rb +15 -0
  24. data/examples/old/ex_tick_loop_counter.rb +32 -0
  25. data/ext/binder.cpp +0 -1
  26. data/ext/cmain.cpp +40 -29
  27. data/ext/ed.cpp +189 -134
  28. data/ext/ed.h +34 -40
  29. data/ext/em.cpp +388 -340
  30. data/ext/em.h +29 -32
  31. data/ext/eventmachine.h +7 -6
  32. data/ext/extconf.rb +57 -48
  33. data/ext/fastfilereader/extconf.rb +5 -3
  34. data/ext/fastfilereader/mapper.cpp +1 -1
  35. data/ext/fastfilereader/rubymain.cpp +0 -1
  36. data/ext/kb.cpp +1 -3
  37. data/ext/pipe.cpp +9 -11
  38. data/ext/project.h +12 -8
  39. data/ext/rubymain.cpp +158 -112
  40. data/java/src/com/rubyeventmachine/EmReactor.java +3 -2
  41. data/lib/em/buftok.rb +35 -63
  42. data/lib/em/callback.rb +43 -11
  43. data/lib/em/channel.rb +22 -15
  44. data/lib/em/completion.rb +303 -0
  45. data/lib/em/connection.rb +341 -208
  46. data/lib/em/deferrable/pool.rb +2 -0
  47. data/lib/em/deferrable.rb +20 -2
  48. data/lib/em/file_watch.rb +37 -18
  49. data/lib/em/iterator.rb +270 -0
  50. data/lib/em/pool.rb +146 -0
  51. data/lib/em/process_watch.rb +5 -4
  52. data/lib/em/processes.rb +8 -4
  53. data/lib/em/protocols/httpclient.rb +27 -11
  54. data/lib/em/protocols/httpclient2.rb +15 -5
  55. data/lib/em/protocols/line_protocol.rb +29 -0
  56. data/lib/em/protocols/memcache.rb +17 -9
  57. data/lib/em/protocols/object_protocol.rb +2 -1
  58. data/lib/em/protocols/postgres3.rb +2 -1
  59. data/lib/em/protocols/smtpclient.rb +19 -11
  60. data/lib/em/protocols/smtpserver.rb +101 -8
  61. data/lib/em/protocols/stomp.rb +9 -7
  62. data/lib/em/protocols/tcptest.rb +3 -2
  63. data/lib/em/protocols.rb +1 -1
  64. data/lib/{pr_eventmachine.rb → em/pure_ruby.rb} +188 -205
  65. data/lib/em/queue.rb +23 -13
  66. data/lib/em/resolver.rb +192 -0
  67. data/lib/em/spawnable.rb +9 -10
  68. data/lib/em/streamer.rb +34 -46
  69. data/lib/em/threaded_resource.rb +90 -0
  70. data/lib/em/tick_loop.rb +85 -0
  71. data/lib/em/timers.rb +8 -3
  72. data/lib/em/version.rb +1 -1
  73. data/lib/eventmachine.rb +582 -686
  74. data/lib/jeventmachine.rb +25 -3
  75. data/tasks/package.rake +98 -0
  76. data/tasks/test.rake +8 -0
  77. data/tests/em_test_helper.rb +64 -0
  78. data/tests/test_attach.rb +56 -56
  79. data/tests/test_basic.rb +111 -168
  80. data/tests/test_channel.rb +5 -6
  81. data/tests/test_completion.rb +177 -0
  82. data/tests/test_connection_count.rb +1 -3
  83. data/tests/test_defer.rb +3 -32
  84. data/tests/test_deferrable.rb +35 -0
  85. data/tests/test_epoll.rb +27 -57
  86. data/tests/test_error_handler.rb +10 -7
  87. data/tests/test_exc.rb +6 -33
  88. data/tests/test_file_watch.rb +51 -35
  89. data/tests/test_futures.rb +10 -38
  90. data/tests/test_get_sock_opt.rb +27 -20
  91. data/tests/test_handler_check.rb +1 -3
  92. data/tests/test_hc.rb +49 -112
  93. data/tests/test_httpclient.rb +34 -62
  94. data/tests/test_httpclient2.rb +14 -39
  95. data/tests/test_inactivity_timeout.rb +44 -40
  96. data/tests/test_kb.rb +26 -52
  97. data/tests/test_ltp.rb +27 -71
  98. data/tests/test_ltp2.rb +1 -30
  99. data/tests/test_next_tick.rb +2 -31
  100. data/tests/test_object_protocol.rb +8 -9
  101. data/tests/test_pause.rb +45 -37
  102. data/tests/test_pending_connect_timeout.rb +42 -38
  103. data/tests/test_pool.rb +128 -0
  104. data/tests/test_process_watch.rb +37 -37
  105. data/tests/test_processes.rb +92 -110
  106. data/tests/test_proxy_connection.rb +137 -61
  107. data/tests/test_pure.rb +30 -67
  108. data/tests/test_queue.rb +10 -4
  109. data/tests/test_resolver.rb +55 -0
  110. data/tests/test_running.rb +1 -29
  111. data/tests/test_sasl.rb +8 -33
  112. data/tests/test_send_file.rb +163 -188
  113. data/tests/test_servers.rb +12 -55
  114. data/tests/test_shutdown_hooks.rb +23 -0
  115. data/tests/test_smtpclient.rb +1 -29
  116. data/tests/test_smtpserver.rb +1 -29
  117. data/tests/test_spawn.rb +2 -31
  118. data/tests/test_ssl_args.rb +9 -10
  119. data/tests/test_ssl_methods.rb +1 -3
  120. data/tests/test_ssl_verify.rb +63 -63
  121. data/tests/test_threaded_resource.rb +53 -0
  122. data/tests/test_tick_loop.rb +59 -0
  123. data/tests/test_timers.rb +52 -91
  124. data/tests/test_ud.rb +1 -29
  125. data/tests/test_unbind_reason.rb +31 -0
  126. metadata +113 -70
  127. data/README +0 -82
  128. data/docs/DEFERRABLES +0 -133
  129. data/docs/LIGHTWEIGHT_CONCURRENCY +0 -70
  130. data/docs/SMTP +0 -2
  131. data/docs/SPAWNED_PROCESSES +0 -89
  132. data/ext/cplusplus.cpp +0 -202
  133. data/ext/emwin.cpp +0 -300
  134. data/ext/emwin.h +0 -94
  135. data/ext/epoll.cpp +0 -26
  136. data/ext/epoll.h +0 -25
  137. data/ext/eventmachine_cpp.h +0 -96
  138. data/ext/files.cpp +0 -94
  139. data/ext/files.h +0 -65
  140. data/ext/sigs.cpp +0 -89
  141. data/ext/sigs.h +0 -32
  142. data/java/src/com/rubyeventmachine/application/Application.java +0 -194
  143. data/java/src/com/rubyeventmachine/application/Connection.java +0 -74
  144. data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +0 -37
  145. data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +0 -46
  146. data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +0 -38
  147. data/java/src/com/rubyeventmachine/application/Timer.java +0 -54
  148. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +0 -109
  149. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +0 -148
  150. data/java/src/com/rubyeventmachine/tests/EMTest.java +0 -80
  151. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +0 -53
  152. data/java/src/com/rubyeventmachine/tests/TestServers.java +0 -75
  153. data/java/src/com/rubyeventmachine/tests/TestTimers.java +0 -90
  154. data/lib/evma/callback.rb +0 -32
  155. data/lib/evma/container.rb +0 -75
  156. data/lib/evma/factory.rb +0 -77
  157. data/lib/evma/protocol.rb +0 -87
  158. data/lib/evma/reactor.rb +0 -48
  159. data/lib/evma.rb +0 -32
  160. data/setup.rb +0 -1585
  161. data/tests/test_errors.rb +0 -82
  162. data/tests/testem.rb +0 -31
  163. data/web/whatis +0 -7
  164. /data/{docs/GNU → GNU} +0 -0
  165. /data/{docs/COPYING → LICENSE} +0 -0
  166. /data/docs/{ChangeLog → old/ChangeLog} +0 -0
  167. /data/docs/{EPOLL → old/EPOLL} +0 -0
  168. /data/docs/{INSTALL → old/INSTALL} +0 -0
  169. /data/docs/{LEGAL → old/LEGAL} +0 -0
  170. /data/docs/{PURE_RUBY → old/PURE_RUBY} +0 -0
  171. /data/docs/{RELEASE_NOTES → old/RELEASE_NOTES} +0 -0
  172. /data/docs/{TODO → old/TODO} +0 -0
  173. /data/examples/{ex_queue.rb → old/ex_queue.rb} +0 -0
  174. /data/examples/{helper.rb → old/helper.rb} +0 -0
data/lib/em/queue.rb CHANGED
@@ -1,29 +1,31 @@
1
1
  module EventMachine
2
2
  # A cross thread, reactor scheduled, linear queue.
3
3
  #
4
- # This class provides a simple "Queue" like abstraction on top of the reactor
4
+ # This class provides a simple queue abstraction on top of the reactor
5
5
  # scheduler. It services two primary purposes:
6
+ #
6
7
  # * API sugar for stateful protocols
7
- # * Pushing processing onto the same thread as the reactor
8
+ # * Pushing processing onto the reactor thread
8
9
  #
9
- # See examples/ex_queue.rb for a detailed example.
10
+ # @example
10
11
  #
11
12
  # q = EM::Queue.new
12
13
  # q.push('one', 'two', 'three')
13
14
  # 3.times do
14
- # q.pop{ |msg| puts(msg) }
15
+ # q.pop { |msg| puts(msg) }
15
16
  # end
16
17
  #
17
18
  class Queue
18
- # Create a new queue
19
19
  def initialize
20
20
  @items = []
21
21
  @popq = []
22
22
  end
23
23
 
24
24
  # Pop items off the queue, running the block on the reactor thread. The pop
25
- # will not happen immediately, but at some point in the future, either in
25
+ # will not happen immediately, but at some point in the future, either in
26
26
  # the next tick, if the queue has data, or when the queue is populated.
27
+ #
28
+ # @return [NilClass] nil
27
29
  def pop(*a, &b)
28
30
  cb = EM::Callback(*a, &b)
29
31
  EM.schedule do
@@ -37,7 +39,7 @@ module EventMachine
37
39
  end
38
40
 
39
41
  # Push items onto the queue in the reactor thread. The items will not appear
40
- # in the queue immediately, but will be scheduled for addition during the
42
+ # in the queue immediately, but will be scheduled for addition during the
41
43
  # next reactor tick.
42
44
  def push(*items)
43
45
  EM.schedule do
@@ -45,17 +47,25 @@ module EventMachine
45
47
  @popq.shift.call @items.shift until @items.empty? || @popq.empty?
46
48
  end
47
49
  end
50
+ alias :<< :push
48
51
 
49
- # N.B. This is a peek, it's not thread safe, and may only tend toward
50
- # accuracy.
52
+ # @return [Boolean]
53
+ # @note This is a peek, it's not thread safe, and may only tend toward accuracy.
51
54
  def empty?
52
55
  @items.empty?
53
56
  end
54
57
 
55
- # N.B. This is a peek, it's not thread safe, and may only tend toward
56
- # accuracy.
58
+ # @return [Integer] Queue size
59
+ # @note This is a peek, it's not thread safe, and may only tend toward accuracy.
57
60
  def size
58
61
  @items.size
59
62
  end
60
- end
61
- end
63
+
64
+ # @return [Integer] Waiting size
65
+ # @note This is a peek at the number of jobs that are currently waiting on the Queue
66
+ def num_waiting
67
+ @popq.size
68
+ end
69
+
70
+ end # Queue
71
+ end # EventMachine
@@ -0,0 +1,192 @@
1
+ module EventMachine
2
+ module DNS
3
+ class Resolver
4
+
5
+ def self.resolve(hostname)
6
+ Request.new(socket, hostname)
7
+ end
8
+
9
+ @socket = @nameservers = nil
10
+
11
+ def self.socket
12
+ if !@socket || (@socket && @socket.error?)
13
+ @socket = Socket.open
14
+
15
+ @hosts = {}
16
+ IO.readlines('/etc/hosts').each do |line|
17
+ next if line =~ /^#/
18
+ addr, host = line.split(/\s+/)
19
+
20
+ if @hosts[host]
21
+ @hosts[host] << addr
22
+ else
23
+ @hosts[host] = [addr]
24
+ end
25
+ end
26
+ end
27
+
28
+ @socket
29
+ end
30
+
31
+ def self.nameservers=(ns)
32
+ @nameservers = ns
33
+ end
34
+
35
+ def self.nameservers
36
+ if !@nameservers
37
+ @nameservers = []
38
+ IO.readlines('/etc/resolv.conf').each do |line|
39
+ if line =~ /^nameserver (.+)$/
40
+ @nameservers << $1.split(/\s+/).first
41
+ end
42
+ end
43
+ end
44
+ @nameservers
45
+ end
46
+
47
+ def self.nameserver
48
+ nameservers.shuffle.first
49
+ end
50
+
51
+ def self.hosts
52
+ @hosts
53
+ end
54
+ end
55
+
56
+ class RequestIdAlreadyUsed < RuntimeError; end
57
+
58
+ class Socket < EventMachine::Connection
59
+ def self.open
60
+ EventMachine::open_datagram_socket('0.0.0.0', 0, self)
61
+ end
62
+
63
+ def initialize
64
+ @nameserver = nil
65
+ end
66
+
67
+ def post_init
68
+ @requests = {}
69
+ EM.add_periodic_timer(0.1, &method(:tick))
70
+ end
71
+
72
+ def unbind
73
+ end
74
+
75
+ def tick
76
+ @requests.each do |id,req|
77
+ req.tick
78
+ end
79
+ end
80
+
81
+ def register_request(id, req)
82
+ if @requests.has_key?(id)
83
+ raise RequestIdAlreadyUsed
84
+ else
85
+ @requests[id] = req
86
+ end
87
+ end
88
+
89
+ def send_packet(pkt)
90
+ send_datagram(pkt, nameserver, 53)
91
+ end
92
+
93
+ def nameserver=(ns)
94
+ @nameserver = ns
95
+ end
96
+
97
+ def nameserver
98
+ @nameserver || Resolver.nameserver
99
+ end
100
+
101
+ # Decodes the packet, looks for the request and passes the
102
+ # response over to the requester
103
+ def receive_data(data)
104
+ msg = nil
105
+ begin
106
+ msg = Resolv::DNS::Message.decode data
107
+ rescue
108
+ else
109
+ req = @requests[msg.id]
110
+ if req
111
+ @requests.delete(msg.id)
112
+ req.receive_answer(msg)
113
+ end
114
+ end
115
+ end
116
+ end
117
+
118
+ class Request
119
+ include Deferrable
120
+ attr_accessor :retry_interval, :max_tries
121
+
122
+ def initialize(socket, hostname)
123
+ @socket = socket
124
+ @hostname = hostname
125
+ @tries = 0
126
+ @last_send = Time.at(0)
127
+ @retry_interval = 3
128
+ @max_tries = 5
129
+
130
+ if addrs = Resolver.hosts[hostname]
131
+ succeed addrs
132
+ else
133
+ EM.next_tick { tick }
134
+ end
135
+ end
136
+
137
+ def tick
138
+ # Break early if nothing to do
139
+ return if @last_send + @retry_interval > Time.now
140
+ if @tries < @max_tries
141
+ send
142
+ else
143
+ fail 'retries exceeded'
144
+ end
145
+ end
146
+
147
+ def receive_answer(msg)
148
+ addrs = []
149
+ msg.each_answer do |name,ttl,data|
150
+ if data.kind_of?(Resolv::DNS::Resource::IN::A) ||
151
+ data.kind_of?(Resolv::DNS::Resource::IN::AAAA)
152
+ addrs << data.address.to_s
153
+ end
154
+ end
155
+
156
+ if addrs.empty?
157
+ fail "rcode=#{msg.rcode}"
158
+ else
159
+ succeed addrs
160
+ end
161
+ end
162
+
163
+ private
164
+
165
+ def send
166
+ @tries += 1
167
+ @last_send = Time.now
168
+ @socket.send_packet(packet.encode)
169
+ end
170
+
171
+ def id
172
+ begin
173
+ @id = rand(65535)
174
+ @socket.register_request(@id, self)
175
+ rescue RequestIdAlreadyUsed
176
+ retry
177
+ end unless defined?(@id)
178
+
179
+ @id
180
+ end
181
+
182
+ def packet
183
+ msg = Resolv::DNS::Message.new
184
+ msg.id = id
185
+ msg.rd = 1
186
+ msg.add_question @hostname, Resolv::DNS::Resource::IN::A
187
+ msg
188
+ end
189
+
190
+ end
191
+ end
192
+ end
data/lib/em/spawnable.rb CHANGED
@@ -45,21 +45,18 @@ module EventMachine
45
45
  end
46
46
  alias_method :resume, :notify
47
47
  alias_method :run, :notify # for formulations like (EM.spawn {xxx}).run
48
- #attr_accessor :receiver
49
48
 
50
- #--
51
- # I know I'm missing something stupid, but the inside of class << s
52
- # can't see locally-bound values. It can see globals, though.
53
49
  def set_receiver blk
54
- $em______tmpglobal = blk
55
- class << self
56
- define_method :call, $em______tmpglobal.dup
50
+ (class << self ; self ; end).class_eval do
51
+ remove_method :call if method_defined? :call
52
+ define_method :call, blk
57
53
  end
58
54
  end
59
55
 
60
56
  end
61
57
 
62
- class YieldBlockFromSpawnedProcess # :nodoc:
58
+ # @private
59
+ class YieldBlockFromSpawnedProcess
63
60
  def initialize block, notify
64
61
  @block = [block,notify]
65
62
  end
@@ -75,11 +72,13 @@ module EventMachine
75
72
  s
76
73
  end
77
74
 
78
- def self.yield &block # :nodoc:
75
+ # @private
76
+ def self.yield &block
79
77
  return YieldBlockFromSpawnedProcess.new( block, false )
80
78
  end
81
79
 
82
- def self.yield_and_notify &block # :nodoc:
80
+ # @private
81
+ def self.yield_and_notify &block
83
82
  return YieldBlockFromSpawnedProcess.new( block, true )
84
83
  end
85
84
  end
data/lib/em/streamer.rb CHANGED
@@ -1,30 +1,24 @@
1
- #--
2
- #
3
- # Author:: Francis Cianfrocca (gmail: blackhedd)
4
- # Homepage:: http://rubyeventmachine.com
5
- # Date:: 16 Jul 2006
6
- #
7
- # See EventMachine and EventMachine::Connection for documentation and
8
- # usage examples.
9
- #
10
- #----------------------------------------------------------------------------
11
- #
12
- # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13
- # Gmail: blackhedd
14
- #
15
- # This program is free software; you can redistribute it and/or modify
16
- # it under the terms of either: 1) the GNU General Public License
17
- # as published by the Free Software Foundation; either version 2 of the
18
- # License, or (at your option) any later version; or 2) Ruby's License.
19
- #
20
- # See the file COPYING for complete licensing information.
21
- #
22
- #---------------------------------------------------------------------------
23
- #
24
- #
25
-
26
-
27
1
  module EventMachine
2
+ # Streams a file over a given connection. Streaming begins once the object is
3
+ # instantiated. Typically FileStreamer instances are not reused.
4
+ #
5
+ # Streaming uses buffering for files larger than 16K and uses so-called fast file reader (a C++ extension)
6
+ # if available (it is part of eventmachine gem itself).
7
+ #
8
+ # @example
9
+ #
10
+ # module FileSender
11
+ # def post_init
12
+ # streamer = EventMachine::FileStreamer.new(self, '/tmp/bigfile.tar')
13
+ # streamer.callback{
14
+ # # file was sent successfully
15
+ # close_connection_after_writing
16
+ # }
17
+ # end
18
+ # end
19
+ #
20
+ #
21
+ # @author Francis Cianfrocca
28
22
  class FileStreamer
29
23
  include Deferrable
30
24
 
@@ -35,25 +29,16 @@ module EventMachine
35
29
  # Send 16k chunks at a time
36
30
  ChunkSize = 16384
37
31
 
38
- # Stream a file over a given connection. An optional :http_chunks => true argument will
39
- # use HTTP 1.1 style chunked-encoding semantics.
40
- #
41
- # module FileSender
42
- # def post_init
43
- # streamer = EventMachine::FileStreamer.new(self, '/tmp/bigfile.tar')
44
- # streamer.callback{
45
- # # file was sent successfully
46
- # close_connection_after_writing
47
- # }
48
- # end
49
- # end
32
+ # @param [EventMachine::Connection] connection
33
+ # @param [String] filename File path
50
34
  #
35
+ # @option args [Boolean] :http_chunks (false) Use HTTP 1.1 style chunked-encoding semantics.
51
36
  def initialize connection, filename, args = {}
52
37
  @connection = connection
53
38
  @http_chunks = args[:http_chunks]
54
39
 
55
40
  if File.exist?(filename)
56
- @size = File.size?(filename)
41
+ @size = File.size(filename)
57
42
  if @size <= MappingThreshold
58
43
  stream_without_mapping filename
59
44
  else
@@ -64,7 +49,8 @@ module EventMachine
64
49
  end
65
50
  end
66
51
 
67
- def stream_without_mapping filename # :nodoc:
52
+ # @private
53
+ def stream_without_mapping filename
68
54
  if @http_chunks
69
55
  @connection.send_data "#{@size.to_s(16)}\r\n"
70
56
  @connection.send_file_data filename
@@ -76,7 +62,8 @@ module EventMachine
76
62
  end
77
63
  private :stream_without_mapping
78
64
 
79
- def stream_with_mapping filename # :nodoc:
65
+ # @private
66
+ def stream_with_mapping filename
80
67
  ensure_mapping_extension_is_present
81
68
 
82
69
  @position = 0
@@ -86,6 +73,7 @@ module EventMachine
86
73
  private :stream_with_mapping
87
74
 
88
75
  # Used internally to stream one chunk at a time over multiple reactor ticks
76
+ # @private
89
77
  def stream_one_chunk
90
78
  loop {
91
79
  if @position < @size
@@ -111,7 +99,7 @@ module EventMachine
111
99
  }
112
100
  end
113
101
 
114
- #--
102
+ #
115
103
  # We use an outboard extension class to get memory-mapped files.
116
104
  # It's outboard to avoid polluting the core distro, but that means
117
105
  # there's a "hidden" dependency on it. The first time we get here in
@@ -120,11 +108,11 @@ module EventMachine
120
108
  # mapped files will work fine without it. This is a somewhat difficult
121
109
  # compromise between usability and proper modularization.
122
110
  #
123
- def ensure_mapping_extension_is_present # :nodoc:
111
+ # @private
112
+ def ensure_mapping_extension_is_present
124
113
  @@fastfilereader ||= (require 'fastfilereaderext')
125
114
  end
126
115
  private :ensure_mapping_extension_is_present
127
116
 
128
- end
129
- end
130
-
117
+ end # FileStreamer
118
+ end # EventMachine
@@ -0,0 +1,90 @@
1
+ module EventMachine
2
+ # = EventMachine::ThreadedResource
3
+ #
4
+ # A threaded resource is a "quick and dirty" wrapper around the concept of
5
+ # wiring up synchronous code into a standard EM::Pool. This is useful to keep
6
+ # interfaces coherent and provide a simple approach at "making an interface
7
+ # async-ish".
8
+ #
9
+ # General usage is to wrap libraries that do not support EventMachine, or to
10
+ # have a specific number of dedicated high-cpu worker resources.
11
+ #
12
+ # == Basic Usage example
13
+ #
14
+ # This example requires the cassandra gem. The cassandra gem contains an
15
+ # EventMachine interface, but it's sadly Fiber based and thus only works on
16
+ # 1.9. It also requires (potentially) complex stack switching logic to reach
17
+ # completion of nested operations. By contrast this approach provides a block
18
+ # in which normal synchronous code can occur, but makes no attempt to wire the
19
+ # IO into EventMachines C++ IO implementations, instead relying on the reactor
20
+ # pattern in rb_thread_select.
21
+ #
22
+ # cassandra_dispatcher = ThreadedResource.new do
23
+ # Cassandra.new('allthethings', '127.0.0.1:9160')
24
+ # end
25
+ #
26
+ # pool = EM::Pool.new
27
+ #
28
+ # pool.add cassandra_dispatcher
29
+ #
30
+ # # If we don't care about the result:
31
+ # pool.perform do |dispatcher|
32
+ # # The following blcok executes inside a dedicated thread, and should not
33
+ # # access EventMachine things:
34
+ # dispatcher.dispatch do |cassandra|
35
+ # cassandra.insert(:Things, '10', 'stuff' => 'things')
36
+ # end
37
+ # end
38
+ #
39
+ # # Example where we care about the result:
40
+ # pool.perform do |dispatcher|
41
+ # # The dispatch block is executed in the resources thread.
42
+ # completion = dispatcher.dispatch do |cassandra|
43
+ # cassandra.get(:Things, '10', 'stuff')
44
+ # end
45
+ #
46
+ # # This block will be yielded on the EM thread:
47
+ # completion.callback do |result|
48
+ # EM.do_something_with(result)
49
+ # end
50
+ #
51
+ # completion
52
+ # end
53
+ class ThreadedResource
54
+
55
+ # The block should return the resource that will be yielded in a dispatch.
56
+ def initialize
57
+ @resource = yield
58
+
59
+ @running = true
60
+ @queue = ::Queue.new
61
+ @thread = Thread.new do
62
+ @queue.pop.call while @running
63
+ end
64
+ end
65
+
66
+ # Called on the EM thread, generally in a perform block to return a
67
+ # completion for the work.
68
+ def dispatch
69
+ completion = EM::Completion.new
70
+ @queue << lambda do
71
+ begin
72
+ result = yield @resource
73
+ completion.succeed result
74
+ rescue Exception => e
75
+ completion.fail e
76
+ end
77
+ end
78
+ completion
79
+ end
80
+
81
+ # Kill the internal thread. should only be used to cleanup - generally
82
+ # only required for tests.
83
+ def shutdown
84
+ @running = false
85
+ @queue << lambda {}
86
+ @thread.join
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,85 @@
1
+ module EventMachine
2
+ # Creates and immediately starts an EventMachine::TickLoop
3
+ def self.tick_loop(*a, &b)
4
+ TickLoop.new(*a, &b).start
5
+ end
6
+
7
+ # A TickLoop is useful when one needs to distribute amounts of work
8
+ # throughout ticks in order to maintain response times. It is also useful for
9
+ # simple repeated checks and metrics.
10
+ #
11
+ # # Here we run through an array one item per tick until it is empty,
12
+ # # printing each element.
13
+ # # When the array is empty, we return :stop from the callback, and the
14
+ # # loop will terminate.
15
+ # # When the loop terminates, the on_stop callbacks will be called.
16
+ # EM.run do
17
+ # array = (1..100).to_a
18
+ #
19
+ # tickloop = EM.tick_loop do
20
+ # if array.empty?
21
+ # :stop
22
+ # else
23
+ # puts array.shift
24
+ # end
25
+ # end
26
+ #
27
+ # tickloop.on_stop { EM.stop }
28
+ # end
29
+ #
30
+ class TickLoop
31
+
32
+ # Arguments: A callback (EM::Callback) to call each tick. If the call
33
+ # returns +:stop+ then the loop will be stopped. Any other value is
34
+ # ignored.
35
+ def initialize(*a, &b)
36
+ @work = EM::Callback(*a, &b)
37
+ @stops = []
38
+ @stopped = true
39
+ end
40
+
41
+ # Arguments: A callback (EM::Callback) to call once on the next stop (or
42
+ # immediately if already stopped).
43
+ def on_stop(*a, &b)
44
+ if @stopped
45
+ EM::Callback(*a, &b).call
46
+ else
47
+ @stops << EM::Callback(*a, &b)
48
+ end
49
+ end
50
+
51
+ # Stop the tick loop immediately, and call it's on_stop callbacks.
52
+ def stop
53
+ @stopped = true
54
+ until @stops.empty?
55
+ @stops.shift.call
56
+ end
57
+ end
58
+
59
+ # Query if the loop is stopped.
60
+ def stopped?
61
+ @stopped
62
+ end
63
+
64
+ # Start the tick loop, will raise argument error if the loop is already
65
+ # running.
66
+ def start
67
+ raise ArgumentError, "double start" unless @stopped
68
+ @stopped = false
69
+ schedule
70
+ end
71
+
72
+ private
73
+ def schedule
74
+ EM.next_tick do
75
+ next if @stopped
76
+ if @work.call == :stop
77
+ stop
78
+ else
79
+ schedule
80
+ end
81
+ end
82
+ self
83
+ end
84
+ end
85
+ end
data/lib/em/timers.rb CHANGED
@@ -20,6 +20,7 @@ module EventMachine
20
20
 
21
21
  # Creates a periodic timer
22
22
  #
23
+ # @example
23
24
  # n = 0
24
25
  # timer = EventMachine::PeriodicTimer.new(5) do
25
26
  # puts "the time is #{Time.now}"
@@ -32,6 +33,7 @@ module EventMachine
32
33
  @interval = interval
33
34
  @code = callback || block
34
35
  @cancelled = false
36
+ @work = method(:fire)
35
37
  schedule
36
38
  end
37
39
 
@@ -43,10 +45,13 @@ module EventMachine
43
45
  # Fire the timer every interval seconds
44
46
  attr_accessor :interval
45
47
 
46
- def schedule # :nodoc:
47
- EventMachine::add_timer @interval, method(:fire)
48
+ # @private
49
+ def schedule
50
+ EventMachine::add_timer @interval, @work
48
51
  end
49
- def fire # :nodoc:
52
+
53
+ # @private
54
+ def fire
50
55
  unless @cancelled
51
56
  @code.call
52
57
  schedule
data/lib/em/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module EventMachine
2
- VERSION = "0.12.10"
2
+ VERSION = "1.0.0.beta.4"
3
3
  end