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