eventmachine 0.12.0-i386-mswin32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. data/COPYING +60 -0
  2. data/DEFERRABLES +138 -0
  3. data/EPOLL +141 -0
  4. data/GNU +281 -0
  5. data/KEYBOARD +38 -0
  6. data/LEGAL +25 -0
  7. data/LIGHTWEIGHT_CONCURRENCY +72 -0
  8. data/PURE_RUBY +77 -0
  9. data/README +74 -0
  10. data/RELEASE_NOTES +96 -0
  11. data/SMTP +9 -0
  12. data/SPAWNED_PROCESSES +93 -0
  13. data/TODO +10 -0
  14. data/ext/Makefile +180 -0
  15. data/ext/binder.cpp +126 -0
  16. data/ext/binder.h +48 -0
  17. data/ext/cmain.cpp +527 -0
  18. data/ext/cplusplus.cpp +172 -0
  19. data/ext/ed.cpp +1442 -0
  20. data/ext/ed.h +351 -0
  21. data/ext/em.cpp +1781 -0
  22. data/ext/em.h +167 -0
  23. data/ext/emwin.cpp +300 -0
  24. data/ext/emwin.h +94 -0
  25. data/ext/epoll.cpp +26 -0
  26. data/ext/epoll.h +25 -0
  27. data/ext/eventmachine.h +83 -0
  28. data/ext/eventmachine_cpp.h +94 -0
  29. data/ext/extconf.rb +203 -0
  30. data/ext/files.cpp +94 -0
  31. data/ext/files.h +65 -0
  32. data/ext/kb.cpp +368 -0
  33. data/ext/mkmf.log +129 -0
  34. data/ext/page.cpp +107 -0
  35. data/ext/page.h +51 -0
  36. data/ext/pipe.cpp +327 -0
  37. data/ext/project.h +119 -0
  38. data/ext/rubyeventmachine-i386-mswin32.def +2 -0
  39. data/ext/rubyeventmachine-i386-mswin32.exp +0 -0
  40. data/ext/rubyeventmachine-i386-mswin32.lib +0 -0
  41. data/ext/rubyeventmachine-i386-mswin32.pdb +0 -0
  42. data/ext/rubyeventmachine.so +0 -0
  43. data/ext/rubymain.cpp +630 -0
  44. data/ext/sigs.cpp +89 -0
  45. data/ext/sigs.h +32 -0
  46. data/ext/ssl.cpp +408 -0
  47. data/ext/ssl.h +86 -0
  48. data/ext/vc60.pdb +0 -0
  49. data/lib/em/deferrable.rb +208 -0
  50. data/lib/em/eventable.rb +39 -0
  51. data/lib/em/future.rb +62 -0
  52. data/lib/em/messages.rb +66 -0
  53. data/lib/em/processes.rb +68 -0
  54. data/lib/em/spawnable.rb +88 -0
  55. data/lib/em/streamer.rb +112 -0
  56. data/lib/eventmachine.rb +1621 -0
  57. data/lib/eventmachine_version.rb +31 -0
  58. data/lib/evma.rb +32 -0
  59. data/lib/evma/callback.rb +32 -0
  60. data/lib/evma/container.rb +75 -0
  61. data/lib/evma/factory.rb +77 -0
  62. data/lib/evma/protocol.rb +87 -0
  63. data/lib/evma/reactor.rb +48 -0
  64. data/lib/jeventmachine.rb +106 -0
  65. data/lib/pr_eventmachine.rb +1011 -0
  66. data/lib/protocols/buftok.rb +127 -0
  67. data/lib/protocols/header_and_content.rb +123 -0
  68. data/lib/protocols/httpcli2.rb +784 -0
  69. data/lib/protocols/httpclient.rb +253 -0
  70. data/lib/protocols/line_and_text.rb +122 -0
  71. data/lib/protocols/linetext2.rb +145 -0
  72. data/lib/protocols/saslauth.rb +179 -0
  73. data/lib/protocols/smtpclient.rb +308 -0
  74. data/lib/protocols/smtpserver.rb +543 -0
  75. data/lib/protocols/stomp.rb +127 -0
  76. data/lib/protocols/tcptest.rb +57 -0
  77. data/lib/rubyeventmachine.so +0 -0
  78. data/tests/test_basic.rb +142 -0
  79. data/tests/test_defer.rb +63 -0
  80. data/tests/test_epoll.rb +168 -0
  81. data/tests/test_errors.rb +82 -0
  82. data/tests/test_eventables.rb +78 -0
  83. data/tests/test_exc.rb +58 -0
  84. data/tests/test_futures.rb +214 -0
  85. data/tests/test_hc.rb +221 -0
  86. data/tests/test_httpclient.rb +194 -0
  87. data/tests/test_httpclient2.rb +133 -0
  88. data/tests/test_kb.rb +61 -0
  89. data/tests/test_ltp.rb +190 -0
  90. data/tests/test_ltp2.rb +261 -0
  91. data/tests/test_next_tick.rb +58 -0
  92. data/tests/test_processes.rb +56 -0
  93. data/tests/test_pure.rb +128 -0
  94. data/tests/test_running.rb +47 -0
  95. data/tests/test_sasl.rb +73 -0
  96. data/tests/test_send_file.rb +238 -0
  97. data/tests/test_servers.rb +90 -0
  98. data/tests/test_smtpclient.rb +81 -0
  99. data/tests/test_smtpserver.rb +93 -0
  100. data/tests/test_spawn.rb +329 -0
  101. data/tests/test_timers.rb +138 -0
  102. data/tests/test_ud.rb +43 -0
  103. data/tests/testem.rb +5 -0
  104. metadata +170 -0
