Browse files

migrated version_0 to trunk

git-svn-id: 2c60eb90-88cd-488e-9351-1edcb1d70dbc
  • Loading branch information...
1 parent 659d385 commit 9d5b7ca46047a2c73a5ccfa8f5cf2e8301703f33 blackhedd committed Jan 4, 2008
Showing with 22,575 additions and 0 deletions.
  1. +60 −0 COPYING
  2. +106 −0 ChangeLog
  3. +138 −0 DEFERRABLES
  4. +141 −0 EPOLL
  5. +281 −0 GNU
  6. +15 −0 INSTALL
  7. +38 −0 KEYBOARD
  8. +25 −0 LEGAL
  10. +77 −0 PURE_RUBY
  11. +74 −0 README
  12. +96 −0 RELEASE_NOTES
  13. +467 −0 Rakefile
  14. +9 −0 SMTP
  16. +10 −0 TODO
  17. +126 −0 ext/binder.cpp
  18. +48 −0 ext/binder.h
  19. +511 −0 ext/cmain.cpp
  20. +172 −0 ext/cplusplus.cpp
  21. +1,412 −0 ext/ed.cpp
  22. +348 −0 ext/ed.h
  23. +1,676 −0 ext/em.cpp
  24. +167 −0 ext/em.h
  25. +300 −0 ext/emwin.cpp
  26. +94 −0 ext/emwin.h
  27. +26 −0 ext/epoll.cpp
  28. +25 −0 ext/epoll.h
  29. +82 −0 ext/eventmachine.h
  30. +94 −0 ext/eventmachine_cpp.h
  31. +185 −0 ext/extconf.rb
  32. +94 −0 ext/files.cpp
  33. +65 −0 ext/files.h
  34. +368 −0 ext/kb.cpp
  35. +107 −0 ext/page.cpp
  36. +51 −0 ext/page.h
  37. +327 −0 ext/pipe.cpp
  38. +118 −0 ext/project.h
  39. +615 −0 ext/rubymain.cpp
  40. +89 −0 ext/sigs.cpp
  41. +32 −0 ext/sigs.h
  42. +405 −0 ext/ssl.cpp
  43. +86 −0 ext/ssl.h
  44. +8 −0 java/.classpath
  45. +17 −0 java/.project
  46. +196 −0 java/src/com/rubyeventmachine/
  47. +74 −0 java/src/com/rubyeventmachine/
  48. +37 −0 java/src/com/rubyeventmachine/
  49. +47 −0 java/src/com/rubyeventmachine/
  50. +405 −0 java/src/com/rubyeventmachine/
  51. +39 −0 java/src/com/rubyeventmachine/
  52. +55 −0 java/src/com/rubyeventmachine/
  53. +166 −0 java/src/com/rubyeventmachine/
  54. +234 −0 java/src/com/rubyeventmachine/
  55. +38 −0 java/src/com/rubyeventmachine/
  56. +54 −0 java/src/com/rubyeventmachine/
  57. +108 −0 java/src/com/rubyeventmachine/tests/
  58. +124 −0 java/src/com/rubyeventmachine/tests/
  59. +80 −0 java/src/com/rubyeventmachine/tests/
  60. +53 −0 java/src/com/rubyeventmachine/tests/
  61. +74 −0 java/src/com/rubyeventmachine/tests/
  62. +89 −0 java/src/com/rubyeventmachine/tests/
  63. +208 −0 lib/em/deferrable.rb
  64. +39 −0 lib/em/eventable.rb
  65. +62 −0 lib/em/future.rb
  66. +66 −0 lib/em/messages.rb
  67. +68 −0 lib/em/processes.rb
  68. +88 −0 lib/em/spawnable.rb
  69. +112 −0 lib/em/streamer.rb
  70. +1,606 −0 lib/eventmachine.rb
  71. +31 −0 lib/eventmachine_version.rb
  72. +32 −0 lib/evma.rb
  73. +32 −0 lib/evma/callback.rb
  74. +75 −0 lib/evma/container.rb
  75. +77 −0 lib/evma/factory.rb
  76. +87 −0 lib/evma/protocol.rb
  77. +48 −0 lib/evma/reactor.rb
  78. +106 −0 lib/jeventmachine.rb
  79. +1,011 −0 lib/pr_eventmachine.rb
  80. +127 −0 lib/protocols/buftok.rb
  81. +123 −0 lib/protocols/header_and_content.rb
  82. +784 −0 lib/protocols/httpcli2.rb
  83. +253 −0 lib/protocols/httpclient.rb
  84. +122 −0 lib/protocols/line_and_text.rb
  85. +145 −0 lib/protocols/linetext2.rb
  86. +179 −0 lib/protocols/saslauth.rb
  87. +308 −0 lib/protocols/smtpclient.rb
  88. +543 −0 lib/protocols/smtpserver.rb
  89. +127 −0 lib/protocols/stomp.rb
  90. +57 −0 lib/protocols/tcptest.rb
  91. +1,585 −0 setup.rb
  92. +142 −0 tests/test_basic.rb
  93. +63 −0 tests/test_defer.rb
  94. +168 −0 tests/test_epoll.rb
  95. +82 −0 tests/test_errors.rb
  96. +78 −0 tests/test_eventables.rb
  97. +58 −0 tests/test_exc.rb
  98. +214 −0 tests/test_futures.rb
  99. +221 −0 tests/test_hc.rb
  100. +194 −0 tests/test_httpclient.rb
  101. +133 −0 tests/test_httpclient2.rb
  102. +61 −0 tests/test_kb.rb
  103. +190 −0 tests/test_ltp.rb
  104. +261 −0 tests/test_ltp2.rb
  105. +58 −0 tests/test_next_tick.rb
  106. +56 −0 tests/test_processes.rb
  107. +128 −0 tests/test_pure.rb
  108. +47 −0 tests/test_running.rb
  109. +73 −0 tests/test_sasl.rb
  110. +238 −0 tests/test_send_file.rb
  111. +90 −0 tests/test_servers.rb
  112. +81 −0 tests/test_smtpclient.rb
  113. +93 −0 tests/test_smtpserver.rb
  114. +329 −0 tests/test_spawn.rb
  115. +138 −0 tests/test_timers.rb
  116. +43 −0 tests/test_ud.rb
  117. +5 −0 tests/testem.rb
  118. +7 −0 web/whatis
