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/channel.rb CHANGED
@@ -1,33 +1,38 @@
1
1
  module EventMachine
2
- # Provides a simple interface to push items to a number of subscribers. The
3
- # channel will schedule all operations on the main reactor thread for thread
4
- # safe reactor operations.
2
+ # Provides a simple thread-safe way to transfer data between (typically) long running
3
+ # tasks in {EventMachine.defer} and event loop thread.
5
4
  #
6
- # This provides a convenient way for connections to consume messages from
7
- # long running code in defer, without threading issues.
5
+ # @example
6
+ #
7
+ # channel = EventMachine::Channel.new
8
+ # sid = channel.subscribe { |msg| p [:got, msg] }
8
9
  #
9
- # channel = EM::Channel.new
10
- # sid = channel.subscribe{ |msg| p [:got, msg] }
11
10
  # channel.push('hello world')
12
11
  # channel.unsubscribe(sid)
13
12
  #
14
- # See examples/ex_channel.rb for a detailed example.
13
+ #
15
14
  class Channel
16
- # Create a new channel
17
15
  def initialize
18
16
  @subs = {}
19
- @uid = 0
17
+ @uid = 0
20
18
  end
21
19
 
22
20
  # Takes any arguments suitable for EM::Callback() and returns a subscriber
23
21
  # id for use when unsubscribing.
22
+ #
23
+ # @return [Integer] Subscribe identifier
24
+ # @see #unsubscribe
24
25
  def subscribe(*a, &b)
25
26
  name = gen_id
26
27
  EM.schedule { @subs[name] = EM::Callback(*a, &b) }
28
+
27
29
  name
28
30
  end
29
31
 
30
- # Removes this subscriber from the list.
32
+ # Removes subscriber from the list.
33
+ #
34
+ # @param [Integer] Subscriber identifier
35
+ # @see #subscribe
31
36
  def unsubscribe(name)
32
37
  EM.schedule { @subs.delete name }
33
38
  end
@@ -35,11 +40,11 @@ module EventMachine
35
40
  # Add items to the channel, which are pushed out to all subscribers.
36
41
  def push(*items)
37
42
  items = items.dup
38
- EM.schedule { @subs.values.each { |s| items.each { |i| s.call i } } }
43
+ EM.schedule { items.each { |i| @subs.values.each { |s| s.call i } } }
39
44
  end
40
45
  alias << push
41
46
 
42
- # Receive exactly one message from the channel.
47
+ # Fetches one message from the channel.
43
48
  def pop(*a, &b)
