eventmachine 1.2.0.dev.2-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (181) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +105 -0
  3. data/GNU +281 -0
  4. data/LICENSE +60 -0
  5. data/README.md +108 -0
  6. data/docs/DocumentationGuidesIndex.md +27 -0
  7. data/docs/GettingStarted.md +521 -0
  8. data/docs/old/ChangeLog +211 -0
  9. data/docs/old/DEFERRABLES +246 -0
  10. data/docs/old/EPOLL +141 -0
  11. data/docs/old/INSTALL +13 -0
  12. data/docs/old/KEYBOARD +42 -0
  13. data/docs/old/LEGAL +25 -0
  14. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  15. data/docs/old/PURE_RUBY +75 -0
  16. data/docs/old/RELEASE_NOTES +94 -0
  17. data/docs/old/SMTP +4 -0
  18. data/docs/old/SPAWNED_PROCESSES +148 -0
  19. data/docs/old/TODO +8 -0
  20. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  21. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  22. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  23. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  24. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  25. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  26. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  27. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  28. data/examples/old/ex_channel.rb +43 -0
  29. data/examples/old/ex_queue.rb +2 -0
  30. data/examples/old/ex_tick_loop_array.rb +15 -0
  31. data/examples/old/ex_tick_loop_counter.rb +32 -0
  32. data/examples/old/helper.rb +2 -0
  33. data/ext/binder.cpp +124 -0
  34. data/ext/binder.h +46 -0
  35. data/ext/cmain.cpp +988 -0
  36. data/ext/ed.cpp +2111 -0
  37. data/ext/ed.h +442 -0
  38. data/ext/em.cpp +2379 -0
  39. data/ext/em.h +308 -0
  40. data/ext/eventmachine.h +143 -0
  41. data/ext/extconf.rb +270 -0
  42. data/ext/fastfilereader/extconf.rb +110 -0
  43. data/ext/fastfilereader/mapper.cpp +216 -0
  44. data/ext/fastfilereader/mapper.h +59 -0
  45. data/ext/fastfilereader/rubymain.cpp +127 -0
  46. data/ext/kb.cpp +79 -0
  47. data/ext/page.cpp +107 -0
  48. data/ext/page.h +51 -0
  49. data/ext/pipe.cpp +354 -0
  50. data/ext/project.h +176 -0
  51. data/ext/rubymain.cpp +1504 -0
  52. data/ext/ssl.cpp +615 -0
  53. data/ext/ssl.h +103 -0
  54. data/java/.classpath +8 -0
  55. data/java/.project +17 -0
  56. data/java/src/com/rubyeventmachine/EmReactor.java +591 -0
  57. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  58. data/java/src/com/rubyeventmachine/EventableChannel.java +72 -0
  59. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +201 -0
  60. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +415 -0
  61. data/lib/2.0/fastfilereaderext.so +0 -0
  62. data/lib/2.0/rubyeventmachine.so +0 -0
  63. data/lib/2.1/fastfilereaderext.so +0 -0
  64. data/lib/2.1/rubyeventmachine.so +0 -0
  65. data/lib/2.2/fastfilereaderext.so +0 -0
  66. data/lib/2.2/rubyeventmachine.so +0 -0
  67. data/lib/2.3/fastfilereaderext.so +0 -0
  68. data/lib/2.3/rubyeventmachine.so +0 -0
  69. data/lib/em/buftok.rb +59 -0
  70. data/lib/em/callback.rb +58 -0
  71. data/lib/em/channel.rb +69 -0
  72. data/lib/em/completion.rb +304 -0
  73. data/lib/em/connection.rb +770 -0
  74. data/lib/em/deferrable.rb +210 -0
  75. data/lib/em/deferrable/pool.rb +2 -0
  76. data/lib/em/file_watch.rb +73 -0
  77. data/lib/em/future.rb +61 -0
  78. data/lib/em/iterator.rb +252 -0
  79. data/lib/em/messages.rb +66 -0
  80. data/lib/em/pool.rb +151 -0
  81. data/lib/em/process_watch.rb +45 -0
  82. data/lib/em/processes.rb +123 -0
  83. data/lib/em/protocols.rb +37 -0
  84. data/lib/em/protocols/header_and_content.rb +138 -0
  85. data/lib/em/protocols/httpclient.rb +299 -0
  86. data/lib/em/protocols/httpclient2.rb +600 -0
  87. data/lib/em/protocols/line_and_text.rb +125 -0
  88. data/lib/em/protocols/line_protocol.rb +29 -0
  89. data/lib/em/protocols/linetext2.rb +166 -0
  90. data/lib/em/protocols/memcache.rb +331 -0
  91. data/lib/em/protocols/object_protocol.rb +46 -0
  92. data/lib/em/protocols/postgres3.rb +246 -0
  93. data/lib/em/protocols/saslauth.rb +175 -0
  94. data/lib/em/protocols/smtpclient.rb +394 -0
  95. data/lib/em/protocols/smtpserver.rb +666 -0
  96. data/lib/em/protocols/socks4.rb +66 -0
  97. data/lib/em/protocols/stomp.rb +205 -0
  98. data/lib/em/protocols/tcptest.rb +54 -0
  99. data/lib/em/pure_ruby.rb +1022 -0
  100. data/lib/em/queue.rb +80 -0
  101. data/lib/em/resolver.rb +232 -0
  102. data/lib/em/spawnable.rb +84 -0
  103. data/lib/em/streamer.rb +118 -0
  104. data/lib/em/threaded_resource.rb +90 -0
  105. data/lib/em/tick_loop.rb +85 -0
  106. data/lib/em/timers.rb +61 -0
  107. data/lib/em/version.rb +3 -0
  108. data/lib/eventmachine.rb +1584 -0
  109. data/lib/fastfilereaderext.rb +2 -0
  110. data/lib/jeventmachine.rb +301 -0
  111. data/lib/rubyeventmachine.rb +2 -0
  112. data/rakelib/package.rake +120 -0
  113. data/rakelib/test.rake +8 -0
  114. data/tests/client.crt +31 -0
  115. data/tests/client.key +51 -0
  116. data/tests/dhparam.pem +13 -0
  117. data/tests/em_test_helper.rb +151 -0
  118. data/tests/test_attach.rb +151 -0
  119. data/tests/test_basic.rb +283 -0
  120. data/tests/test_channel.rb +75 -0
  121. data/tests/test_completion.rb +178 -0
  122. data/tests/test_connection_count.rb +54 -0
  123. data/tests/test_connection_write.rb +35 -0
  124. data/tests/test_defer.rb +35 -0
  125. data/tests/test_deferrable.rb +35 -0
  126. data/tests/test_epoll.rb +142 -0
  127. data/tests/test_error_handler.rb +38 -0
  128. data/tests/test_exc.rb +28 -0
  129. data/tests/test_file_watch.rb +66 -0
  130. data/tests/test_fork.rb +75 -0
  131. data/tests/test_futures.rb +170 -0
  132. data/tests/test_get_sock_opt.rb +37 -0
  133. data/tests/test_handler_check.rb +35 -0
  134. data/tests/test_hc.rb +155 -0
  135. data/tests/test_httpclient.rb +233 -0
  136. data/tests/test_httpclient2.rb +128 -0
  137. data/tests/test_idle_connection.rb +25 -0
  138. data/tests/test_inactivity_timeout.rb +54 -0
  139. data/tests/test_ipv4.rb +125 -0
  140. data/tests/test_ipv6.rb +131 -0
  141. data/tests/test_iterator.rb +115 -0
  142. data/tests/test_kb.rb +28 -0
  143. data/tests/test_line_protocol.rb +33 -0
  144. data/tests/test_ltp.rb +138 -0
  145. data/tests/test_ltp2.rb +308 -0
  146. data/tests/test_many_fds.rb +22 -0
  147. data/tests/test_next_tick.rb +104 -0
  148. data/tests/test_object_protocol.rb +36 -0
  149. data/tests/test_pause.rb +107 -0
  150. data/tests/test_pending_connect_timeout.rb +52 -0
  151. data/tests/test_pool.rb +196 -0
  152. data/tests/test_process_watch.rb +50 -0
  153. data/tests/test_processes.rb +128 -0
  154. data/tests/test_proxy_connection.rb +180 -0
  155. data/tests/test_pure.rb +88 -0
  156. data/tests/test_queue.rb +64 -0
  157. data/tests/test_resolver.rb +104 -0
  158. data/tests/test_running.rb +14 -0
  159. data/tests/test_sasl.rb +47 -0
  160. data/tests/test_send_file.rb +217 -0
  161. data/tests/test_servers.rb +33 -0
  162. data/tests/test_set_sock_opt.rb +39 -0
  163. data/tests/test_shutdown_hooks.rb +23 -0
  164. data/tests/test_smtpclient.rb +75 -0
  165. data/tests/test_smtpserver.rb +57 -0
  166. data/tests/test_spawn.rb +293 -0
  167. data/tests/test_ssl_args.rb +78 -0
  168. data/tests/test_ssl_dhparam.rb +83 -0
  169. data/tests/test_ssl_ecdh_curve.rb +79 -0
  170. data/tests/test_ssl_extensions.rb +49 -0
  171. data/tests/test_ssl_methods.rb +65 -0
  172. data/tests/test_ssl_protocols.rb +246 -0
  173. data/tests/test_ssl_verify.rb +126 -0
  174. data/tests/test_stomp.rb +37 -0
  175. data/tests/test_system.rb +46 -0
  176. data/tests/test_threaded_resource.rb +61 -0
  177. data/tests/test_tick_loop.rb +59 -0
  178. data/tests/test_timers.rb +123 -0
  179. data/tests/test_ud.rb +8 -0
  180. data/tests/test_unbind_reason.rb +52 -0
  181. metadata +381 -0