@@ -0,0 +1,60 @@
+EventMachine is copyrighted free software owned by Francis Cianfrocca
+(blackhedd ... The Owner of this software permits you to
+redistribute and/or modify the software under either the terms of the GPL
+version 2 (see the file GPL), or the conditions below ("Ruby License"):
+ 1. You may make and give away verbatim copies of the source form of this
+ software without restriction, provided that you retain ALL of the
+ original copyright notices and associated disclaimers.
+ 2. You may modify your copy of the software in any way, provided that
+ you do at least ONE of the following:
+ a) place your modifications in the Public Domain or otherwise
+ make them Freely Available, such as by posting said
+ modifications to Usenet or an equivalent medium, or by allowing
+ the author to include your modifications in the software.
+ b) use the modified software only within your corporation or
+ organization.
+ c) give non-standard binaries non-standard names, with
+ instructions on where to get the original software distribution.
+ d) make other distribution arrangements with the Owner.
+ 3. You may distribute the software in object code or binary form,
+ provided that you do at least ONE of the following:
+ a) distribute the binaries and library files of the software,
+ together with instructions (in a manual page or equivalent)
+ on where to get the original distribution.
+ b) accompany the distribution with the machine-readable source of
+ the software.
+ c) give non-standard binaries non-standard names, with
+ instructions on where to get the original software distribution.
+ d) make other distribution arrangements with the Owner.
+ 4. You may modify and include parts of the software into any other
+ software (possibly commercial), provided you comply with the terms in
+ Sections 1, 2, and 3 above. But some files in the distribution
+ are not written by the Owner, so they may be made available to you
+ under different terms.
+ For the list of those files and their copying conditions, see the
+ file LEGAL.
+ 5. The scripts and library files supplied as input to or produced as
+ output from the software do not automatically fall under the
+ copyright of the software, but belong to whoever generated them,
+ and may be sold commercially, and may be aggregated with this
+ software.
106 ChangeLog
@@ -0,0 +1,106 @@
+# $Id$
+01Oct06: Replaced EventMachine#open_datagram_server with a version that can take
+a Class or a Module, instead of just a Module. Thanks to Tobias Gustafsson for
+pointing out the missing case.
+04Oct06: Supported subsecond timer resolutions, per request by Jason Roelofs.
+05Oct06: Added EventMachine#set_quantum, which sets the timer resolution.
+15Nov06: Added Connection#set_comm_inactivity_timeout.
+15Nov06: Checked in a Line-and-Text Protocol Handler.
+18Nov06: Checked in a Header-and-Body Protocol Handler.
+22Nov06: Changed EventMachine#reconnect: no longer excepts when called on an
+ already-connected handler.
+28Nov06: Supported a binary-unix gem.
+19Dec06: Added EventMachine#set_effective_user.
+05Jan07: Upped max outstanding timers to 1000.
+15May07: Applied Solaris patches from Brett Eisenberg
+22May07: Cleaned up the license text in all the source files.
+22May07: Released version 0.7.2
+23May07: Per suggestion from Bill Kelly, fixed a bug with the initialization
+ of the network libraries under Windows. The goal is to enable EM to
+ be used without Ruby.
+28May07: Applied patch from Bill Kelly, refactors the declarations of
+ event names to make EM easier to use from C programs without Ruby.
+31May07: Added a preliminary implementation of EventMachine#popen.
+01Jun07: Added EM, a "pseudo-alias" for EventMachine.
+01Jun07: Added EM#next_tick.
+01Jun07: Added EM::Connection#get_outbound_data_size
+05Jun07: Removed the code which loads a pure-Ruby EM library in case the
+ compiled extension is unavailable. Suggested by Moshe Litvin.
+06Jun07: Preliminary epoll implementation.
+12Jun07: Added an evented popen implementation that, like Ruby's, is
+ full-duplex and makes the subprocess PID available to the caller.
+06Jul07: Performance-tweaked the callback dispatcher in eventmachine.rb.
+10Jul07: Released version 0.8.0.
+12Jul07: Applied patches from Tim Pease to fix Solaris build problems.
+15Jul07: Created a new provisional source branch, experiments/jruby-1.
+ This is a preliminary implementation of the EM reactor in Java,
+ suitable for use with JRuby.
+17Jul07: Added EventMachine#stop_server, per request from Kirk Haines,
+ and associated unit tests.
+22Jul07: Added EventMachine#stream_file_data. This is a very fast and scalable
+ way of sending data from static files over network connections. It
+ has separate implementations for small files and large file, and
+ has tunings to minimize memory consumption.
+26Jul07: Added some patches by Kirk Haines to improve the behavior of
+ EM::Connection#send_file_data_to_connection.
+26Jul07: Added a C++ module for directly integrating EM into C++ programs
+ with no Ruby dependencies. Needs example code.
+29Jul07: Added EventMachine::Protocols::LineText2.
+29Jul07: Added EventMachine::Protocols::Stomp.
+30Jul07: Added sys/stat.h to project.h to fix compilation bug on Darwin.
+13Aug07: Added EventMachine#reactor_running?
+15Aug07: Added parameters for EventMachine::Connection:start_tls that can be
+ used to specify client-side private keys and certificates.
+17Aug07: Added EventMachine#run_block, a sugaring for a common use case.
+24Aug07: Added a preliminary keyboard handler. Needs docs and testing on
+ windows.
+26Aug07: Created EventMachine::Spawnable, an implementation of Erlang-like
+ processes.
+27Aug07: Silenced some -w warnings, requested by James Edward Gray II.
+30Aug07: Added cookies to EM::HttpClient#request.
+04Sep07: Added an initial implementation of an evented SMTP client.
+04Sep07: Added an initial implementation of an evented SMTP server.
+10Sep07: Changed EM#spawn to run spawned blocks in the context of the
+ SpawnedProcess object, not of whatever was the active object at the
+ time of the spawn.
+14Sep07: Heartbeats weren't working with EPOLL. Noticed by Brian Candler.
+15Sep07: Added some features, tests and documents to Deferrable.
+16Sep07: Added [:content] parameter to EM::Protocols::SmtpClient#send.
+16Sep07: Bumped version to 0.9.0 in anticipation of a release.
+18Sep07: Released version 0.9.0.
+19Sep07: Added #receive_reset to EM::Protocols::SmtpServer.
+19Sep07: User overrides of EM::Protocols::SmtpServer#receive_recipient can now
+ return a Deferrable. Also fixed bug: SmtpClient now raises a protocol
+ error if none of its RCPT TO: commands are accepted by the server.
+26Sep07: Fixed missing keyboard support for Windows.
+03Oct07: Added a default handler for RuntimeErrors emitted from user-written
+ code. Suggested by Brian Candler.
+19Oct07: Set the SO_BROADCAST option automatically on all UDP sockets.
+10Nov07: Forced integer conversion of send_datagram's port parameter.
+Suggested by Matthieu Riou.
+12Nov07: Added saslauth.rb, a protocol module to replace the Cyrus SASL
+daemons saslauthd and pwcheck.
+15Nov07: Fixed bug reported by Mark Zvillius. We were failing to dispatch
+ zero-length datagrams under certain conditions.
+19Nov07: Added EventMachine#set_max_timers. Requested by Matthieu Riou and
+ others.
+19Nov07: Fixed bug with EM::Connection#start_tls. Was not working with server
+ connections. Reported by Michael S. Fischer.
+26Nov07: Supported a hack for EventMachine#popen so it can return an exit
+ status from subprocesses. Requested by Michael S. Fischer.
+30Nov07: Changed Pipe descriptors so that the child-side of the socketpair is
+ NOT set nonblocking. Suggested by Duane Johnson.
+05Dec07: Re-enabled the pure-Ruby implementation.
+06Dec07: Released Version 0.10.0.
+13Dec07: Added EM::DeferrableChildProcess
+24Dec07: Added a SASL client for simple password authentication.
+27Dec07: Removed the hookable error handler. No one was using it and it significantly
+ degraded performance.
+30Dec07: Implemented Kqueue support for OSX and BSD.
@@ -0,0 +1,138 @@
+EventMachine (EM) adds two different formalisms for lightweight concurrency to the Ruby programmer's toolbox: spawned processes and deferrables. This note will show you how to use deferrables. For more information, see the separate document LIGHTWEIGHT_CONCURRENCY.
+=== What are Deferrables?
+EventMachine's Deferrable borrows heavily from the "deferred" object in Python's "Twisted" event-handling framework. Here's a minimal example that illustrates Deferrable:
+ require 'eventmachine'
+ class MyClass
+ include EM::Deferrable
+ def print_value x
+ puts "MyClass instance received #{x}"
+ end
+ end
+ {
+ df =
+ df.callback {|x|
+ df.print_value(x)
+ EM.stop
+ }
+ {
+ df.set_deferred_status :succeeded, 100
+ }
+ }
+This program will spin for two seconds, print out the string "MyClass instance received 100" and then exit. The Deferrable pattern relies on an unusual metaphor that may be unfamiliar to you, unless you've used Python's Twisted. You may need to read the following material through more than once before you get the idea.
+EventMachine::Deferrable is simply a Ruby Module that you can include in your own classes. (There also is a class named EventMachine::DefaultDeferrable for when you want to create one without including it in code of your own.)
+An object that includes EventMachine::Deferrable is like any other Ruby object: it can be created whenever you want, returned from your functions, or passed as an argument to other functions.
+The Deferrable pattern allows you to specify any number of Ruby code blocks (callbacks or errbacks) that will be executed at some future time when the status of the Deferrable object changes.
+How might that be useful? Well, imagine that you're implementing an HTTP server, but you need to make a call to some other server in order to fulfill a client request.
+When you receive a request from one of your clients, you can create and return a Deferrable object. Some other section of your program can add a callback to the Deferrable that will cause the client's request to be fulfilled. Simultaneously, you initiate an event-driven or threaded client request to some different server. And then your EM program will continue to process other events and service other client requests.
+When your client request to the other server completes some time later, you will call the #set_deferred_status method on the Deferrable object, passing either a success or failure status, and an arbitrary number of parameters (which might include the data you received from the other server).
+At that point, the status of the Deferrable object becomes known, and its callback or errback methods are immediately executed. Callbacks and errbacks are code blocks that are attached to Deferrable objects at any time through the methods #callback and #errback.
+The deep beauty of this pattern is that it decouples the disposition of one operation (such as a client request to an outboard server) from the subsequent operations that depend on that disposition (which may include responding to a different client or any other operation).
+The code which invokes the deferred operation (that will eventually result in a success or failure status together with associated data) is completely separate from the code which depends on that status and data. This achieves one of the primary goals for which threading is typically used in sophisticated applications, with none of the nondeterminacy or debugging difficulties of threads.
+As soon as the deferred status of a Deferrable becomes known by way of a call to #set_deferred_status, the Deferrable will IMMEDIATELY execute all of its callbacks or errbacks in the order in which they were added to the Deferrable.
+Callbacks and errbacks can be added to a Deferrable object at any time, not just when the object is created. They can even be added after the status of the object has been determined! (In this case, they will be executed immediately when they are added.)
+A call to Deferrable#set_deferred_status takes :succeeded or :failed as its first argument. (This determines whether the object will call its callbacks or its errbacks.) #set_deferred_status also takes zero or more additional parameters, that will in turn be passed as parameters to the callbacks or errbacks.
+In general, you can only call #set_deferred_status ONCE on a Deferrable object. A call to #set_deferred_status will not return until all of the associated callbacks or errbacks have been called. If you add callbacks or errbacks AFTER making a call to #set_deferred_status, those additional callbacks or errbacks will execute IMMEDIATELY. Any given callback or errback will be executed AT MOST once.
+It's possible to call #set_deferred_status AGAIN, during the execution a callback or errback. This makes it possible to change the parameters which will be sent to the callbacks or errbacks farther down the chain, enabling some extremely elegant use-cases. You can transform the data returned from a deferred operation in arbitrary ways as needed by subsequent users, without changing any of the code that generated the original data.
+A call to #set_deferred_status will not return until all of the associated callbacks or errbacks have been called. If you add callbacks or errbacks AFTER making a call to #set_deferred_status, those additional callbacks or errbacks will execute IMMEDIATELY.
+Let's look at some more sample code. It turns out that many of the internal protocol implementations in the EventMachine package rely on Deferrable. One of these is EM::Protocols::HttpClient.
+To make an evented HTTP request, use the module function EM::Protocols::HttpClient#request, which returns a Deferrable object. Here's how:
+ require 'eventmachine'
+ {
+ df = EM::Protocols::HttpClient.request( :host=>"", :request=>"/index.html" )
+ df.callback {|response|
+ puts "Succeeded: #{response[:content]}"
+ EM.stop
+ }
+ df.errback {|response|
+ puts "ERROR: #{response[:status]}"
+ EM.stop
+ }
+ }
+(See the documentation of EventMachine::Protocols::HttpClient for information on the object returned by #request.)
+In this code, we make a call to HttpClient#request, which immediately returns a Deferrable object. In the background, an HTTP client request is being made to, although your code will continue to run concurrently.
+At some future point, the HTTP client request will complete, and the code in EM::Protocols::HttpClient will process either a valid HTTP response (including returned content), or an error.
+At that point, EM::Protocols::HttpClient will call EM::Deferrable#set_deferred_status on the Deferrable object that was returned to your program, as the return value from EM::Protocols::HttpClient.request. You don't have to do anything to make this happen. All you have to do is tell the Deferrable what to do in case of either success, failure, or both.
+In our code sample, we set one callback and one errback. The former will be called if the HTTP call succeeds, and the latter if it fails. (For simplicity, we have both of them calling EM#stop to end the program, although real programs would be very unlikely to do this.)
+Setting callbacks and errbacks is optional. They are handlers to defined events in the lifecycle of the Deferrable event. It's not an error if you fail to set either a callback, an errback, or both. But of course your program will then fail to receive those notifications.
+If through some bug it turns out that #set_deferred_status is never called on a Deferrable object, then that object's callbacks or errbacks will NEVER be called. It's also possible to set a timeout on a Deferrable. If the timeout elapses before any other call to #set_deferred_status, the Deferrable object will behave as is you had called set_deferred_status(:failed) on it.
+Now let's modify the example to illustrate some additional points:
+ require 'eventmachine'
+ {
+ df = EM::Protocols::HttpClient.request( :host=>"", :request=>"/index.html" )
+ df.callback {|response|
+ df.set_deferred_status :succeeded, response[:content]
+ }
+ df.callback {|string|
+ puts "Succeeded: #{string}"
+ EM.stop
+ }
+ df.errback {|response|
+ puts "ERROR: #{response[:status]}"
+ EM.stop
+ }
+ }
+Just for the sake of illustration, we've now set two callbacks instead of one. If the deferrable operation (the HTTP client-request) succeeds, then both of the callbacks will be executed in order.
+But notice that we've also made our own call to #set_deferred_status in the first callback. This isn't required, because the HttpClient implementation already made a call to #set_deferred_status. (Otherwise, of course, the callback would not be executing.)
+But we used #set_deferred_status in the first callback in order to change the parameters that will be sent to subsequent callbacks in the chain. In this way, you can construct powerful sequences of layered functionality. If you want, you can even change the status of the Deferrable from :succeeded to :failed, which would abort the chain of callback calls, and invoke the chain of errbacks instead.
+Now of course it's somewhat trivial to define two callbacks in the same method, even with the parameter-changing effect we just described. It would be much more interesting to pass the Deferrable to some other function (for example, a function defined in another module or a different gem), that would in turn add callbacks and/or errbacks of its own. That would illustrate the true power of the Deferrable pattern: to isolate the HTTP client-request from other functions that use the data that it returns without caring where those data came from.
+Remember that you can add a callback or an errback to a Deferrable at any point in time, regardless of whether the status of the deferred operation is known (more precisely, regardless of when #set_deferred_status is called on the object). Even hours or days later.
+When you add a callback or errback to a Deferrable object on which #set_deferred_status has not yet been called, the callback/errback is queued up for future execution, inside the Deferrable object. When you add a callback or errback to a Deferrable on which #set_deferred_status has already been called, the callback/errback will be executed immediately. Your code doesn't have to worry about the ordering, and there are no timing issues, as there would be with a threaded approach.
+For more information on Deferrables and their typical usage patterns, look in the EM unit tests. There are also quite a few sugarings (including EM::Deferrable#future) that make typical Deferrable usages syntactically easier to work with.
Oops, something went wrong.

0 comments on commit 9d5b7ca

Please sign in to comment.