44
49
  EM.schedule {
45
50
  name = subscribe do |*args|
@@ -50,8 +55,10 @@ module EventMachine
50
55
  end
51
56
 
52
57
  private
53
- def gen_id # :nodoc:
58
+
59
+ # @private
60
+ def gen_id
54
61
  @uid += 1
55
62
  end
56
63
  end
57
- end
64
+ end
@@ -0,0 +1,303 @@
1
+ # = EM::Completion
2
+ #
3
+ # A completion is a callback container for various states of completion. In
4
+ # it's most basic form it has a start state and a finish state.
5
+ #
6
+ # This implementation includes some hold-back from the EM::Deferrable
7
+ # interface in order to be compatible - but it has a much cleaner
8
+ # implementation.
9
+ #
10
+ # In general it is preferred that this implementation be used as a state
11
+ # callback container than EM::DefaultDeferrable or other classes including
12
+ # EM::Deferrable. This is because it is generally more sane to keep this level
13
+ # of state in a dedicated state-back container. This generally leads to more
14
+ # malleable interfaces and software designs, as well as eradicating nasty bugs
15
+ # that result from abstraction leakage.
16
+ #
17
+ # == Basic Usage
18
+ #
19
+ # As already mentioned, the basic usage of a Completion is simply for it's two
20
+ # final states, :succeeded and :failed.
21
+ #
22
+ # An asynchronous operation will complete at some future point in time, and
23
+ # users often want to react to this event. API authors will want to expose
24
+ # some common interface to react to these events.
25
+ #
26
+ # In the following example, the user wants to know when a short lived
27
+ # connection has completed it's exchange with the remote server. The simple
28
+ # protocol just waits for an ack to it's message.
29
+ #
30
+ # class Protocol < EM::Connection
31
+ # include EM::P::LineText2
32
+ #
33
+ # def initialize(message, completion)
34
+ # @message, @completion = message, completion
35
+ # @completion.completion { close_connection }
36
+ # @completion.timeout(1, :timeout)
37
+ # end
38
+ #
39
+ # def post_init
40
+ # send_data(@message)
41
+ # end
42
+ #
43
+ # def receive_line(line)
44
+ # case line
45
+ # when /ACK/i
46
+ # @completion.succeed line
47
+ # when /ERR/i
48
+ # @completion.fail :error, line
49
+ # else
50
+ # @completion.fail :unknown, line
51
+ # end
52
+ # end
53
+ #
54
+ # def unbind
55
+ # @completion.fail :disconnected unless @completion.completed?
56
+ # end
57
+ # end
58
+ #
59
+ # class API
60
+ # attr_reader :host, :port
61
+ #
62
+ # def initialize(host = 'example.org', port = 8000)
63
+ # @host, @port = host, port
64
+ # end
65
+ #
66
+ # def request(message)
67
+ # completion = EM::Deferrable::Completion.new
68
+ # EM.connect(host, port, Protocol, message, completion)
69
+ # completion
70
+ # end
71
+ # end
72
+ #
73
+ # api = API.new
74
+ # completion = api.request('stuff')
75
+ # completion.callback do |line|
76
+ # puts "API responded with: #{line}"
77
+ # end
78
+ # completion.errback do |type, line|
79
+ # case type
80
+ # when :error
81
+ # puts "API error: #{line}"
82
+ # when :unknown
83
+ # puts "API returned unknown response: #{line}"
84
+ # when :disconnected
85
+ # puts "API server disconnected prematurely"
86
+ # when :timeout
87
+ # puts "API server did not respond in a timely fashion"
88
+ # end
89
+ # end
90
+ #
91
+ # == Advanced Usage
92
+ #
93
+ # This completion implementation also supports more state callbacks and
94
+ # arbitrary states (unlike the original Deferrable API). This allows for basic
95
+ # stateful process encapsulation. One might use this to setup state callbacks
96
+ # for various states in an exchange like in the basic usage example, except
97
+ # where the applicaiton could be made to react to "connected" and
98
+ # "disconnected" states additionally.
99
+ #
100
+ # class Protocol < EM::Connection
101
+ # def initialize(completion)
102
+ # @response = []
103
+ # @completion = completion
104
+ # @completion.stateback(:disconnected) do
105
+ # @completion.succeed @response.join
106
+ # end
107
+ # end
108
+ #
109
+ # def connection_completed
110
+ # @host, @port = Socket.unpack_sockaddr_in get_peername
111
+ # @completion.change_state(:connected, @host, @port)
112
+ # send_data("GET http://example.org/ HTTP/1.0\r\n\r\n")
113
+ # end
114
+ #
115
+ # def receive_data(data)
116
+ # @response << data
117
+ # end
118
+ #
119
+ # def unbind
120
+ # @completion.change_state(:disconnected, @host, @port)
121
+ # end
122
+ # end
123
+ #
124
+ # completion = EM::Deferrable::Completion.new
125
+ # completion.stateback(:connected) do |host, port|
126
+ # puts "Connected to #{host}:#{port}"
127
+ # end
128
+ # completion.stateback(:disconnected) do |host, port|
129
+ # puts "Disconnected from #{host}:#{port}"
130
+ # end
131
+ # completion.callback do |response|
132
+ # puts response
133
+ # end
134
+ #
135
+ # EM.connect('example.org', 80, Protocol, completion)
136
+ #
137
+ # == Timeout
138
+ #
139
+ # The Completion also has a timeout. The timeout is global and is not aware of
140
+ # states apart from completion states. The timeout is only engaged if #timeout
141
+ # is called, and it will call fail if it is reached.
142
+ #
143
+ # == Completion states
144
+ #
145
+ # By default there are two completion states, :succeeded and :failed. These
146
+ # states can be modified by subclassing and overrding the #completion_states
147
+ # method. Completion states are special, in that callbacks for all completion
148
+ # states are explcitly cleared when a completion state is entered. This
149
+ # prevents errors that could arise from accidental unterminated timeouts, and
150
+ # other such user errors.
151
+ #
152
+ # == Other notes
153
+ #
154
+ # Several APIs have been carried over from EM::Deferrable for compatibility
155
+ # reasons during a transitionary period. Specifically cancel_errback and
156
+ # cancel_callback are implemented, but their usage is to be strongly
157
+ # discouraged. Due to the already complex nature of reaction systems, dynamic
158
+ # callback deletion only makes the problem much worse. It is always better to
159
+ # add correct conditionals to the callback code, or use more states, than to
160
+ # address such implementaiton issues with conditional callbacks.
161
+
162
+ module EventMachine
163
+
164
+ class Completion
165
+ # This is totally not used (re-implemented), it's here in case people check
166
+ # for kind_of?
167
+ include EventMachine::Deferrable
168
+
169
+ attr_reader :state, :value
170
+
171
+ def initialize
172
+ @state = :unknown
173
+ @callbacks = Hash.new { |h,k| h[k] = [] }
174
+ @value = []
175
+ @timeout_timer = nil
176
+ end
177
+
178
+ # Enter the :succeeded state, setting the result value if given.
179
+ def succeed(*args)
180
+ change_state(:succeeded, *args)
181
+ end
182
+ # The old EM method:
183
+ alias set_deferred_success succeed
184
+
185
+ # Enter the :failed state, setting the result value if given.
186
+ def fail(*args)
187
+ change_state(:failed, *args)
188
+ end
189
+ # The old EM method:
190
+ alias set_deferred_failure fail
191
+
192
+ # Statebacks are called when you enter (or are in) the named state.
193
+ def stateback(state, *a, &b)
194
+ # The following is quite unfortunate special casing for :completed
195
+ # statebacks, but it's a necessary evil for latent completion
196
+ # definitions.
197
+
198
+ if :completed == state || !completed? || @state == state
199
+ @callbacks[state] << EM::Callback(*a, &b)
200
+ end
201
+ execute_callbacks
202
+ end
203
+
204
+ # Callbacks are called when you enter (or are in) a :succeeded state.
205
+ def callback(*a, &b)
206
+ stateback(:succeeded, *a, &b)
207
+ end
208
+
209
+ # Errbacks are called when you enter (or are in) a :failed state.
210
+ def errback(*a, &b)
211
+ stateback(:failed, *a, &b)
212
+ end
213
+
214
+ # Completions are called when you enter (or are in) either a :failed or a
215
+ # :succeeded state. They are stored as a special (reserved) state called
216
+ # :completed.
217
+ def completion(*a, &b)
218
+ stateback(:completed, *a, &b)
219
+ end
220
+
221
+ # Enter a new state, setting the result value if given. If the state is one
222
+ # of :succeeded or :failed, then :completed callbacks will also be called.
223
+ def change_state(state, *args)
224
+ @value = args
225
+ @state = state
226
+
227
+ EM.schedule { execute_callbacks }
228
+ end
229
+
230
+ # The old EM method:
231
+ alias set_deferred_status change_state
232
+
233
+ # Indicates that we've reached some kind of completion state, by default
234
+ # this is :succeeded or :failed. Due to these semantics, the :completed
235
+ # state is reserved for internal use.
236
+ def completed?
237
+ completion_states.any? { |s| state == s }
238
+ end
239
+
240
+ # Completion states simply returns a list of completion states, by default
241
+ # this is :succeeded and :failed.
242
+ def completion_states
243
+ [:succeeded, :failed]
244
+ end
245
+
246
+ # Schedule a time which if passes before we enter a completion state, this
247
+ # deferrable will be failed with the given arguments.
248
+ def timeout(time, *args)
249
+ cancel_timeout
250
+ @timeout_timer = EM::Timer.new(time) do
251
+ fail(*args) unless completed?
252
+ end
253
+ end
254
+
255
+ # Disable the timeout
256
+ def cancel_timeout
257
+ if @timeout_timer
258
+ @timeout_timer.cancel
259
+ @timeout_timer = nil
260
+ end
261
+ end
262
+
263
+ # Remove an errback. N.B. Some errbacks cannot be deleted. Usage is NOT
264
+ # recommended, this is an anti-pattern.
265
+ def cancel_errback(*a, &b)
266
+ @callbacks[:failed].delete(EM::Callback(*a, &b))
267
+ end
268
+
269
+ # Remove a callback. N.B. Some callbacks cannot be deleted. Usage is NOT
270
+ # recommended, this is an anti-pattern.
271
+ def cancel_callback(*a, &b)
272
+ @callbacks[:succeeded].delete(EM::Callback(*a, &b))
273
+ end
274
+
275
+ private
276
+ # Execute all callbacks for the current state. If in a completed state, then
277
+ # call any statebacks associated with the completed state.
278
+ def execute_callbacks
279
+ execute_state_callbacks(state)
280
+ if completed?
281
+ execute_state_callbacks(:completed)
282
+ clear_dead_callbacks
283
+ cancel_timeout
284
+ end
285
+ end
286
+
287
+ # Iterate all callbacks for a given state, and remove then call them.
288
+ def execute_state_callbacks(state)
289
+ while callback = @callbacks[state].shift
290
+ callback.call(*value)
291
+ end
292
+ end
293
+
294
+ # If we enter a completion state, clear other completion states after all
295
+ # callback chains are completed. This means that operation specific
296
+ # callbacks can't be dual-called, which is most common user error.
297
+ def clear_dead_callbacks
298
+ completion_states.each do |state|
299
+ @callbacks[state].clear
300
+ end
301
+ end
302
+ end
303
+ end