libc-eventmachine 0.12.5.42

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.
Files changed (131) hide show
  1. data/Rakefile +195 -0
  2. data/docs/COPYING +60 -0
  3. data/docs/ChangeLog +211 -0
  4. data/docs/DEFERRABLES +138 -0
  5. data/docs/EPOLL +141 -0
  6. data/docs/GNU +281 -0
  7. data/docs/INSTALL +15 -0
  8. data/docs/KEYBOARD +38 -0
  9. data/docs/LEGAL +25 -0
  10. data/docs/LIGHTWEIGHT_CONCURRENCY +72 -0
  11. data/docs/PURE_RUBY +77 -0
  12. data/docs/README +74 -0
  13. data/docs/RELEASE_NOTES +96 -0
  14. data/docs/SMTP +9 -0
  15. data/docs/SPAWNED_PROCESSES +93 -0
  16. data/docs/TODO +10 -0
  17. data/ext/binder.cpp +126 -0
  18. data/ext/binder.h +48 -0
  19. data/ext/cmain.cpp +582 -0
  20. data/ext/cplusplus.cpp +177 -0
  21. data/ext/ed.cpp +1522 -0
  22. data/ext/ed.h +380 -0
  23. data/ext/em.cpp +1947 -0
  24. data/ext/em.h +186 -0
  25. data/ext/emwin.cpp +300 -0
  26. data/ext/emwin.h +94 -0
  27. data/ext/epoll.cpp +26 -0
  28. data/ext/epoll.h +25 -0
  29. data/ext/eventmachine.h +98 -0
  30. data/ext/eventmachine_cpp.h +96 -0
  31. data/ext/extconf.rb +129 -0
  32. data/ext/fastfilereader/extconf.rb +77 -0
  33. data/ext/fastfilereader/mapper.cpp +214 -0
  34. data/ext/fastfilereader/mapper.h +59 -0
  35. data/ext/fastfilereader/rubymain.cpp +127 -0
  36. data/ext/files.cpp +94 -0
  37. data/ext/files.h +65 -0
  38. data/ext/kb.cpp +82 -0
  39. data/ext/page.cpp +107 -0
  40. data/ext/page.h +51 -0
  41. data/ext/pipe.cpp +351 -0
  42. data/ext/project.h +119 -0
  43. data/ext/rubymain.cpp +858 -0
  44. data/ext/sigs.cpp +89 -0
  45. data/ext/sigs.h +32 -0
  46. data/ext/ssl.cpp +423 -0
  47. data/ext/ssl.h +90 -0
  48. data/java/src/com/rubyeventmachine/Application.java +196 -0
  49. data/java/src/com/rubyeventmachine/Connection.java +74 -0
  50. data/java/src/com/rubyeventmachine/ConnectionFactory.java +37 -0
  51. data/java/src/com/rubyeventmachine/DefaultConnectionFactory.java +46 -0
  52. data/java/src/com/rubyeventmachine/EmReactor.java +408 -0
  53. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  54. data/java/src/com/rubyeventmachine/EventableChannel.java +57 -0
  55. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +171 -0
  56. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +244 -0
  57. data/java/src/com/rubyeventmachine/PeriodicTimer.java +38 -0
  58. data/java/src/com/rubyeventmachine/Timer.java +54 -0
  59. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +108 -0
  60. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +124 -0
  61. data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
  62. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
  63. data/java/src/com/rubyeventmachine/tests/TestServers.java +74 -0
  64. data/java/src/com/rubyeventmachine/tests/TestTimers.java +89 -0
  65. data/lib/em/deferrable.rb +208 -0
  66. data/lib/em/eventable.rb +39 -0
  67. data/lib/em/future.rb +62 -0
  68. data/lib/em/messages.rb +66 -0
  69. data/lib/em/processes.rb +68 -0
  70. data/lib/em/spawnable.rb +88 -0
  71. data/lib/em/streamer.rb +112 -0
  72. data/lib/eventmachine.rb +1920 -0
  73. data/lib/eventmachine_version.rb +31 -0
  74. data/lib/evma/callback.rb +32 -0
  75. data/lib/evma/container.rb +75 -0
  76. data/lib/evma/factory.rb +77 -0
  77. data/lib/evma/protocol.rb +87 -0
  78. data/lib/evma/reactor.rb +48 -0
  79. data/lib/evma.rb +32 -0
  80. data/lib/jeventmachine.rb +140 -0
  81. data/lib/pr_eventmachine.rb +1017 -0
  82. data/lib/protocols/buftok.rb +127 -0
  83. data/lib/protocols/header_and_content.rb +129 -0
  84. data/lib/protocols/httpcli2.rb +803 -0
  85. data/lib/protocols/httpclient.rb +270 -0
  86. data/lib/protocols/line_and_text.rb +126 -0
  87. data/lib/protocols/linetext2.rb +161 -0
  88. data/lib/protocols/memcache.rb +293 -0
  89. data/lib/protocols/postgres.rb +261 -0
  90. data/lib/protocols/saslauth.rb +179 -0
  91. data/lib/protocols/smtpclient.rb +308 -0
  92. data/lib/protocols/smtpserver.rb +556 -0
  93. data/lib/protocols/stomp.rb +153 -0
  94. data/lib/protocols/tcptest.rb +57 -0
  95. data/tasks/cpp.rake +77 -0
  96. data/tasks/project.rake +78 -0
  97. data/tasks/tests.rake +193 -0
  98. data/tests/test_attach.rb +83 -0
  99. data/tests/test_basic.rb +231 -0
  100. data/tests/test_bind.rb +73 -0
  101. data/tests/test_connection_count.rb +35 -0
  102. data/tests/test_defer.rb +47 -0
  103. data/tests/test_epoll.rb +163 -0
  104. data/tests/test_error_handler.rb +32 -0
  105. data/tests/test_errors.rb +82 -0
  106. data/tests/test_eventables.rb +77 -0
  107. data/tests/test_exc.rb +58 -0
  108. data/tests/test_futures.rb +214 -0
  109. data/tests/test_handler_check.rb +37 -0
  110. data/tests/test_hc.rb +218 -0
  111. data/tests/test_httpclient.rb +215 -0
  112. data/tests/test_httpclient2.rb +155 -0
  113. data/tests/test_kb.rb +61 -0
  114. data/tests/test_ltp.rb +188 -0
  115. data/tests/test_ltp2.rb +320 -0
  116. data/tests/test_next_tick.rb +109 -0
  117. data/tests/test_processes.rb +56 -0
  118. data/tests/test_pure.rb +129 -0
  119. data/tests/test_running.rb +47 -0
  120. data/tests/test_sasl.rb +74 -0
  121. data/tests/test_send_file.rb +243 -0
  122. data/tests/test_servers.rb +80 -0
  123. data/tests/test_smtpclient.rb +83 -0
  124. data/tests/test_smtpserver.rb +93 -0
  125. data/tests/test_spawn.rb +329 -0
  126. data/tests/test_ssl_args.rb +68 -0
  127. data/tests/test_ssl_methods.rb +50 -0
  128. data/tests/test_timers.rb +148 -0
  129. data/tests/test_ud.rb +43 -0
  130. data/tests/testem.rb +31 -0
  131. metadata +230 -0