@@ -0,0 +1,90 @@
1
+ module EventMachine
2
+ # = EventMachine::ThreadedResource
3
+ #
4
+ # A threaded resource is a "quick and dirty" wrapper around the concept of
5
+ # wiring up synchronous code into a standard EM::Pool. This is useful to keep
6
+ # interfaces coherent and provide a simple approach at "making an interface
7
+ # async-ish".
8
+ #
9
+ # General usage is to wrap libraries that do not support EventMachine, or to
10
+ # have a specific number of dedicated high-cpu worker resources.
11
+ #
12
+ # == Basic Usage example
13
+ #
14
+ # This example requires the cassandra gem. The cassandra gem contains an
15
+ # EventMachine interface, but it's sadly Fiber based and thus only works on
16
+ # 1.9. It also requires (potentially) complex stack switching logic to reach
17
+ # completion of nested operations. By contrast this approach provides a block
18
+ # in which normal synchronous code can occur, but makes no attempt to wire the
19
+ # IO into EventMachines C++ IO implementations, instead relying on the reactor
20
+ # pattern in rb_thread_select.
21
+ #
22
+ # cassandra_dispatcher = ThreadedResource.new do
23
+ # Cassandra.new('allthethings', '127.0.0.1:9160')
24
+ # end
25
+ #
26
+ # pool = EM::Pool.new
27
+ #
28
+ # pool.add cassandra_dispatcher
29
+ #
30
+ # # If we don't care about the result:
31
+ # pool.perform do |dispatcher|
32
+ # # The following block executes inside a dedicated thread, and should not
33
+ # # access EventMachine things:
34
+ # dispatcher.dispatch do |cassandra|
35
+ # cassandra.insert(:Things, '10', 'stuff' => 'things')
36
+ # end
37
+ # end
38
+ #
39
+ # # Example where we care about the result:
40
+ # pool.perform do |dispatcher|
41
+ # # The dispatch block is executed in the resources thread.
42
+ # completion = dispatcher.dispatch do |cassandra|
43
+ # cassandra.get(:Things, '10', 'stuff')
44
+ # end
45
+ #
46
+ # # This block will be yielded on the EM thread:
47
+ # completion.callback do |result|
48
+ # EM.do_something_with(result)
49
+ # end
50
+ #
51
+ # completion
52
+ # end
53
+ class ThreadedResource
54
+
55
+ # The block should return the resource that will be yielded in a dispatch.
56
+ def initialize
57
+ @resource = yield
58
+
59
+ @running = true
60
+ @queue = ::Queue.new
61
+ @thread = Thread.new do
62
+ @queue.pop.call while @running
63
+ end
64
+ end
65
+
66
+ # Called on the EM thread, generally in a perform block to return a
67
+ # completion for the work.
68
+ def dispatch
69
+ completion = EM::Completion.new
70
+ @queue << lambda do
71
+ begin
72
+ result = yield @resource
73
+ completion.succeed result
74
+ rescue => e
75
+ completion.fail e
76
+ end
77
+ end
78
+ completion
79
+ end
80
+
81
+ # Kill the internal thread. should only be used to cleanup - generally
82
+ # only required for tests.
83
+ def shutdown
84
+ @running = false
85
+ @queue << lambda {}
86
+ @thread.join
87
+ end
88
+
89
+ end
90
+ end
@@ -0,0 +1,85 @@
1
+ module EventMachine
2
+ # Creates and immediately starts an EventMachine::TickLoop
3
+ def self.tick_loop(*a, &b)
4
+ TickLoop.new(*a, &b).start
5
+ end
6
+
7
+ # A TickLoop is useful when one needs to distribute amounts of work
8
+ # throughout ticks in order to maintain response times. It is also useful for
9
+ # simple repeated checks and metrics.
10
+ # @example
11
+ # # Here we run through an array one item per tick until it is empty,
12
+ # # printing each element.
13
+ # # When the array is empty, we return :stop from the callback, and the
14
+ # # loop will terminate.
15
+ # # When the loop terminates, the on_stop callbacks will be called.
16
+ # EM.run do
17
+ # array = (1..100).to_a
18
+ #
19
+ # tickloop = EM.tick_loop do
20
+ # if array.empty?
21
+ # :stop
22
+ # else
23
+ # puts array.shift
24
+ # end
25
+ # end
26
+ #
27
+ # tickloop.on_stop { EM.stop }
28
+ # end
29
+ #
30
+ class TickLoop
31
+
32
+ # Arguments: A callback (EM::Callback) to call each tick. If the call
33
+ # returns +:stop+ then the loop will be stopped. Any other value is
34
+ # ignored.
35
+ def initialize(*a, &b)
36
+ @work = EM::Callback(*a, &b)
37
+ @stops = []
38
+ @stopped = true
39
+ end
40
+
41
+ # Arguments: A callback (EM::Callback) to call once on the next stop (or
42
+ # immediately if already stopped).
43
+ def on_stop(*a, &b)
44
+ if @stopped
45
+ EM::Callback(*a, &b).call
46
+ else
47
+ @stops << EM::Callback(*a, &b)
48
+ end
49
+ end
50
+
51
+ # Stop the tick loop immediately, and call it's on_stop callbacks.
52
+ def stop
53
+ @stopped = true
54
+ until @stops.empty?
55
+ @stops.shift.call
56
+ end
57
+ end
58
+
59
+ # Query if the loop is stopped.
60
+ def stopped?
61
+ @stopped
62
+ end
63
+
64
+ # Start the tick loop, will raise argument error if the loop is already
65
+ # running.
66
+ def start
67
+ raise ArgumentError, "double start" unless @stopped
68
+ @stopped = false
69
+ schedule
70
+ end
71
+
72
+ private
73
+ def schedule
74
+ EM.next_tick do
75
+ next if @stopped
76
+ if @work.call == :stop
77
+ stop
78
+ else
79
+ schedule
80
+ end
81
+ end
82
+ self
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,61 @@
1
+ module EventMachine
2
+ # Creates a one-time timer
3
+ #
4
+ # timer = EventMachine::Timer.new(5) do
5
+ # # this will never fire because we cancel it
6
+ # end
7
+ # timer.cancel
8
+ #
9
+ class Timer
10
+ # Create a new timer that fires after a given number of seconds
11
+ def initialize interval, callback=nil, &block
12
+ @signature = EventMachine::add_timer(interval, callback || block)
13
+ end
14
+
15
+ # Cancel the timer
16
+ def cancel
17
+ EventMachine.send :cancel_timer, @signature
18
+ end
19
+ end
20
+
21
+ # Creates a periodic timer
22
+ #
23
+ # @example
24
+ # n = 0
25
+ # timer = EventMachine::PeriodicTimer.new(5) do
26
+ # puts "the time is #{Time.now}"
27
+ # timer.cancel if (n+=1) > 5
28
+ # end
29
+ #
30
+ class PeriodicTimer
31
+ # Create a new periodic timer that executes every interval seconds
32
+ def initialize interval, callback=nil, &block
33
+ @interval = interval
34
+ @code = callback || block
35
+ @cancelled = false
36
+ @work = method(:fire)
37
+ schedule
38
+ end
39
+
40
+ # Cancel the periodic timer
41
+ def cancel
42
+ @cancelled = true
43
+ end
44
+
45
+ # Fire the timer every interval seconds
46
+ attr_accessor :interval
47
+
48
+ # @private
49
+ def schedule
50
+ EventMachine::add_timer @interval, @work
51
+ end
52
+
53
+ # @private
54
+ def fire
55
+ unless @cancelled
56
+ @code.call
57
+ schedule
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module EventMachine
2
+ VERSION = "1.2.0.dev.2"
3
+ end
@@ -0,0 +1,1584 @@
1
+ if defined?(EventMachine.library_type) and EventMachine.library_type == :pure_ruby
2
+ # assume 'em/pure_ruby' was loaded already
3
+ elsif RUBY_PLATFORM =~ /java/
4
+ require 'java'
5
+ require 'jeventmachine'
6
+ else
7
+ begin
8
+ require 'rubyeventmachine'
9
+ rescue LoadError
10
+ warn "Unable to load the EventMachine C extension; To use the pure-ruby reactor, require 'em/pure_ruby'"
11
+ raise
12
+ end
13
+ end
14
+
15
+ require 'em/version'
16
+ require 'em/pool'
17
+ require 'em/deferrable'
18
+ require 'em/future'
19
+ require 'em/streamer'
20
+ require 'em/spawnable'
21
+ require 'em/processes'
22
+ require 'em/iterator'
23
+ require 'em/buftok'
24
+ require 'em/timers'
25
+ require 'em/protocols'
26
+ require 'em/connection'
27
+ require 'em/callback'
28
+ require 'em/queue'
29
+ require 'em/channel'
30
+ require 'em/file_watch'
31
+ require 'em/process_watch'
32
+ require 'em/tick_loop'
33
+ require 'em/resolver'
34
+ require 'em/completion'
35
+ require 'em/threaded_resource'
36
+
37
+ require 'shellwords'
38
+ require 'thread'
39
+ require 'resolv'
40
+
41
+ # Top-level EventMachine namespace. If you are looking for EventMachine examples, see {file:docs/GettingStarted.md EventMachine tutorial}.
42
+ #
43
+ # ## Key methods ##
44
+ # ### Starting and stopping the event loop ###
45
+ #
46
+ # * {EventMachine.run}
47
+ # * {EventMachine.stop_event_loop}
48
+ #
49
+ # ### Implementing clients ###
50
+ #
51
+ # * {EventMachine.connect}
52
+ #
53
+ # ### Implementing servers ###
54
+ #
55
+ # * {EventMachine.start_server}
56
+ #
57
+ # ### Working with timers ###
58
+ #
59
+ # * {EventMachine.add_timer}
60
+ # * {EventMachine.add_periodic_timer}
61
+ # * {EventMachine.cancel_timer}
62
+ #
63
+ # ### Working with blocking tasks ###
64
+ #
65
+ # * {EventMachine.defer}
66
+ # * {EventMachine.next_tick}
67
+ #
68
+ # ### Efficient proxying ###
69
+ #
70
+ # * {EventMachine.enable_proxy}
71
+ # * {EventMachine.disable_proxy}
72
+ module EventMachine
73
+ class << self
74
+ # Exposed to allow joining on the thread, when run in a multithreaded
75
+ # environment. Performing other actions on the thread has undefined
76
+ # semantics (read: a dangerous endevor).
77
+ #
78
+ # @return [Thread]
79
+ attr_reader :reactor_thread
80
+ end
81
+ @next_tick_mutex = Mutex.new
82
+ @reactor_running = false
83
+ @next_tick_queue = []
84
+ @tails = []
85
+ @threadpool = @threadqueue = @resultqueue = nil
86
+ @all_threads_spawned = false
87
+
88
+ # System errnos
89
+ # @private
90
+ ERRNOS = Errno::constants.grep(/^E/).inject(Hash.new(:unknown)) { |hash, name|
91
+ errno = Errno.__send__(:const_get, name)
92
+ hash[errno::Errno] = errno
93
+ hash
94
+ }
95
+
96
+ # Initializes and runs an event loop. This method only returns if code inside the block passed to this method
97
+ # calls {EventMachine.stop_event_loop}. The block is executed after initializing its internal event loop but *before* running the loop,
98
+ # therefore this block is the right place to call any code that needs event loop to run, for example, {EventMachine.start_server},
99
+ # {EventMachine.connect} or similar methods of libraries that use EventMachine under the hood
100
+ # (like `EventMachine::HttpRequest.new` or `AMQP.start`).
101
+ #
102
+ # Programs that are run for long periods of time (e.g. servers) usually start event loop by calling {EventMachine.run}, and let it
103
+ # run "forever". It's also possible to use {EventMachine.run} to make a single client-connection to a remote server,
104
+ # process the data flow from that single connection, and then call {EventMachine.stop_event_loop} to stop, in other words,
105
+ # to run event loop for a short period of time (necessary to complete some operation) and then shut it down.
106
+ #
107
+ # Once event loop is running, it is perfectly possible to start multiple servers and clients simultaneously: content-aware
108
+ # proxies like [Proxymachine](https://github.com/mojombo/proxymachine) do just that.
109
+ #
110
+ # ## Using EventMachine with Ruby on Rails and other Web application frameworks ##
111
+ #
112
+ # Standalone applications often run event loop on the main thread, thus blocking for their entire lifespan. In case of Web applications,
113
+ # if you are running an EventMachine-based app server such as [Thin](http://code.macournoyer.com/thin/) or [Goliath](https://github.com/postrank-labs/goliath/),
114
+ # they start event loop for you. Servers like Unicorn, Apache Passenger or Mongrel occupy main Ruby thread to serve HTTP(S) requests. This means
115
+ # that calling {EventMachine.run} on the same thread is not an option (it will result in Web server never binding to the socket).
116
+ # In that case, start event loop in a separate thread as demonstrated below.
117
+ #
118
+ #
119
+ # @example Starting EventMachine event loop in the current thread to run the "Hello, world"-like Echo server example
120
+ #
121
+ # #!/usr/bin/env ruby
122
+ #
123
+ # require 'rubygems' # or use Bundler.setup
124
+ # require 'eventmachine'
125
+ #
126
+ # class EchoServer < EM::Connection
127
+ # def receive_data(data)
128
+ # send_data(data)
129
+ # end
130
+ # end
131
+ #
132
+ # EventMachine.run do
133
+ # EventMachine.start_server("0.0.0.0", 10000, EchoServer)
134
+ # end
135
+ #
136
+ #
137
+ # @example Starting EventMachine event loop in a separate thread
138
+ #
139
+ # # doesn't block current thread, can be used with Ruby on Rails, Sinatra, Merb, Rack
140
+ # # and any other application server that occupies main Ruby thread.
141
+ # Thread.new { EventMachine.run }
142
+ #
143
+ #
144
+ # @note This method blocks calling thread. If you need to start EventMachine event loop from a Web app
145
+ # running on a non event-driven server (Unicorn, Apache Passenger, Mongrel), do it in a separate thread like demonstrated
146
+ # in one of the examples.
147
+ # @see file:docs/GettingStarted.md Getting started with EventMachine
148
+ # @see EventMachine.stop_event_loop
149
+ def self.run blk=nil, tail=nil, &block
150
+ # Obsoleted the use_threads mechanism.
151
+ # 25Nov06: Added the begin/ensure block. We need to be sure that release_machine
152
+ # gets called even if an exception gets thrown within any of the user code
153
+ # that the event loop runs. The best way to see this is to run a unit
154
+ # test with two functions, each of which calls {EventMachine.run} and each of
155
+ # which throws something inside of #run. Without the ensure, the second test
156
+ # will start without release_machine being called and will immediately throw
157
+
158
+ #
159
+ if @reactor_running and @reactor_pid != Process.pid
160
+ # Reactor was started in a different parent, meaning we have forked.
161
+ # Clean up reactor state so a new reactor boots up in this child.
162
+ stop_event_loop
163
+ release_machine
164
+ cleanup_machine
165
+ @reactor_running = false
166
+ end
167
+
168
+ tail and @tails.unshift(tail)
169
+
170
+ if reactor_running?
171
+ (b = blk || block) and b.call # next_tick(b)
172
+ else
173
+ @conns = {}
174
+ @acceptors = {}
175
+ @timers = {}
176
+ @wrapped_exception = nil
177
+ @next_tick_queue ||= []
178
+ @tails ||= []
179
+ begin
180
+ @reactor_pid = Process.pid
181
+ @reactor_running = true
182
+ initialize_event_machine
183
+ (b = blk || block) and add_timer(0, b)
184
+ if @next_tick_queue && !@next_tick_queue.empty?
185
+ add_timer(0) { signal_loopbreak }
186
+ end
187
+ @reactor_thread = Thread.current
188
+
189
+ # Rubinius needs to come back into "Ruby space" for GC to work,
190
+ # so we'll crank the machine here.
191
+ if defined?(RUBY_ENGINE) && RUBY_ENGINE == "rbx"
192
+ while run_machine_once; end
193
+ else
194
+ run_machine
195
+ end
196
+
197
+ ensure
198
+ until @tails.empty?
199
+ @tails.pop.call
200
+ end
201
+
202
+ release_machine
203
+ cleanup_machine
204
+ @reactor_running = false
205
+ @reactor_thread = nil
206
+ end
207
+
208
+ raise @wrapped_exception if @wrapped_exception
209
+ end
210
+ end
211
+
212
+ # Sugars a common use case. Will pass the given block to #run, but will terminate
213
+ # the reactor loop and exit the function as soon as the code in the block completes.
214
+ # (Normally, {EventMachine.run} keeps running indefinitely, even after the block supplied to it
215
+ # finishes running, until user code calls {EventMachine.stop})
216
+ #
217
+ def self.run_block &block
218
+ pr = proc {
219
+ block.call
220
+ EventMachine::stop
221
+ }
222
+ run(&pr)
223
+ end
224
+
225
+ # @return [Boolean] true if the calling thread is the same thread as the reactor.
226
+ def self.reactor_thread?
227
+ Thread.current == @reactor_thread
228
+ end
229
+
230
+ # Runs the given callback on the reactor thread, or immediately if called
231
+ # from the reactor thread. Accepts the same arguments as {EventMachine::Callback}
232
+ def self.schedule(*a, &b)
233
+ cb = Callback(*a, &b)
234
+ if reactor_running? && reactor_thread?
235
+ cb.call
236
+ else
237
+ next_tick { cb.call }
238
+ end
239
+ end
240
+
241
+ # Forks a new process, properly stops the reactor and then calls {EventMachine.run} inside of it again, passing your block.
242
+ def self.fork_reactor &block
243
+ # This implementation is subject to change, especially if we clean up the relationship
244
+ # of EM#run to @reactor_running.
245
+ # Original patch by Aman Gupta.
246
+ #
247
+ Kernel.fork do
248
+ if reactor_running?
249
+ stop_event_loop
250
+ release_machine
251
+ cleanup_machine
252
+ @reactor_running = false
253
+ @reactor_thread = nil
254
+ end
255
+ run block
256
+ end
257
+ end
258
+
259
+ # Clean up Ruby space following a release_machine
260
+ def self.cleanup_machine
261
+ if @threadpool && !@threadpool.empty?
262
+ # Tell the threads to stop
263
+ @threadpool.each { |t| t.exit }
264
+ # Join the threads or bump the stragglers one more time
265
+ @threadpool.each { |t| t.join 0.01 || t.exit }
266
+ end
267
+ @threadpool = nil
268
+ @threadqueue = nil
269
+ @resultqueue = nil
270
+ @all_threads_spawned = false
271
+ @next_tick_queue = []
272
+ end
273
+
274
+ # Adds a block to call as the reactor is shutting down.
275
+ #
276
+ # These callbacks are called in the _reverse_ order to which they are added.
277
+ #
278
+ # @example Scheduling operations to be run when EventMachine event loop is stopped
279
+ #
280
+ # EventMachine.run do
281
+ # EventMachine.add_shutdown_hook { puts "b" }
282
+ # EventMachine.add_shutdown_hook { puts "a" }
283
+ # EventMachine.stop
284
+ # end
285
+ #
286
+ # # Outputs:
287
+ # # a
288
+ # # b
289
+ #
290
+ def self.add_shutdown_hook &block
291
+ @tails << block
292
+ end
293
+
294
+ # Adds a one-shot timer to the event loop.
295
+ # Call it with one or two parameters. The first parameters is a delay-time
296
+ # expressed in *seconds* (not milliseconds). The second parameter, if
297
+ # present, must be an object that responds to :call. If 2nd parameter is not given, then you
298
+ # can also simply pass a block to the method call.
299
+ #
300
+ # This method may be called from the block passed to {EventMachine.run}
301
+ # or from any callback method. It schedules execution of the proc or block
302
+ # passed to it, after the passage of an interval of time equal to
303
+ # *at least* the number of seconds specified in the first parameter to
304
+ # the call.
305
+ #
306
+ # {EventMachine.add_timer} is a non-blocking method. Callbacks can and will
307
+ # be called during the interval of time that the timer is in effect.
308
+ # There is no built-in limit to the number of timers that can be outstanding at
309
+ # any given time.
310
+ #
311
+ # @example Setting a one-shot timer with EventMachine
312
+ #
313
+ # EventMachine.run {
314
+ # puts "Starting the run now: #{Time.now}"
315
+ # EventMachine.add_timer 5, proc { puts "Executing timer event: #{Time.now}" }
316
+ # EventMachine.add_timer(10) { puts "Executing timer event: #{Time.now}" }
317
+ # }
318
+ #
319
+ # @param [Integer] delay Delay in seconds
320
+ # @see EventMachine::Timer
321
+ # @see EventMachine.add_periodic_timer
322
+ def self.add_timer *args, &block
323
+ interval = args.shift
324
+ code = args.shift || block
325
+ if code
326
+ # check too many timers!
327
+ s = add_oneshot_timer((interval.to_f * 1000).to_i)
328
+ @timers[s] = code
329
+ s
330
+ end
331
+ end
332
+
333
+ # Adds a periodic timer to the event loop.
334
+ # It takes the same parameters as the one-shot timer method, {EventMachine.add_timer}.
335
+ # This method schedules execution of the given block repeatedly, at intervals
336
+ # of time *at least* as great as the number of seconds given in the first
337
+ # parameter to the call.
338
+ #
339
+ # @example Write a dollar-sign to stderr every five seconds, without blocking
340
+ #
341
+ # EventMachine.run {
342
+ # EventMachine.add_periodic_timer( 5 ) { $stderr.write "$" }
343
+ # }
344
+ #
345
+ # @param [Integer] delay Delay in seconds
346
+ #
347
+ # @see EventMachine::PeriodicTimer
348
+ # @see EventMachine.add_timer
349
+ #
350
+ def self.add_periodic_timer *args, &block
351
+ interval = args.shift
352
+ code = args.shift || block
353
+
354
+ EventMachine::PeriodicTimer.new(interval, code)
355
+ end
356
+
357
+
358
+ # Cancel a timer (can be a callback or an {EventMachine::Timer} instance).
359
+ #
360
+ # @param [#cancel, #call] timer_or_sig A timer to cancel
361
+ # @see EventMachine::Timer#cancel
362
+ def self.cancel_timer timer_or_sig
363
+ if timer_or_sig.respond_to? :cancel
364
+ timer_or_sig.cancel
365
+ else
366
+ @timers[timer_or_sig] = false if @timers.has_key?(timer_or_sig)
367
+ end
368
+ end
369
+
370
+
371
+ # Causes the processing loop to stop executing, which will cause all open connections and accepting servers
372
+ # to be run down and closed. Connection termination callbacks added using {EventMachine.add_shutdown_hook}
373
+ # will be called as part of running this method.
374
+ #
375
+ # When all of this processing is complete, the call to {EventMachine.run} which started the processing loop
376
+ # will return and program flow will resume from the statement following {EventMachine.run} call.
377
+ #
378
+ # @example Stopping a running EventMachine event loop
379
+ #
380
+ # require 'rubygems'
381
+ # require 'eventmachine'
382
+ #
383
+ # module Redmond
384
+ # def post_init
385
+ # puts "We're sending a dumb HTTP request to the remote peer."
386
+ # send_data "GET / HTTP/1.1\r\nHost: www.microsoft.com\r\n\r\n"
387
+ # end
388
+ #
389
+ # def receive_data data
390
+ # puts "We received #{data.length} bytes from the remote peer."
391
+ # puts "We're going to stop the event loop now."
392
+ # EventMachine::stop_event_loop
393
+ # end
394
+ #
395
+ # def unbind
396
+ # puts "A connection has terminated."
397
+ # end
398
+ # end
399
+ #
400
+ # puts "We're starting the event loop now."
401
+ # EventMachine.run {
402
+ # EventMachine.connect "www.microsoft.com", 80, Redmond
403
+ # }
404
+ # puts "The event loop has stopped."
405
+ #
406
+ # # This program will produce approximately the following output:
407
+ # #
408
+ # # We're starting the event loop now.
409
+ # # We're sending a dumb HTTP request to the remote peer.
410
+ # # We received 1440 bytes from the remote peer.
411
+ # # We're going to stop the event loop now.
412
+ # # A connection has terminated.
413
+ # # The event loop has stopped.
414
+ #
415
+ #
416
+ def self.stop_event_loop
417
+ EventMachine::stop
418
+ end
419
+
420
+ # Initiates a TCP server (socket acceptor) on the specified IP address and port.
421
+ #
422
+ # The IP address must be valid on the machine where the program
423
+ # runs, and the process must be privileged enough to listen
424
+ # on the specified port (on Unix-like systems, superuser privileges
425
+ # are usually required to listen on any port lower than 1024).
426
+ # Only one listener may be running on any given address/port
427
+ # combination. start_server will fail if the given address and port
428
+ # are already listening on the machine, either because of a prior call
429
+ # to {.start_server} or some unrelated process running on the machine.
430
+ # If {.start_server} succeeds, the new network listener becomes active
431
+ # immediately and starts accepting connections from remote peers,
432
+ # and these connections generate callback events that are processed
433
+ # by the code specified in the handler parameter to {.start_server}.
434
+ #
435
+ # The optional handler which is passed to this method is the key
436
+ # to EventMachine's ability to handle particular network protocols.
437
+ # The handler parameter passed to start_server must be a Ruby Module
438
+ # that you must define. When the network server that is started by
439
+ # start_server accepts a new connection, it instantiates a new
440
+ # object of an anonymous class that is inherited from {EventMachine::Connection},
441
+ # *into which your handler module have been included*. Arguments passed into start_server
442
+ # after the class name are passed into the constructor during the instantiation.
443
+ #
444
+ # Your handler module may override any of the methods in {EventMachine::Connection},
445
+ # such as {EventMachine::Connection#receive_data}, in order to implement the specific behavior
446
+ # of the network protocol.
447
+ #
448
+ # Callbacks invoked in response to network events *always* take place
449
+ # within the execution context of the object derived from {EventMachine::Connection}
450
+ # extended by your handler module. There is one object per connection, and
451
+ # all of the callbacks invoked for a particular connection take the form
452
+ # of instance methods called against the corresponding {EventMachine::Connection}
453
+ # object. Therefore, you are free to define whatever instance variables you
454
+ # wish, in order to contain the per-connection state required by the network protocol you are
455
+ # implementing.
456
+ #
457
+ # {EventMachine.start_server} is usually called inside the block passed to {EventMachine.run},
458
+ # but it can be called from any EventMachine callback. {EventMachine.start_server} will fail
459
+ # unless the EventMachine event loop is currently running (which is why
460
+ # it's often called in the block suppled to {EventMachine.run}).
461
+ #
462
+ # You may call start_server any number of times to start up network
463
+ # listeners on different address/port combinations. The servers will
464
+ # all run simultaneously. More interestingly, each individual call to start_server
465
+ # can specify a different handler module and thus implement a different
466
+ # network protocol from all the others.
467
+ #
468
+ # @example
469
+ #
470
+ # require 'rubygems'
471
+ # require 'eventmachine'
472
+ #
473
+ # # Here is an example of a server that counts lines of input from the remote
474
+ # # peer and sends back the total number of lines received, after each line.
475
+ # # Try the example with more than one client connection opened via telnet,
476
+ # # and you will see that the line count increments independently on each
477
+ # # of the client connections. Also very important to note, is that the
478
+ # # handler for the receive_data function, which our handler redefines, may
479
+ # # not assume that the data it receives observes any kind of message boundaries.
480
+ # # Also, to use this example, be sure to change the server and port parameters
481
+ # # to the start_server call to values appropriate for your environment.
482
+ # module LineCounter
483
+ # MaxLinesPerConnection = 10
484
+ #
485
+ # def post_init
486
+ # puts "Received a new connection"
487
+ # @data_received = ""
488
+ # @line_count = 0
489
+ # end
490
+ #
491
+ # def receive_data data
492
+ # @data_received << data
493
+ # while @data_received.slice!( /^[^\n]*[\n]/m )
494
+ # @line_count += 1
495
+ # send_data "received #{@line_count} lines so far\r\n"
496
+ # @line_count == MaxLinesPerConnection and close_connection_after_writing
497
+ # end
498
+ # end
499
+ # end
500
+ #
501
+ # EventMachine.run {
502
+ # host, port = "192.168.0.100", 8090
503
+ # EventMachine.start_server host, port, LineCounter
504
+ # puts "Now accepting connections on address #{host}, port #{port}..."
505
+ # EventMachine.add_periodic_timer(10) { $stderr.write "*" }
506
+ # }
507
+ #
508
+ # @param [String] server Host to bind to.
509
+ # @param [Integer] port Port to bind to.
510
+ # @param [Module, Class] handler A module or class that implements connection callbacks
511
+ #
512
+ # @note Don't forget that in order to bind to ports < 1024 on Linux, *BSD and Mac OS X your process must have superuser privileges.
513
+ #
514
+ # @see file:docs/GettingStarted.md EventMachine tutorial
515
+ # @see EventMachine.stop_server
516
+ def self.start_server server, port=nil, handler=nil, *args, &block
517
+ begin
518
+ port = Integer(port)
519
+ rescue ArgumentError, TypeError
520
+ # there was no port, so server must be a unix domain socket
521
+ # the port argument is actually the handler, and the handler is one of the args
522
+ args.unshift handler if handler
523
+ handler = port
524
+ port = nil
525
+ end if port
526
+
527
+ klass = klass_from_handler(Connection, handler, *args)
528
+
529
+ s = if port
530
+ start_tcp_server server, port
531
+ else
532
+ start_unix_server server
533
+ end
534
+ @acceptors[s] = [klass,args,block]
535
+ s
536
+ end
537
+
538
+ # Attach to an existing socket's file descriptor. The socket may have been
539
+ # started with {EventMachine.start_server}.
540
+ def self.attach_server sock, handler=nil, *args, &block
541
+ klass = klass_from_handler(Connection, handler, *args)
542
+ sd = sock.respond_to?(:fileno) ? sock.fileno : sock
543
+ s = attach_sd(sd)
544
+ @acceptors[s] = [klass,args,block,sock]
545
+ s
546
+ end
547
+
548
+ # Stop a TCP server socket that was started with {EventMachine.start_server}.
549
+ # @see EventMachine.start_server
550
+ def self.stop_server signature
551
+ EventMachine::stop_tcp_server signature
552
+ end
553
+
554
+ # Start a Unix-domain server.
555
+ #
556
+ # Note that this is an alias for {EventMachine.start_server}, which can be used to start both
557
+ # TCP and Unix-domain servers.
558
+ #
559
+ # @see EventMachine.start_server
560
+ def self.start_unix_domain_server filename, *args, &block
561
+ start_server filename, *args, &block
562
+ end
563
+
564
+ # Initiates a TCP connection to a remote server and sets up event handling for the connection.
565
+ # {EventMachine.connect} requires event loop to be running (see {EventMachine.run}).
566
+ #
567
+ # {EventMachine.connect} takes the IP address (or hostname) and
568
+ # port of the remote server you want to connect to.
569
+ # It also takes an optional handler (a module or a subclass of {EventMachine::Connection}) which you must define, that
570
+ # contains the callbacks that will be invoked by the event loop on behalf of the connection.
571
+ #
572
+ # Learn more about connection lifecycle callbacks in the {file:docs/GettingStarted.md EventMachine tutorial} and
573
+ # {file:docs/ConnectionLifecycleCallbacks.md Connection lifecycle guide}.
574
+ #
575
+ #
576
+ # @example
577
+ #
578
+ # # Here's a program which connects to a web server, sends a naive
579
+ # # request, parses the HTTP header of the response, and then
580
+ # # (antisocially) ends the event loop, which automatically drops the connection
581
+ # # (and incidentally calls the connection's unbind method).
582
+ # module DumbHttpClient
583
+ # def post_init
584
+ # send_data "GET / HTTP/1.1\r\nHost: _\r\n\r\n"
585
+ # @data = ""
586
+ # @parsed = false
587
+ # end
588
+ #
589
+ # def receive_data data
590
+ # @data << data
591
+ # if !@parsed and @data =~ /[\n][\r]*[\n]/m
592
+ # @parsed = true
593
+ # puts "RECEIVED HTTP HEADER:"
594
+ # $`.each {|line| puts ">>> #{line}" }
595
+ #
596
+ # puts "Now we'll terminate the loop, which will also close the connection"
597
+ # EventMachine::stop_event_loop
598
+ # end
599
+ # end
600
+ #
601
+ # def unbind
602
+ # puts "A connection has terminated"
603
+ # end
604
+ # end
605
+ #
606
+ # EventMachine.run {
607
+ # EventMachine.connect "www.bayshorenetworks.com", 80, DumbHttpClient
608
+ # }
609
+ # puts "The event loop has ended"
610
+ #
611
+ #
612
+ # @example Defining protocol handler as a class
613
+ #
614
+ # class MyProtocolHandler < EventMachine::Connection
615
+ # def initialize *args
616
+ # super
617
+ # # whatever else you want to do here
618
+ # end
619
+ #
620
+ # # ...
621
+ # end
622
+ #
623
+ #
624
+ # @param [String] server Host to connect to
625
+ # @param [Integer] port Port to connect to
626
+ # @param [Module, Class] handler A module or class that implements connection lifecycle callbacks
627
+ #
628
+ # @see EventMachine.start_server
629
+ # @see file:docs/GettingStarted.md EventMachine tutorial
630
+ def self.connect server, port=nil, handler=nil, *args, &blk
631
+ # EventMachine::connect initiates a TCP connection to a remote
632
+ # server and sets up event-handling for the connection.
633
+ # It internally creates an object that should not be handled
634
+ # by the caller. HOWEVER, it's often convenient to get the
635
+ # object to set up interfacing to other objects in the system.
636
+ # We return the newly-created anonymous-class object to the caller.
637
+ # It's expected that a considerable amount of code will depend
638
+ # on this behavior, so don't change it.
639
+ #
640
+ # Ok, added support for a user-defined block, 13Apr06.
641
+ # This leads us to an interesting choice because of the
642
+ # presence of the post_init call, which happens in the
643
+ # initialize method of the new object. We call the user's
644
+ # block and pass the new object to it. This is a great
645
+ # way to do protocol-specific initiation. It happens
646
+ # AFTER post_init has been called on the object, which I
647
+ # certainly hope is the right choice.
648
+ # Don't change this lightly, because accepted connections
649
+ # are different from connected ones and we don't want
650
+ # to have them behave differently with respect to post_init
651
+ # if at all possible.
652
+
653
+ bind_connect nil, nil, server, port, handler, *args, &blk
654
+ end
655
+
656
+ # This method is like {EventMachine.connect}, but allows for a local address/port
657
+ # to bind the connection to.
658
+ #
659
+ # @see EventMachine.connect
660
+ def self.bind_connect bind_addr, bind_port, server, port=nil, handler=nil, *args
661
+ begin
662
+ port = Integer(port)
663
+ rescue ArgumentError, TypeError
664
+ # there was no port, so server must be a unix domain socket
665
+ # the port argument is actually the handler, and the handler is one of the args
666
+ args.unshift handler if handler
667
+ handler = port
668
+ port = nil
669
+ end if port
670
+
671
+ klass = klass_from_handler(Connection, handler, *args)
672
+
673
+ s = if port
674
+ if bind_addr
675
+ bind_connect_server bind_addr, bind_port.to_i, server, port
676
+ else
677
+ connect_server server, port
678
+ end
679
+ else
680
+ connect_unix_server server
681
+ end
682
+
683
+ c = klass.new s, *args
684
+ @conns[s] = c
685
+ block_given? and yield c
686
+ c
687
+ end
688
+
689
+ # {EventMachine.watch} registers a given file descriptor or IO object with the eventloop. The
690
+ # file descriptor will not be modified (it will remain blocking or non-blocking).
691
+ #
692
+ # The eventloop can be used to process readable and writable events on the file descriptor, using
693
+ # {EventMachine::Connection#notify_readable=} and {EventMachine::Connection#notify_writable=}
694
+ #
695
+ # {EventMachine::Connection#notify_readable?} and {EventMachine::Connection#notify_writable?} can be used
696
+ # to check what events are enabled on the connection.
697
+ #
698
+ # To detach the file descriptor, use {EventMachine::Connection#detach}
699
+ #
700
+ # @example
701
+ #
702
+ # module SimpleHttpClient
703
+ # def notify_readable
704
+ # header = @io.readline
705
+ #
706
+ # if header == "\r\n"
707
+ # # detach returns the file descriptor number (fd == @io.fileno)
708
+ # fd = detach
709
+ # end
710
+ # rescue EOFError
711
+ # detach
712
+ # end
713
+ #
714
+ # def unbind
715
+ # EM.next_tick do
716
+ # # socket is detached from the eventloop, but still open
717
+ # data = @io.read
718
+ # end
719
+ # end
720
+ # end
721
+ #
722
+ # EventMachine.run {
723
+ # sock = TCPSocket.new('site.com', 80)
724
+ # sock.write("GET / HTTP/1.0\r\n\r\n")
725
+ # conn = EventMachine.watch(sock, SimpleHttpClient)
726
+ # conn.notify_readable = true
727
+ # }
728
+ #
729
+ # @author Riham Aldakkak (eSpace Technologies)
730
+ def EventMachine::watch io, handler=nil, *args, &blk
731
+ attach_io io, true, handler, *args, &blk
732
+ end
733
+
734
+ # Attaches an IO object or file descriptor to the eventloop as a regular connection.
735
+ # The file descriptor will be set as non-blocking, and EventMachine will process
736
+ # receive_data and send_data events on it as it would for any other connection.
737
+ #
738
+ # To watch a fd instead, use {EventMachine.watch}, which will not alter the state of the socket
739
+ # and fire notify_readable and notify_writable events instead.
740
+ def EventMachine::attach io, handler=nil, *args, &blk
741
+ attach_io io, false, handler, *args, &blk
742
+ end
743
+
744
+ # @private
745
+ def EventMachine::attach_io io, watch_mode, handler=nil, *args
746
+ klass = klass_from_handler(Connection, handler, *args)
747
+
748
+ if !watch_mode and klass.public_instance_methods.any?{|m| [:notify_readable, :notify_writable].include? m.to_sym }
749
+ raise ArgumentError, "notify_readable/writable with EM.attach is not supported. Use EM.watch(io){ |c| c.notify_readable = true }"
750
+ end
751
+
752
+ if io.respond_to?(:fileno)
753
+ # getDescriptorByFileno deprecated in JRuby 1.7.x, removed in JRuby 9000
754
+ if defined?(JRuby) && JRuby.runtime.respond_to?(:getDescriptorByFileno)
755
+ fd = JRuby.runtime.getDescriptorByFileno(io.fileno).getChannel
756
+ else
757
+ fd = io.fileno
758
+ end
759
+ else
760
+ fd = io
761
+ end
762
+
763
+ s = attach_fd fd, watch_mode
764
+ c = klass.new s, *args
765
+
766
+ c.instance_variable_set(:@io, io)
767
+ c.instance_variable_set(:@watch_mode, watch_mode)
768
+ c.instance_variable_set(:@fd, fd)
769
+
770
+ @conns[s] = c
771
+ block_given? and yield c
772
+ c
773
+ end
774
+
775
+
776
+ # Connect to a given host/port and re-use the provided {EventMachine::Connection} instance.
777
+ # Consider also {EventMachine::Connection#reconnect}.
778
+ #
779
+ # @see EventMachine::Connection#reconnect
780
+ def self.reconnect server, port, handler
781
+ # Observe, the test for already-connected FAILS if we call a reconnect inside post_init,
782
+ # because we haven't set up the connection in @conns by that point.
783
+ # RESIST THE TEMPTATION to "fix" this problem by redefining the behavior of post_init.
784
+ #
785
+ # Changed 22Nov06: if called on an already-connected handler, just return the
786
+ # handler and do nothing more. Originally this condition raised an exception.
787
+ # We may want to change it yet again and call the block, if any.
788
+
789
+ raise "invalid handler" unless handler.respond_to?(:connection_completed)
790
+ #raise "still connected" if @conns.has_key?(handler.signature)
791
+ return handler if @conns.has_key?(handler.signature)
792
+
793
+ s = if port
794
+ connect_server server, port
795
+ else
796
+ connect_unix_server server
797
+ end
798
+ handler.signature = s
799
+ @conns[s] = handler
800
+ block_given? and yield handler
801
+ handler
802
+ end
803
+
804
+
805
+ # Make a connection to a Unix-domain socket. This method is simply an alias for {.connect},
806
+ # which can connect to both TCP and Unix-domain sockets. Make sure that your process has sufficient
807
+ # permissions to open the socket it is given.
808
+ #
809
+ # @param [String] socketname Unix domain socket (local fully-qualified path) you want to connect to.
810
+ #
811
+ # @note UNIX sockets, as the name suggests, are not available on Microsoft Windows.
812
+ def self.connect_unix_domain socketname, *args, &blk
813
+ connect socketname, *args, &blk
814
+ end
815
+
816
+
817
+ # Used for UDP-based protocols. Its usage is similar to that of {EventMachine.start_server}.
818
+ #
819
+ # This method will create a new UDP (datagram) socket and
820
+ # bind it to the address and port that you specify.
821
+ # The normal callbacks (see {EventMachine.start_server}) will
822
+ # be called as events of interest occur on the newly-created
823
+ # socket, but there are some differences in how they behave.
824
+ #
825
+ # {Connection#receive_data} will be called when a datagram packet
826
+ # is received on the socket, but unlike TCP sockets, the message
827
+ # boundaries of the received data will be respected. In other words,
828
+ # if the remote peer sent you a datagram of a particular size,
829
+ # you may rely on {Connection#receive_data} to give you the
830
+ # exact data in the packet, with the original data length.
831
+ # Also observe that Connection#receive_data may be called with a
832
+ # *zero-length* data payload, since empty datagrams are permitted in UDP.
833
+ #
834
+ # {Connection#send_data} is available with UDP packets as with TCP,
835
+ # but there is an important difference. Because UDP communications
836
+ # are *connectionless*, there is no implicit recipient for the packets you
837
+ # send. Ordinarily you must specify the recipient for each packet you send.
838
+ # However, EventMachine provides for the typical pattern of receiving a UDP datagram
839
+ # from a remote peer, performing some operation, and then sending
840
+ # one or more packets in response to the same remote peer.
841
+ # To support this model easily, just use {Connection#send_data}
842
+ # in the code that you supply for {Connection#receive_data}.
843
+ #
844
+ # EventMachine will provide an implicit return address for any messages sent to
845
+ # {Connection#send_data} within the context of a {Connection#receive_data} callback,
846
+ # and your response will automatically go to the correct remote peer.
847
+ #
848
+ # Observe that the port number that you supply to {EventMachine.open_datagram_socket}
849
+ # may be zero. In this case, EventMachine will create a UDP socket
850
+ # that is bound to an [ephemeral port](http://en.wikipedia.org/wiki/Ephemeral_port).
851
+ # This is not appropriate for servers that must publish a well-known
852
+ # port to which remote peers may send datagrams. But it can be useful
853
+ # for clients that send datagrams to other servers.
854
+ # If you do this, you will receive any responses from the remote
855
+ # servers through the normal {Connection#receive_data} callback.
856
+ # Observe that you will probably have issues with firewalls blocking
857
+ # the ephemeral port numbers, so this technique is most appropriate for LANs.
858
+ #
859
+ # If you wish to send datagrams to arbitrary remote peers (not
860
+ # necessarily ones that have sent data to which you are responding),
861
+ # then see {Connection#send_datagram}.
862
+ #
863
+ # DO NOT call send_data from a datagram socket outside of a {Connection#receive_data} method. Use {Connection#send_datagram}.
864
+ # If you do use {Connection#send_data} outside of a {Connection#receive_data} method, you'll get a confusing error
865
+ # because there is no "peer," as #send_data requires (inside of {EventMachine::Connection#receive_data},
866
+ # {EventMachine::Connection#send_data} "fakes" the peer as described above).
867
+ #
868
+ # @param [String] address IP address
869
+ # @param [String] port Port
870
+ # @param [Class, Module] handler A class or a module that implements connection lifecycle callbacks.
871
+ def self.open_datagram_socket address, port, handler=nil, *args
872
+ # Replaced the implementation on 01Oct06. Thanks to Tobias Gustafsson for pointing
873
+ # out that this originally did not take a class but only a module.
874
+
875
+
876
+ klass = klass_from_handler(Connection, handler, *args)
877
+ s = open_udp_socket address, port.to_i
878
+ c = klass.new s, *args
879
+ @conns[s] = c
880
+ block_given? and yield c
881
+ c
882
+ end
883
+
884
+
885
+ # For advanced users. This function sets the default timer granularity, which by default is
886
+ # slightly smaller than 100 milliseconds. Call this function to set a higher or lower granularity.
887
+ # The function affects the behavior of {EventMachine.add_timer} and {EventMachine.add_periodic_timer}.
888
+ # Most applications will not need to call this function.
889
+ #
890
+ # Avoid setting the quantum to very low values because that may reduce performance under some extreme conditions.
891
+ # We recommend that you not use values lower than 10.
892
+ #
893
+ # This method only can be used if event loop is running.
894
+ #
895
+ # @param [Integer] mills New timer granularity, in milliseconds
896
+ #
897
+ # @see EventMachine.add_timer
898
+ # @see EventMachine.add_periodic_timer
899
+ # @see EventMachine::Timer
900
+ # @see EventMachine.run
901
+ def self.set_quantum mills
902
+ set_timer_quantum mills.to_i
903
+ end
904
+
905
+ # Sets the maximum number of timers and periodic timers that may be outstanding at any
906
+ # given time. You only need to call {.set_max_timers} if you need more than the default
907
+ # number of timers, which on most platforms is 1000.
908
+ #
909
+ # @note This method has to be used *before* event loop is started.
910
+ #
911
+ # @param [Integer] ct Maximum number of timers that may be outstanding at any given time
912
+ #
913
+ # @see EventMachine.add_timer
914
+ # @see EventMachine.add_periodic_timer
915
+ # @see EventMachine::Timer
916
+ def self.set_max_timers ct
917
+ set_max_timer_count ct
918
+ end
919
+
920
+ # Gets the current maximum number of allowed timers
921
+ #
922
+ # @return [Integer] Maximum number of timers that may be outstanding at any given time
923
+ def self.get_max_timers
924
+ get_max_timer_count
925
+ end
926
+
927
+ # Returns the total number of connections (file descriptors) currently held by the reactor.
928
+ # Note that a tick must pass after the 'initiation' of a connection for this number to increment.
929
+ # It's usually accurate, but don't rely on the exact precision of this number unless you really know EM internals.
930
+ #
931
+ # @example
932
+ #
933
+ # EventMachine.run {
934
+ # EventMachine.connect("rubyeventmachine.com", 80)
935
+ # # count will be 0 in this case, because connection is not
936
+ # # established yet
937
+ # count = EventMachine.connection_count
938
+ # }
939
+ #
940
+ #
941
+ # @example
942
+ #
943
+ # EventMachine.run {
944
+ # EventMachine.connect("rubyeventmachine.com", 80)
945
+ #
946
+ # EventMachine.next_tick {
947
+ # # In this example, count will be 1 since the connection has been established in
948
+ # # the next loop of the reactor.
949
+ # count = EventMachine.connection_count
950
+ # }
951
+ # }
952
+ #
953
+ # @return [Integer] Number of connections currently held by the reactor.
954
+ def self.connection_count
955
+ self.get_connection_count
956
+ end
957
+
958
+ # The is the responder for the loopback-signalled event.
959
+ # It can be fired either by code running on a separate thread ({EventMachine.defer}) or on
960
+ # the main thread ({EventMachine.next_tick}).
961
+ # It will often happen that a next_tick handler will reschedule itself. We
962
+ # consume a copy of the tick queue so that tick events scheduled by tick events
963
+ # have to wait for the next pass through the reactor core.
964
+ #
965
+ # @private
966
+ def self.run_deferred_callbacks
967
+ until (@resultqueue ||= []).empty?
968
+ result,cback = @resultqueue.pop
969
+ cback.call result if cback
970
+ end
971
+
972
+ # Capture the size at the start of this tick...
973
+ size = @next_tick_mutex.synchronize { @next_tick_queue.size }
974
+ size.times do |i|
975
+ callback = @next_tick_mutex.synchronize { @next_tick_queue.shift }
976
+ begin
977
+ callback.call
978
+ rescue
979
+ exception_raised = true
980
+ raise
981
+ ensure
982
+ # This is a little nasty. The problem is, if an exception occurs during
983
+ # the callback, then we need to send a signal to the reactor to actually
984
+ # do some work during the next_tick. The only mechanism we have from the
985
+ # ruby side is next_tick itself, although ideally, we'd just drop a byte
986
+ # on the loopback descriptor.
987
+ EM.next_tick {} if exception_raised
988
+ end
989
+ end
990
+ end
991
+
992
+
993
+ # EventMachine.defer is used for integrating blocking operations into EventMachine's control flow.
994
+ # The action of {.defer} is to take the block specified in the first parameter (the "operation")
995
+ # and schedule it for asynchronous execution on an internal thread pool maintained by EventMachine.
996
+ # When the operation completes, it will pass the result computed by the block (if any) back to the
997
+ # EventMachine reactor. Then, EventMachine calls the block specified in the second parameter to
998
+ # {.defer} (the "callback"), as part of its normal event handling loop. The result computed by the
999
+ # operation block is passed as a parameter to the callback. You may omit the callback parameter if
1000
+ # you don't need to execute any code after the operation completes. If the operation raises an
1001
+ # unhandled exception, the exception will be passed to the third parameter to {.defer} (the
1002
+ # "errback"), as part of its normal event handling loop. If no errback is provided, the exception
1003
+ # will be allowed to blow through to the main thread immediately.
1004
+ #
1005
+ # ## Caveats ##
1006
+ #
1007
+ # Note carefully that the code in your deferred operation will be executed on a separate
1008
+ # thread from the main EventMachine processing and all other Ruby threads that may exist in
1009
+ # your program. Also, multiple deferred operations may be running at once! Therefore, you
1010
+ # are responsible for ensuring that your operation code is threadsafe.
1011
+ #
1012
+ # Don't write a deferred operation that will block forever. If so, the current implementation will
1013
+ # not detect the problem, and the thread will never be returned to the pool. EventMachine limits
1014
+ # the number of threads in its pool, so if you do this enough times, your subsequent deferred
1015
+ # operations won't get a chance to run.
1016
+ #
1017
+ # The threads within the EventMachine's thread pool have abort_on_exception set to true. As a result,
1018
+ # if an unhandled exception is raised by the deferred operation and an errback is not provided, it
1019
+ # will blow through to the main thread immediately. If the main thread is within an indiscriminate
1020
+ # rescue block at that time, the exception could be handled improperly by the main thread.
1021
+ #
1022
+ # @example
1023
+ #
1024
+ # operation = proc {
1025
+ # # perform a long-running operation here, such as a database query.
1026
+ # "result" # as usual, the last expression evaluated in the block will be the return value.
1027
+ # }
1028
+ # callback = proc {|result|
1029
+ # # do something with result here, such as send it back to a network client.
1030
+ # }
1031
+ # errback = proc {|error|
1032
+ # # do something with error here, such as re-raising or logging.
1033
+ # }
1034
+ #
1035
+ # EventMachine.defer(operation, callback, errback)
1036
+ #
1037
+ # @param [#call] op An operation you want to offload to EventMachine thread pool
1038
+ # @param [#call] callback A callback that will be run on the event loop thread after `operation` finishes.
1039
+ # @param [#call] errback An errback that will be run on the event loop thread after `operation` raises an exception.
1040
+ #
1041
+ # @see EventMachine.threadpool_size
1042
+ def self.defer op = nil, callback = nil, errback = nil, &blk
1043
+ # OBSERVE that #next_tick hacks into this mechanism, so don't make any changes here
1044
+ # without syncing there.
1045
+ #
1046
+ # Running with $VERBOSE set to true gives a warning unless all ivars are defined when
1047
+ # they appear in rvalues. But we DON'T ever want to initialize @threadqueue unless we
1048
+ # need it, because the Ruby threads are so heavyweight. We end up with this bizarre
1049
+ # way of initializing @threadqueue because EventMachine is a Module, not a Class, and
1050
+ # has no constructor.
1051
+
1052
+ unless @threadpool
1053
+ @threadpool = []
1054
+ @threadqueue = ::Queue.new
1055
+ @resultqueue = ::Queue.new
1056
+ spawn_threadpool
1057
+ end
1058
+
1059
+ @threadqueue << [op||blk,callback,errback]
1060
+ end
1061
+
1062
+
1063
+ # @private
1064
+ def self.spawn_threadpool
1065
+ until @threadpool.size == @threadpool_size.to_i
1066
+ thread = Thread.new do
1067
+ Thread.current.abort_on_exception = true
1068
+ while true
1069
+ begin
1070
+ op, cback, eback = *@threadqueue.pop
1071
+ rescue ThreadError
1072
+ $stderr.puts $!.message
1073
+ break # Ruby 2.0 may fail at Queue.pop
1074
+ end
1075
+ begin
1076
+ result = op.call
1077
+ @resultqueue << [result, cback]
1078
+ rescue Exception => error
1079
+ raise error unless eback
1080
+ @resultqueue << [error, eback]
1081
+ end
1082
+ EventMachine.signal_loopbreak
1083
+ end
1084
+ end
1085
+ @threadpool << thread
1086
+ end
1087
+ @all_threads_spawned = true
1088
+ end
1089
+
1090
+ ##
1091
+ # Returns +true+ if all deferred actions are done executing and their
1092
+ # callbacks have been fired.
1093
+ #
1094
+ def self.defers_finished?
1095
+ return false if @threadpool and !@all_threads_spawned
1096
+ return false if @threadqueue and not @threadqueue.empty?
1097
+ return false if @resultqueue and not @resultqueue.empty?
1098
+ return false if @threadpool and @threadqueue.num_waiting != @threadpool.size
1099
+ return true
1100
+ end
1101
+
1102
+ class << self
1103
+ # @private
1104
+ attr_reader :threadpool
1105
+
1106
+ # Size of the EventMachine.defer threadpool (defaults to 20)
1107
+ # @return [Number]
1108
+ attr_accessor :threadpool_size
1109
+ EventMachine.threadpool_size = 20
1110
+ end
1111
+
1112
+ # Schedules a proc for execution immediately after the next "turn" through the reactor
1113
+ # core. An advanced technique, this can be useful for improving memory management and/or
1114
+ # application responsiveness, especially when scheduling large amounts of data for
1115
+ # writing to a network connection.
1116
+ #
1117
+ # This method takes either a single argument (which must be a callable object) or a block.
1118
+ #
1119
+ # @param [#call] pr A callable object to run
1120
+ def self.next_tick pr=nil, &block
1121
+ # This works by adding to the @resultqueue that's used for #defer.
1122
+ # The general idea is that next_tick is used when we want to give the reactor a chance
1123
+ # to let other operations run, either to balance the load out more evenly, or to let
1124
+ # outbound network buffers drain, or both. So we probably do NOT want to block, and
1125
+ # we probably do NOT want to be spinning any threads. A program that uses next_tick
1126
+ # but not #defer shouldn't suffer the penalty of having Ruby threads running. They're
1127
+ # extremely expensive even if they're just sleeping.
1128
+
1129
+ raise ArgumentError, "no proc or block given" unless ((pr && pr.respond_to?(:call)) or block)
1130
+ @next_tick_mutex.synchronize do
1131
+ @next_tick_queue << ( pr || block )
1132
+ end
1133
+ signal_loopbreak if reactor_running?
1134
+ end
1135
+
1136
+ # A wrapper over the setuid system call. Particularly useful when opening a network
1137
+ # server on a privileged port because you can use this call to drop privileges
1138
+ # after opening the port. Also very useful after a call to {.set_descriptor_table_size},
1139
+ # which generally requires that you start your process with root privileges.
1140
+ #
1141
+ # This method is intended for use in enforcing security requirements, consequently
1142
+ # it will throw a fatal error and end your program if it fails.
1143
+ #
1144
+ # @param [String] username The effective name of the user whose privilege-level your process should attain.
1145
+ #
1146
+ # @note This method has no effective implementation on Windows or in the pure-Ruby
1147
+ # implementation of EventMachine
1148
+ def self.set_effective_user username
1149
+ EventMachine::setuid_string username
1150
+ end
1151
+
1152
+
1153
+ # Sets the maximum number of file or socket descriptors that your process may open.
1154
+ # If you call this method with no arguments, it will simply return
1155
+ # the current size of the descriptor table without attempting to change it.
1156
+ #
1157
+ # The new limit on open descriptors **only** applies to sockets and other descriptors
1158
+ # that belong to EventMachine. It has **no effect** on the number of descriptors
1159
+ # you can create in ordinary Ruby code.
1160
+ #
1161
+ # Not available on all platforms. Increasing the number of descriptors beyond its
1162
+ # default limit usually requires superuser privileges. (See {.set_effective_user}
1163
+ # for a way to drop superuser privileges while your program is running.)
1164
+ #
1165
+ # @param [Integer] n_descriptors The maximum number of file or socket descriptors that your process may open
1166
+ # @return [Integer] The new descriptor table size.
1167
+ def self.set_descriptor_table_size n_descriptors=nil
1168
+ EventMachine::set_rlimit_nofile n_descriptors
1169
+ end
1170
+
1171
+
1172
+
1173
+ # Runs an external process.
1174
+ #
1175
+ # @example
1176
+ #
1177
+ # module RubyCounter
1178
+ # def post_init
1179
+ # # count up to 5
1180
+ # send_data "5\n"
1181
+ # end
1182
+ # def receive_data data
1183
+ # puts "ruby sent me: #{data}"
1184
+ # end
1185
+ # def unbind
1186
+ # puts "ruby died with exit status: #{get_status.exitstatus}"
1187
+ # end
1188
+ # end
1189
+ #
1190
+ # EventMachine.run {
1191
+ # EventMachine.popen("ruby -e' $stdout.sync = true; gets.to_i.times{ |i| puts i+1; sleep 1 } '", RubyCounter)
1192
+ # }
1193
+ #
1194
+ # @note This method is not supported on Microsoft Windows
1195
+ # @see EventMachine::DeferrableChildProcess
1196
+ # @see EventMachine.system
1197
+ def self.popen cmd, handler=nil, *args
1198
+ # At this moment, it's only available on Unix.
1199
+ # Perhaps misnamed since the underlying function uses socketpair and is full-duplex.
1200
+
1201
+ klass = klass_from_handler(Connection, handler, *args)
1202
+ w = case cmd
1203
+ when Array
1204
+ cmd
1205
+ when String
1206
+ Shellwords::shellwords( cmd )
1207
+ end
1208
+ w.unshift( w.first ) if w.first
1209
+ s = invoke_popen( w )
1210
+ c = klass.new s, *args
1211
+ @conns[s] = c
1212
+ yield(c) if block_given?
1213
+ c
1214
+ end
1215
+
1216
+
1217
+ # Tells you whether the EventMachine reactor loop is currently running.
1218
+ #
1219
+ # Useful when writing libraries that want to run event-driven code, but may
1220
+ # be running in programs that are already event-driven. In such cases, if {EventMachine.reactor_running?}
1221
+ # returns false, your code can invoke {EventMachine.run} and run your application code inside
1222
+ # the block passed to that method. If this method returns true, just
1223
+ # execute your event-aware code.
1224
+ #
1225
+ # @return [Boolean] true if the EventMachine reactor loop is currently running
1226
+ def self.reactor_running?
1227
+ @reactor_running && Process.pid == @reactor_pid
1228
+ end
1229
+
1230
+
1231
+ # (Experimental)
1232
+ #
1233
+ # @private
1234
+ def self.open_keyboard handler=nil, *args
1235
+ klass = klass_from_handler(Connection, handler, *args)
1236
+
1237
+ s = read_keyboard
1238
+ c = klass.new s, *args
1239
+ @conns[s] = c
1240
+ block_given? and yield c
1241
+ c
1242
+ end
1243
+
1244
+ # EventMachine's file monitoring API. Currently supported are the following events
1245
+ # on individual files, using inotify on Linux systems, and kqueue for *BSD and Mac OS X:
1246
+ #
1247
+ # * File modified (written to)
1248
+ # * File moved/renamed
1249
+ # * File deleted
1250
+ #
1251
+ # EventMachine::watch_file takes a filename and a handler Module containing your custom callback methods.
1252
+ # This will setup the low level monitoring on the specified file, and create a new EventMachine::FileWatch
1253
+ # object with your Module mixed in. FileWatch is a subclass of {EventMachine::Connection}, so callbacks on this object
1254
+ # work in the familiar way. The callbacks that will be fired by EventMachine are:
1255
+ #
1256
+ # * file_modified
1257
+ # * file_moved
1258
+ # * file_deleted
1259
+ #
1260
+ # You can access the filename being monitored from within this object using {FileWatch#path}.
1261
+ #
1262
+ # When a file is deleted, {FileWatch#stop_watching} will be called after your file_deleted callback,
1263
+ # to clean up the underlying monitoring and remove EventMachine's reference to the now-useless {FileWatch} instance.
1264
+ # This will in turn call unbind, if you wish to use it.
1265
+ #
1266
+ # The corresponding system-level Errno will be raised when attempting to monitor non-existent files,
1267
+ # files with wrong permissions, or if an error occurs dealing with inotify/kqueue.
1268
+ #
1269
+ # @example
1270
+ #
1271
+ # # Before running this example, make sure we have a file to monitor:
1272
+ # # $ echo "bar" > /tmp/foo
1273
+ #
1274
+ # module Handler
1275
+ # def file_modified
1276
+ # puts "#{path} modified"
1277
+ # end
1278
+ #
1279
+ # def file_moved
1280
+ # puts "#{path} moved"
1281
+ # end
1282
+ #
1283
+ # def file_deleted
1284
+ # puts "#{path} deleted"
1285
+ # end
1286
+ #
1287
+ # def unbind
1288
+ # puts "#{path} monitoring ceased"
1289
+ # end
1290
+ # end
1291
+ #
1292
+ # # for efficient file watching, use kqueue on Mac OS X
1293
+ # EventMachine.kqueue = true if EventMachine.kqueue?
1294
+ #
1295
+ # EventMachine.run {
1296
+ # EventMachine.watch_file("/tmp/foo", Handler)
1297
+ # }
1298
+ #
1299
+ # # $ echo "baz" >> /tmp/foo => "/tmp/foo modified"
1300
+ # # $ mv /tmp/foo /tmp/oof => "/tmp/foo moved"
1301
+ # # $ rm /tmp/oof => "/tmp/foo deleted"
1302
+ #
1303
+ # @note The ability to pick up on the new filename after a rename is not yet supported.
1304
+ # Calling #path will always return the filename you originally used.
1305
+ #
1306
+ # @param [String] filename Local path to the file to watch.
1307
+ # @param [Class, Module] handler A class or module that implements event handlers associated with the file.
1308
+ def self.watch_file(filename, handler=nil, *args)
1309
+ klass = klass_from_handler(FileWatch, handler, *args)
1310
+
1311
+ s = EM::watch_filename(filename)
1312
+ c = klass.new s, *args
1313
+ # we have to set the path like this because of how Connection.new works
1314
+ c.instance_variable_set("@path", filename)
1315
+ @conns[s] = c
1316
+ block_given? and yield c
1317
+ c
1318
+ end
1319
+
1320
+ # EventMachine's process monitoring API. On Mac OS X and *BSD this method is implemented using kqueue.
1321
+ #
1322
+ # @example
1323
+ #
1324
+ # module ProcessWatcher
1325
+ # def process_exited
1326
+ # put 'the forked child died!'
1327
+ # end
1328
+ # end
1329
+ #
1330
+ # pid = fork{ sleep }
1331
+ #
1332
+ # EventMachine.run {
1333
+ # EventMachine.watch_process(pid, ProcessWatcher)
1334
+ # EventMachine.add_timer(1){ Process.kill('TERM', pid) }
1335
+ # }
1336
+ #
1337
+ # @param [Integer] pid PID of the process to watch.
1338
+ # @param [Class, Module] handler A class or module that implements event handlers associated with the file.
1339
+ def self.watch_process(pid, handler=nil, *args)
1340
+ pid = pid.to_i
1341
+
1342
+ klass = klass_from_handler(ProcessWatch, handler, *args)
1343
+
1344
+ s = EM::watch_pid(pid)
1345
+ c = klass.new s, *args
1346
+ # we have to set the path like this because of how Connection.new works
1347
+ c.instance_variable_set("@pid", pid)
1348
+ @conns[s] = c
1349
+ block_given? and yield c
1350
+ c
1351
+ end
1352
+
1353
+ # Catch-all for errors raised during event loop callbacks.
1354
+ #
1355
+ # @example
1356
+ #
1357
+ # EventMachine.error_handler{ |e|
1358
+ # puts "Error raised during event loop: #{e.message}"
1359
+ # }
1360
+ #
1361
+ # @param [#call] cb Global catch-all errback
1362
+ def self.error_handler cb = nil, &blk
1363
+ if cb or blk
1364
+ @error_handler = cb || blk
1365
+ elsif instance_variable_defined? :@error_handler
1366
+ remove_instance_variable :@error_handler
1367
+ end
1368
+ end
1369
+
1370
+ # This method allows for direct writing of incoming data back out to another descriptor, at the C++ level in the reactor.
1371
+ # This is very efficient and especially useful for proxies where high performance is required. Propogating data from a server response
1372
+ # all the way up to Ruby, and then back down to the reactor to be sent back to the client, is often unnecessary and
1373
+ # incurs a significant performance decrease.
1374
+ #
1375
+ # The two arguments are instance of {EventMachine::Connection} subclasses, 'from' and 'to'. 'from' is the connection whose inbound data you want
1376
+ # relayed back out. 'to' is the connection to write it to.
1377
+ #
1378
+ # Once you call this method, the 'from' connection will no longer get receive_data callbacks from the reactor,
1379
+ # except in the case that 'to' connection has already closed when attempting to write to it. You can see
1380
+ # in the example, that proxy_target_unbound will be called when this occurs. After that, further incoming
1381
+ # data will be passed into receive_data as normal.
1382
+ #
1383
+ # Note also that this feature supports different types of descriptors: TCP, UDP, and pipes. You can relay
1384
+ # data from one kind to another, for example, feed a pipe from a UDP stream.
1385
+ #
1386
+ # @example
1387
+ #
1388
+ # module ProxyConnection
1389
+ # def initialize(client, request)
1390
+ # @client, @request = client, request
1391
+ # end
1392
+ #
1393
+ # def post_init
1394
+ # EM::enable_proxy(self, @client)
1395
+ # end
1396
+ #
1397
+ # def connection_completed
1398
+ # send_data @request
1399
+ # end
1400
+ #
1401
+ # def proxy_target_unbound
1402
+ # close_connection
1403
+ # end
1404
+ #
1405
+ # def unbind
1406
+ # @client.close_connection_after_writing
1407
+ # end
1408
+ # end
1409
+ #
1410
+ # module ProxyServer
1411
+ # def receive_data(data)
1412
+ # (@buf ||= "") << data
1413
+ # if @buf =~ /\r\n\r\n/ # all http headers received
1414
+ # EventMachine.connect("10.0.0.15", 80, ProxyConnection, self, data)
1415
+ # end
1416
+ # end
1417
+ # end
1418
+ #
1419
+ # EventMachine.run {
1420
+ # EventMachine.start_server("127.0.0.1", 8080, ProxyServer)
1421
+ # }
1422
+ #
1423
+ # @param [EventMachine::Connection] from Source of data to be proxies/streamed.
1424
+ # @param [EventMachine::Connection] to Destination of data to be proxies/streamed.
1425
+ # @param [Integer] bufsize Buffer size to use
1426
+ # @param [Integer] length Maximum number of bytes to proxy.
1427
+ #
1428
+ # @see EventMachine.disable_proxy
1429
+ def self.enable_proxy(from, to, bufsize=0, length=0)
1430
+ EM::start_proxy(from.signature, to.signature, bufsize, length)
1431
+ end
1432
+
1433
+ # Takes just one argument, a {Connection} that has proxying enabled via {EventMachine.enable_proxy}.
1434
+ # Calling this method will remove that functionality and your connection will begin receiving
1435
+ # data via {Connection#receive_data} again.
1436
+ #
1437
+ # @param [EventMachine::Connection] from Source of data that is being proxied
1438
+ # @see EventMachine.enable_proxy
1439
+ def self.disable_proxy(from)
1440
+ EM::stop_proxy(from.signature)
1441
+ end
1442
+
1443
+ # Retrieve the heartbeat interval. This is how often EventMachine will check for dead connections
1444
+ # that have had an inactivity timeout set via {Connection#set_comm_inactivity_timeout}.
1445
+ # Default is 2 seconds.
1446
+ #
1447
+ # @return [Integer] Heartbeat interval, in seconds
1448
+ def self.heartbeat_interval
1449
+ EM::get_heartbeat_interval
1450
+ end
1451
+
1452
+ # Set the heartbeat interval. This is how often EventMachine will check for dead connections
1453
+ # that have had an inactivity timeout set via {Connection#set_comm_inactivity_timeout}.
1454
+ # Takes a Numeric number of seconds. Default is 2.
1455
+ #
1456
+ # @param [Integer] time Heartbeat interval, in seconds
1457
+ def self.heartbeat_interval=(time)
1458
+ EM::set_heartbeat_interval time.to_f
1459
+ end
1460
+
1461
+ # @private
1462
+ def self.event_callback conn_binding, opcode, data
1463
+ #
1464
+ # Changed 27Dec07: Eliminated the hookable error handling.
1465
+ # No one was using it, and it degraded performance significantly.
1466
+ # It's in original_event_callback, which is dead code.
1467
+ #
1468
+ # Changed 25Jul08: Added a partial solution to the problem of exceptions
1469
+ # raised in user-written event-handlers. If such exceptions are not caught,
1470
+ # we must cause the reactor to stop, and then re-raise the exception.
1471
+ # Otherwise, the reactor doesn't stop and it's left on the call stack.
1472
+ # This is partial because we only added it to #unbind, where it's critical
1473
+ # (to keep unbind handlers from being re-entered when a stopping reactor
1474
+ # runs down open connections). It should go on the other calls to user
1475
+ # code, but the performance impact may be too large.
1476
+ #
1477
+ if opcode == ConnectionUnbound
1478
+ if c = @conns.delete( conn_binding )
1479
+ begin
1480
+ if c.original_method(:unbind).arity != 0
1481
+ c.unbind(data == 0 ? nil : EventMachine::ERRNOS[data])
1482
+ else
1483
+ c.unbind
1484
+ end
1485
+ # If this is an attached (but not watched) connection, close the underlying io object.
1486
+ if c.instance_variable_defined?(:@io) and !c.instance_variable_get(:@watch_mode)
1487
+ io = c.instance_variable_get(:@io)
1488
+ begin
1489
+ io.close
1490
+ rescue Errno::EBADF, IOError
1491
+ end
1492
+ end
1493
+ rescue Exception => e
1494
+ if stopping?
1495
+ @wrapped_exception = $!
1496
+ stop
1497
+ else
1498
+ raise e
1499
+ end
1500
+ end
1501
+ elsif c = @acceptors.delete( conn_binding )
1502
+ # no-op
1503
+ else
1504
+ if $! # Bubble user generated errors.
1505
+ @wrapped_exception = $!
1506
+ EM.stop
1507
+ else
1508
+ raise ConnectionNotBound, "received ConnectionUnbound for an unknown signature: #{conn_binding}"
1509
+ end
1510
+ end
1511
+ elsif opcode == ConnectionAccepted
1512
+ accep,args,blk = @acceptors[conn_binding]
1513
+ raise NoHandlerForAcceptedConnection unless accep
1514
+ c = accep.new data, *args
1515
+ @conns[data] = c
1516
+ blk and blk.call(c)
1517
+ c # (needed?)
1518
+ ##
1519
+ # The remaining code is a fallback for the pure ruby and java reactors.
1520
+ # In the C++ reactor, these events are handled in the C event_callback() in rubymain.cpp
1521
+ elsif opcode == ConnectionCompleted
1522
+ c = @conns[conn_binding] or raise ConnectionNotBound, "received ConnectionCompleted for unknown signature: #{conn_binding}"
1523
+ c.connection_completed
1524
+ elsif opcode == TimerFired
1525
+ t = @timers.delete( data )
1526
+ return if t == false # timer cancelled
1527
+ t or raise UnknownTimerFired, "timer data: #{data}"
1528
+ t.call
1529
+ elsif opcode == ConnectionData
1530
+ c = @conns[conn_binding] or raise ConnectionNotBound, "received data #{data} for unknown signature: #{conn_binding}"
1531
+ c.receive_data data
1532
+ elsif opcode == LoopbreakSignalled
1533
+ run_deferred_callbacks
1534
+ elsif opcode == ConnectionNotifyReadable
1535
+ c = @conns[conn_binding] or raise ConnectionNotBound
1536
+ c.notify_readable
1537
+ elsif opcode == ConnectionNotifyWritable
1538
+ c = @conns[conn_binding] or raise ConnectionNotBound
1539
+ c.notify_writable
1540
+ end
1541
+ end
1542
+
1543
+ #
1544
+ #
1545
+ # @private
1546
+ def self._open_file_for_writing filename, handler=nil
1547
+ klass = klass_from_handler(Connection, handler)
1548
+
1549
+ s = _write_file filename
1550
+ c = klass.new s
1551
+ @conns[s] = c
1552
+ block_given? and yield c
1553
+ c
1554
+ end
1555
+
1556
+ # @private
1557
+ def self.klass_from_handler(klass = Connection, handler = nil, *args)
1558
+ klass = if handler and handler.is_a?(Class)
1559
+ raise ArgumentError, "must provide module or subclass of #{klass.name}" unless klass >= handler
1560
+ handler
1561
+ elsif handler
1562
+ if defined?(handler::EM_CONNECTION_CLASS)
1563
+ handler::EM_CONNECTION_CLASS
1564
+ else
1565
+ handler::const_set(:EM_CONNECTION_CLASS, Class.new(klass) {include handler})
1566
+ end
1567
+ else
1568
+ klass
1569
+ end
1570
+
1571
+ arity = klass.instance_method(:initialize).arity
1572
+ expected = arity >= 0 ? arity : -(arity + 1)
1573
+ if (arity >= 0 and args.size != expected) or (arity < 0 and args.size < expected)
1574
+ raise ArgumentError, "wrong number of arguments for #{klass}#initialize (#{args.size} for #{expected})"
1575
+ end
1576
+
1577
+ klass
1578
+ end
1579
+ end # module EventMachine
1580
+
1581
+ # Alias for {EventMachine}
1582
+ EM = EventMachine
1583
+ # Alias for {EventMachine::Protocols}
1584
+ EM::P = EventMachine::Protocols