@@ -0,0 +1,38 @@
1
+ EventMachine (EM) can respond to keyboard events. This gives your event-driven programs the ability to respond to input from local users.
2
+
3
+ Programming EM to handle keyboard input in Ruby is simplicity itself. Just use EventMachine#open_keyboard, and supply the name of a Ruby module or class that will receive the input:
4
+
5
+ require 'rubygems'
6
+ require 'eventmachine'
7
+
8
+ module MyKeyboardHandler
9
+ def receive_data keystrokes
10
+ puts "I received the following data from the keyboard: #{keystrokes}"
11
+ end
12
+ end
13
+
14
+ EM.run {
15
+ EM.open_keyboard(MyKeyboardHandler)
16
+ }
17
+
18
+
19
+ If you want EM to send line-buffered keyboard input to your program, just include the LineText2 protocol module in your handler class or module:
20
+
21
+
22
+
23
+ require 'rubygems'
24
+ require 'eventmachine'
25
+
26
+ module MyKeyboardHandler
27
+ include EM::Protocols::LineText2
28
+ def receive_line data
29
+ puts "I received the following line from the keyboard: #{data}"
30
+ end
31
+ end
32
+
33
+ EM.run {
34
+ EM.open_keyboard(MyKeyboardHandler)
35
+ }
36
+
37
+ As we said, simplicity itself. You can call EventMachine#open_keyboard at any time while the EM reactor loop is running. In other words, the method invocation may appear anywhere in an EventMachine#run block, or in any code invoked in the #run block.
38
+
data/LEGAL ADDED
@@ -0,0 +1,25 @@
1
+ LEGAL NOTICE INFORMATION
2
+ ------------------------
3
+
4
+ EventMachine is Copyright (C) 2006-07 by Francis Cianfrocca.
5
+
6
+ EventMachine is copyrighted software owned by Francis Cianfrocca
7
+ (blackhedd ... gmail.com). You may redistribute and/or modify this
8
+ software as long as you comply with either the terms of the GPL
9
+ (see the file GPL), or Ruby's license (see the file COPYING).
10
+
11
+ Your use of all the files in this distribution is controlled by these
12
+ license terms, except for those files specifically mentioned below:
13
+
14
+
15
+
16
+ setup.rb
17
+ This file is Copyright (C) 2000-2005 by Minero Aoki
18
+ You can distribute/modify this file under the terms of
19
+ the GNU LGPL, Lesser General Public License version 2.1.
20
+
21
+
22
+ lib/protocols/buftok.rb
23
+ This file is Copyright (C) 2007 by Tony Arcieri. This file is
24
+ covered by the terms of Ruby's License (see the file COPYING).
25
+
@@ -0,0 +1,72 @@
1
+ $Id: LIGHTWEIGHT_CONCURRENCY 668 2008-01-04 23:00:34Z blackhedd $
2
+
3
+ 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 them.
4
+
5
+
6
+ === What is Lightweight Concurrency?
7
+
8
+ We use the term "Lightweight Concurrency" (LC) to refer to concurrency mechanisms that are lighter than Ruby threads. By "lighter," we mean: less resource-intensive in one or more dimensions, usually including memory and CPU usage. In general, you turn to LC in the hope of improving the performance and scalability of your programs.
9
+
10
+ In addition to the two EventMachine mechanisms we will discuss here, Ruby has at least one other LC construct: Fibers, which are currently under development in Ruby 1.9.
11
+
12
+ The technical feature that makes all of these LC mechanisms different from standard Ruby threads is that they are not scheduled automatically.
13
+
14
+ When you create and run Ruby threads, you can assume (within certain constraints) that your threads will all be scheduled fairly by Ruby's runtime. Ruby itself is responsible for giving each of your threads its own share of the total runtime.
15
+
16
+ But with LC, your program is responsible for causing different execution paths to run. In effect, your program has to act as a "thread scheduler." Scheduled entities in LC run to completion and are never preempted. The runtime system has far less work to do since it has no need to interrupt threads or to schedule them fairly. This is what makes LC lighter and faster.
17
+
18
+ You'll learn exactly how LC scheduling works in practice as we work through specific examples.
19
+
20
+
21
+ === EventMachine Lightweight Concurrency
22
+
23
+ Recall that EM provides a reactor loop that must be running in order for your programs to perform event-driven logic. An EM program typically has a structure like this:
24
+
25
+ require 'eventmachine'
26
+
27
+ # your initializations
28
+
29
+ EM.run {
30
+ # perform event-driven I/O here, including network clients,
31
+ # servers, timers, and thread-pool operations.
32
+ }
33
+
34
+ # your cleanup
35
+ # end of the program
36
+
37
+
38
+ EventMachine#run executes the reactor loop, which causes your code to be called as events of interest to your program occur. The block you pass to EventMachine#run is executed right after the reactor loop starts, and is the right place to start socket acceptors, etc.
39
+
40
+ Because the reactor loop runs constantly in an EM program (until it is stopped by a call to EventMachine#stop), it has the ability to schedule blocks of code for asynchronous execution. Unlike a pre-emptive thread scheduler, it's NOT able to interrupt code blocks while they execute. But the scheduling capability it does have is enough to enable lightweight concurrency.
41
+
42
+
43
+ For information on Spawned Processes, see the separate document SPAWNED_PROCESSES.
44
+
45
+ For information on Deferrables, see the separate document DEFERRABLES.
46
+
47
+
48
+ === [SIDEBAR]: I Heard That EventMachine Doesn't Work With Ruby Threads.
49
+
50
+ This is incorrect. EM is fully interoperable with all versions of Ruby threads, and has been since its earliest releases.
51
+
52
+ It's very true that EM encourages an "evented" (non-threaded) programming style. The specific benefits of event-driven programming are far better performance and scalabiity for well-written programs, and far easier debugging.
53
+
54
+ The benefit of using threads for similar applications is a possibly more intuitive programming model, as well as the fact that threads are already familiar to most programmers. Also, bugs in threaded programs often fail to show up until programs go into production. These factors create the illusion that threaded programs are easier to write.
55
+
56
+ However, some operations that occur frequently in professional-caliber applications simply can't be done without threads. (The classic example is making calls to database client-libraries that block on network I/O until they complete.)
57
+
58
+ EventMachine not only allows the use of Ruby threads in these cases, but it even provides a built-in thread-pool object to make them easier to work with.
59
+
60
+ You may have heard a persistent criticism that evented I/O is fundamentally incompatible with Ruby threads. It is true that some well-publicized attempts to incorporate event-handling libraries into Ruby were not successful. But EventMachine was designed from the ground up with Ruby compatibility in mind, so EM never suffered from the problems that defeated the earlier attempts.
61
+
62
+
63
+ === [SIDEBAR]: I Heard That EventMachine Doesn't Work Very Well On Windows.
64
+
65
+ This too is incorrect. EventMachine is an extension written in C++ and Java, and therefore it requires compilation. Many Windows computers (and some Unix computers, especially in production environments) don't have a build stack. Attempting to install EventMachine on a machine without a compiler usually produces a confusing error.
66
+
67
+ In addition, Ruby has a much-debated issue with Windows compiler versions. Ruby on Windows works best with Visual Studio 6, a compiler version that is long out-of-print, no longer supported by Microsoft, and difficult to obtain. (This problem is not specific to EventMachine.)
68
+
69
+ Shortly after EventMachine was first released, the compiler issues led to criticism that EM was incompatible with Windows. Since that time, every EventMachine release has been supplied in a precompiled binary form for Windows users, that does not require you to compile the code yourself. EM binary Gems for Windows are compiled using Visual Studio 6.
70
+
71
+ EventMachine does supply some advanced features (such as Linux EPOLL support, reduced-privilege operation, UNIX-domain sockets, etc.) that have no meaningful implementation on Windows. Apart from these special cases, all EM functionality (including lightweight concurrency) works perfectly well on Windows.
72
+
@@ -0,0 +1,77 @@
1
+ $Id: PURE_RUBY 668 2008-01-04 23:00:34Z blackhedd $
2
+
3
+ EventMachine is supplied in three alternative versions.
4
+
5
+ 1) A version that includes a Ruby extension written in C++. This version requires compilation;
6
+ 2) A version for JRuby that contains a precompiled JAR file written in Java;
7
+ 3) A pure Ruby version that has no external dependencies and can run in any Ruby environment.
8
+
9
+ The Java version of EventMachine is packaged in a distinct manner and must be installed using a
10
+ special procedure. This version is described fully in a different document, and not considered
11
+ further here.
12
+
13
+ The C++ and pure-Ruby versions, however, are shipped in the same distribution. You use the same
14
+ files (either tarball or Ruby gem) to install both of these versions.
15
+
16
+ If you intend to use the C++ version, you must successfully compile EventMachine after you install it.
17
+ (The gem installation attempts to perform this step automatically.)
18
+
19
+ If you choose not to compile the EventMachine C++ extension, or if your compilation fails for any
20
+ reason, you still have a fully-functional installation of the pure-Ruby version of EM.
21
+
22
+ However, for technical reasons, a default EM installation (whether or not the compilation succeeds)
23
+ will always assume that the compiled ("extension") implementation should be used.
24
+
25
+ If you want your EM program to use the pure Ruby version, you must specifically request it. There
26
+ are two ways to do this: by setting either a Ruby global variable, or an environment string.
27
+
28
+ The following code will invoke the pure-Ruby implementation of EM:
29
+
30
+ $eventmachine_library = :pure_ruby
31
+ require 'eventmachine'
32
+
33
+ EM.library_type #=> "pure_ruby"
34
+
35
+ Notice that this requires a code change and is not the preferred way to select pure Ruby, unless
36
+ for some reason you are absolutely sure you will never want the compiled implementation.
37
+
38
+ Setting the following environment string has the same effect:
39
+
40
+ export EVENTMACHINE_LIBRARY="pure_ruby"
41
+
42
+ This technique gives you the flexibility to select either version at runtime with no code changes.
43
+
44
+ Support
45
+
46
+ The EventMachine development team has committed to support precisely the same APIs for all the
47
+ various implementations of EM.
48
+
49
+ This means that you can expect any EM program to behave identically, whether you use pure Ruby,
50
+ the compiled C++ extension, or JRuby. Deviations from this behavior are to be considered bugs
51
+ and should be reported as such.
52
+
53
+ There is a small number of exceptions to this rule, which arise from underlying platform
54
+ distinctions. Notably, EM#epoll is a silent no-op in the pure Ruby implementation.
55
+
56
+
57
+ When Should You Use the Pure-Ruby Implementation of EM?
58
+
59
+
60
+ Use the pure Ruby implementation of EM when you must support a platform for which no C++ compiler
61
+ is available, or on which the standard EM C++ code can't be compiled.
62
+
63
+ Keep in mind that you don't need a C++ compiler in order to deploy EM applications that rely on
64
+ the compiled version, so long as appropriate C++ runtime libraries are available on the target platform.
65
+
66
+ In extreme cases, you may find that you can develop software with the compiled EM version, but are
67
+ not allowed to install required runtime libraries on the deployment system(s). This would be another
68
+ case in which the pure Ruby implementation can be useful.
69
+
70
+ In general you should avoid the pure Ruby version of EM when performance and scalability are important.
71
+ EM in pure Ruby will necessarily run slower than the compiled version. Depending on your application
72
+ this may or may not be a key issue.
73
+
74
+ Also, since EPOLL is not supported in pure Ruby, your applications will be affected by Ruby's built-in
75
+ limit of 1024 file and socket descriptors that may be open in a single process. For maximum scalability
76
+ and performance, always use EPOLL if possible.
77
+
data/README ADDED
@@ -0,0 +1,74 @@
1
+ $Id: README 668 2008-01-04 23:00:34Z blackhedd $
2
+
3
+ = RUBY/EventMachine
4
+
5
+ Homepage:: http://rubyeventmachine.com
6
+ Copyright:: (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
7
+ Email:: gmail address: garbagecat10
8
+
9
+ EventMachine is copyrighted free software made available under the terms
10
+ of either the GPL or Ruby's License. See the file COPYING for full licensing
11
+ information.
12
+ See EventMachine and EventMachine::Connection for documentation and
13
+ usage examples.
14
+
15
+ EventMachine implements a fast, single-threaded engine for arbitrary network
16
+ communications. It's extremely easy to use in Ruby. EventMachine wraps all
17
+ interactions with IP sockets, allowing programs to concentrate on the
18
+ implementation of network protocols. It can be used to create both network
19
+ servers and clients. To create a server or client, a Ruby program only needs
20
+ to specify the IP address and port, and provide a Module that implements the
21
+ communications protocol. Implementations of several standard network protocols
22
+ are provided with the package, primarily to serve as examples. The real goal
23
+ of EventMachine is to enable programs to easily interface with other programs
24
+ using TCP/IP, especially if custom protocols are required.
25
+
26
+ A Ruby program uses EventMachine by registering the addresses and ports of
27
+ network servers and clients, and then entering an event-handling loop.
28
+ EventMachine contains glue code in Ruby which will execute callbacks to
29
+ user-supplied code for all significant events occurring in the clients
30
+ and servers. These events include connection acceptance, startup, data-receipt,
31
+ shutdown, and timer events. Arbitrary processing can be performed by user code
32
+ during event callbacks, including sending data to one or more remote network
33
+ peers, startup and shutdown of network connections, and installation of new
34
+ event handlers.
35
+
36
+ The EventMachine implements a very familiar model for network programming.
37
+ It emphasizes: 1) the maximum possible isolation of user code from network
38
+ objects like sockets; 2) maximum performance and scalability; and 3) extreme
39
+ ease-of-use for user code. It attempts to provide a higher-level interface
40
+ than similar projects which expose a variety of low-level event-handling
41
+ and networking objects to Ruby programs.
42
+
43
+ The design and implementation of EventMachine grows out of nearly ten years
44
+ of experience writing high-performance, high-scaling network server applications.
45
+ We have taken particular account of the challenges and lessons described as
46
+ the "C10K problem" by Dan Kegel and others.
47
+
48
+ EventMachine consists of an extension library written in C++ (which can be
49
+ accessed from languages other than Ruby), and a Ruby module which can be dropped
50
+ into user programs. On most platforms, EventMachine uses the
51
+ <tt>select(2)</tt> system call,
52
+ so it will run on a large range of Unix-like systems and on Microsoft
53
+ Windows with good performance and scalability. On Linux 2.6 kernels, EventMachine
54
+ automatically configures itself to use <tt>epoll(4)</tt> instead of
55
+ <tt>select(2),</tt> so scalability on that platform can be significantly
56
+ improved.
57
+
58
+ Here's a fully-functional echo server written with EventMachine:
59
+
60
+ require 'rubygems'
61
+ require 'eventmachine'
62
+
63
+ module EchoServer
64
+ def receive_data data
65
+ send_data ">>>you sent: #{data}"
66
+ close_connection if data =~ /quit/i
67
+ end
68
+ end
69
+
70
+ EventMachine::run {
71
+ EventMachine::start_server "192.168.0.100", 8081, EchoServer
72
+ }
73
+
74
+
@@ -0,0 +1,96 @@
1
+ $Id: RELEASE_NOTES 668 2008-01-04 23:00:34Z blackhedd $
2
+
3
+ RUBY/EventMachine RELEASE NOTES
4
+
5
+ --------------------------------------------------
6
+ Version: 0.9.0, released xxXXX07
7
+ Added Erlang-like distributed-computing features
8
+
9
+ --------------------------------------------------
10
+ Version: 0.8.0, released 23Jun07
11
+ Added an epoll implementation for Linux 2.6 kernels.
12
+ Added evented #popen.
13
+
14
+ --------------------------------------------------
15
+ Version: 0.7.3, released 22May07
16
+ Added a large variety of small features. See the ChangeLog.
17
+
18
+ --------------------------------------------------
19
+ Version: 0.7.1, released xxNov06
20
+ Added protocol handlers for line-oriented protocols.
21
+ Various bug fixes.
22
+
23
+ --------------------------------------------------
24
+ Version: 0.7.0, released 20Nov06
25
+ Added a fix in em.cpp/ConnectToServer to fix a fatal exception that
26
+ occurred in FreeBSD when connecting successfully to a remote server.
27
+
28
+ --------------------------------------------------
29
+ Version: 0.6.0, released xxJul06
30
+ Added deferred operations, suggested by Don Stocks, amillionhitpoints@yahoo.com.
31
+
32
+ --------------------------------------------------
33
+ Version: 0.5.4, released xxJun06
34
+ Added get_peername support for streams and datagrams.
35
+
36
+ --------------------------------------------------
37
+ Version: 0.5.3, released 17May06
38
+ Fixed bugs in extconf.rb, thanks to Daniel Harple, dharple@generalconsumption.org.
39
+ Added proper setup.rb and rake tasks, thanks to Austin Ziegler.
40
+ Fixed a handful of reported problems with builds on various platforms.
41
+
42
+ --------------------------------------------------
43
+ Version: 0.5.2, released 05May06
44
+ Made several nonvisible improvements to the Windows
45
+ implementation.
46
+ Added an exception-handling patch contributed by Jeff Rose, jeff@rosejn.net.
47
+ Added a dir-config patch contributed anonymously.
48
+ Supported builds on Solaris.
49
+
50
+ --------------------------------------------------
51
+ Version: 0.5.1, released 05May06
52
+ Made it possible to pass a Class rather than a Module
53
+ to a protocol handler.
54
+ Added Windows port.
55
+
56
+ --------------------------------------------------
57
+ Version: 0.5.0, released 30Apr06
58
+ Added a preliminary SSL/TLS extension. This will probably
59
+ change over the next few releases.
60
+
61
+ --------------------------------------------------
62
+ Version: 0.4.5, released 29Apr06
63
+ Changed ext files so the ruby.h is installed after unistd.h
64
+ otherwise it doesn't compile on gcc 4.1
65
+
66
+ --------------------------------------------------
67
+ Version: 0.4.2, released 19Apr06
68
+ Changed the Ruby-glue so the extension will play nicer
69
+ in the sandbox with Ruby threads.
70
+ Added an EventMachine::run_without_threads API to
71
+ switch off the thread-awareness for better performance
72
+ in programs that do not spin any Ruby threads.
73
+
74
+ --------------------------------------------------
75
+ Version: 0.4.1, released 15Apr06
76
+ Reworked the shared-object interface to make it easier to
77
+ use EventMachine from languages other than Ruby.
78
+
79
+ --------------------------------------------------
80
+ Version: 0.3.2, released 12Apr06
81
+ Added support for a user-supplied block in EventMachine#connect.
82
+
83
+ --------------------------------------------------
84
+ Version: 0.3.1, released 11Apr06
85
+ Fixed bug that prevented EventMachine from being run multiple
86
+ times in a single process.
87
+
88
+ --------------------------------------------------
89
+ Version: 0.3.0, released 10Apr06
90
+ Added method EventHandler::Connection::post_init
91
+
92
+ --------------------------------------------------
93
+ Version: 0.2.0, released 10Apr06
94
+ Added method EventHandler::stop
95
+
96
+
data/SMTP ADDED
@@ -0,0 +1,9 @@
1
+ $Id: SMTP 668 2008-01-04 23:00:34Z blackhedd $
2
+
3
+
4
+ [DOCUMENT UNDER CONSTRUCTION]
5
+
6
+ This note details the usage of EventMachine's built-in support for SMTP. EM supports both client and server connections, which will be described in separate sections.
7
+
8
+
9
+
@@ -0,0 +1,93 @@
1
+ $Id: SPAWNED_PROCESSES 668 2008-01-04 23:00:34Z blackhedd $
2
+
3
+ [DOCUMENT UNDER CONSTRUCTION]
4
+
5
+ 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 spawned processes. For more information, see the separate document LIGHTWEIGHT_CONCURRENCY.
6
+
7
+
8
+ === What are Spawned Processes?
9
+
10
+ Spawned Processes in EventMachine are inspired directly by the "processes" found in the Erlang programming language. EM deliberately borrows much (but not all) of Erlang's terminology. However, EM's spawned processes differ from Erlang's in ways that reflect not only Ruby style, but also the fact that Ruby is not a functional language like Erlang.
11
+
12
+ Let's proceed with a complete, working code sample that we will analyze line by line. Here's an EM implementation of the "ping-pong" program that also appears in the Erlang tutorial:
13
+
14
+
15
+ require 'eventmachine'
16
+
17
+ EM.run {
18
+ pong = EM.spawn {|x, ping|
19
+ puts "Pong received #{x}"
20
+ ping.notify( x-1 )
21
+ }
22
+
23
+ ping = EM.spawn {|x|
24
+ if x > 0
25
+ puts "Pinging #{x}"
26
+ pong.notify x, self
27
+ else
28
+ EM.stop
29
+ end
30
+ }
31
+
32
+ ping.notify 3
33
+ }
34
+
35
+ If you run this program, you'll see the following output:
36
+
37
+ Pinging 3
38
+ Pong received 3
39
+ Pinging 2
40
+ Pong received 2
41
+ Pinging 1
42
+ Pong received 1
43
+
44
+ Let's take it step by step.
45
+
46
+ EventMachine#spawn works very much like the built-in function spawn in Erlang. It returns a reference to a Ruby object of class EventMachine::SpawnedProcess, which is actually a schedulable entity. In Erlang, the value returned from spawn is called a "process identifier" or "pid." But we'll refer to the Ruby object returned from EM#spawn simply as a "spawned process."
47
+
48
+ You pass a Ruby block with zero or more parameters to EventMachine#spawn. Like all Ruby blocks, this one is a closure, so it can refer to variables defined in the local context when you call EM#spawn.
49
+
50
+ However, the code block passed to EM#spawn does NOT execute immediately by default. Rather, it will execute only when the Spawned Object is "notified." In Erlang, this process is called "message passing," and is done with the operator !, but in Ruby it's done simply by calling the #notify method of a spawned-process object. The parameters you pass to #notify must match those defined in the block that was originally passed to EM#spawn.
51
+
52
+ When you call the #notify method of a spawned-process object, EM's reactor core will execute the code block originally passed to EM#spawn, at some point in the future. (#notify itself merely adds a notification to the object's message queue and ALWAYS returns immediately.)
53
+
54
+ When a SpawnedProcess object executes a notification, it does so in the context of the SpawnedProcess object itself. The notified code block can see local context from the point at which EM#spawn was called. However, the value of "self" inside the notified code block is a reference to the SpawnedProcesss object itself.
55
+
56
+ An EM spawned process is nothing more than a Ruby object with a message queue attached to it. You can have any number of spawned processes in your program without compromising scalability. You can notify a spawned process any number of times, and each notification will cause a "message" to be placed in the queue of the spawned process. Spawned processes with non-empty message queues are scheduled for execution automatically by the EM reactor. Spawned processes with no visible references are garbage-collected like any other Ruby object.
57
+
58
+ Back to our code sample:
59
+
60
+ pong = EM.spawn {|x, ping|
61
+ puts "Pong received #{x}"
62
+ ping.notify( x-1 )
63
+ }
64
+
65
+ This simply creates a spawned process and assigns it to the local variable pong. You can see that the spawned code block takes a numeric parameter and a reference to another spawned process. When pong is notified, it expects to receive arguments corresponding to these two parameters. It simply prints out the number it receives as the first argument. Then it notifies the spawned process referenced by the second argument, passing it the first argument minus 1.
66
+
67
+ And then the block ends, which is crucial because otherwise nothing else can run. (Remember that in LC, scheduled entities run to completion and are never preempted.)
68
+
69
+ On to the next bit of the code sample:
70
+
71
+ ping = EM.spawn {|x|
72
+ if x > 0
73
+ puts "Pinging #{x}"
74
+ pong.notify x, self
75
+ else
76
+ EM.stop
77
+ end
78
+ }
79
+
80
+ Here, we're spawning a process that takes a single (numeric) parameter. If the parameter is greater than zero, the block writes it to the console. It then notifies the spawned process referenced by the pong local variable, passing as arguments its number argument, and a reference to itself. The latter reference, as you saw above, is used by pong to send a return notification.
81
+
82
+ If the ping process receives a zero value, it will stop the reactor loop and end the program.
83
+
84
+ Now we've created a pair of spawned processes, but nothing else has happened. If we stop now, the program will spin in the EM reactor loop, doing nothing at all. Our spawned processes will never be scheduled for execution.
85
+
86
+ But look at the next line in the code sample:
87
+
88
+ ping.notify 3
89
+
90
+ This line gets the ping-pong ball rolling. We call ping's #notify method, passing the argument 3. This causes a message to be sent to the ping spawned process. The message contains the single argument, and it causes the EM reactor to schedule the ping process. And this in turn results in the execution of the Ruby code block passed to EM#spawn when ping was created. Everything else proceeds as a result of the messages that are subsequently passed to each other by the spawned processes.
91
+
92
+ [TODO, present the outbound network i/o use case, and clarify that spawned processes are interleaved with normal i/o operations and don't interfere with them at all. Also, blame Erlang for the confusing term "process"]
93
+