@@ -0,0 +1,1920 @@
1
+ # $Id$
2
+ #
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
5
+ # Date:: 8 Apr 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
+ #-- Select in a library based on a global variable.
28
+ # PROVISIONALLY commented out this whole mechanism which selects
29
+ # a pure-Ruby EM implementation if the extension is not available.
30
+ # I expect this will cause a lot of people's code to break, as it
31
+ # exposes misconfigurations and path problems that were masked up
32
+ # till now. The reason I'm disabling it is because the pure-Ruby
33
+ # code will have problems of its own, and it's not nearly as fast
34
+ # anyway. Suggested by a problem report from Moshe Litvin. 05Jun07.
35
+ #
36
+ # 05Dec07: Re-enabled the pure-ruby mechanism, but without the automatic
37
+ # fallback feature that tripped up Moshe Litvin. We shouldn't fail over to
38
+ # the pure Ruby version because it's possible that the user intended to
39
+ # run the extension but failed to do so because of a compilation or
40
+ # similar error. So we require either a global variable or an environment
41
+ # string be set in order to select the pure-Ruby version.
42
+ #
43
+
44
+
45
+ unless defined?($eventmachine_library)
46
+ $eventmachine_library = ENV['EVENTMACHINE_LIBRARY'] || :cascade
47
+ end
48
+ $eventmachine_library = $eventmachine_library.to_sym
49
+
50
+ case $eventmachine_library
51
+ when :pure_ruby
52
+ require 'pr_eventmachine'
53
+ when :extension
54
+ require 'rubyeventmachine'
55
+ when :java
56
+ require 'jeventmachine'
57
+ else # :cascade
58
+ # This is the case that most user code will take.
59
+ # Prefer the extension if available.
60
+ begin
61
+ if RUBY_PLATFORM =~ /java/
62
+ require 'java'
63
+ require 'jeventmachine'
64
+ $eventmachine_library = :java
65
+ else
66
+ require 'rubyeventmachine'
67
+ $eventmachine_library = :extension
68
+ end
69
+ rescue LoadError
70
+ warn "# EventMachine fell back to pure ruby mode" if $DEBUG
71
+ require 'pr_eventmachine'
72
+ $eventmachine_library = :pure_ruby
73
+ end
74
+ end
75
+
76
+ require "eventmachine_version"
77
+ require 'em/deferrable'
78
+ require 'em/future'
79
+ require 'em/eventable'
80
+ require 'em/messages'
81
+ require 'em/streamer'
82
+ require 'em/spawnable'
83
+
84
+ require 'shellwords'
85
+
86
+ #-- Additional requires are at the BOTTOM of this file, because they
87
+ #-- depend on stuff defined in here. Refactor that someday.
88
+
89
+
90
+
91
+ # == Introduction
92
+ # EventMachine provides a fast, lightweight framework for implementing
93
+ # Ruby programs that can use the network to communicate with other
94
+ # processes. Using EventMachine, Ruby programmers can easily connect
95
+ # to remote servers and act as servers themselves. EventMachine does not
96
+ # supplant the Ruby IP libraries. It does provide an alternate technique
97
+ # for those applications requiring better performance, scalability,
98
+ # and discipline over the behavior of network sockets, than is easily
99
+ # obtainable using the built-in libraries, especially in applications
100
+ # which are structurally well-suited for the event-driven programming model.
101
+ #
102
+ # EventMachine provides a perpetual event-loop which your programs can
103
+ # start and stop. Within the event loop, TCP network connections are
104
+ # initiated and accepted, based on EventMachine methods called by your
105
+ # program. You also define callback methods which are called by EventMachine
106
+ # when events of interest occur within the event-loop.
107
+ #
108
+ # User programs will be called back when the following events occur:
109
+ # * When the event loop accepts network connections from remote peers
110
+ # * When data is received from network connections
111
+ # * When connections are closed, either by the local or the remote side
112
+ # * When user-defined timers expire
113
+ #
114
+ # == Usage example
115
+ #
116
+ # Here's a fully-functional echo server implemented in EventMachine:
117
+ #
118
+ # require 'rubygems'
119
+ # require 'eventmachine'
120
+ #
121
+ # module EchoServer
122
+ # def receive_data data
123
+ # send_data ">>>you sent: #{data}"
124
+ # close_connection if data =~ /quit/i
125
+ # end
126
+ # end
127
+ #
128
+ # EventMachine::run {
129
+ # EventMachine::start_server "192.168.0.100", 8081, EchoServer
130
+ # }
131
+ #
132
+ # What's going on here? Well, we have defined the module EchoServer to
133
+ # implement the semantics of the echo protocol (more about that shortly).
134
+ # The last three lines invoke the event-machine itself, which runs forever
135
+ # unless one of your callbacks terminates it. The block that you supply
136
+ # to EventMachine::run contains code that runs immediately after the event
137
+ # machine is initialized and before it starts looping. This is the place
138
+ # to open up a TCP server by specifying the address and port it will listen
139
+ # on, together with the module that will process the data.
140
+ #
141
+ # Our EchoServer is extremely simple as the echo protocol doesn't require
142
+ # much work. Basically you want to send back to the remote peer whatever
143
+ # data it sends you. We'll dress it up with a little extra text to make it
144
+ # interesting. Also, we'll close the connection in case the received data
145
+ # contains the word "quit."
146
+ #
147
+ # So what about this module EchoServer? Well, whenever a network connection
148
+ # (either a client or a server) starts up, EventMachine instantiates an anonymous
149
+ # class, that your module has been mixed into. Exactly one of these class
150
+ # instances is created for each connection. Whenever an event occurs on a
151
+ # given connection, its corresponding object automatically calls specific
152
+ # instance methods which your module may redefine. The code in your module
153
+ # always runs in the context of a class instance, so you can create instance
154
+ # variables as you wish and they will be carried over to other callbacks
155
+ # made on that same connection.
156
+ #
157
+ # Looking back up at EchoServer, you can see that we've defined the method
158
+ # receive_data which (big surprise) is called whenever data has been received
159
+ # from the remote end of the connection. Very simple. We get the data
160
+ # (a String object) and can do whatever we wish with it. In this case,
161
+ # we use the method send_data to return the received data to the caller,
162
+ # with some extra text added in. And if the user sends the word "quit,"
163
+ # we'll close the connection with (naturally) close_connection.
164
+ # (Notice that closing the connection doesn't terminate the processing loop,
165
+ # or change the fact that your echo server is still accepting connections!)
166
+ #
167
+ #
168
+ # == Questions and Futures
169
+ # Would it be useful for EventMachine to incorporate the Observer pattern
170
+ # and make use of the corresponding Ruby <tt>observer</tt> package?
171
+ # Interesting thought.
172
+ #
173
+ #
174
+ module EventMachine
175
+ class FileNotFoundException < Exception; end
176
+
177
+ class << self
178
+ attr_reader :threadpool
179
+ end
180
+
181
+
182
+ # EventMachine::run initializes and runs an event loop.
183
+ # This method only returns if user-callback code calls stop_event_loop.
184
+ # Use the supplied block to define your clients and servers.
185
+ # The block is called by EventMachine::run immediately after initializing
186
+ # its internal event loop but <i>before</i> running the loop.
187
+ # Therefore this block is the right place to call start_server if you
188
+ # want to accept connections from remote clients.
189
+ #
190
+ # For programs that are structured as servers, it's usually appropriate
191
+ # to start an event loop by calling EventMachine::run, and let it
192
+ # run forever. It's also possible to use EventMachine::run to make a single
193
+ # client-connection to a remote server, process the data flow from that
194
+ # single connection, and then call stop_event_loop to force EventMachine::run
195
+ # to return. Your program will then continue from the point immediately
196
+ # following the call to EventMachine::run.
197
+ #
198
+ # You can of course do both client and servers simultaneously in the same program.
199
+ # One of the strengths of the event-driven programming model is that the
200
+ # handling of network events on many different connections will be interleaved,
201
+ # and scheduled according to the actual events themselves. This maximizes
202
+ # efficiency.
203
+ #
204
+ # === Server usage example
205
+ #
206
+ # See the text at the top of this file for an example of an echo server.
207
+ #
208
+ # === Client usage example
209
+ #
210
+ # See the description of stop_event_loop for an extremely simple client example.
211
+ #
212
+ #--
213
+ # Obsoleted the use_threads mechanism.
214
+ # 25Nov06: Added the begin/ensure block. We need to be sure that release_machine
215
+ # gets called even if an exception gets thrown within any of the user code
216
+ # that the event loop runs. The best way to see this is to run a unit
217
+ # test with two functions, each of which calls EventMachine#run and each of
218
+ # which throws something inside of #run. Without the ensure, the second test
219
+ # will start without release_machine being called and will immediately throw
220
+ # a C++ runtime error.
221
+ #
222
+ def EventMachine::run blk=nil, tail=nil, &block
223
+ @tails ||= []
224
+ tail and @tails.unshift(tail)
225
+
226
+ if reactor_running?
227
+ (b = blk || block) and b.call # next_tick(b)
228
+ else
229
+ @conns = {}
230
+ @acceptors = {}
231
+ @timers = {}
232
+ @wrapped_exception = nil
233
+ begin
234
+ @reactor_running = true
235
+ initialize_event_machine
236
+ (b = blk || block) and add_timer(0, b)
237
+ if @next_tick_queue && !@next_tick_queue.empty?
238
+ add_timer(0) { signal_loopbreak }
239
+ end
240
+ run_machine
241
+ ensure
242
+ begin
243
+ release_machine
244
+ ensure
245
+ if @threadpool
246
+ @threadpool.each { |t| t.exit }
247
+ @threadpool.each { |t| t.kill! if t.alive? }
248
+ @threadqueue = nil
249
+ @resultqueue = nil
250
+ end
251
+ @threadpool = nil
252
+ end
253
+ @reactor_running = false
254
+ end
255
+
256
+ until @tails.empty?
257
+ @tails.pop.call
258
+ end
259
+
260
+ raise @wrapped_exception if @wrapped_exception
261
+ end
262
+ end
263
+
264
+
265
+ # Sugars a common use case. Will pass the given block to #run, but will terminate
266
+ # the reactor loop and exit the function as soon as the code in the block completes.
267
+ # (Normally, #run keeps running indefinitely, even after the block supplied to it
268
+ # finishes running, until user code calls #stop.)
269
+ #
270
+ def EventMachine::run_block &block
271
+ pr = proc {
272
+ block.call
273
+ EventMachine::stop
274
+ }
275
+ run(&pr)
276
+ end
277
+
278
+ # fork_reactor forks a new process and calls EM#run inside of it, passing your block.
279
+ #--
280
+ # This implementation is subject to change, especially if we clean up the relationship
281
+ # of EM#run to @reactor_running.
282
+ # Original patch by Aman Gupta.
283
+ #
284
+ def EventMachine::fork_reactor &block
285
+ Kernel.fork do
286
+ if self.reactor_running?
287
+ self.stop_event_loop
288
+ self.release_machine
289
+ self.instance_variable_set( '@reactor_running', false )
290
+ end
291
+ self.run block
292
+ end
293
+ end
294
+
295
+
296
+ # +deprecated+
297
+ #--
298
+ # EventMachine#run_without_threads is semantically identical
299
+ # to EventMachine#run, but it runs somewhat faster.
300
+ # However, it must not be used in applications that spin
301
+ # Ruby threads.
302
+ def EventMachine::run_without_threads &block
303
+ #EventMachine::run false, &block
304
+ EventMachine::run(&block)
305
+ end
306
+
307
+ # EventMachine#add_timer adds a one-shot timer to the event loop.
308
+ # Call it with one or two parameters. The first parameters is a delay-time
309
+ # expressed in <i>seconds</i> (not milliseconds). The second parameter, if
310
+ # present, must be a proc object. If a proc object is not given, then you
311
+ # can also simply pass a block to the method call.
312
+ #
313
+ # EventMachine#add_timer may be called from the block passed to EventMachine#run
314
+ # or from any callback method. It schedules execution of the proc or block
315
+ # passed to add_timer, after the passage of an interval of time equal to
316
+ # <i>at least</i> the number of seconds specified in the first parameter to
317
+ # the call.
318
+ #
319
+ # EventMachine#add_timer is a <i>non-blocking</i> call. Callbacks can and will
320
+ # be called during the interval of time that the timer is in effect.
321
+ # There is no built-in limit to the number of timers that can be outstanding at
322
+ # any given time.
323
+ #
324
+ # === Usage example
325
+ #
326
+ # This example shows how easy timers are to use. Observe that two timers are
327
+ # initiated simultaneously. Also, notice that the event loop will continue
328
+ # to run even after the second timer event is processed, since there was
329
+ # no call to EventMachine#stop_event_loop. There will be no activity, of
330
+ # course, since no network clients or servers are defined. Stop the program
331
+ # with Ctrl-C.
332
+ #
333
+ # require 'rubygems'
334
+ # require 'eventmachine'
335
+ #
336
+ # EventMachine::run {
337
+ # puts "Starting the run now: #{Time.now}"
338
+ # EventMachine::add_timer 5, proc { puts "Executing timer event: #{Time.now}" }
339
+ # EventMachine::add_timer( 10 ) { puts "Executing timer event: #{Time.now}" }
340
+ # }
341
+ #
342
+ #
343
+ #--
344
+ # Changed 04Oct06: We now pass the interval as an integer number of milliseconds.
345
+ #
346
+ def EventMachine::add_timer *args, &block
347
+ interval = args.shift
348
+ code = args.shift || block
349
+ if code
350
+ # check too many timers!
351
+ s = add_oneshot_timer((interval * 1000).to_i)
352
+ @timers[s] = code
353
+ s
354
+ end
355
+ end
356
+
357
+ # EventMachine#add_periodic_timer adds a periodic timer to the event loop.
358
+ # It takes the same parameters as the one-shot timer method, EventMachine#add_timer.
359
+ # This method schedules execution of the given block repeatedly, at intervals
360
+ # of time <i>at least</i> as great as the number of seconds given in the first
361
+ # parameter to the call.
362
+ #
363
+ # === Usage example
364
+ #
365
+ # The following sample program will write a dollar-sign to stderr every five seconds.
366
+ # (Of course if the program defined network clients and/or servers, they would
367
+ # be doing their work while the periodic timer is counting off.)
368
+ #
369
+ # EventMachine::run {
370
+ # EventMachine::add_periodic_timer( 5 ) { $stderr.write "$" }
371
+ # }
372
+ #
373
+ def EventMachine::add_periodic_timer *args, &block
374
+ interval = args.shift
375
+ code = args.shift || block
376
+ if code
377
+ block_1 = proc {
378
+ code.call
379
+ EventMachine::add_periodic_timer interval, code
380
+ }
381
+ add_timer interval, block_1
382
+ end
383
+ end
384
+
385
+ #--
386
+ #
387
+ def EventMachine::cancel_timer signature
388
+ @timers[signature] = proc{} if @timers.has_key?(signature)
389
+ end
390
+
391
+
392
+ # stop_event_loop may called from within a callback method
393
+ # while EventMachine's processing loop is running.
394
+ # It causes the processing loop to stop executing, which
395
+ # will cause all open connections and accepting servers
396
+ # to be run down and closed. <i>Callbacks for connection-termination
397
+ # will be called</i> as part of the processing of stop_event_loop.
398
+ # (There currently is no option to panic-stop the loop without
399
+ # closing connections.) When all of this processing is complete,
400
+ # the call to EventMachine::run which started the processing loop
401
+ # will return and program flow will resume from the statement
402
+ # following EventMachine::run call.
403
+ #
404
+ # === Usage example
405
+ #
406
+ # require 'rubygems'
407
+ # require 'eventmachine'
408
+ #
409
+ # module Redmond
410
+ #
411
+ # def post_init
412
+ # puts "We're sending a dumb HTTP request to the remote peer."
413
+ # send_data "GET / HTTP/1.1\r\nHost: www.microsoft.com\r\n\r\n"
414
+ # end
415
+ #
416
+ # def receive_data data
417
+ # puts "We received #{data.length} bytes from the remote peer."
418
+ # puts "We're going to stop the event loop now."
419
+ # EventMachine::stop_event_loop
420
+ # end
421
+ #
422
+ # def unbind
423
+ # puts "A connection has terminated."
424
+ # end
425
+ #
426
+ # end
427
+ #
428
+ # puts "We're starting the event loop now."
429
+ # EventMachine::run {
430
+ # EventMachine::connect "www.microsoft.com", 80, Redmond
431
+ # }
432
+ # puts "The event loop has stopped."
433
+ #
434
+ # This program will produce approximately the following output:
435
+ #
436
+ # We're starting the event loop now.
437
+ # We're sending a dumb HTTP request to the remote peer.
438
+ # We received 1440 bytes from the remote peer.
439
+ # We're going to stop the event loop now.
440
+ # A connection has terminated.
441
+ # The event loop has stopped.
442
+ #
443
+ #
444
+ def EventMachine::stop_event_loop
445
+ EventMachine::stop
446
+ end
447
+
448
+ # EventMachine::start_server initiates a TCP server (socket
449
+ # acceptor) on the specified IP address and port.
450
+ # The IP address must be valid on the machine where the program
451
+ # runs, and the process must be privileged enough to listen
452
+ # on the specified port (on Unix-like systems, superuser privileges
453
+ # are usually required to listen on any port lower than 1024).
454
+ # Only one listener may be running on any given address/port
455
+ # combination. start_server will fail if the given address and port
456
+ # are already listening on the machine, either because of a prior call
457
+ # to start_server or some unrelated process running on the machine.
458
+ # If start_server succeeds, the new network listener becomes active
459
+ # immediately and starts accepting connections from remote peers,
460
+ # and these connections generate callback events that are processed
461
+ # by the code specified in the handler parameter to start_server.
462
+ #
463
+ # The optional handler which is passed to start_server is the key
464
+ # to EventMachine's ability to handle particular network protocols.
465
+ # The handler parameter passed to start_server must be a Ruby Module
466
+ # that you must define. When the network server that is started by
467
+ # start_server accepts a new connection, it instantiates a new
468
+ # object of an anonymous class that is inherited from EventMachine::Connection,
469
+ # <i>into which the methods from your handler have been mixed.</i>
470
+ # Your handler module may redefine any of the methods in EventMachine::Connection
471
+ # in order to implement the specific behavior of the network protocol.
472
+ #
473
+ # Callbacks invoked in response to network events <i>always</i> take place
474
+ # within the execution context of the object derived from EventMachine::Connection
475
+ # extended by your handler module. There is one object per connection, and
476
+ # all of the callbacks invoked for a particular connection take the form
477
+ # of instance methods called against the corresponding EventMachine::Connection
478
+ # object. Therefore, you are free to define whatever instance variables you
479
+ # wish, in order to contain the per-connection state required by the network protocol you are
480
+ # implementing.
481
+ #
482
+ # start_server is often called inside the block passed to EventMachine::run,
483
+ # but it can be called from any EventMachine callback. start_server will fail
484
+ # unless the EventMachine event loop is currently running (which is why
485
+ # it's often called in the block suppled to EventMachine::run).
486
+ #
487
+ # You may call start_server any number of times to start up network
488
+ # listeners on different address/port combinations. The servers will
489
+ # all run simultaneously. More interestingly, each individual call to start_server
490
+ # can specify a different handler module and thus implement a different
491
+ # network protocol from all the others.
492
+ #
493
+ # === Usage example
494
+ # Here is an example of a server that counts lines of input from the remote
495
+ # peer and sends back the total number of lines received, after each line.
496
+ # Try the example with more than one client connection opened via telnet,
497
+ # and you will see that the line count increments independently on each
498
+ # of the client connections. Also very important to note, is that the
499
+ # handler for the receive_data function, which our handler redefines, may
500
+ # not assume that the data it receives observes any kind of message boundaries.
501
+ # Also, to use this example, be sure to change the server and port parameters
502
+ # to the start_server call to values appropriate for your environment.
503
+ #
504
+ # require 'rubygems'
505
+ # require 'eventmachine'
506
+ #
507
+ # module LineCounter
508
+ #
509
+ # MaxLinesPerConnection = 10
510
+ #
511
+ # def post_init
512
+ # puts "Received a new connection"
513
+ # @data_received = ""
514
+ # @line_count = 0
515
+ # end
516
+ #
517
+ # def receive_data data
518
+ # @data_received << data
519
+ # while @data_received.slice!( /^[^\n]*[\n]/m )
520
+ # @line_count += 1
521
+ # send_data "received #{@line_count} lines so far\r\n"
522
+ # @line_count == MaxLinesPerConnection and close_connection_after_writing
523
+ # end
524
+ # end
525
+ #
526
+ # end # module LineCounter
527
+ #
528
+ # EventMachine::run {
529
+ # host,port = "192.168.0.100", 8090
530
+ # EventMachine::start_server host, port, LineCounter
531
+ # puts "Now accepting connections on address #{host}, port #{port}..."
532
+ # EventMachine::add_periodic_timer( 10 ) { $stderr.write "*" }
533
+ # }
534
+ #
535
+ #
536
+ def EventMachine::start_server server, port=nil, handler=nil, *args, &block
537
+
538
+ begin
539
+ port = Integer(port)
540
+ rescue ArgumentError, TypeError
541
+ args.unshift handler if handler
542
+ handler = port
543
+ port = nil
544
+ end if port
545
+
546
+ klass = if (handler and handler.is_a?(Class))
547
+ raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
548
+ handler
549
+ else
550
+ Class.new( Connection ) {handler and include handler}
551
+ end
552
+
553
+ arity = klass.instance_method(:initialize).arity
554
+ expected = arity >= 0 ? arity : -(arity + 1)
555
+ if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
556
+ raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
557
+ end
558
+
559
+ s = if port
560
+ start_tcp_server server, port
561
+ else
562
+ start_unix_server server
563
+ end
564
+ @acceptors[s] = [klass,args,block]
565
+ s
566
+ end
567
+
568
+
569
+ # Stop a TCP server socket that was started with EventMachine#start_server.
570
+ #--
571
+ # Requested by Kirk Haines. TODO, this isn't OOP enough. We ought somehow
572
+ # to have #start_server return an object that has a close or a stop method on it.
573
+ #
574
+ def EventMachine::stop_server signature
575
+ EventMachine::stop_tcp_server signature
576
+ end
577
+
578
+ def EventMachine::start_unix_domain_server filename, *args, &block
579
+ start_server filename, *args, &block
580
+ end
581
+
582
+ # EventMachine#connect initiates a TCP connection to a remote
583
+ # server and sets up event-handling for the connection.
584
+ # You can call EventMachine#connect in the block supplied
585
+ # to EventMachine#run or in any callback method.
586
+ #
587
+ # EventMachine#connect takes the IP address (or hostname) and
588
+ # port of the remote server you want to connect to.
589
+ # It also takes an optional handler Module which you must define, that
590
+ # contains the callbacks that will be invoked by the event loop
591
+ # on behalf of the connection.
592
+ #
593
+ # See the description of EventMachine#start_server for a discussion
594
+ # of the handler Module. All of the details given in that description
595
+ # apply for connections created with EventMachine#connect.
596
+ #
597
+ # === Usage Example
598
+ #
599
+ # Here's a program which connects to a web server, sends a naive
600
+ # request, parses the HTTP header of the response, and then
601
+ # (antisocially) ends the event loop, which automatically drops the connection
602
+ # (and incidentally calls the connection's unbind method).
603
+ #
604
+ # require 'rubygems'
605
+ # require 'eventmachine'
606
+ #
607
+ # module DumbHttpClient
608
+ #
609
+ # def post_init
610
+ # send_data "GET / HTTP/1.1\r\nHost: _\r\n\r\n"
611
+ # @data = ""
612
+ # end
613
+ #
614
+ # def receive_data data
615
+ # @data << data
616
+ # if @data =~ /[\n][\r]*[\n]/m
617
+ # puts "RECEIVED HTTP HEADER:"
618
+ # $`.each {|line| puts ">>> #{line}" }
619
+ #
620
+ # puts "Now we'll terminate the loop, which will also close the connection"
621
+ # EventMachine::stop_event_loop
622
+ # end
623
+ # end
624
+ #
625
+ # def unbind
626
+ # puts "A connection has terminated"
627
+ # end
628
+ #
629
+ # end # DumbHttpClient
630
+ #
631
+ #
632
+ # EventMachine::run {
633
+ # EventMachine::connect "www.bayshorenetworks.com", 80, DumbHttpClient
634
+ # }
635
+ # puts "The event loop has ended"
636
+ #
637
+ #
638
+ # There are times when it's more convenient to define a protocol handler
639
+ # as a Class rather than a Module. Here's how to do this:
640
+ #
641
+ # class MyProtocolHandler < EventMachine::Connection
642
+ # def initialize *args
643
+ # super
644
+ # # whatever else you want to do here
645
+ # end
646
+ #
647
+ # #.......your other class code
648
+ # end # class MyProtocolHandler
649
+ #
650
+ # If you do this, then an instance of your class will be instantiated to handle
651
+ # every network connection created by your code or accepted by servers that you
652
+ # create. If you redefine #post_init in your protocol-handler class, your
653
+ # #post_init method will be called _inside_ the call to #super that you will
654
+ # make in your #initialize method (if you provide one).
655
+ #
656
+ #--
657
+ # EventMachine::connect initiates a TCP connection to a remote
658
+ # server and sets up event-handling for the connection.
659
+ # It internally creates an object that should not be handled
660
+ # by the caller. HOWEVER, it's often convenient to get the
661
+ # object to set up interfacing to other objects in the system.
662
+ # We return the newly-created anonymous-class object to the caller.
663
+ # It's expected that a considerable amount of code will depend
664
+ # on this behavior, so don't change it.
665
+ #
666
+ # Ok, added support for a user-defined block, 13Apr06.
667
+ # This leads us to an interesting choice because of the
668
+ # presence of the post_init call, which happens in the
669
+ # initialize method of the new object. We call the user's
670
+ # block and pass the new object to it. This is a great
671
+ # way to do protocol-specific initiation. It happens
672
+ # AFTER post_init has been called on the object, which I
673
+ # certainly hope is the right choice.
674
+ # Don't change this lightly, because accepted connections
675
+ # are different from connected ones and we don't want
676
+ # to have them behave differently with respect to post_init
677
+ # if at all possible.
678
+ #
679
+ def EventMachine::connect server, port=nil, handler=nil, *args, &block
680
+ connect2 server, port, handler, {}, *args, &block
681
+ end
682
+
683
+ def EventMachine::connect2 server, port=nil, handler=nil, options={}, *args
684
+ begin
685
+ port = Integer(port)
686
+ rescue ArgumentError, TypeError
687
+ args.unshift handler if handler
688
+ handler = port
689
+ port = nil
690
+ end if port
691
+
692
+ klass = if (handler and handler.is_a?(Class))
693
+ raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
694
+ handler
695
+ else
696
+ Class.new( Connection ) {handler and include handler}
697
+ end
698
+
699
+ arity = klass.instance_method(:initialize).arity
700
+ expected = arity >= 0 ? arity : -(arity + 1)
701
+ if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
702
+ raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
703
+ end
704
+
705
+ s = if port
706
+ connect_server server, port, options[:bind_host]
707
+ else
708
+ connect_unix_server server
709
+ end
710
+
711
+ c = klass.new s, *args
712
+ @conns[s] = c
713
+ block_given? and yield c
714
+ c
715
+ end
716
+
717
+ # EventMachine::attach registers a given file descriptor or IO object with the eventloop
718
+ #
719
+ # If the handler provided has the functions notify_readable or notify_writable defined,
720
+ # EventMachine will not read or write from the socket, and instead fire the corresponding
721
+ # callback on the handler.
722
+ #
723
+ # To detach the file descriptor, use EventMachine::Connection#detach
724
+ #
725
+ # === Usage Example
726
+ #
727
+ # module SimpleHttpClient
728
+ # def initialize sock
729
+ # @sock = sock
730
+ # end
731
+ #
732
+ # def notify_readable
733
+ # header = @sock.readline
734
+ #
735
+ # if header == "\r\n"
736
+ # # detach returns the file descriptor number (fd == @sock.fileno)
737
+ # fd = detach
738
+ # end
739
+ # rescue EOFError
740
+ # detach
741
+ # end
742
+ #
743
+ # def unbind
744
+ # EM.next_tick do
745
+ # # socket is detached from the eventloop, but still open
746
+ # data = @sock.read
747
+ # end
748
+ # end
749
+ # end
750
+ #
751
+ # EM.run{
752
+ # $sock = TCPSocket.new('site.com', 80)
753
+ # $sock.write("GET / HTTP/1.0\r\n\r\n")
754
+ # EM.attach $sock, SimpleHttpClient, $sock
755
+ # }
756
+ #
757
+ #--
758
+ # Thanks to Riham Aldakkak (eSpace Technologies) for the initial patch
759
+ def EventMachine::attach io, handler=nil, *args
760
+ klass = if (handler and handler.is_a?(Class))
761
+ raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
762
+ handler
763
+ else
764
+ Class.new( Connection ) {handler and include handler}
765
+ end
766
+
767
+ arity = klass.instance_method(:initialize).arity
768
+ expected = arity >= 0 ? arity : -(arity + 1)
769
+ if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
770
+ raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
771
+ end
772
+
773
+ readmode = klass.public_instance_methods.any?{|m| m.to_sym == :notify_readable }
774
+ writemode = klass.public_instance_methods.any?{|m| m.to_sym == :notify_writable }
775
+
776
+ s = attach_fd io.respond_to?(:fileno) ? io.fileno : io, readmode, writemode
777
+
778
+ c = klass.new s, *args
779
+ @conns[s] = c
780
+ block_given? and yield c
781
+ c
782
+ end
783
+
784
+ #--
785
+ # EXPERIMENTAL. DO NOT RELY ON THIS METHOD TO BE HERE IN THIS FORM, OR AT ALL.
786
+ # (03Nov06)
787
+ # Observe, the test for already-connected FAILS if we call a reconnect inside post_init,
788
+ # because we haven't set up the connection in @conns by that point.
789
+ # RESIST THE TEMPTATION to "fix" this problem by redefining the behavior of post_init.
790
+ #
791
+ # Changed 22Nov06: if called on an already-connected handler, just return the
792
+ # handler and do nothing more. Originally this condition raised an exception.
793
+ # We may want to change it yet again and call the block, if any.
794
+ #
795
+ def EventMachine::reconnect server, port, handler
796
+ raise "invalid handler" unless handler.respond_to?(:connection_completed)
797
+ #raise "still connected" if @conns.has_key?(handler.signature)
798
+ return handler if @conns.has_key?(handler.signature)
799
+ s = connect_server server, port
800
+ handler.signature = s
801
+ @conns[s] = handler
802
+ block_given? and yield handler
803
+ handler
804
+ end
805
+
806
+
807
+
808
+
809
+ # Make a connection to a Unix-domain socket. This is not implemented on Windows platforms.
810
+ # The parameter socketname is a String which identifies the Unix-domain socket you want
811
+ # to connect to. socketname is the name of a file on your local system, and in most cases
812
+ # is a fully-qualified path name. Make sure that your process has enough local permissions
813
+ # to open the Unix-domain socket.
814
+ # See also the documentation for #connect_server. This method behaves like #connect_server
815
+ # in all respects except for the fact that it connects to a local Unix-domain
816
+ # socket rather than a TCP socket.
817
+ # NOTE: this functionality will soon be subsumed into the #connect method. This method
818
+ # will still be supported as an alias.
819
+ #--
820
+ # For making connections to Unix-domain sockets.
821
+ # Eventually this has to get properly documented and unified with the TCP-connect methods.
822
+ # Note how nearly identical this is to EventMachine#connect
823
+ def EventMachine::connect_unix_domain socketname, *args, &blk
824
+ connect socketname, *args, &blk
825
+ end
826
+
827
+
828
+ # EventMachine#open_datagram_socket is for support of UDP-based
829
+ # protocols. Its usage is similar to that of EventMachine#start_server.
830
+ # It takes three parameters: an IP address (which must be valid
831
+ # on the machine which executes the method), a port number,
832
+ # and an optional Module name which will handle the data.
833
+ # This method will create a new UDP (datagram) socket and
834
+ # bind it to the address and port that you specify.
835
+ # The normal callbacks (see EventMachine#start_server) will
836
+ # be called as events of interest occur on the newly-created
837
+ # socket, but there are some differences in how they behave.
838
+ #
839
+ # Connection#receive_data will be called when a datagram packet
840
+ # is received on the socket, but unlike TCP sockets, the message
841
+ # boundaries of the received data will be respected. In other words,
842
+ # if the remote peer sent you a datagram of a particular size,
843
+ # you may rely on Connection#receive_data to give you the
844
+ # exact data in the packet, with the original data length.
845
+ # Also observe that Connection#receive_data may be called with a
846
+ # <i>zero-length</i> data payload, since empty datagrams are permitted
847
+ # in UDP.
848
+ #
849
+ # Connection#send_data is available with UDP packets as with TCP,
850
+ # but there is an important difference. Because UDP communications
851
+ # are <i>connectionless,</i> there is no implicit recipient for the packets you
852
+ # send. Ordinarily you must specify the recipient for each packet you send.
853
+ # However, EventMachine
854
+ # provides for the typical pattern of receiving a UDP datagram
855
+ # from a remote peer, performing some operation, and then sending
856
+ # one or more packets in response to the same remote peer.
857
+ # To support this model easily, just use Connection#send_data
858
+ # in the code that you supply for Connection:receive_data.
859
+ # EventMachine will
860
+ # provide an implicit return address for any messages sent to
861
+ # Connection#send_data within the context of a Connection#receive_data callback,
862
+ # and your response will automatically go to the correct remote peer.
863
+ # (TODO: Example-code needed!)
864
+ #
865
+ # Observe that the port number that you supply to EventMachine#open_datagram_socket
866
+ # may be zero. In this case, EventMachine will create a UDP socket
867
+ # that is bound to an <i>ephemeral</i> (not well-known) port.
868
+ # This is not appropriate for servers that must publish a well-known
869
+ # port to which remote peers may send datagrams. But it can be useful
870
+ # for clients that send datagrams to other servers.
871
+ # If you do this, you will receive any responses from the remote
872
+ # servers through the normal Connection#receive_data callback.
873
+ # Observe that you will probably have issues with firewalls blocking
874
+ # the ephemeral port numbers, so this technique is most appropriate for LANs.
875
+ # (TODO: Need an example!)
876
+ #
877
+ # If you wish to send datagrams to arbitrary remote peers (not
878
+ # necessarily ones that have sent data to which you are responding),
879
+ # then see Connection#send_datagram.
880
+ #
881
+ # DO NOT call send_data from a datagram socket
882
+ # outside of a #receive_data method. Use #send_datagram. If you do use #send_data
883
+ # outside of a #receive_data method, you'll get a confusing error
884
+ # because there is no "peer," as #send_data requires. (Inside of #receive_data,
885
+ # #send_data "fakes" the peer as described above.)
886
+ #
887
+ #--
888
+ # Replaced the implementation on 01Oct06. Thanks to Tobias Gustafsson for pointing
889
+ # out that this originally did not take a class but only a module.
890
+ #
891
+ def self::open_datagram_socket address, port, handler=nil, *args
892
+ klass = if (handler and handler.is_a?(Class))
893
+ raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
894
+ handler
895
+ else
896
+ Class.new( Connection ) {handler and include handler}
897
+ end
898
+
899
+ arity = klass.instance_method(:initialize).arity
900
+ expected = arity >= 0 ? arity : -(arity + 1)
901
+ if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
902
+ raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
903
+ end
904
+
905
+ s = open_udp_socket address, port
906
+ c = klass.new s, *args
907
+ @conns[s] = c
908
+ block_given? and yield c
909
+ c
910
+ end
911
+
912
+
913
+ # For advanced users. This function sets the default timer granularity, which by default is
914
+ # slightly smaller than 100 milliseconds. Call this function to set a higher or lower granularity.
915
+ # The function affects the behavior of #add_timer and #add_periodic_timer. Most applications
916
+ # will not need to call this function.
917
+ #
918
+ # The argument is a number of milliseconds. Avoid setting the quantum to very low values because
919
+ # that may reduce performance under some extreme conditions. We recommend that you not set a quantum
920
+ # lower than 10.
921
+ #
922
+ # You may only call this function while an EventMachine loop is running (that is, after a call to
923
+ # EventMachine#run and before a subsequent call to EventMachine#stop).
924
+ #
925
+ def self::set_quantum mills
926
+ set_timer_quantum mills.to_i
927
+ end
928
+
929
+ # Sets the maximum number of timers and periodic timers that may be outstanding at any
930
+ # given time. You only need to call #set_max_timers if you need more than the default
931
+ # number of timers, which on most platforms is 1000.
932
+ # Call this method before calling EventMachine#run.
933
+ #
934
+ def self::set_max_timers ct
935
+ set_max_timer_count ct
936
+ end
937
+
938
+ # Gets the current maximum number of allowed timers
939
+ #
940
+ def self::get_max_timers
941
+ get_max_timer_count
942
+ end
943
+
944
+ # Returns the total number of connections (file descriptors) currently held by the reactor.
945
+ # Note that a tick must pass after the 'initiation' of a connection for this number to increment.
946
+ #
947
+ # For example, $count will be 0 in this case:
948
+ #
949
+ # EM.run {
950
+ # EM.connect("rubyeventmachine.com", 80)
951
+ # $count = EM.connection_count
952
+ # }
953
+ #
954
+ # In this example, $count will be 1 since the connection has been established in the next loop of the reactor.
955
+ #
956
+ # EM.run {
957
+ # EM.connect("rubyeventmachine.com", 80)
958
+ # EM.next_tick {
959
+ # $count = EM.connection_count
960
+ # }
961
+ # }
962
+ #
963
+ def self::connection_count
964
+ self::get_connection_count
965
+ end
966
+
967
+ #--
968
+ # The is the responder for the loopback-signalled event.
969
+ # It can be fired either by code running on a separate thread (EM#defer) or on
970
+ # the main thread (EM#next_tick).
971
+ # It will often happen that a next_tick handler will reschedule itself. We
972
+ # consume a copy of the tick queue so that tick events scheduled by tick events
973
+ # have to wait for the next pass through the reactor core.
974
+ #
975
+ def self::run_deferred_callbacks # :nodoc:
976
+ until (@resultqueue ||= []).empty?
977
+ result,cback = @resultqueue.pop
978
+ cback.call result if cback
979
+ end
980
+
981
+ @next_tick_queue ||= []
982
+ if (l = @next_tick_queue.length) > 0
983
+ l.times {|i| @next_tick_queue[i].call}
984
+ @next_tick_queue.slice!( 0...l )
985
+ end
986
+
987
+ =begin
988
+ (@next_tick_queue ||= []).length.times {
989
+ cback=@next_tick_queue.pop and cback.call
990
+ }
991
+ =end
992
+ =begin
993
+ if (@next_tick_queue ||= []) and @next_tick_queue.length > 0
994
+ ary = @next_tick_queue.dup
995
+ @next_tick_queue.clear
996
+ until ary.empty?
997
+ cback=ary.pop and cback.call
998
+ end
999
+ end
1000
+ =end
1001
+ end
1002
+
1003
+
1004
+ # #defer is for integrating blocking operations into EventMachine's control flow.
1005
+ # Call #defer with one or two blocks, as shown below (the second block is <i>optional</i>):
1006
+ #
1007
+ # operation = proc {
1008
+ # # perform a long-running operation here, such as a database query.
1009
+ # "result" # as usual, the last expression evaluated in the block will be the return value.
1010
+ # }
1011
+ # callback = proc {|result|
1012
+ # # do something with result here, such as send it back to a network client.
1013
+ # }
1014
+ #
1015
+ # EventMachine.defer( operation, callback )
1016
+ #
1017
+ # The action of #defer is to take the block specified in the first parameter (the "operation")
1018
+ # and schedule it for asynchronous execution on an internal thread pool maintained by EventMachine.
1019
+ # When the operation completes, it will pass the result computed by the block (if any)
1020
+ # back to the EventMachine reactor. Then, EventMachine calls the block specified in the
1021
+ # second parameter to #defer (the "callback"), as part of its normal, synchronous
1022
+ # event handling loop. The result computed by the operation block is passed as a parameter
1023
+ # to the callback. You may omit the callback parameter if you don't need to execute any code
1024
+ # after the operation completes.
1025
+ #
1026
+ # <i>Caveats:</i>
1027
+ # Note carefully that the code in your deferred operation will be executed on a separate
1028
+ # thread from the main EventMachine processing and all other Ruby threads that may exist in
1029
+ # your program. Also, multiple deferred operations may be running at once! Therefore, you
1030
+ # are responsible for ensuring that your operation code is threadsafe. [Need more explanation
1031
+ # and examples.]
1032
+ # Don't write a deferred operation that will block forever. If so, the current implementation will
1033
+ # not detect the problem, and the thread will never be returned to the pool. EventMachine limits
1034
+ # the number of threads in its pool, so if you do this enough times, your subsequent deferred
1035
+ # operations won't get a chance to run. [We might put in a timer to detect this problem.]
1036
+ #
1037
+ #--
1038
+ # OBSERVE that #next_tick hacks into this mechanism, so don't make any changes here
1039
+ # without syncing there.
1040
+ #
1041
+ # Running with $VERBOSE set to true gives a warning unless all ivars are defined when
1042
+ # they appear in rvalues. But we DON'T ever want to initialize @threadqueue unless we
1043
+ # need it, because the Ruby threads are so heavyweight. We end up with this bizarre
1044
+ # way of initializing @threadqueue because EventMachine is a Module, not a Class, and
1045
+ # has no constructor.
1046
+ #
1047
+ def self::defer op = nil, callback = nil, &blk
1048
+ unless @threadpool
1049
+ require 'thread'
1050
+ @threadpool = []
1051
+ @threadqueue = Queue.new
1052
+ @resultqueue = Queue.new
1053
+ spawn_threadpool
1054
+ end
1055
+
1056
+ @threadqueue << [op||blk,callback]
1057
+ end
1058
+
1059
+ def self.spawn_threadpool
1060
+ until @threadpool.size == 20
1061
+ thread = Thread.new do
1062
+ while true
1063
+ op, cback = *@threadqueue.pop
1064
+ result = op.call
1065
+ @resultqueue << [result, cback]
1066
+ EventMachine.signal_loopbreak
1067
+ end
1068
+ end
1069
+ @threadpool << thread
1070
+ end
1071
+ end
1072
+
1073
+
1074
+ # Schedules a proc for execution immediately after the next "turn" through the reactor
1075
+ # core. An advanced technique, this can be useful for improving memory management and/or
1076
+ # application responsiveness, especially when scheduling large amounts of data for
1077
+ # writing to a network connection. TODO, we need a FAQ entry on this subject.
1078
+ #
1079
+ # #next_tick takes either a single argument (which must be a Proc) or a block.
1080
+ # And I'm taking suggestions for a better name for this method.
1081
+ #--
1082
+ # This works by adding to the @resultqueue that's used for #defer.
1083
+ # The general idea is that next_tick is used when we want to give the reactor a chance
1084
+ # to let other operations run, either to balance the load out more evenly, or to let
1085
+ # outbound network buffers drain, or both. So we probably do NOT want to block, and
1086
+ # we probably do NOT want to be spinning any threads. A program that uses next_tick
1087
+ # but not #defer shouldn't suffer the penalty of having Ruby threads running. They're
1088
+ # extremely expensive even if they're just sleeping.
1089
+ #
1090
+ def self::next_tick pr=nil, &block
1091
+ raise "no argument or block given" unless ((pr && pr.respond_to?(:call)) or block)
1092
+ (@next_tick_queue ||= []) << ( pr || block )
1093
+ signal_loopbreak if reactor_running?
1094
+ =begin
1095
+ (@next_tick_procs ||= []) << (pr || block)
1096
+ if @next_tick_procs.length == 1
1097
+ add_timer(0) {
1098
+ @next_tick_procs.each {|t| t.call}
1099
+ @next_tick_procs.clear
1100
+ }
1101
+ end
1102
+ =end
1103
+ end
1104
+
1105
+ # A wrapper over the setuid system call. Particularly useful when opening a network
1106
+ # server on a privileged port because you can use this call to drop privileges
1107
+ # after opening the port. Also very useful after a call to #set_descriptor_table_size,
1108
+ # which generally requires that you start your process with root privileges.
1109
+ #
1110
+ # This method has no effective implementation on Windows or in the pure-Ruby
1111
+ # implementation of EventMachine.
1112
+ # Call #set_effective_user by passing it a string containing the effective name
1113
+ # of the user whose privilege-level your process should attain.
1114
+ # This method is intended for use in enforcing security requirements, consequently
1115
+ # it will throw a fatal error and end your program if it fails.
1116
+ #
1117
+ def self::set_effective_user username
1118
+ EventMachine::setuid_string username
1119
+ end
1120
+
1121
+
1122
+ # Sets the maximum number of file or socket descriptors that your process may open.
1123
+ # You can pass this method an integer specifying the new size of the descriptor table.
1124
+ # Returns the new descriptor-table size, which may be less than the number you
1125
+ # requested. If you call this method with no arguments, it will simply return
1126
+ # the current size of the descriptor table without attempting to change it.
1127
+ #
1128
+ # The new limit on open descriptors ONLY applies to sockets and other descriptors
1129
+ # that belong to EventMachine. It has NO EFFECT on the number of descriptors
1130
+ # you can create in ordinary Ruby code.
1131
+ #
1132
+ # Not available on all platforms. Increasing the number of descriptors beyond its
1133
+ # default limit usually requires superuser privileges. (See #set_effective_user
1134
+ # for a way to drop superuser privileges while your program is running.)
1135
+ #
1136
+ def self::set_descriptor_table_size n_descriptors=nil
1137
+ EventMachine::set_rlimit_nofile n_descriptors
1138
+ end
1139
+
1140
+
1141
+
1142
+ # Run an external process. This does not currently work on Windows.
1143
+ #
1144
+ # module RubyCounter
1145
+ # def post_init
1146
+ # # count up to 5
1147
+ # send_data "5\n"
1148
+ # end
1149
+ # def receive_data data
1150
+ # puts "ruby sent me: #{data}"
1151
+ # end
1152
+ # def unbind
1153
+ # puts "ruby died with exit status: #{get_status.exitstatus}"
1154
+ # end
1155
+ # end
1156
+ #
1157
+ # EM.run{
1158
+ # EM.popen("ruby -e' $stdout.sync = true; gets.to_i.times{ |i| puts i+1; sleep 1 } '", RubyCounter)
1159
+ # }
1160
+ #
1161
+ # Also see EM::DeferrableChildProcess and EM::system
1162
+ #--
1163
+ # At this moment, it's only available on Unix.
1164
+ # Perhaps misnamed since the underlying function uses socketpair and is full-duplex.
1165
+ #
1166
+ def self::popen cmd, handler=nil, *args
1167
+ klass = if (handler and handler.is_a?(Class))
1168
+ raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
1169
+ handler
1170
+ else
1171
+ Class.new( Connection ) {handler and include handler}
1172
+ end
1173
+
1174
+ w = Shellwords::shellwords( cmd )
1175
+ w.unshift( w.first ) if w.first
1176
+ s = invoke_popen( w )
1177
+ c = klass.new s, *args
1178
+ @conns[s] = c
1179
+ yield(c) if block_given?
1180
+ c
1181
+ end
1182
+
1183
+
1184
+ # Tells you whether the EventMachine reactor loop is currently running. Returns true or
1185
+ # false. Useful when writing libraries that want to run event-driven code, but may
1186
+ # be running in programs that are already event-driven. In such cases, if EventMachine#reactor_running?
1187
+ # returns false, your code can invoke EventMachine#run and run your application code inside
1188
+ # the block passed to that method. If EventMachine#reactor_running? returns true, just
1189
+ # execute your event-aware code.
1190
+ #
1191
+ # This method is necessary because calling EventMachine#run inside of another call to
1192
+ # EventMachine#run generates a fatal error.
1193
+ #
1194
+ def self::reactor_running?
1195
+ (@reactor_running || false)
1196
+ end
1197
+
1198
+
1199
+ # (Experimental)
1200
+ #
1201
+ #
1202
+ def EventMachine::open_keyboard handler=nil, *args
1203
+ klass = if (handler and handler.is_a?(Class))
1204
+ raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
1205
+ handler
1206
+ else
1207
+ Class.new( Connection ) {handler and include handler}
1208
+ end
1209
+
1210
+ arity = klass.instance_method(:initialize).arity
1211
+ expected = arity >= 0 ? arity : -(arity + 1)
1212
+ if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
1213
+ raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
1214
+ end
1215
+
1216
+ s = read_keyboard
1217
+ c = klass.new s, *args
1218
+ @conns[s] = c
1219
+ block_given? and yield c
1220
+ c
1221
+ end
1222
+
1223
+ # Catch-all for errors raised during event loop callbacks.
1224
+ #
1225
+ # EM.error_handler{ |e|
1226
+ # puts "Error raised during event loop: #{e.message}"
1227
+ # }
1228
+ #
1229
+ def EventMachine::error_handler cb = nil, &blk
1230
+ if cb or blk
1231
+ @error_handler = cb || blk
1232
+ elsif instance_variable_defined? :@error_handler
1233
+ remove_instance_variable :@error_handler
1234
+ end
1235
+ end
1236
+
1237
+ private
1238
+ def EventMachine::event_callback conn_binding, opcode, data
1239
+ #
1240
+ # Changed 27Dec07: Eliminated the hookable error handling.
1241
+ # No one was using it, and it degraded performance significantly.
1242
+ # It's in original_event_callback, which is dead code.
1243
+ #
1244
+ # Changed 25Jul08: Added a partial solution to the problem of exceptions
1245
+ # raised in user-written event-handlers. If such exceptions are not caught,
1246
+ # we must cause the reactor to stop, and then re-raise the exception.
1247
+ # Otherwise, the reactor doesn't stop and it's left on the call stack.
1248
+ # This is partial because we only added it to #unbind, where it's critical
1249
+ # (to keep unbind handlers from being re-entered when a stopping reactor
1250
+ # runs down open connections). It should go on the other calls to user
1251
+ # code, but the performance impact may be too large.
1252
+ #
1253
+ if opcode == ConnectionData
1254
+ c = @conns[conn_binding] or raise ConnectionNotBound, "received data #{data} for unknown signature: #{conn_binding}"
1255
+ c.receive_data data
1256
+ elsif opcode == ConnectionUnbound
1257
+ if c = @conns.delete( conn_binding )
1258
+ begin
1259
+ c.unbind
1260
+ rescue
1261
+ @wrapped_exception = $!
1262
+ stop
1263
+ end
1264
+ elsif c = @acceptors.delete( conn_binding )
1265
+ # no-op
1266
+ else
1267
+ raise ConnectionNotBound, "recieved ConnectionUnbound for an unknown signature: #{conn_binding}"
1268
+ end
1269
+ elsif opcode == ConnectionAccepted
1270
+ accep,args,blk = @acceptors[conn_binding]
1271
+ raise NoHandlerForAcceptedConnection unless accep
1272
+ c = accep.new data, *args
1273
+ @conns[data] = c
1274
+ blk and blk.call(c)
1275
+ c # (needed?)
1276
+ elsif opcode == TimerFired
1277
+ t = @timers.delete( data ) or raise UnknownTimerFired, "timer data: #{data}"
1278
+ t.call
1279
+ elsif opcode == ConnectionCompleted
1280
+ c = @conns[conn_binding] or raise ConnectionNotBound, "received ConnectionCompleted for unknown signature: #{conn_binding}"
1281
+ c.connection_completed
1282
+ elsif opcode == LoopbreakSignalled
1283
+ run_deferred_callbacks
1284
+ elsif opcode == ConnectionNotifyReadable
1285
+ c = @conns[conn_binding] or raise ConnectionNotBound
1286
+ c.notify_readable
1287
+ elsif opcode == ConnectionNotifyWritable
1288
+ c = @conns[conn_binding] or raise ConnectionNotBound
1289
+ c.notify_writable
1290
+ end
1291
+ end
1292
+
1293
+ private
1294
+ def EventMachine::original_event_callback conn_binding, opcode, data
1295
+ #
1296
+ # Added 03Oct07: Any code path that invokes user-written code must
1297
+ # wrap itself in a begin/rescue for RuntimeErrors, that calls the
1298
+ # user-overridable class method #handle_runtime_error.
1299
+ #
1300
+ if opcode == ConnectionData
1301
+ c = @conns[conn_binding] or raise ConnectionNotBound
1302
+ begin
1303
+ c.receive_data data
1304
+ rescue
1305
+ EventMachine.handle_runtime_error
1306
+ end
1307
+ elsif opcode == ConnectionUnbound
1308
+ if c = @conns.delete( conn_binding )
1309
+ begin
1310
+ c.unbind
1311
+ rescue
1312
+ EventMachine.handle_runtime_error
1313
+ end
1314
+ elsif c = @acceptors.delete( conn_binding )
1315
+ # no-op
1316
+ else
1317
+ raise ConnectionNotBound
1318
+ end
1319
+ elsif opcode == ConnectionAccepted
1320
+ accep,args,blk = @acceptors[conn_binding]
1321
+ raise NoHandlerForAcceptedConnection unless accep
1322
+ c = accep.new data, *args
1323
+ @conns[data] = c
1324
+ begin
1325
+ blk and blk.call(c)
1326
+ rescue
1327
+ EventMachine.handle_runtime_error
1328
+ end
1329
+ c # (needed?)
1330
+ elsif opcode == TimerFired
1331
+ t = @timers.delete( data ) or raise UnknownTimerFired
1332
+ begin
1333
+ t.call
1334
+ rescue
1335
+ EventMachine.handle_runtime_error
1336
+ end
1337
+ elsif opcode == ConnectionCompleted
1338
+ c = @conns[conn_binding] or raise ConnectionNotBound
1339
+ begin
1340
+ c.connection_completed
1341
+ rescue
1342
+ EventMachine.handle_runtime_error
1343
+ end
1344
+ elsif opcode == LoopbreakSignalled
1345
+ begin
1346
+ run_deferred_callbacks
1347
+ rescue
1348
+ EventMachine.handle_runtime_error
1349
+ end
1350
+ end
1351
+ end
1352
+
1353
+
1354
+ # Default handler for RuntimeErrors that are raised in user code.
1355
+ # The default behavior is to re-raise the error, which ends your program.
1356
+ # To override the default behavior, re-implement this method in your code.
1357
+ # For example:
1358
+ #
1359
+ # module EventMachine
1360
+ # def self.handle_runtime_error
1361
+ # $>.puts $!
1362
+ # end
1363
+ # end
1364
+ #
1365
+ #--
1366
+ # We need to ensure that any code path which invokes user code rescues RuntimeError
1367
+ # and calls this method. The obvious place to do that is in #event_callback,
1368
+ # but, scurrilously, it turns out that we need to be finer grained that that.
1369
+ # Periodic timers, in particular, wrap their invocations of user code inside
1370
+ # procs that do other stuff we can't not do, like schedule the next invocation.
1371
+ # This is a potential non-robustness, since we need to remember to hook in the
1372
+ # error handler whenever and wherever we change how user code is invoked.
1373
+ #
1374
+ def EventMachine::handle_runtime_error
1375
+ @runtime_error_hook ? @runtime_error_hook.call : raise
1376
+ end
1377
+
1378
+ # Sets a handler for RuntimeErrors that are raised in user code.
1379
+ # Pass a block with no parameters. You can also call this method without a block,
1380
+ # which restores the default behavior (see #handle_runtime_error).
1381
+ #
1382
+ def EventMachine::set_runtime_error_hook &blk
1383
+ @runtime_error_hook = blk
1384
+ end
1385
+
1386
+ # Documentation stub
1387
+ #--
1388
+ # This is a provisional implementation of a stream-oriented file access object.
1389
+ # We also experiment with wrapping up some better exception reporting.
1390
+ class << self
1391
+ def _open_file_for_writing filename, handler=nil
1392
+ klass = if (handler and handler.is_a?(Class))
1393
+ raise ArgumentError, 'must provide module or subclass of EventMachine::Connection' unless Connection > handler
1394
+ handler
1395
+ else
1396
+ Class.new( Connection ) {handler and include handler}
1397
+ end
1398
+
1399
+ s = _write_file filename
1400
+ c = klass.new s
1401
+ @conns[s] = c
1402
+ block_given? and yield c
1403
+ c
1404
+ end
1405
+ end
1406
+
1407
+
1408
+ # EventMachine::Connection is a class that is instantiated
1409
+ # by EventMachine's processing loop whenever a new connection
1410
+ # is created. (New connections can be either initiated locally
1411
+ # to a remote server or accepted locally from a remote client.)
1412
+ # When a Connection object is instantiated, it <i>mixes in</i>
1413
+ # the functionality contained in the user-defined module
1414
+ # specified in calls to EventMachine#connect or EventMachine#start_server.
1415
+ # User-defined handler modules may redefine any or all of the standard
1416
+ # methods defined here, as well as add arbitrary additional code
1417
+ # that will also be mixed in.
1418
+ #
1419
+ # EventMachine manages one object inherited from EventMachine::Connection
1420
+ # (and containing the mixed-in user code) for every network connection
1421
+ # that is active at any given time.
1422
+ # The event loop will automatically call methods on EventMachine::Connection
1423
+ # objects whenever specific events occur on the corresponding connections,
1424
+ # as described below.
1425
+ #
1426
+ # This class is never instantiated by user code, and does not publish an
1427
+ # initialize method. The instance methods of EventMachine::Connection
1428
+ # which may be called by the event loop are: post_init, receive_data,
1429
+ # and unbind. All of the other instance methods defined here are called
1430
+ # only by user code.
1431
+ #
1432
+ class Connection
1433
+ # EXPERIMENTAL. Added the reconnect methods, which may go away.
1434
+ attr_accessor :signature
1435
+
1436
+ # Override .new so subclasses don't have to call super and can ignore
1437
+ # connection-specific arguments
1438
+ #
1439
+ def self.new(sig, *args) #:nodoc:
1440
+ allocate.instance_eval do
1441
+ # Call a superclass's #initialize if it has one
1442
+ initialize(*args)
1443
+
1444
+ # Store signature and run #post_init
1445
+ @signature = sig
1446
+ associate_callback_target sig
1447
+ post_init
1448
+
1449
+ self
1450
+ end
1451
+ end
1452
+
1453
+ # Stubbed initialize so legacy superclasses can safely call super
1454
+ #
1455
+ def initialize(*args) #:nodoc:
1456
+ end
1457
+
1458
+ def associate_callback_target(sig) #:nodoc:
1459
+ # no-op for the time being, to match similar no-op in rubymain.cpp
1460
+ end
1461
+
1462
+ # EventMachine::Connection#post_init is called by the event loop
1463
+ # immediately after the network connection has been established,
1464
+ # and before resumption of the network loop.
1465
+ # This method is generally not called by user code, but is called automatically
1466
+ # by the event loop. The base-class implementation is a no-op.
1467
+ # This is a very good place to initialize instance variables that will
1468
+ # be used throughout the lifetime of the network connection.
1469
+ #
1470
+ def post_init
1471
+ end
1472
+
1473
+ # EventMachine::Connection#receive_data is called by the event loop
1474
+ # whenever data has been received by the network connection.
1475
+ # It is never called by user code.
1476
+ # receive_data is called with a single parameter, a String containing
1477
+ # the network protocol data, which may of course be binary. You will
1478
+ # generally redefine this method to perform your own processing of the incoming data.
1479
+ #
1480
+ # Here's a key point which is essential to understanding the event-driven
1481
+ # programming model: <i>EventMachine knows absolutely nothing about the protocol
1482
+ # which your code implements.</i> You must not make any assumptions about
1483
+ # the size of the incoming data packets, or about their alignment on any
1484
+ # particular intra-message or PDU boundaries (such as line breaks).
1485
+ # receive_data can and will send you arbitrary chunks of data, with the
1486
+ # only guarantee being that the data is presented to your code in the order
1487
+ # it was collected from the network. Don't even assume that the chunks of
1488
+ # data will correspond to network packets, as EventMachine can and will coalesce
1489
+ # several incoming packets into one, to improve performance. The implication for your
1490
+ # code is that you generally will need to implement some kind of a state machine
1491
+ # in your redefined implementation of receive_data. For a better understanding
1492
+ # of this, read through the examples of specific protocol handlers given
1493
+ # elsewhere in this package. (STUB, WE MUST ADD THESE!)
1494
+ #
1495
+ # The base-class implementation of receive_data (which will be invoked if
1496
+ # you don't redefine it) simply prints the size of each incoming data packet
1497
+ # to stdout.
1498
+ #
1499
+ def receive_data data
1500
+ puts "............>>>#{data.length}"
1501
+ end
1502
+
1503
+ # #ssl_handshake_completed is called by EventMachine when the SSL/TLS handshake has
1504
+ # been completed, as a result of calling #start_tls to initiate SSL/TLS on the connection.
1505
+ #
1506
+ # This callback exists because #post_init and #connection_completed are <b>not</b> reliable
1507
+ # for indicating when an SSL/TLS connection is ready to have it's certificate queried for.
1508
+ #
1509
+ # See #get_peer_cert for application and example.
1510
+ def ssl_handshake_completed
1511
+ end
1512
+
1513
+ # EventMachine::Connection#unbind is called by the framework whenever a connection
1514
+ # (either a server or client connection) is closed. The close can occur because
1515
+ # your code intentionally closes it (see close_connection and close_connection_after_writing),
1516
+ # because the remote peer closed the connection, or because of a network error.
1517
+ # You may not assume that the network connection is still open and able to send or
1518
+ # receive data when the callback to unbind is made. This is intended only to give
1519
+ # you a chance to clean up associations your code may have made to the connection
1520
+ # object while it was open.
1521
+ #
1522
+ def unbind
1523
+ end
1524
+
1525
+ # EventMachine::Connection#close_connection is called only by user code, and never
1526
+ # by the event loop. You may call this method against a connection object in any
1527
+ # callback handler, whether or not the callback was made against the connection
1528
+ # you want to close. close_connection <i>schedules</i> the connection to be closed
1529
+ # at the next available opportunity within the event loop. You may not assume that
1530
+ # the connection is closed when close_connection returns. In particular, the framework
1531
+ # will callback the unbind method for the particular connection at a point shortly
1532
+ # after you call close_connection. You may assume that the unbind callback will
1533
+ # take place sometime after your call to close_connection completes. In other words,
1534
+ # the unbind callback will not re-enter your code "inside" of your call to close_connection.
1535
+ # However, it's not guaranteed that a future version of EventMachine will not change
1536
+ # this behavior.
1537
+ #
1538
+ # close_connection will <i>silently discard</i> any outbound data which you have
1539
+ # sent to the connection using EventMachine::Connection#send_data but which has not
1540
+ # yet been sent across the network. If you want to avoid this behavior, use
1541
+ # EventMachine::Connection#close_connection_after_writing.
1542
+ #
1543
+ def close_connection after_writing = false
1544
+ EventMachine::close_connection @signature, after_writing
1545
+ end
1546
+
1547
+ # EventMachine::Connection#detach will remove the given connection from the event loop.
1548
+ # The connection's socket remains open and its file descriptor number is returned
1549
+ def detach
1550
+ EventMachine::detach_fd @signature
1551
+ end
1552
+
1553
+ # EventMachine::Connection#close_connection_after_writing is a variant of close_connection.
1554
+ # All of the descriptive comments given for close_connection also apply to
1555
+ # close_connection_after_writing, <i>with one exception:</i> If the connection has
1556
+ # outbound data sent using send_dat but which has not yet been sent across the network,
1557
+ # close_connection_after_writing will schedule the connection to be closed <i>after</i>
1558
+ # all of the outbound data has been safely written to the remote peer.
1559
+ #
1560
+ # Depending on the amount of outgoing data and the speed of the network,
1561
+ # considerable time may elapse between your call to close_connection_after_writing
1562
+ # and the actual closing of the socket (at which time the unbind callback will be called
1563
+ # by the event loop). During this time, you <i>may not</i> call send_data to transmit
1564
+ # additional data (that is, the connection is closed for further writes). In very
1565
+ # rare cases, you may experience a receive_data callback after your call to close_connection_after_writing,
1566
+ # depending on whether incoming data was in the process of being received on the connection
1567
+ # at the moment when you called close_connection_after_writing. Your protocol handler must
1568
+ # be prepared to properly deal with such data (probably by ignoring it).
1569
+ #
1570
+ def close_connection_after_writing
1571
+ close_connection true
1572
+ end
1573
+
1574
+ # EventMachine::Connection#send_data is only called by user code, never by
1575
+ # the event loop. You call this method to send data to the remote end of the
1576
+ # network connection. send_data is called with a single String argument, which
1577
+ # may of course contain binary data. You can call send_data any number of times.
1578
+ # send_data is an instance method of an object derived from EventMachine::Connection
1579
+ # and containing your mixed-in handler code), so if you call it without qualification
1580
+ # within a callback function, the data will be sent to the same network connection
1581
+ # that generated the callback. Calling self.send_data is exactly equivalent.
1582
+ #
1583
+ # You can also call send_data to write to a connection <i>other than the one
1584
+ # whose callback you are calling send_data from.</i> This is done by recording
1585
+ # the value of the connection in any callback function (the value self), in any
1586
+ # variable visible to other callback invocations on the same or different
1587
+ # connection objects. (Need an example to make that clear.)
1588
+ #
1589
+ def send_data data
1590
+ size = data.bytesize if data.respond_to?(:bytesize)
1591
+ size ||= data.size
1592
+ EventMachine::send_data @signature, data, size
1593
+ end
1594
+
1595
+ # Returns true if the connection is in an error state, false otherwise.
1596
+ # In general, you can detect the occurrence of communication errors or unexpected
1597
+ # disconnection by the remote peer by handing the #unbind method. In some cases, however,
1598
+ # it's useful to check the status of the connection using #error? before attempting to send data.
1599
+ # This function is synchronous: it will return immediately without blocking.
1600
+ #
1601
+ #
1602
+ def error?
1603
+ EventMachine::report_connection_error_status(@signature) != 0
1604
+ end
1605
+
1606
+ # #connection_completed is called by the event loop when a remote TCP connection
1607
+ # attempt completes successfully. You can expect to get this notification after calls
1608
+ # to EventMachine#connect. Remember that EventMachine makes remote connections
1609
+ # asynchronously, just as with any other kind of network event. #connection_completed
1610
+ # is intended primarily to assist with network diagnostics. For normal protocol
1611
+ # handling, use #post_init to perform initial work on a new connection (such as
1612
+ # send an initial set of data).
1613
+ # #post_init will always be called. #connection_completed will only be called in case
1614
+ # of a successful completion. A connection-attempt which fails will receive a call
1615
+ # to #unbind after the failure.
1616
+ def connection_completed
1617
+ end
1618
+
1619
+ # Call #start_tls at any point to initiate TLS encryption on connected streams.
1620
+ # The method is smart enough to know whether it should perform a server-side
1621
+ # or a client-side handshake. An appropriate place to call #start_tls is in
1622
+ # your redefined #post_init method, or in the #connection_completed handler for
1623
+ # an outbound connection.
1624
+ #
1625
+ # #start_tls takes an optional parameter hash that allows you to specify certificate
1626
+ # and other options to be used with this Connection object. Here are the currently-supported
1627
+ # options:
1628
+ # :cert_chain_file : takes a String, which is interpreted as the name of a readable file in the
1629
+ # local filesystem. The file is expected to contain a chain of X509 certificates in
1630
+ # PEM format, with the most-resolved certificate at the top of the file, successive
1631
+ # intermediate certs in the middle, and the root (or CA) cert at the bottom.
1632
+ #
1633
+ # :private_key_file : tales a String, which is interpreted as the name of a readable file in the
1634
+ # local filesystem. The file must contain a private key in PEM format.
1635
+ #
1636
+ #--
1637
+ # TODO: support passing an encryption parameter, which can be string or Proc, to get a passphrase
1638
+ # for encrypted private keys.
1639
+ # TODO: support passing key material via raw strings or Procs that return strings instead of
1640
+ # just filenames.
1641
+ # What will get nasty is whether we have to define a location for storing this stuff as files.
1642
+ # In general, the OpenSSL interfaces for dealing with certs and keys in files are much better
1643
+ # behaved than the ones for raw chunks of memory.
1644
+ #
1645
+ def start_tls args={}
1646
+ priv_key, cert_chain = args.values_at(:private_key_file, :cert_chain_file)
1647
+
1648
+ [priv_key, cert_chain].each do |file|
1649
+ next if file.nil? or file.empty?
1650
+ raise FileNotFoundException,
1651
+ "Could not find #{file} for start_tls" unless File.exists? file
1652
+ end
1653
+
1654
+ EventMachine::set_tls_parms(@signature, priv_key || '', cert_chain || '')
1655
+
1656
+ EventMachine::start_tls @signature
1657
+ end
1658
+
1659
+ # If SSL/TLS is active on the connection, #get_peer_cert returns the remote X509 certificate
1660
+ # as a String, in the popular PEM format. This can then be used for arbitrary validation
1661
+ # of a peer's certificate in your code.
1662
+ #
1663
+ # This should be called in/after the #ssl_handshake_completed callback, which indicates
1664
+ # that SSL/TLS is active. Using this callback is important, because the certificate may not
1665
+ # be available until the time it is executed. Using #post_init or #connection_completed is
1666
+ # not adequate, because the SSL handshake may still be taking place.
1667
+ #
1668
+ # #get_peer_cert will return <b>nil</b> if:
1669
+ #
1670
+ # * EventMachine is not built with OpenSSL support
1671
+ # * SSL/TLS is not active on the connection
1672
+ # * SSL/TLS handshake is not yet complete
1673
+ # * Remote peer for any other reason has not presented a certificate
1674
+ #
1675
+ # === Example:
1676
+ #
1677
+ # module Handler
1678
+ #
1679
+ # def post_init
1680
+ # puts "Starting TLS"
1681
+ # start_tls
1682
+ # end
1683
+ #
1684
+ # def ssl_handshake_completed
1685
+ # puts get_peer_cert
1686
+ # close_connection
1687
+ # end
1688
+ #
1689
+ # def unbind
1690
+ # EventMachine::stop_event_loop
1691
+ # end
1692
+ #
1693
+ # end
1694
+ #
1695
+ # EM.run {
1696
+ # EventMachine::connect "mail.google.com", 443, Handler
1697
+ # }
1698
+ #
1699
+ # Output:
1700
+ # -----BEGIN CERTIFICATE-----
1701
+ # MIIDIjCCAougAwIBAgIQbldpChBPqv+BdPg4iwgN8TANBgkqhkiG9w0BAQUFADBM
1702
+ # MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkg
1703
+ # THRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0wODA1MDIxNjMyNTRaFw0w
1704
+ # OTA1MDIxNjMyNTRaMGkxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlh
1705
+ # MRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgSW5jMRgw
1706
+ # FgYDVQQDEw9tYWlsLmdvb2dsZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ
1707
+ # AoGBALlkxdh2QXegdElukCSOV2+8PKiONIS+8Tu9K7MQsYpqtLNC860zwOPQ2NLI
1708
+ # 3Zp4jwuXVTrtzGuiqf5Jioh35Ig3CqDXtLyZoypjZUQcq4mlLzHlhIQ4EhSjDmA7
1709
+ # Ffw9y3ckSOQgdBQWNLbquHh9AbEUjmhkrYxIqKXeCnRKhv6nAgMBAAGjgecwgeQw
1710
+ # KAYDVR0lBCEwHwYIKwYBBQUHAwEGCCsGAQUFBwMCBglghkgBhvhCBAEwNgYDVR0f
1711
+ # BC8wLTAroCmgJ4YlaHR0cDovL2NybC50aGF3dGUuY29tL1RoYXd0ZVNHQ0NBLmNy
1712
+ # bDByBggrBgEFBQcBAQRmMGQwIgYIKwYBBQUHMAGGFmh0dHA6Ly9vY3NwLnRoYXd0
1713
+ # ZS5jb20wPgYIKwYBBQUHMAKGMmh0dHA6Ly93d3cudGhhd3RlLmNvbS9yZXBvc2l0
1714
+ # b3J5L1RoYXd0ZV9TR0NfQ0EuY3J0MAwGA1UdEwEB/wQCMAAwDQYJKoZIhvcNAQEF
1715
+ # BQADgYEAsRwpLg1dgCR1gYDK185MFGukXMeQFUvhGqF8eT/CjpdvezyKVuz84gSu
1716
+ # 6ccMXgcPQZGQN/F4Xug+Q01eccJjRSVfdvR5qwpqCj+6BFl5oiKDBsveSkrmL5dz
1717
+ # s2bn7TdTSYKcLeBkjXxDLHGBqLJ6TNCJ3c4/cbbG5JhGvoema94=
1718
+ # -----END CERTIFICATE-----
1719
+ #
1720
+ # You can do whatever you want with the certificate String, such as load it
1721
+ # as a certificate object using the OpenSSL library, and check it's fields.
1722
+ def get_peer_cert
1723
+ EventMachine::get_peer_cert @signature
1724
+ end
1725
+
1726
+
1727
+ # send_datagram is for sending UDP messages.
1728
+ # This method may be called from any Connection object that refers
1729
+ # to an open datagram socket (see EventMachine#open_datagram_socket).
1730
+ # The method sends a UDP (datagram) packet containing the data you specify,
1731
+ # to a remote peer specified by the IP address and port that you give
1732
+ # as parameters to the method.
1733
+ # Observe that you may send a zero-length packet (empty string).
1734
+ # However, you may not send an arbitrarily-large data packet because
1735
+ # your operating system will enforce a platform-specific limit on
1736
+ # the size of the outbound packet. (Your kernel
1737
+ # will respond in a platform-specific way if you send an overlarge
1738
+ # packet: some will send a truncated packet, some will complain, and
1739
+ # some will silently drop your request).
1740
+ # On LANs, it's usually OK to send datagrams up to about 4000 bytes in length,
1741
+ # but to be really safe, send messages smaller than the Ethernet-packet
1742
+ # size (typically about 1400 bytes). Some very restrictive WANs
1743
+ # will either drop or truncate packets larger than about 500 bytes.
1744
+ #--
1745
+ # Added the Integer wrapper around the port parameter per suggestion by
1746
+ # Matthieu Riou, after he passed a String and spent hours tearing his hair out.
1747
+ #
1748
+ def send_datagram data, recipient_address, recipient_port
1749
+ data = data.to_s
1750
+ EventMachine::send_datagram @signature, data, data.length, recipient_address, Integer(recipient_port)
1751
+ end
1752
+
1753
+
1754
+ # #get_peername is used with stream-connections to obtain the identity
1755
+ # of the remotely-connected peer. If a peername is available, this method
1756
+ # returns a sockaddr structure. The method returns nil if no peername is available.
1757
+ # You can use Socket#unpack_sockaddr_in and its variants to obtain the
1758
+ # values contained in the peername structure returned from #get_peername.
1759
+ def get_peername
1760
+ EventMachine::get_peername @signature
1761
+ end
1762
+
1763
+ # #get_sockname is used with stream-connections to obtain the identity
1764
+ # of the local side of the connection. If a local name is available, this method
1765
+ # returns a sockaddr structure. The method returns nil if no local name is available.
1766
+ # You can use Socket#unpack_sockaddr_in and its variants to obtain the
1767
+ # values contained in the local-name structure returned from #get_sockname.
1768
+ def get_sockname
1769
+ EventMachine::get_sockname @signature
1770
+ end
1771
+
1772
+ # Returns the PID (kernel process identifier) of a subprocess
1773
+ # associated with this Connection object. For use with EventMachine#popen
1774
+ # and similar methods. Returns nil when there is no meaningful subprocess.
1775
+ #--
1776
+ #
1777
+ def get_pid
1778
+ EventMachine::get_subprocess_pid @signature
1779
+ end
1780
+
1781
+ # Returns a subprocess exit status. Only useful for #popen. Call it in your
1782
+ # #unbind handler.
1783
+ #
1784
+ def get_status
1785
+ EventMachine::get_subprocess_status @signature
1786
+ end
1787
+
1788
+ # comm_inactivity_timeout returns the current value (in seconds) of the inactivity-timeout
1789
+ # property of network-connection and datagram-socket objects. A nonzero value
1790
+ # indicates that the connection or socket will automatically be closed if no read or write
1791
+ # activity takes place for at least that number of seconds.
1792
+ # A zero value (the default) specifies that no automatic timeout will take place.
1793
+ def comm_inactivity_timeout
1794
+ EventMachine::get_comm_inactivity_timeout @signature
1795
+ end
1796
+
1797
+ # Alias for #set_comm_inactivity_timeout.
1798
+ def comm_inactivity_timeout= value
1799
+ self.send :set_comm_inactivity_timeout, value
1800
+ end
1801
+
1802
+ # comm_inactivity_timeout= allows you to set the inactivity-timeout property for
1803
+ # a network connection or datagram socket. Specify a non-negative numeric value in seconds.
1804
+ # If the value is greater than zero, the connection or socket will automatically be closed
1805
+ # if no read or write activity takes place for at least that number of seconds.
1806
+ # Specify a value of zero to indicate that no automatic timeout should take place.
1807
+ # Zero is the default value.
1808
+ def set_comm_inactivity_timeout value
1809
+ EventMachine::set_comm_inactivity_timeout @signature, value
1810
+ end
1811
+
1812
+ #--
1813
+ # EXPERIMENTAL. DO NOT RELY ON THIS METHOD TO REMAIN SUPPORTED.
1814
+ # (03Nov06)
1815
+ def reconnect server, port
1816
+ EventMachine::reconnect server, port, self
1817
+ end
1818
+
1819
+
1820
+ # Like EventMachine::Connection#send_data, this sends data to the remote end of
1821
+ # the network connection. EventMachine::Connection@send_file_data takes a
1822
+ # filename as an argument, though, and sends the contents of the file, in one
1823
+ # chunk. Contributed by Kirk Haines.
1824
+ #
1825
+ def send_file_data filename
1826
+ EventMachine::send_file_data @signature, filename
1827
+ end
1828
+
1829
+ # Open a file on the filesystem and send it to the remote peer. This returns an
1830
+ # object of type EventMachine::Deferrable. The object's callbacks will be executed
1831
+ # on the reactor main thread when the file has been completely scheduled for
1832
+ # transmission to the remote peer. Its errbacks will be called in case of an error
1833
+ # (such as file-not-found). #stream_file_data employs various strategems to achieve
1834
+ # the fastest possible performance, balanced against minimum consumption of memory.
1835
+ #
1836
+ # You can control the behavior of #stream_file_data with the optional arguments parameter.
1837
+ # Currently-supported arguments are:
1838
+ # :http_chunks, a boolean flag which defaults false. If true, this flag streams the
1839
+ # file data in a format compatible with the HTTP chunked-transfer encoding.
1840
+ #
1841
+ # Warning: this feature has an implicit dependency on an outboard extension,
1842
+ # evma_fastfilereader. You must install this extension in order to use #stream_file_data
1843
+ # with files larger than a certain size (currently 8192 bytes).
1844
+ #
1845
+ def stream_file_data filename, args={}
1846
+ EventMachine::FileStreamer.new( self, filename, args )
1847
+ end
1848
+
1849
+
1850
+ # TODO, document this
1851
+ #
1852
+ #
1853
+ class EventMachine::PeriodicTimer
1854
+ attr_accessor :interval
1855
+ def initialize *args, &block
1856
+ @interval = args.shift
1857
+ @code = args.shift || block
1858
+ schedule
1859
+ end
1860
+ def schedule
1861
+ EventMachine::add_timer @interval, proc {self.fire}
1862
+ end
1863
+ def fire
1864
+ unless @cancelled
1865
+ @code.call
1866
+ schedule
1867
+ end
1868
+ end
1869
+ def cancel
1870
+ @cancelled = true
1871
+ end
1872
+ end
1873
+
1874
+ # TODO, document this
1875
+ #
1876
+ #
1877
+ class EventMachine::Timer
1878
+ def initialize *args, &block
1879
+ @signature = EventMachine::add_timer(*args, &block)
1880
+ end
1881
+ def cancel
1882
+ EventMachine.send :cancel_timer, @signature
1883
+ end
1884
+ end
1885
+
1886
+ end
1887
+
1888
+ # Is inside of protocols/ but not in the namespace?
1889
+ require 'protocols/buftok'
1890
+
1891
+ module Protocols
1892
+ # In this module, we define standard protocol implementations.
1893
+ # They get included from separate source files.
1894
+
1895
+ # TODO / XXX: We're munging the LOAD_PATH!
1896
+ # A good citizen would use eventmachine/protocols/tcptest.
1897
+ # TODO : various autotools are completely useless with the lack of naming
1898
+ # convention, we need to correct that!
1899
+ autoload :TcpConnectTester, 'protocols/tcptest'
1900
+ autoload :HttpClient, 'protocols/httpclient'
1901
+ autoload :LineAndTextProtocol, 'protocols/line_and_text'
1902
+ autoload :HeaderAndContentProtocol, 'protocols/header_and_content'
1903
+ autoload :LineText2, 'protocols/linetext2'
1904
+ autoload :HttpClient2, 'protocols/httpcli2'
1905
+ autoload :Stomp, 'protocols/stomp'
1906
+ autoload :SmtpClient, 'protocols/smtpclient'
1907
+ autoload :SmtpServer, 'protocols/smtpserver'
1908
+ autoload :SASLauth, 'protocols/saslauth'
1909
+ autoload :Memcache, 'protocols/memcache'
1910
+
1911
+ #require 'protocols/postgres' UNCOMMENT THIS LINE WHEN THE POSTGRES CODE IS READY FOR PRIME TIME.
1912
+ end
1913
+
1914
+ end # module EventMachine
1915
+
1916
+ # Save everyone some typing.
1917
+ EM = EventMachine
1918
+ EM::P = EventMachine::Protocols
1919
+
1920
+ require 'em/processes'